001/* 002 * Copyright (c) 2018 The Regents of the University of California. 003 * All rights reserved. 004 * 005 * '$Author: crawl $' 006 * '$Date: 2017-09-04 12:58:25 -0700 (Mon, 04 Sep 2017) $' 007 * '$Revision: 1406 $' 008 * 009 * Permission is hereby granted, without written agreement and without 010 * license or royalty fees, to use, copy, modify, and distribute this 011 * software and its documentation for any purpose, provided that the above 012 * copyright notice and the following two paragraphs appear in all copies 013 * of this software. 014 * 015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY 016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF 018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF 019 * SUCH DAMAGE. 020 * 021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, 022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE 024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF 025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, 026 * ENHANCEMENTS, OR MODIFICATIONS. 027 */ 028package org.kepler.webview.server.app; 029 030import java.net.URI; 031import java.util.HashSet; 032import java.util.Set; 033 034import org.kepler.configuration.ConfigurationProperty; 035import org.kepler.webview.server.WebViewConfiguration; 036import org.kepler.webview.server.WebViewServer; 037 038import io.vertx.core.AsyncResult; 039import io.vertx.core.Future; 040import io.vertx.core.Handler; 041import io.vertx.core.json.JsonArray; 042import io.vertx.core.json.JsonObject; 043import io.vertx.ext.auth.User; 044 045/** App to perform HTTP GET and return body. Useful for sites 046 * without CORS or JSONP. 047 * 048 * @author Daniel Crawl 049 */ 050public class GetURL extends AbstractApp { 051 052 public GetURL() { 053 054 super(); 055 056 for(ConfigurationProperty property: 057 WebViewConfiguration.getAppProperties("GetURL", "allowed.site")) { 058 _allowedSites.add(property.getValue()); 059 } 060 061 } 062 063 @Override 064 public void exec(User user, JsonObject inputs, Handler<AsyncResult<JsonArray>> handler) 065 throws Exception { 066 067 if(!inputs.containsKey("url")) { 068 throw new Exception("Missing url parameter."); 069 } 070 071 URI uri = null; 072 try { 073 uri = new URI(inputs.getString("url")); 074 } catch(Exception e) { 075 handler.handle(Future.failedFuture("Error parsing url: " + e.getMessage())); 076 return; 077 } 078 079 if(!_allowedSites.contains(uri.getHost())) { 080 handler.handle(Future.failedFuture("Site is not allowed.")); 081 return; 082 } 083 084 JsonObject data = new JsonObject(); 085 086 int port = uri.getPort(); 087 if(port == -1) { 088 port = uri.getScheme().startsWith("https") ? 443 : 80; 089 } 090 091 WebViewServer.vertx().createHttpClient().get(port, 092 uri.getHost(), uri.getPath(), response -> { 093 data.put("status", response.statusCode()); 094 response.bodyHandler(body -> { 095 data.put("body", body.toString()); 096 handler.handle(Future.succeededFuture(new JsonArray().add(data))); 097 }); 098 }).setFollowRedirects(true).end(); 099 } 100 101 private Set<String> _allowedSites = new HashSet<String>(); 102 103}