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