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 */
022import java.io.*;
023import java.nio.charset.Charset;
024import java.util.*;
025
026import org.apache.commons.io.IOUtils;
027import org.apache.http.*;
028import org.apache.http.client.methods.CloseableHttpResponse;
029import org.apache.http.entity.ContentType;
030
031import ca.uhn.fhir.rest.api.Constants;
032import ca.uhn.fhir.rest.client.api.IHttpResponse;
033import ca.uhn.fhir.rest.server.exceptions.InternalErrorException;
034
035/**
036 * A Http Response based on Apache. This is an adapter around the class
037 * {@link org.apache.http.HttpResponse HttpResponse}
038 * 
039 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
040 */
041public class ApacheHttpResponse implements IHttpResponse {
042
043        private static final org.slf4j.Logger ourLog = org.slf4j.LoggerFactory.getLogger(ApacheHttpResponse.class);
044
045        private boolean myEntityBuffered = false;
046        private byte[] myEntityBytes;
047        private final HttpResponse myResponse;
048
049        public ApacheHttpResponse(HttpResponse theResponse) {
050                this.myResponse = theResponse;
051        }
052
053        @Deprecated // override deprecated method
054        @Override
055        public void bufferEntitity() throws IOException {
056                bufferEntity();
057        }
058
059        @Override
060        public void bufferEntity() throws IOException {
061                if (myEntityBuffered) {
062                        return;
063                }
064                InputStream respEntity = readEntity();
065                if (respEntity != null) {
066                        this.myEntityBuffered = true;
067                        try {
068                                this.myEntityBytes = IOUtils.toByteArray(respEntity);
069                        } catch (IllegalStateException e) {
070                                // FIXME resouce leak
071                                throw new InternalErrorException(e);
072                        }
073                }
074        }
075
076        @Override
077        public void close() {
078                if (myResponse instanceof CloseableHttpResponse) {
079                        try {
080                                ((CloseableHttpResponse) myResponse).close();
081                        } catch (IOException e) {
082                                ourLog.debug("Failed to close response", e);
083                        }
084                }
085        }
086
087        @Override
088        public Reader createReader() throws IOException {
089                HttpEntity entity = myResponse.getEntity();
090                if (entity == null) {
091                        return new StringReader("");
092                }
093                Charset charset = null;
094                if (entity.getContentType() != null && entity.getContentType().getElements() != null
095                                && entity.getContentType().getElements().length > 0) {
096                        ContentType ct = ContentType.get(entity);
097                        charset = ct.getCharset();
098                }
099                if (charset == null) {
100                        if (Constants.STATUS_HTTP_204_NO_CONTENT != myResponse.getStatusLine().getStatusCode()) {
101                                ourLog.debug("Response did not specify a charset, defaulting to utf-8");
102                        }
103                        charset = Charset.forName("UTF-8");
104                }
105
106                return new InputStreamReader(readEntity(), charset);
107        }
108
109        @Override
110        public Map<String, List<String>> getAllHeaders() {
111                Map<String, List<String>> headers = new HashMap<>();
112                if (myResponse.getAllHeaders() != null) {
113                        for (Header next : myResponse.getAllHeaders()) {
114                                String name = next.getName().toLowerCase();
115                                List<String> list = headers.get(name);
116                                if (list == null) {
117                                        list = new ArrayList<>();
118                                        headers.put(name, list);
119                                }
120                                list.add(next.getValue());
121                        }
122
123                }
124                return headers;
125        }
126
127        @Override
128        public List<String> getHeaders(String theName) {
129                Header[] headers = myResponse.getHeaders(theName);
130                if (headers == null) {
131                        headers = new Header[0];
132                }
133                List<String> retVal = new ArrayList<>();
134                for (Header next : headers) {
135                        retVal.add(next.getValue());
136                }
137                return retVal;
138        }
139
140        @Override
141        public String getMimeType() {
142                ContentType ct = ContentType.get(myResponse.getEntity());
143                return ct != null ? ct.getMimeType() : null;
144        }
145
146        @Override
147        public HttpResponse getResponse() {
148                return myResponse;
149        }
150
151        @Override
152        public int getStatus() {
153                return myResponse.getStatusLine().getStatusCode();
154        }
155
156        @Override
157        public String getStatusInfo() {
158                return myResponse.getStatusLine().getReasonPhrase();
159        }
160
161        @Override
162        public InputStream readEntity() throws IOException {
163                if (this.myEntityBuffered) {
164                        return new ByteArrayInputStream(myEntityBytes);
165                } else if (myResponse.getEntity() != null) {
166                        return myResponse.getEntity().getContent();
167                } else {
168                        return null;
169                }
170        }
171}