001package ca.uhn.fhir.rest.server; 002 003/* 004 * #%L 005 * HAPI FHIR - Server Framework 006 * %% 007 * Copyright (C) 2014 - 2019 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.*; 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 public Map<String, List<String>> getHeaders() { 053 return theHeaders; 054 } 055 056 /** 057 * Get the requestDetails 058 * @return the requestDetails 059 */ 060 public T getRequestDetails() { 061 return theRequestDetails; 062 } 063 064 @Override 065 public void setOperationResourceId(IIdType theOperationResourceId) { 066 myOperationResourceId = theOperationResourceId; 067 } 068 069 @Override 070 public void setOperationResourceLastUpdated(IPrimitiveType<Date> theOperationResourceLastUpdated) { 071 myOperationResourceLastUpdated = theOperationResourceLastUpdated; 072 } 073 074 /** 075 * Set the requestDetails 076 * @param requestDetails the requestDetails to set 077 */ 078 public void setRequestDetails(T requestDetails) { 079 this.theRequestDetails = requestDetails; 080 } 081 082 @Override 083 public final Object streamResponseAsResource(IBaseResource theResource, boolean thePrettyPrint, Set<SummaryEnum> theSummaryMode, 084 int theStatusCode, String theStatusMessage, boolean theRespondGzip, boolean theAddContentLocation) 085 throws IOException { 086 return RestfulServerUtils.streamResponseAsResource(theRequestDetails.getServer(), theResource, theSummaryMode, theStatusCode, theStatusMessage, theAddContentLocation, theRespondGzip, getRequestDetails(), myOperationResourceId, myOperationResourceLastUpdated); 087 088 } 089 090}