001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU 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, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.operations.daemon.handlers; 019 020import io.undertow.server.HttpHandler; 021import io.undertow.server.HttpServerExchange; 022import io.undertow.util.Headers; 023 024import java.util.List; 025import java.util.logging.Level; 026import java.util.logging.Logger; 027 028import org.syncany.plugins.Plugins; 029import org.syncany.plugins.web.WebInterfacePlugin; 030import org.syncany.util.StringUtil; 031import org.syncany.util.StringUtil.StringJoinListener; 032 033/** 034 * InternalWebInterfaceHandler is responsible for handling requests 035 * to the web interface. 036 * 037 * @author Philipp C. Heckel (philipp.heckel@gmail.com) 038 */ 039public class InternalWebInterfaceHandler implements HttpHandler { 040 private static final Logger logger = Logger.getLogger(InternalWebInterfaceHandler.class.getSimpleName()); 041 042 private List<WebInterfacePlugin> webInterfacePlugins; 043 private WebInterfacePlugin webInterfacePlugin; 044 private HttpHandler requestHandler; 045 046 public InternalWebInterfaceHandler() { 047 webInterfacePlugins = Plugins.list(WebInterfacePlugin.class); 048 049 if (webInterfacePlugins.size() == 1) { 050 initWebInterfacePlugin(); 051 } 052 } 053 054 private void initWebInterfacePlugin() { 055 try { 056 webInterfacePlugin = webInterfacePlugins.iterator().next(); 057 requestHandler = webInterfacePlugin.createRequestHandler(); 058 059 logger.log(Level.INFO, "Starting webInterfacePlugin: " + webInterfacePlugin.getId()); 060 webInterfacePlugin.start(); 061 } 062 catch (Exception e) { 063 throw new RuntimeException(e); 064 } 065 } 066 067 @Override 068 public void handleRequest(HttpServerExchange exchange) throws Exception { 069 if (requestHandler != null) { 070 handleRequestWithResourceHandler(exchange); 071 } 072 else { 073 handleRequestNoHandler(exchange); 074 } 075 } 076 077 private void handleRequestWithResourceHandler(HttpServerExchange exchange) throws Exception { 078 logger.log(Level.FINE, "Sending request to webInterfacePlugin handler: " + exchange.toString()); 079 requestHandler.handleRequest(exchange); 080 } 081 082 private void handleRequestNoHandler(HttpServerExchange exchange) { 083 exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html"); 084 085 if (webInterfacePlugins.size() == 0) { 086 String responseMessage = "No web interface installed.<br>Use <code>sy plugin install simpleweb --snapshot</code> " 087 + "to install a web interface, then restart the daemon."; 088 089 exchange.getResponseSender().send(responseMessage); 090 } 091 else { 092 String webInterfacePluginsList = StringUtil.join(webInterfacePlugins, ", ", new StringJoinListener<WebInterfacePlugin>() { 093 public String getString(WebInterfacePlugin webInterfacePlugin) { 094 return webInterfacePlugin.getId(); 095 } 096 }); 097 098 String responseMessage = "Only one web interface can be installed, but " + webInterfacePlugins.size() + " plugins found: " 099 + webInterfacePluginsList + "<br>Use <code>sy plugin remove <pluginId></code> to remove plugins, then restart the daemon."; 100 101 exchange.getResponseSender().send(responseMessage); 102 } 103 } 104}