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        public ServletRestfulResponse(ServletRequestDetails servletRequestDetails) {
044                super(servletRequestDetails);
045        }
046
047        @Override
048        public Object sendAttachmentResponse(IBaseBinary bin, int stausCode, String contentType) throws IOException {
049                addHeaders();
050                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
051                theHttpResponse.setStatus(stausCode);
052                theHttpResponse.setContentType(contentType);
053                theHttpResponse.setCharacterEncoding(null);
054                if (bin.getContent() == null || bin.getContent().length == 0) {
055                        return theHttpResponse.getOutputStream();
056                }
057                theHttpResponse.setContentLength(bin.getContent().length);
058                ServletOutputStream oos = theHttpResponse.getOutputStream();
059                oos.write(bin.getContent());
060                return oos;
061        }
062
063        @Override
064        public Writer getResponseWriter(int theStatusCode, String theStatusMessage, String theContentType, String theCharset, boolean theRespondGzip) throws UnsupportedEncodingException, IOException {
065                addHeaders();
066                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
067                theHttpResponse.setCharacterEncoding(theCharset);
068                theHttpResponse.setStatus(theStatusCode);
069                theHttpResponse.setContentType(theContentType);
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                }
074                return theHttpResponse.getWriter();
075        }
076
077        private void addHeaders() {
078                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
079                getRequestDetails().getServer().addHeadersToResponse(theHttpResponse);
080                for (Entry<String, List<String>> header : getHeaders().entrySet()) {
081                        final String key = header.getKey();
082                        boolean first = true;
083                        for (String value : header.getValue()) {
084                                // existing headers should be overridden
085                                if (first) {
086                                        theHttpResponse.setHeader(key, value);
087                                        first = false;
088                                } else {
089                                        theHttpResponse.addHeader(key, value);
090                                }
091                        }
092                }
093        }
094
095        @Override
096        public final Object sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) throws IOException {
097                return theWriter;
098        }
099
100        @Override
101        public Object returnResponse(ParseAction<?> outcome, int operationStatus, boolean allowPrefer, MethodOutcome response, String resourceName) throws IOException {
102                addHeaders();
103                return getRequestDetails().getServer().returnResponse(getRequestDetails(), outcome, operationStatus, allowPrefer, response, resourceName);
104        }
105}