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