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