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.webconsole.base;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.time.Instant;
025import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
026import org.jdrupes.httpcodec.protocols.http.HttpField;
027import org.jdrupes.httpcodec.protocols.http.HttpResponse;
028import org.jdrupes.httpcodec.types.MediaType;
029import org.jgrapes.http.ResponseCreationSupport;
030import org.jgrapes.http.events.Response;
031import org.jgrapes.io.util.InputStreamPipeline;
032import org.jgrapes.webconsole.base.events.ResourceRequest;
033
034/**
035 * Returns an {@link OutputStream} as result.
036 */
037public class ResourceByInputStream extends ResourceResult {
038
039    private final InputStream stream;
040    private final MediaType mediaType;
041    private final Instant lastModifiedAt;
042    private final int maxAge;
043
044    /**
045     * Instantiates a result that is provided by an {@link OutputStream}.
046     *
047     * @param request the request
048     * @param stream the stream
049     * @param mediaType the media type, may be `null`
050     * @param lastModifiedAt the last modified at
051     * @param maxAge the max age
052     */
053    public ResourceByInputStream(ResourceRequest request,
054            InputStream stream, MediaType mediaType,
055            Instant lastModifiedAt, int maxAge) {
056        super(request);
057        this.stream = stream;
058        this.mediaType = mediaType;
059        this.lastModifiedAt = lastModifiedAt;
060        this.maxAge = maxAge;
061    }
062
063    @Override
064    @SuppressWarnings("PMD.ConfusingTernary")
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}