001package ca.uhn.fhir.rest.server.servlet;
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.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 null;
054                } else {
055                        theHttpResponse.setContentLength(bin.getContent().length);
056                        ServletOutputStream oos = theHttpResponse.getOutputStream();
057                        oos.write(bin.getContent());
058                        oos.close();
059                        return null;
060                }
061        }
062
063        @Override
064        public Writer getResponseWriter(int statusCode, String contentType, String charset, boolean theRespondGzip) throws UnsupportedEncodingException, IOException {
065                addHeaders();
066                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
067                theHttpResponse.setCharacterEncoding(charset);
068                theHttpResponse.setStatus(statusCode);
069                theHttpResponse.setContentType(contentType);
070                if (theRespondGzip) {
071                        theHttpResponse.addHeader(Constants.HEADER_CONTENT_ENCODING, Constants.ENCODING_GZIP);
072                        return new OutputStreamWriter(new GZIPOutputStream(theHttpResponse.getOutputStream()), Constants.CHARSET_NAME_UTF8);
073                } else {
074                        return theHttpResponse.getWriter();
075                }
076        }
077
078        private void addHeaders() {
079                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
080                getRequestDetails().getServer().addHeadersToResponse(theHttpResponse);
081                for (Entry<String, String> header : getHeaders().entrySet()) {
082                        theHttpResponse.setHeader(header.getKey(), header.getValue());
083                }
084        }
085
086        @Override
087        public final Object sendWriterResponse(int status, String contentType, String charset, Writer writer) throws IOException {
088                writer.close();
089                return null;
090        }
091
092        @Override
093        public Object returnResponse(ParseAction<?> outcome, int operationStatus, boolean allowPrefer, MethodOutcome response, String resourceName) throws IOException {
094                addHeaders();
095                return getRequestDetails().getServer().returnResponse(getRequestDetails(), outcome, operationStatus, allowPrefer, response, resourceName);
096        }
097}