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