001/*
002 *
003 * Copyright (c) 2015 The Regents of the University of California.
004 * All rights reserved.
005 *
006 * '$Author: crawl $'
007 * '$Date: 2017-08-23 23:13:47 -0700 (Wed, 23 Aug 2017) $' 
008 * '$Revision: 1382 $'
009 * 
010 * Permission is hereby granted, without written agreement and without
011 * license or royalty fees, to use, copy, modify, and distribute this
012 * software and its documentation for any purpose, provided that the above
013 * copyright notice and the following two paragraphs appear in all copies
014 * of this software.
015 *
016 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
017 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
018 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
019 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
020 * SUCH DAMAGE.
021 *
022 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
023 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
024 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
025 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
026 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
027 * ENHANCEMENTS, OR MODIFICATIONS.
028 *
029 */
030package org.kepler.webview.server.handler;
031
032import java.io.File;
033import java.net.HttpURLConnection;
034
035import org.kepler.webview.server.WebViewServer;
036
037import io.vertx.core.Handler;
038import io.vertx.core.http.HttpServerRequest;
039import io.vertx.core.json.JsonObject;
040import io.vertx.ext.web.RoutingContext;
041
042/** Handler base class.
043 * 
044 *  @author Daniel Crawl
045 *  @version $Id: BaseHandler.java 1382 2017-08-24 06:13:47Z crawl $
046 *  
047 */
048public abstract class BaseHandler implements Handler<RoutingContext> {
049
050    public BaseHandler(WebViewServer server) {
051        _server = server;
052    }
053
054    /** Do nothing. */
055    @Override
056    public void handle(RoutingContext context) {
057        
058    }
059
060    /** Send a file with the response.
061     * @param request The http request.
062     * @param file The file to send.
063     * @param delete If true, delete the file once response is sent. 
064     */
065    protected void _sendResponseWithFile(HttpServerRequest request, File file, boolean delete) {       
066        if(delete) {            
067            request.response().sendFile(file.getAbsolutePath(), res -> {
068                // delete file after sent
069                if(!file.delete()) {
070                    System.err.println("WARNING: unable to delete " + file.getAbsolutePath());
071                }                        
072            });
073        } else {
074            request.response().sendFile(file.getAbsolutePath());            
075        }
076    }
077    
078    /** Send an error response.
079     * @param request The http request.
080     * @param errorStr The error text. 
081     */
082    protected void _sendResponseWithError(HttpServerRequest request, String errorStr) {
083        request.response()
084            .putHeader("Content-Type", "application/json")
085            .setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST)
086            .end(new JsonObject().put("error", "Error: " + errorStr).encode());
087    }
088    
089    /** Send an success response with json.
090     * @param request The http request.
091     * @param json The json object.
092     */
093    protected void _sendResponseWithSuccessJson(HttpServerRequest request,
094            JsonObject json) {
095        _sendResponseWithSuccessText(request, "application/json", json.encode());
096    }
097    
098    /** Send an success response with text.
099     * @param request The http request.
100     * @param contentType The value for Content-Type.
101     * @param text The success text.
102     */
103    protected void _sendResponseWithSuccessText(HttpServerRequest request,
104        String contentType, String text) {        
105        request.response()
106            .putHeader("Content-Type", contentType)
107            .setStatusCode(HttpURLConnection.HTTP_OK)
108            .end(text);        
109    }
110
111    protected WebViewServer _server;
112}