001/*
002 * JGrapes Event Driven Framework
003 * Copyright (C) 2018 Michael N. Lipp
004 * 
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Affero General Public License as published by 
007 * the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 * 
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Affero General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jgrapes.portal.base;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.time.Instant;
025
026import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
027import org.jdrupes.httpcodec.protocols.http.HttpField;
028import org.jdrupes.httpcodec.protocols.http.HttpResponse;
029import org.jdrupes.httpcodec.types.MediaType;
030import org.jgrapes.http.ResponseCreationSupport;
031import org.jgrapes.http.events.Response;
032import org.jgrapes.io.util.InputStreamPipeline;
033import org.jgrapes.portal.base.events.ResourceRequest;
034
035/**
036 * Returns an {@link OutputStream} as result.
037 */
038public class ResourceByInputStream extends ResourceResult {
039
040    private final InputStream stream;
041    private final MediaType mediaType;
042    private final Instant lastModifiedAt;
043    private final int maxAge;
044
045    /**
046     * Instantiates a result that is provided by an {@link OutputStream}.
047     *
048     * @param request the request
049     * @param stream the stream
050     * @param mediaType the media type, may be `null`
051     * @param lastModifiedAt the last modified at
052     * @param maxAge the max age
053     */
054    public ResourceByInputStream(ResourceRequest request,
055            InputStream stream, MediaType mediaType,
056            Instant lastModifiedAt, int maxAge) {
057        super(request);
058        this.stream = stream;
059        this.mediaType = mediaType;
060        this.lastModifiedAt = lastModifiedAt;
061        this.maxAge = maxAge;
062    }
063
064    @Override
065    public void process() throws IOException, InterruptedException {
066        if (stream == null) {
067            ResponseCreationSupport.sendResponse(request().httpRequest(),
068                request().httpChannel(), HttpStatus.NOT_FOUND);
069            return;
070        }
071
072        HttpResponse response = request().httpRequest().response().get();
073        if (lastModifiedAt != null) {
074            response.setField(HttpField.LAST_MODIFIED, lastModifiedAt);
075        }
076        if (mediaType != null) {
077            response.setContentType(mediaType);
078        } else {
079            // Usually a set by setContentType
080            response.setHasPayload(true);
081        }
082        ResponseCreationSupport.setMaxAge(response, maxAge);
083        response.setStatus(HttpStatus.OK);
084        request().httpChannel().respond(new Response(response));
085        // Start sending content
086        new InputStreamPipeline(stream, request().httpChannel()).run();
087    }
088
089}