001package ca.uhn.fhir.jaxrs.client; 002 003import java.io.*; 004 005/* 006 * #%L 007 * HAPI FHIR JAX-RS Server 008 * %% 009 * Copyright (C) 2014 - 2019 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.util.List; 026import java.util.Map; 027import java.util.Map.Entry; 028import java.util.concurrent.ConcurrentHashMap; 029 030import javax.ws.rs.core.MediaType; 031import javax.ws.rs.core.Response; 032 033import ca.uhn.fhir.rest.client.impl.BaseHttpResponse; 034import ca.uhn.fhir.rest.server.exceptions.InternalErrorException; 035import ca.uhn.fhir.util.StopWatch; 036 037import ca.uhn.fhir.rest.client.api.IHttpResponse; 038import org.apache.commons.io.IOUtils; 039 040/** 041 * A Http Response based on JaxRs. This is an adapter around the class {@link javax.ws.rs.core.Response Response} 042 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 043 */ 044public class JaxRsHttpResponse extends BaseHttpResponse implements IHttpResponse { 045 046 private boolean myBufferedEntity = false; 047 private final Response myResponse; 048 049 public JaxRsHttpResponse(Response theResponse, StopWatch theResponseStopWatch) { 050 super(theResponseStopWatch); 051 this.myResponse = theResponse; 052 } 053 054 @Override 055 public void bufferEntitity() throws IOException { 056 bufferEntity(); 057 } 058 059 @Override 060 public void bufferEntity() throws IOException { 061 if(!myBufferedEntity && myResponse.hasEntity()) { 062 myBufferedEntity = true; 063 myResponse.bufferEntity(); 064 } else { 065 myResponse.bufferEntity(); 066 } 067 } 068 069 @Override 070 public void close() { 071 // automatically done by jax-rs 072 } 073 074 @Override 075 public Reader createReader() { 076 if (!myBufferedEntity && !myResponse.hasEntity()) { 077 return new StringReader(""); 078 } else { 079 return new StringReader(myResponse.readEntity(String.class)); 080 } 081 } 082 083 @Override 084 public Map<String, List<String>> getAllHeaders() { 085 Map<String, List<String>> theHeaders = new ConcurrentHashMap<String, List<String>>(); 086 for (Entry<String, List<String>> iterable_element : myResponse.getStringHeaders().entrySet()) { 087 theHeaders.put(iterable_element.getKey().toLowerCase(), iterable_element.getValue()); 088 } 089 return theHeaders; 090 } 091 092 @Override 093 public String getMimeType() { 094 MediaType mediaType = myResponse.getMediaType(); 095 if (mediaType == null) { 096 return null; 097 } 098 //Keep only type and subtype and do not include the parameters such as charset 099 return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString(); 100 } 101 102 @Override 103 public Response getResponse() { 104 return myResponse; 105 } 106 107 @Override 108 public int getStatus() { 109 return myResponse.getStatus(); 110 } 111 112 113 @Override 114 public String getStatusInfo() { 115 return myResponse.getStatusInfo().getReasonPhrase(); 116 } 117 118 @Override 119 public InputStream readEntity() { 120 if (!myBufferedEntity && !myResponse.hasEntity()) { 121 return new ByteArrayInputStream(new byte[0]); 122 } else { 123 return new ByteArrayInputStream(myResponse.readEntity(byte[].class)); 124 } 125 } 126 127 @Override 128 public List<String> getHeaders(String theName) { 129 List<String> retVal = myResponse.getStringHeaders().get(theName); 130 return retVal; 131 } 132 133 134}