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