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