001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.rest.client.mock;
014
015import java.io.*;
016import java.util.*;
017
018import org.apache.http.*;
019import org.apache.http.entity.*;
020import org.apache.http.message.*;
021import org.apache.juneau.internal.*;
022import org.apache.juneau.utils.*;
023
024/**
025 * An implementation of {@link HttpClientConnection} specifically for use in mocked connections using the {@link MockHttpConnection} class.
026 *
027 * This implementation is NOT thread safe.
028 */
029public class MockHttpClientConnection implements HttpClientConnection {
030
031   private final MockHttpConnection c;
032   private volatile MockHttpRequest req;
033   private volatile MockHttpResponse res;
034
035   /**
036    * Constructor.
037    *
038    * @param c The API for performing the connections.
039    */
040   public MockHttpClientConnection(MockHttpConnection c) {
041      this.c = c;
042   }
043
044   @Override /* HttpClientConnection */
045   public void close() throws IOException {}
046
047   @Override /* HttpClientConnection */
048   public boolean isOpen() {
049      return true;
050   }
051
052   @Override /* HttpClientConnection */
053   public boolean isStale() {
054      return false;
055   }
056
057   @Override /* HttpClientConnection */
058   public void setSocketTimeout(int timeout) {}
059
060   @Override /* HttpClientConnection */
061   public int getSocketTimeout() {
062      return Integer.MAX_VALUE;
063   }
064
065   @Override /* HttpClientConnection */
066   public void shutdown() throws IOException {}
067
068   @Override /* HttpClientConnection */
069   public HttpConnectionMetrics getMetrics() {
070      return null;
071   }
072
073   @Override /* HttpClientConnection */
074   public boolean isResponseAvailable(int timeout) throws IOException {
075      return true;
076   }
077
078   @Override /* HttpClientConnection */
079   public void sendRequestHeader(HttpRequest request) throws HttpException, IOException {
080      try {
081         RequestLine rl = request.getRequestLine();
082         req = c.request(rl.getMethod(), rl.getUri(), null);
083         for (Header h : request.getAllHeaders())
084            req.header(h.getName(), h.getValue());
085      } catch (Exception e) {
086         throw new HttpException(e.getMessage(), e);
087      }
088   }
089
090   @Override /* HttpClientConnection */
091   public void sendRequestEntity(HttpEntityEnclosingRequest request) throws HttpException, IOException {
092      req.body(request.getEntity() == null ? "" : IOUtils.readBytes(request.getEntity().getContent(), 1024));
093   }
094
095   @Override /* HttpClientConnection */
096   public HttpResponse receiveResponseHeader() throws HttpException, IOException {
097      try {
098         res = req.execute();
099         HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, res.getStatus(), res.getMessage()));
100         for (Map.Entry<String,String[]> e : res.getHeaders().entrySet())
101            for (String hv : e.getValue())
102               response.addHeader(e.getKey(), hv);
103         return response;
104      } catch (Exception e) {
105         throw new HttpException(e.getMessage(), e);
106      }
107   }
108
109   @Override /* HttpClientConnection */
110   public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException {
111      BasicHttpEntity e = new BasicHttpEntity();
112      e.setContent(new ByteArrayInputStream(res.getBody()));
113      response.setEntity(e);
114   }
115
116   @Override /* HttpClientConnection */
117   public void flush() throws IOException {}
118}