001package ca.uhn.fhir.okhttp.client; 002 003import ca.uhn.fhir.rest.api.RequestTypeEnum; 004import ca.uhn.fhir.rest.client.api.BaseHttpRequest; 005import ca.uhn.fhir.rest.client.api.IHttpRequest; 006import ca.uhn.fhir.rest.client.api.IHttpResponse; 007import ca.uhn.fhir.util.StopWatch; 008import okhttp3.Call; 009import okhttp3.Call.Factory; 010import okhttp3.Request; 011import okhttp3.RequestBody; 012 013import java.io.IOException; 014import java.util.Collections; 015import java.util.List; 016import java.util.Map; 017 018/* 019 * #%L 020 * HAPI FHIR OkHttp Client 021 * %% 022 * Copyright (C) 2014 - 2021 Smile CDR, Inc. 023 * %% 024 * Licensed under the Apache License, Version 2.0 (the "License"); 025 * you may not use this file except in compliance with the License. 026 * You may obtain a copy of the License at 027 * 028 * http://www.apache.org/licenses/LICENSE-2.0 029 * 030 * Unless required by applicable law or agreed to in writing, software 031 * distributed under the License is distributed on an "AS IS" BASIS, 032 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 033 * See the License for the specific language governing permissions and 034 * limitations under the License. 035 * #L% 036 */ 037 038/** 039 * Adapter for building an OkHttp-specific request. 040 * 041 * @author Matthew Clarke | matthew.clarke@orionhealth.com | Orion Health 042 */ 043public class OkHttpRestfulRequest extends BaseHttpRequest implements IHttpRequest { 044 045 private final Request.Builder myRequestBuilder; 046 private Factory myClient; 047 private String myUrl; 048 private RequestTypeEnum myRequestTypeEnum; 049 private RequestBody myRequestBody; 050 051 public OkHttpRestfulRequest(Call.Factory theClient, String theUrl, RequestTypeEnum theRequestTypeEnum, RequestBody theRequestBody) { 052 myClient = theClient; 053 myUrl = theUrl; 054 myRequestTypeEnum = theRequestTypeEnum; 055 myRequestBody = theRequestBody; 056 057 myRequestBuilder = new Request.Builder().url(theUrl); 058 } 059 060 public Request.Builder getRequest() { 061 return myRequestBuilder; 062 } 063 064 @Override 065 public void addHeader(String theName, String theValue) { 066 myRequestBuilder.addHeader(theName, theValue); 067 } 068 069 @Override 070 public IHttpResponse execute() throws IOException { 071 StopWatch responseStopWatch = new StopWatch(); 072 myRequestBuilder.method(getHttpVerbName(), myRequestBody); 073 Call call = myClient.newCall(myRequestBuilder.build()); 074 return new OkHttpRestfulResponse(call.execute(), responseStopWatch); 075 } 076 077 @Override 078 public Map<String, List<String>> getAllHeaders() { 079 return Collections.unmodifiableMap(myRequestBuilder.build().headers().toMultimap()); 080 } 081 082 @Override 083 public String getRequestBodyFromStream() { 084 // returning null to indicate this is not supported, as documented in IHttpRequest's contract 085 return null; 086 } 087 088 @Override 089 public String getUri() { 090 return myUrl; 091 } 092 093 @Override 094 public void setUri(String theUrl) { 095 myUrl = theUrl; 096 } 097 098 @Override 099 public String getHttpVerbName() { 100 return myRequestTypeEnum.name(); 101 } 102 103 @Override 104 public void removeHeaders(String theHeaderName) { 105 myRequestBuilder.removeHeader(theHeaderName); 106 } 107 108}