001package ca.uhn.fhir.jaxrs.client;
002
003import java.io.IOException;
004
005/*
006 * #%L
007 * HAPI FHIR JAX-RS Server
008 * %%
009 * Copyright (C) 2014 - 2016 University Health Network
010 * %%
011 * Licensed under the Apache License, Version 2.0 (the "License");
012 * you may not use this file except in compliance with the License.
013 * You may obtain a copy of the License at
014 * 
015 *      http://www.apache.org/licenses/LICENSE-2.0
016 * 
017 * Unless required by applicable law or agreed to in writing, software
018 * distributed under the License is distributed on an "AS IS" BASIS,
019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020 * See the License for the specific language governing permissions and
021 * limitations under the License.
022 * #L%
023 */
024
025import java.io.InputStream;
026import java.io.Reader;
027import java.io.StringReader;
028import java.util.List;
029import java.util.Map;
030import java.util.Map.Entry;
031import java.util.concurrent.ConcurrentHashMap;
032
033import javax.ws.rs.core.MediaType;
034import javax.ws.rs.core.Response;
035
036import ca.uhn.fhir.rest.client.api.IHttpResponse;
037
038/**
039 * A Http Response based on JaxRs. This is an adapter around the class {@link javax.ws.rs.core.Response Response}
040 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare
041 */
042public class JaxRsHttpResponse implements IHttpResponse {
043        
044        private boolean myBufferedEntity = false;
045        private final Response myResponse;
046        
047        public JaxRsHttpResponse(Response theResponse) {
048                this.myResponse = theResponse;
049        }
050
051        @Override
052        public void bufferEntitity() throws IOException {
053                bufferEntity();
054        }
055
056        @Override
057        public void bufferEntity() throws IOException {
058                if(!myBufferedEntity && myResponse.hasEntity()) {
059                        myBufferedEntity = true;
060                        myResponse.bufferEntity();
061                } else {
062                        myResponse.bufferEntity();
063                }
064        }
065
066        @Override
067        public void close() {
068                // automatically done by jax-rs
069        }
070
071        @Override
072        public Reader createReader() {
073                if (!myBufferedEntity && !myResponse.hasEntity()) {
074                        return new StringReader("");
075                } else {
076                        return new StringReader(myResponse.readEntity(String.class));
077                }
078        }
079
080        @Override
081        public Map<String, List<String>> getAllHeaders() {
082                Map<String, List<String>> theHeaders = new ConcurrentHashMap<String, List<String>>();
083                for (Entry<String, List<String>> iterable_element : myResponse.getStringHeaders().entrySet()) {
084                        theHeaders.put(iterable_element.getKey().toLowerCase(), iterable_element.getValue());
085                }
086                return theHeaders;
087        }
088
089        @Override
090        public String getMimeType() {
091                MediaType mediaType = myResponse.getMediaType();
092                if (mediaType == null) {
093                        return null;
094                }
095                //Keep only type and subtype and do not include the parameters such as charset
096                return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString();
097        }
098        
099        @Override
100        public Response getResponse() {
101                return myResponse;
102        }
103        
104        @Override
105        public int getStatus() {
106                return myResponse.getStatus();
107        }
108        
109
110        @Override
111        public String getStatusInfo() {
112                return myResponse.getStatusInfo().getReasonPhrase();
113        }
114
115        @Override
116        public InputStream readEntity() {
117                return myResponse.readEntity(java.io.InputStream.class);
118        }       
119        
120
121}