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