001package ca.uhn.fhir.rest.client.apache; 002 003/* 004 * #%L 005 * HAPI FHIR - Client Framework 006 * %% 007 * Copyright (C) 2014 - 2018 University Health Network 008 * %% 009 * Licensed under the Apache License, Version 2.0 (the "License"); 010 * you may not use this file except in compliance with the License. 011 * You may obtain a copy of the License at 012 * 013 * http://www.apache.org/licenses/LICENSE-2.0 014 * 015 * Unless required by applicable law or agreed to in writing, software 016 * distributed under the License is distributed on an "AS IS" BASIS, 017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 018 * See the License for the specific language governing permissions and 019 * limitations under the License. 020 * #L% 021 */ 022 023import java.io.IOException; 024import java.nio.charset.Charset; 025import java.util.HashMap; 026import java.util.LinkedList; 027import java.util.List; 028import java.util.Map; 029 030import org.apache.commons.io.IOUtils; 031import org.apache.http.Header; 032import org.apache.http.HttpEntity; 033import org.apache.http.HttpEntityEnclosingRequest; 034import org.apache.http.client.HttpClient; 035import org.apache.http.client.methods.HttpRequestBase; 036import org.apache.http.entity.ContentType; 037 038import ca.uhn.fhir.rest.client.api.IHttpRequest; 039import ca.uhn.fhir.rest.client.api.IHttpResponse; 040 041/** 042 * A Http Request based on Apache. This is an adapter around the class 043 * {@link org.apache.http.client.methods.HttpRequestBase HttpRequestBase} 044 * 045 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 046 */ 047public class ApacheHttpRequest implements IHttpRequest { 048 049 private HttpClient myClient; 050 private HttpRequestBase myRequest; 051 052 public ApacheHttpRequest(HttpClient theClient, HttpRequestBase theApacheRequest) { 053 this.myClient = theClient; 054 this.myRequest = theApacheRequest; 055 } 056 057 @Override 058 public void addHeader(String theName, String theValue) { 059 myRequest.addHeader(theName, theValue); 060 } 061 062 @Override 063 public IHttpResponse execute() throws IOException { 064 return new ApacheHttpResponse(myClient.execute(myRequest)); 065 } 066 067 @Override 068 public Map<String, List<String>> getAllHeaders() { 069 Map<String, List<String>> result = new HashMap<String, List<String>>(); 070 for (Header header : myRequest.getAllHeaders()) { 071 if (!result.containsKey(header.getName())) { 072 result.put(header.getName(), new LinkedList<String>()); 073 } 074 result.get(header.getName()).add(header.getValue()); 075 } 076 return result; 077 } 078 079 /** 080 * Get the ApacheRequest 081 * @return the ApacheRequest 082 */ 083 public HttpRequestBase getApacheRequest() { 084 return myRequest; 085 } 086 087 @Override 088 public String getHttpVerbName() { 089 return myRequest.getMethod(); 090 } 091 092 @Override 093 public String getRequestBodyFromStream() throws IOException { 094 if (myRequest instanceof HttpEntityEnclosingRequest) { 095 HttpEntity entity = ((HttpEntityEnclosingRequest) myRequest).getEntity(); 096 if (entity.isRepeatable()) { 097 final Header contentTypeHeader = myRequest.getFirstHeader("Content-Type"); 098 Charset charset = contentTypeHeader == null ? null : ContentType.parse(contentTypeHeader.getValue()).getCharset(); 099 return IOUtils.toString(entity.getContent(), charset); 100 } 101 } 102 return null; 103 } 104 105 @Override 106 public String getUri() { 107 return myRequest.getURI().toString(); 108 } 109 110 @Override 111 public String toString() { 112 return myRequest.toString(); 113 } 114 115}