001package ca.uhn.fhir.rest.server.servlet;
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.io.OutputStreamWriter;
025import java.io.UnsupportedEncodingException;
026import java.io.Writer;
027import java.util.Map.Entry;
028import java.util.zip.GZIPOutputStream;
029
030import javax.servlet.ServletOutputStream;
031import javax.servlet.http.HttpServletResponse;
032
033import org.hl7.fhir.instance.model.api.IBaseBinary;
034
035import ca.uhn.fhir.rest.api.MethodOutcome;
036import ca.uhn.fhir.rest.method.ParseAction;
037import ca.uhn.fhir.rest.server.Constants;
038import ca.uhn.fhir.rest.server.RestfulResponse;
039
040public class ServletRestfulResponse extends RestfulResponse<ServletRequestDetails> {
041
042        public ServletRestfulResponse(ServletRequestDetails servletRequestDetails) {
043                super(servletRequestDetails);
044        }
045
046        @Override
047        public Object sendAttachmentResponse(IBaseBinary bin, int stausCode, String contentType) throws IOException {
048                addHeaders();
049                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
050                theHttpResponse.setStatus(stausCode);
051                theHttpResponse.setContentType(contentType);
052                if (bin.getContent() == null || bin.getContent().length == 0) {
053                        return theHttpResponse.getOutputStream();
054                }
055                theHttpResponse.setContentLength(bin.getContent().length);
056                ServletOutputStream oos = theHttpResponse.getOutputStream();
057                oos.write(bin.getContent());
058                return oos;
059        }
060
061        @Override
062        public Writer getResponseWriter(int theStatusCode, String theStatusMessage, String theContentType, String theCharset, boolean theRespondGzip) throws UnsupportedEncodingException, IOException {
063                addHeaders();
064                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
065                theHttpResponse.setCharacterEncoding(theCharset);
066                theHttpResponse.setStatus(theStatusCode);
067                theHttpResponse.setContentType(theContentType);
068                if (theRespondGzip) {
069                        theHttpResponse.addHeader(Constants.HEADER_CONTENT_ENCODING, Constants.ENCODING_GZIP);
070                        return new OutputStreamWriter(new GZIPOutputStream(theHttpResponse.getOutputStream()), Constants.CHARSET_NAME_UTF8);
071                }
072                return theHttpResponse.getWriter();
073        }
074
075        private void addHeaders() {
076                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
077                getRequestDetails().getServer().addHeadersToResponse(theHttpResponse);
078                for (Entry<String, String> header : getHeaders().entrySet()) {
079                        theHttpResponse.setHeader(header.getKey(), header.getValue());
080                }
081        }
082
083        @Override
084        public final Object sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) throws IOException {
085                return theWriter;
086        }
087
088        @Override
089        public Object returnResponse(ParseAction<?> outcome, int operationStatus, boolean allowPrefer, MethodOutcome response, String resourceName) throws IOException {
090                addHeaders();
091                return getRequestDetails().getServer().returnResponse(getRequestDetails(), outcome, operationStatus, allowPrefer, response, resourceName);
092        }
093}