001package ca.uhn.fhir.rest.client.apache; 002 003/* 004 * #%L 005 * HAPI FHIR - Client Framework 006 * %% 007 * Copyright (C) 2014 - 2019 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 ca.uhn.fhir.rest.client.api.IHttpRequest; 024import ca.uhn.fhir.rest.client.api.IHttpResponse; 025import ca.uhn.fhir.util.StopWatch; 026import org.apache.commons.io.IOUtils; 027import org.apache.commons.lang3.Validate; 028import org.apache.http.Header; 029import org.apache.http.HttpEntity; 030import org.apache.http.HttpEntityEnclosingRequest; 031import org.apache.http.HttpResponse; 032import org.apache.http.client.HttpClient; 033import org.apache.http.client.methods.HttpRequestBase; 034import org.apache.http.entity.ContentType; 035 036import java.io.IOException; 037import java.nio.charset.Charset; 038import java.util.*; 039 040/** 041 * A Http Request based on Apache. This is an adapter around the class 042 * {@link org.apache.http.client.methods.HttpRequestBase HttpRequestBase} 043 * 044 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 045 */ 046public class ApacheHttpRequest implements IHttpRequest { 047 048 private HttpClient myClient; 049 private HttpRequestBase myRequest; 050 051 public ApacheHttpRequest(HttpClient theClient, HttpRequestBase theApacheRequest) { 052 this.myClient = theClient; 053 this.myRequest = theApacheRequest; 054 } 055 056 @Override 057 public void addHeader(String theName, String theValue) { 058 myRequest.addHeader(theName, theValue); 059 } 060 061 @Override 062 public IHttpResponse execute() throws IOException { 063 StopWatch responseStopWatch = new StopWatch(); 064 HttpResponse httpResponse = myClient.execute(myRequest); 065 return new ApacheHttpResponse(httpResponse, responseStopWatch); 066 } 067 068 @Override 069 public Map<String, List<String>> getAllHeaders() { 070 Map<String, List<String>> result = new HashMap<>(); 071 for (Header header : myRequest.getAllHeaders()) { 072 if (!result.containsKey(header.getName())) { 073 result.put(header.getName(), new LinkedList<>()); 074 } 075 result.get(header.getName()).add(header.getValue()); 076 } 077 return Collections.unmodifiableMap(result); 078 } 079 080 /** 081 * Get the ApacheRequest 082 * 083 * @return the ApacheRequest 084 */ 085 public HttpRequestBase getApacheRequest() { 086 return myRequest; 087 } 088 089 @Override 090 public String getHttpVerbName() { 091 return myRequest.getMethod(); 092 } 093 094 @Override 095 public void removeHeaders(String theHeaderName) { 096 Validate.notBlank(theHeaderName, "theHeaderName must not be null or blank"); 097 myRequest.removeHeaders(theHeaderName); 098 } 099 100 @Override 101 public String getRequestBodyFromStream() throws IOException { 102 if (myRequest instanceof HttpEntityEnclosingRequest) { 103 HttpEntity entity = ((HttpEntityEnclosingRequest) myRequest).getEntity(); 104 if (entity.isRepeatable()) { 105 final Header contentTypeHeader = myRequest.getFirstHeader("Content-Type"); 106 Charset charset = contentTypeHeader == null ? null : ContentType.parse(contentTypeHeader.getValue()).getCharset(); 107 return IOUtils.toString(entity.getContent(), charset); 108 } 109 } 110 return null; 111 } 112 113 @Override 114 public String getUri() { 115 return myRequest.getURI().toString(); 116 } 117 118 @Override 119 public String toString() { 120 return myRequest.toString(); 121 } 122 123}