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