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