001package ca.uhn.fhir.rest.server.servlet;
002
003/*
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
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 ca.uhn.fhir.rest.api.Constants;
024import ca.uhn.fhir.rest.api.MethodOutcome;
025import ca.uhn.fhir.rest.api.server.ParseAction;
026import ca.uhn.fhir.rest.server.RestfulResponse;
027import org.apache.commons.lang3.StringUtils;
028import org.hl7.fhir.instance.model.api.IBaseBinary;
029
030import javax.servlet.ServletOutputStream;
031import javax.servlet.http.HttpServletResponse;
032import java.io.IOException;
033import java.io.OutputStream;
034import java.io.OutputStreamWriter;
035import java.io.Writer;
036import java.nio.charset.StandardCharsets;
037import java.util.List;
038import java.util.Map.Entry;
039import java.util.zip.GZIPOutputStream;
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 OutputStream sendAttachmentResponse(IBaseBinary theBinary, int theStatusCode, String contentType) throws IOException {
052                addHeaders();
053                HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
054                theHttpResponse.setStatus(theStatusCode);
055                theHttpResponse.setContentType(contentType);
056                theHttpResponse.setCharacterEncoding(null);
057                if (theBinary.getContent() == null || theBinary.getContent().length == 0) {
058                        return theHttpResponse.getOutputStream();
059                }
060                theHttpResponse.setContentLength(theBinary.getContent().length);
061                ServletOutputStream oos = theHttpResponse.getOutputStream();
062                oos.write(theBinary.getContent());
063                return oos;
064        }
065
066        @Override
067        public Writer getResponseWriter(int theStatusCode, String theStatusMessage, String theContentType, String theCharset, boolean theRespondGzip) throws 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()), StandardCharsets.UTF_8);
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                        String key = header.getKey();
085                        key = sanitizeHeaderField(key);
086                        boolean first = true;
087                        for (String value : header.getValue()) {
088                                value = sanitizeHeaderField(value);
089
090                                // existing headers should be overridden
091                                if (first) {
092                                        theHttpResponse.setHeader(key, value);
093                                        first = false;
094                                } else {
095                                        theHttpResponse.addHeader(key, value);
096                                }
097                        }
098                }
099        }
100
101        static String sanitizeHeaderField(String theKey) {
102                return StringUtils.replaceChars(theKey, "\r\n", null);
103        }
104
105        @Override
106        public final Writer sendWriterResponse(int theStatus, String theContentType, String theCharset, Writer theWriter) {
107                return theWriter;
108        }
109
110        @Override
111        public Object returnResponse(ParseAction<?> outcome, int operationStatus, boolean allowPrefer, MethodOutcome response, String resourceName) throws IOException {
112                addHeaders();
113                return getRequestDetails().getServer().returnResponse(getRequestDetails(), outcome, operationStatus, allowPrefer, response, resourceName);
114        }
115}