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.time.Instant;
023import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus;
024import org.jdrupes.httpcodec.protocols.http.HttpField;
025import org.jdrupes.httpcodec.protocols.http.HttpResponse;
026import org.jgrapes.http.ResponseCreationSupport;
027import org.jgrapes.http.events.Response;
028import org.jgrapes.webconsole.base.events.ResourceRequest;
029
030/**
031 * Indicates that a resource provider found the resource to be
032 * unmodified.
033 */
034public class ResourceNotModified extends ResourceResult {
035
036    private final Instant lastModifiedAt;
037    private final int maxAge;
038
039    /**
040     * Creates a new instance.
041     *
042     * @param request the request
043     */
044    public ResourceNotModified(ResourceRequest request, Instant lastModifiedAt,
045            int maxAge) {
046        super(request);
047        this.lastModifiedAt = lastModifiedAt;
048        this.maxAge = maxAge;
049    }
050
051    /** 
052     * Send header only, because the resource does not have to be sent.
053     */
054    @Override
055    public void process() throws IOException, InterruptedException {
056        HttpResponse response = request().httpRequest().response().get();
057        if (lastModifiedAt != null) {
058            response.setField(HttpField.LAST_MODIFIED, lastModifiedAt);
059        }
060        ResponseCreationSupport.setMaxAge(response, maxAge);
061        response.setStatus(HttpStatus.NOT_MODIFIED);
062        request().httpChannel().respond(new Response(response));
063    }
064
065}