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 - 2022 Smile CDR, Inc. 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 bufferEntity() throws IOException { 056 if(!myBufferedEntity && myResponse.hasEntity()) { 057 myBufferedEntity = true; 058 myResponse.bufferEntity(); 059 } else { 060 myResponse.bufferEntity(); 061 } 062 } 063 064 @Override 065 public void close() { 066 // automatically done by jax-rs 067 } 068 069 @Override 070 public Reader createReader() { 071 if (!myBufferedEntity && !myResponse.hasEntity()) { 072 return new StringReader(""); 073 } else { 074 return new StringReader(myResponse.readEntity(String.class)); 075 } 076 } 077 078 @Override 079 public Map<String, List<String>> getAllHeaders() { 080 Map<String, List<String>> theHeaders = new ConcurrentHashMap<String, List<String>>(); 081 for (Entry<String, List<String>> iterable_element : myResponse.getStringHeaders().entrySet()) { 082 theHeaders.put(iterable_element.getKey().toLowerCase(), iterable_element.getValue()); 083 } 084 return theHeaders; 085 } 086 087 @Override 088 public String getMimeType() { 089 MediaType mediaType = myResponse.getMediaType(); 090 if (mediaType == null) { 091 return null; 092 } 093 //Keep only type and subtype and do not include the parameters such as charset 094 return new MediaType(mediaType.getType(), mediaType.getSubtype()).toString(); 095 } 096 097 @Override 098 public Response getResponse() { 099 return myResponse; 100 } 101 102 @Override 103 public int getStatus() { 104 return myResponse.getStatus(); 105 } 106 107 108 @Override 109 public String getStatusInfo() { 110 return myResponse.getStatusInfo().getReasonPhrase(); 111 } 112 113 @Override 114 public InputStream readEntity() { 115 if (!myBufferedEntity && !myResponse.hasEntity()) { 116 return new ByteArrayInputStream(new byte[0]); 117 } else { 118 return new ByteArrayInputStream(myResponse.readEntity(byte[].class)); 119 } 120 } 121 122 @Override 123 public List<String> getHeaders(String theName) { 124 List<String> retVal = myResponse.getStringHeaders().get(theName); 125 return retVal; 126 } 127 128 129}