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