001package ca.uhn.fhir.rest.server;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2021 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 java.io.IOException;
024import java.util.*;
025
026import org.hl7.fhir.instance.model.api.*;
027
028import ca.uhn.fhir.rest.api.SummaryEnum;
029import ca.uhn.fhir.rest.api.server.IRestfulResponse;
030import ca.uhn.fhir.rest.api.server.RequestDetails;
031
032public abstract class RestfulResponse<T extends RequestDetails> implements IRestfulResponse {
033
034        private IIdType myOperationResourceId;
035        private IPrimitiveType<Date> myOperationResourceLastUpdated;
036        private Map<String, List<String>> theHeaders = new HashMap<>();
037        private T theRequestDetails;
038
039        public RestfulResponse(T requestDetails) {
040                this.theRequestDetails = requestDetails;
041        }
042
043        @Override
044        public void addHeader(String headerKey, String headerValue) {
045                this.getHeaders().computeIfAbsent(headerKey, k -> new ArrayList<>()).add(headerValue);
046        }
047
048        /**
049         * Get the http headers
050         * @return the headers
051         */
052        @Override
053        public Map<String, List<String>> getHeaders() {
054                return theHeaders;
055        }
056
057        /**
058         * Get the requestDetails
059         * @return the requestDetails
060         */
061        public T getRequestDetails() {
062                return theRequestDetails;
063        }
064
065        @Override
066        public void setOperationResourceId(IIdType theOperationResourceId) {
067                myOperationResourceId = theOperationResourceId;
068        }
069
070        @Override
071        public void setOperationResourceLastUpdated(IPrimitiveType<Date> theOperationResourceLastUpdated) {
072                myOperationResourceLastUpdated = theOperationResourceLastUpdated;
073        }
074
075        /**
076         * Set the requestDetails
077         * @param requestDetails the requestDetails to set
078         */
079        public void setRequestDetails(T requestDetails) {
080                this.theRequestDetails = requestDetails;
081        }
082
083        @Override
084        public final Object streamResponseAsResource(IBaseResource theResource, boolean thePrettyPrint, Set<SummaryEnum> theSummaryMode,
085                        int theStatusCode, String theStatusMessage, boolean theRespondGzip, boolean theAddContentLocation)
086                                        throws IOException {
087                return RestfulServerUtils.streamResponseAsResource(theRequestDetails.getServer(), theResource, theSummaryMode, theStatusCode, theStatusMessage, theAddContentLocation, theRespondGzip, getRequestDetails(), myOperationResourceId, myOperationResourceLastUpdated);
088
089        }
090
091}