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.OutputStream;
023import java.time.Instant;
024import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
025import org.jdrupes.httpcodec.protocols.http.HttpField;
026import org.jdrupes.httpcodec.protocols.http.HttpResponse;
027import org.jdrupes.httpcodec.types.MediaType;
028import org.jgrapes.http.ResponseCreationSupport;
029import org.jgrapes.http.events.Response;
030import org.jgrapes.io.IOSubchannel;
031import org.jgrapes.webconsole.base.events.ResourceRequest;
032
033/**
034 * Returns a {@link Runnable} that writes to an {@link OutputStream} as result.
035 */
036public class ResourceByProducer extends ResourceResult {
037
038    private final ContentProducer contentProducer;
039    private final MediaType mediaType;
040    private final Instant lastModifiedAt;
041    private final int maxAge;
042
043    /**
044     * Instantiates a result that is provided by a {@link ContentProducer}.
045     *
046     * @param request the request
047     * @param contentProducer the content producer
048     * @param mediaType the media type, may be `null`
049     * @param lastModifiedAt the last modified at
050     * @param maxAge the max age
051     */
052    public ResourceByProducer(ResourceRequest request,
053            ContentProducer contentProducer, MediaType mediaType,
054            Instant lastModifiedAt, int maxAge) {
055        super(request);
056        this.contentProducer = contentProducer;
057        this.mediaType = mediaType;
058        this.lastModifiedAt = lastModifiedAt;
059        this.maxAge = maxAge;
060    }
061
062    /*
063     * (non-Javadoc)
064     * 
065     * @see org.jgrapes.webconsole.base.base.ResourceResult#process()
066     */
067    @Override
068    public void process() throws IOException, InterruptedException {
069        if (contentProducer == null) {
070            ResponseCreationSupport.sendResponse(request().httpRequest(),
071                request().httpChannel(), HttpStatus.NOT_FOUND);
072            return;
073        }
074
075        HttpResponse response = request().httpRequest().response().get();
076        if (lastModifiedAt != null) {
077            response.setField(HttpField.LAST_MODIFIED, lastModifiedAt);
078        }
079        if (mediaType != null) {
080            response.setContentType(mediaType);
081        } else {
082            // Usually set by setContenType
083            response.setHasPayload(true);
084        }
085        ResponseCreationSupport.setMaxAge(response, maxAge);
086        response.setStatus(HttpStatus.OK);
087        request().httpChannel().respond(new Response(response));
088        // Start sending content
089        contentProducer.produce(request().httpChannel());
090    }
091
092    /**
093     * Must be implemented by a content producer.
094     */
095    @FunctionalInterface
096    public interface ContentProducer {
097
098        /**
099         * Fire output events on the given channel.
100         *
101         * @param channel the channel
102         * @throws IOException Signals that an I/O exception has occurred.
103         */
104        void produce(IOSubchannel channel) throws IOException;
105    }
106}