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