001package ca.uhn.fhir.jaxrs.client; 002 003/* 004 * #%L 005 * HAPI FHIR JAX-RS Server 006 * %% 007 * Copyright (C) 2014 - 2022 Smile CDR, Inc. 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 */ 022 023import ca.uhn.fhir.context.FhirContext; 024import ca.uhn.fhir.rest.api.Constants; 025import ca.uhn.fhir.rest.api.EncodingEnum; 026import ca.uhn.fhir.rest.api.RequestTypeEnum; 027import ca.uhn.fhir.rest.client.api.Header; 028import ca.uhn.fhir.rest.client.api.HttpClientUtil; 029import ca.uhn.fhir.rest.client.api.IHttpClient; 030import ca.uhn.fhir.rest.client.api.IHttpRequest; 031import ca.uhn.fhir.rest.client.impl.BaseHttpClientInvocation; 032import ca.uhn.fhir.rest.client.method.MethodUtil; 033import javax.ws.rs.client.Client; 034import javax.ws.rs.client.Entity; 035import javax.ws.rs.client.Invocation.Builder; 036import javax.ws.rs.core.Form; 037import javax.ws.rs.core.MultivaluedHashMap; 038import javax.ws.rs.core.MultivaluedMap; 039import org.hl7.fhir.instance.model.api.IBaseBinary; 040 041import java.util.List; 042import java.util.Map; 043 044/** 045 * A Http Request based on JaxRs. This is an adapter around the class 046 * {@link javax.ws.rs.client.Client Client} 047 * 048 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 049 */ 050public class JaxRsHttpClient implements IHttpClient { 051 052 private Client myClient; 053 private List<Header> myHeaders; 054 private StringBuilder myUrl; 055 private Map<String, List<String>> myIfNoneExistParams; 056 private String myIfNoneExistString; 057 private RequestTypeEnum myRequestType; 058 059 public JaxRsHttpClient(Client theClient, StringBuilder theUrl, Map<String, List<String>> theIfNoneExistParams, String theIfNoneExistString, 060 RequestTypeEnum theRequestType, List<Header> theHeaders) { 061 this.myClient = theClient; 062 this.myUrl = theUrl; 063 this.myIfNoneExistParams = theIfNoneExistParams; 064 this.myIfNoneExistString = theIfNoneExistString; 065 this.myRequestType = theRequestType; 066 this.myHeaders = theHeaders; 067 } 068 069 @Override 070 public IHttpRequest createByteRequest(FhirContext theContext, String theContents, String theContentType, EncodingEnum theEncoding) { 071 Entity<String> entity = Entity.entity(theContents, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8); 072 JaxRsHttpRequest retVal = createHttpRequest(entity); 073 addHeadersToRequest(retVal, theEncoding, theContext); 074 retVal.addHeader(Constants.HEADER_CONTENT_TYPE, theContentType + Constants.HEADER_SUFFIX_CT_UTF_8); 075 return retVal; 076 } 077 078 @Override 079 public IHttpRequest createParamRequest(FhirContext theContext, Map<String, List<String>> theParams, EncodingEnum theEncoding) { 080 MultivaluedMap<String, String> map = new MultivaluedHashMap<String, String>(); 081 for (Map.Entry<String, List<String>> nextParam : theParams.entrySet()) { 082 List<String> value = nextParam.getValue(); 083 for (String s : value) { 084 map.add(nextParam.getKey(), s); 085 } 086 } 087 Entity<Form> entity = Entity.form(map); 088 JaxRsHttpRequest retVal = createHttpRequest(entity); 089 addHeadersToRequest(retVal, theEncoding, theContext); 090 return retVal; 091 } 092 093 @Override 094 public IHttpRequest createBinaryRequest(FhirContext theContext, IBaseBinary theBinary) { 095 Entity<String> entity = Entity.entity(theBinary.getContentAsBase64(), theBinary.getContentType()); 096 JaxRsHttpRequest retVal = createHttpRequest(entity); 097 addHeadersToRequest(retVal, null, theContext); 098 return retVal; 099 } 100 101 @Override 102 public IHttpRequest createGetRequest(FhirContext theContext, EncodingEnum theEncoding) { 103 JaxRsHttpRequest result = createHttpRequest(null); 104 addHeadersToRequest(result, theEncoding, theContext); 105 return result; 106 } 107 108 public void addHeadersToRequest(JaxRsHttpRequest theHttpRequest, EncodingEnum theEncoding, FhirContext theContext) { 109 if (myHeaders != null) { 110 for (Header next : myHeaders) { 111 theHttpRequest.addHeader(next.getName(), next.getValue()); 112 } 113 } 114 115 theHttpRequest.addHeader("User-Agent", HttpClientUtil.createUserAgentString(theContext, "jax-rs")); 116 theHttpRequest.addHeader("Accept-Charset", "utf-8"); 117 118 Builder request = theHttpRequest.getRequest(); 119 request.acceptEncoding("gzip"); 120 121 MethodUtil.addAcceptHeaderToRequest(theEncoding, theHttpRequest, theContext); 122 } 123 124 private JaxRsHttpRequest createHttpRequest(Entity<?> entity) { 125 Builder request = myClient.target(myUrl.toString()).request(); 126 JaxRsHttpRequest result = new JaxRsHttpRequest(request, myRequestType, entity); 127 addHeaderIfNoneExist(result); 128 return result; 129 } 130 131 private void addHeaderIfNoneExist(IHttpRequest result) { 132 if (myIfNoneExistParams != null) { 133 StringBuilder b = newHeaderBuilder(myUrl); 134 BaseHttpClientInvocation.appendExtraParamsWithQuestionMark(myIfNoneExistParams, b, b.indexOf("?") == -1); 135 result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString()); 136 } 137 138 if (myIfNoneExistString != null) { 139 StringBuilder b = newHeaderBuilder(myUrl); 140 b.append(b.indexOf("?") == -1 ? '?' : '&'); 141 b.append(myIfNoneExistString.substring(myIfNoneExistString.indexOf('?') + 1)); 142 result.addHeader(Constants.HEADER_IF_NONE_EXIST, b.toString()); 143 } 144 } 145 146 private StringBuilder newHeaderBuilder(StringBuilder theUrlBase) { 147 StringBuilder b = new StringBuilder(); 148 b.append(theUrlBase); 149 if (theUrlBase.length() > 0 && theUrlBase.charAt(theUrlBase.length() - 1) == '/') { 150 b.deleteCharAt(b.length() - 1); 151 } 152 return b; 153 } 154 155}