001package ca.uhn.fhir.jaxrs.client; 002 003/* 004 * #%L 005 * HAPI FHIR JAX-RS Server 006 * %% 007 * Copyright (C) 2014 - 2019 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 ca.uhn.fhir.rest.api.RequestTypeEnum; 024import ca.uhn.fhir.rest.client.api.IHttpRequest; 025import ca.uhn.fhir.rest.client.api.IHttpResponse; 026import ca.uhn.fhir.util.StopWatch; 027 028import javax.ws.rs.client.Entity; 029import javax.ws.rs.client.Invocation; 030import javax.ws.rs.core.Response; 031import java.util.*; 032 033/** 034 * A Http Request based on JaxRs. This is an adapter around the class 035 * {@link javax.ws.rs.client.Invocation Invocation} 036 * 037 * @author Peter Van Houte | peter.vanhoute@agfa.com | Agfa Healthcare 038 */ 039public class JaxRsHttpRequest implements IHttpRequest { 040 041 private final Map<String, List<String>> myHeaders = new HashMap<>(); 042 private Invocation.Builder myRequest; 043 private RequestTypeEnum myRequestType; 044 private Entity<?> myEntity; 045 046 public JaxRsHttpRequest(Invocation.Builder theRequest, RequestTypeEnum theRequestType, Entity<?> theEntity) { 047 this.myRequest = theRequest; 048 this.myRequestType = theRequestType; 049 this.myEntity = theEntity; 050 } 051 052 @Override 053 public void addHeader(String theName, String theValue) { 054 if (!myHeaders.containsKey(theName)) { 055 myHeaders.put(theName, new LinkedList<>()); 056 } 057 myHeaders.get(theName).add(theValue); 058 getRequest().header(theName, theValue); 059 } 060 061 @Override 062 public IHttpResponse execute() { 063 StopWatch responseStopWatch = new StopWatch(); 064 Invocation invocation = getRequest().build(getRequestType().name(), getEntity()); 065 Response response = invocation.invoke(); 066 return new JaxRsHttpResponse(response, responseStopWatch); 067 } 068 069 @Override 070 public Map<String, List<String>> getAllHeaders() { 071 return Collections.unmodifiableMap(this.myHeaders); 072 } 073 074 /** 075 * Get the Entity 076 * 077 * @return the entity 078 */ 079 public Entity<?> getEntity() { 080 return myEntity; 081 } 082 083 @Override 084 public String getHttpVerbName() { 085 return myRequestType.name(); 086 } 087 088 @Override 089 public void removeHeaders(String theHeaderName) { 090 myHeaders.remove(theHeaderName); 091 } 092 093 /** 094 * Get the Request 095 * 096 * @return the Request 097 */ 098 public Invocation.Builder getRequest() { 099 return myRequest; 100 } 101 102 @Override 103 public String getRequestBodyFromStream() { 104 // not supported 105 return null; 106 } 107 108 /** 109 * Get the Request Type 110 * 111 * @return the request type 112 */ 113 public RequestTypeEnum getRequestType() { 114 return myRequestType == null ? RequestTypeEnum.GET : myRequestType; 115 } 116 117 @Override 118 public String getUri() { 119 return ""; // TODO: can we get this from somewhere? 120 } 121 122}