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