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    @SuppressWarnings("PMD.ConfusingTernary")
069    public void process() throws IOException, InterruptedException {
070        if (contentProducer == null) {
071            ResponseCreationSupport.sendResponse(request().httpRequest(),
072                request().httpChannel(), HttpStatus.NOT_FOUND);
073            return;
074        }
075
076        HttpResponse response = request().httpRequest().response().get();
077        if (lastModifiedAt != null) {
078            response.setField(HttpField.LAST_MODIFIED, lastModifiedAt);
079        }
080        if (mediaType != null) {
081            response.setContentType(mediaType);
082        } else {
083            // Usually set by setContenType
084            response.setHasPayload(true);
085        }
086        ResponseCreationSupport.setMaxAge(response, maxAge);
087        response.setStatus(HttpStatus.OK);
088        request().httpChannel().respond(new Response(response));
089        // Start sending content
090        contentProducer.produce(request().httpChannel());
091    }
092
093    /**
094     * Must be implemented by a content producer.
095     */
096    @FunctionalInterface
097    public interface ContentProducer {
098
099        /**
100         * Fire output events on the given channel.
101         *
102         * @param channel the channel
103         * @throws IOException Signals that an I/O exception has occurred.
104         */
105        void produce(IOSubchannel channel) throws IOException;
106    }
107}