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.*;
026
027import ca.uhn.fhir.util.StopWatch;
028import org.apache.commons.io.IOUtils;
029import org.apache.http.Header;
030import org.apache.http.HttpEntity;
031import org.apache.http.HttpEntityEnclosingRequest;
032import org.apache.http.HttpResponse;
033import org.apache.http.client.HttpClient;
034import org.apache.http.client.methods.HttpRequestBase;
035import org.apache.http.entity.ContentType;
036
037import ca.uhn.fhir.rest.client.api.IHttpRequest;
038import ca.uhn.fhir.rest.client.api.IHttpResponse;
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         * @return the ApacheRequest
083         */
084        public HttpRequestBase getApacheRequest() {
085                return myRequest;
086        }
087
088        @Override
089        public String getHttpVerbName() {
090                return myRequest.getMethod();
091        }
092
093        @Override
094        public String getRequestBodyFromStream() throws IOException {
095                if (myRequest instanceof HttpEntityEnclosingRequest) {
096                        HttpEntity entity = ((HttpEntityEnclosingRequest) myRequest).getEntity();
097                        if (entity.isRepeatable()) {
098                                final Header contentTypeHeader = myRequest.getFirstHeader("Content-Type");
099                                Charset charset = contentTypeHeader == null ? null : ContentType.parse(contentTypeHeader.getValue()).getCharset();
100                                return IOUtils.toString(entity.getContent(), charset);
101                        }
102                }
103                return null;
104        }
105
106        @Override
107        public String getUri() {
108                return myRequest.getURI().toString();
109        }
110
111        @Override
112        public String toString() {
113                return myRequest.toString();
114        }
115
116}