001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.api.server;
021
022import ca.uhn.fhir.rest.server.BaseRestfulResponse;
023import ca.uhn.fhir.util.IoUtil;
024import jakarta.annotation.Nonnull;
025import jakarta.annotation.Nullable;
026import org.apache.commons.lang3.Validate;
027
028import java.io.ByteArrayOutputStream;
029import java.io.Closeable;
030import java.io.IOException;
031import java.io.OutputStream;
032import java.io.StringWriter;
033import java.io.Writer;
034
035/**
036 * A default RestfulResponse that returns the body as an IBaseResource and ignores everything else.
037 */
038public class SystemRestfulResponse extends BaseRestfulResponse<SystemRequestDetails> {
039        private Writer myWriter;
040        private ByteArrayOutputStream myOutputStream;
041
042        public SystemRestfulResponse(SystemRequestDetails theSystemRequestDetails) {
043                super(theSystemRequestDetails);
044        }
045
046        @Nonnull
047        @Override
048        public Writer getResponseWriter(int theStatusCode, String theContentType, String theCharset, boolean theRespondGzip)
049                        throws IOException {
050                Validate.isTrue(myWriter == null, "getResponseWriter() called multiple times");
051                Validate.isTrue(myOutputStream == null, "getResponseWriter() called after getResponseOutputStream()");
052
053                myWriter = new StringWriter();
054                return myWriter;
055        }
056
057        @Nonnull
058        @Override
059        public OutputStream getResponseOutputStream(
060                        int theStatusCode, String theContentType, @Nullable Integer theContentLength) throws IOException {
061                Validate.isTrue(myWriter == null, "getResponseOutputStream() called multiple times");
062                Validate.isTrue(myOutputStream == null, "getResponseOutputStream() called after getResponseWriter()");
063
064                myOutputStream = new ByteArrayOutputStream();
065                return myOutputStream;
066        }
067
068        @Override
069        public Object commitResponse(@Nonnull Closeable theWriterOrOutputStream) throws IOException {
070                IoUtil.closeQuietly(theWriterOrOutputStream);
071
072                return getRequestDetails().getServer().getFhirContext().newJsonParser().parseResource(myWriter.toString());
073        }
074}