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.net.URL; 023import org.jdrupes.httpcodec.protocols.http.HttpConstants.HttpStatus; 024import org.jgrapes.http.ResponseCreationSupport; 025import org.jgrapes.webconsole.base.events.ResourceRequest; 026 027/** 028 * Returns a {@link URL} as result. The result will be processed by 029 * checking if the resource needs to be sent and, if this is the case, 030 * sending it. 031 */ 032public class ResourceByUrl extends ResourceResult { 033 private final URL resourceUrl; 034 035 /** 036 * Instantiates a new result represented by a {@link URL}. 037 * 038 * @param request the request 039 * @param resourceUrl the resource url 040 */ 041 public ResourceByUrl(ResourceRequest request, URL resourceUrl) { 042 super(request); 043 this.resourceUrl = resourceUrl; 044 } 045 046 @Override 047 public void process() throws IOException, InterruptedException { 048 if (resourceUrl == null) { 049 ResponseCreationSupport.sendResponse(request().httpRequest(), 050 request().httpChannel(), HttpStatus.NOT_FOUND); 051 return; 052 } 053 054 // Send resource 055 ResponseCreationSupport.sendStaticContent(request().httpRequest(), 056 request().httpChannel(), path -> resourceUrl, null); 057 } 058 059}