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 * <div class='warn'>
028 *    <b>Deprecated</b> - Use <c>org.apache.juneau.restmock2</c>
029 * </div>
030 *
031 * This implementation is NOT thread safe.
032 */
033@Deprecated
034public class MockHttpClientConnection implements HttpClientConnection {
035
036   private final MockHttpConnection c;
037   private volatile MockHttpRequest req;
038   private volatile MockHttpResponse res;
039
040   /**
041    * Constructor.
042    *
043    * @param c The API for performing the connections.
044    */
045   public MockHttpClientConnection(MockHttpConnection c) {
046      this.c = c;
047   }
048
049   @Override /* HttpClientConnection */
050   public void close() throws IOException {}
051
052   @Override /* HttpClientConnection */
053   public boolean isOpen() {
054      return true;
055   }
056
057   @Override /* HttpClientConnection */
058   public boolean isStale() {
059      return false;
060   }
061
062   @Override /* HttpClientConnection */
063   public void setSocketTimeout(int timeout) {}
064
065   @Override /* HttpClientConnection */
066   public int getSocketTimeout() {
067      return Integer.MAX_VALUE;
068   }
069
070   @Override /* HttpClientConnection */
071   public void shutdown() throws IOException {}
072
073   @Override /* HttpClientConnection */
074   public HttpConnectionMetrics getMetrics() {
075      return null;
076   }
077
078   @Override /* HttpClientConnection */
079   public boolean isResponseAvailable(int timeout) throws IOException {
080      return true;
081   }
082
083   @Override /* HttpClientConnection */
084   public void sendRequestHeader(HttpRequest request) throws HttpException, IOException {
085      try {
086         RequestLine rl = request.getRequestLine();
087         req = c.request(rl.getMethod(), rl.getUri(), null);
088         for (Header h : request.getAllHeaders())
089            req.header(h.getName(), h.getValue());
090      } catch (Exception e) {
091         throw new HttpException(e.getMessage(), e);
092      }
093   }
094
095   @Override /* HttpClientConnection */
096   public void sendRequestEntity(HttpEntityEnclosingRequest request) throws HttpException, IOException {
097      req.body(request.getEntity() == null ? "" : IOUtils.readBytes(request.getEntity().getContent(), 1024));
098   }
099
100   @Override /* HttpClientConnection */
101   public HttpResponse receiveResponseHeader() throws HttpException, IOException {
102      try {
103         res = req.execute();
104         HttpResponse response = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, res.getStatus(), res.getMessage()));
105         for (Map.Entry<String,String[]> e : res.getHeaders().entrySet())
106            for (String hv : e.getValue())
107               response.addHeader(e.getKey(), hv);
108         return response;
109      } catch (Exception e) {
110         throw new HttpException(e.getMessage(), e);
111      }
112   }
113
114   @Override /* HttpClientConnection */
115   public void receiveResponseEntity(HttpResponse response) throws HttpException, IOException {
116      BasicHttpEntity e = new BasicHttpEntity();
117      e.setContent(new ByteArrayInputStream(res.getBody()));
118      response.setEntity(e);
119   }
120
121   @Override /* HttpClientConnection */
122   public void flush() throws IOException {}
123}