Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance
Java decentralizedintranet-searchintranet-search-enginep2pp2p-networkprivacyprivacy-protectionsearch-enginesearchenginewebweb-search-enginewebsearchwebseearch-engine
* yacy domain handler for jetty * rewrite from / to /index.html
Florian Richter351d264a4895
diff --git a/.classpath b/.classpathindex c648c2c63..3803c1c8a 100644--- a/.classpath+++ b/.classpath@@ -49,5 +49,7 @@<classpathentry kind="lib" path="lib/jetty-io-7.3.0.v20110203.jar"/><classpathentry kind="lib" path="lib/jetty-continuation-7.3.0.v20110203.jar"/><classpathentry kind="lib" path="lib/jetty-security-7.3.0.v20110203.jar"/>+ <classpathentry kind="lib" path="lib/jetty-servlet-7.3.0.v20110203.jar"/>+ <classpathentry kind="lib" path="lib/jetty-servlets-7.3.0.v20110203.jar"/><classpathentry kind="output" path="gen"/></classpath>diff --git a/build.xml b/build.xmlindex 9ce16801a..e94db8deb 100644--- a/build.xml+++ b/build.xml@@ -198,6 +198,8 @@<pathelement location="${lib}/jakarta-oro-2.0.8.jar" /><pathelement location="${lib}/jetty-continuation-7.3.0.v20110203.jar" /><pathelement location="${lib}/jetty-server-7.3.0.v20110203.jar" />+ <pathelement location="${lib}/jetty-servlet-7.3.0.v20110203.jar" />+ <pathelement location="${lib}/jetty-servlets-7.3.0.v20110203.jar" /><pathelement location="${lib}/jetty-http-7.3.0.v20110203.jar" /><pathelement location="${lib}/jetty-util-7.3.0.v20110203.jar" /><pathelement location="${lib}/jetty-io-7.3.0.v20110203.jar" />diff --git a/source/de/anomic/http/server/AlternativeDomainNames.java b/source/de/anomic/http/server/AlternativeDomainNames.javaindex a6f5c87f7..db103ccd8 100644--- a/source/de/anomic/http/server/AlternativeDomainNames.java+++ b/source/de/anomic/http/server/AlternativeDomainNames.java@@ -65,4 +65,28 @@ public interface AlternativeDomainNames {*/public String myID();+ public class ResolverEntry {+ private String host;+ private int port;+ private String path;++ public ResolverEntry(String host, int port, String path) {+ this.host = host;+ this.port = port;+ this.path = path;+ }+ public String getHost() {+ return host;+ }+ public int getPort() {+ return port;+ }+ public String getPath() {+ return path;+ }+ public String getURL() {+ return host + "";+ }+ }+}diff --git a/source/net/yacy/http/ContentModHandler.java b/source/net/yacy/http/ContentModHandler.javaindex aa3052b62..d4a12ab05 100644--- a/source/net/yacy/http/ContentModHandler.java+++ b/source/net/yacy/http/ContentModHandler.java@@ -35,6 +35,7 @@ import javax.servlet.http.HttpServletResponseWrapper;import org.eclipse.jetty.server.Handler;import org.eclipse.jetty.server.HandlerContainer;+import org.eclipse.jetty.server.HttpConnection;import org.eclipse.jetty.server.Request;import org.eclipse.jetty.server.handler.HandlerWrapper;@@ -60,7 +61,11 @@ public abstract class ContentModHandler extends HandlerWrapper implements Handle// wrap responseContentModResponseWrapper wrapped_response = new ContentModResponseWrapper(response);super.handle(target, baseRequest, request, wrapped_response);- this.doContentMod(wrapped_response.getBuffer(), request, response);+ if(baseRequest.isHandled()) {+ //baseRequest.setHandled(false);+ this.doContentMod(wrapped_response.getBuffer(), request, response);+ //baseRequest.setHandled(true);+ }}/**@@ -96,6 +101,10 @@ public abstract class ContentModHandler extends HandlerWrapper implements Handlepublic byte[] getBuffer() {return wrappedOutputStream.getBuffer();}++ public void setContentType(String mime) {+ super.setContentType(mime);+ }}/**diff --git a/source/net/yacy/http/HttpServer.java b/source/net/yacy/http/HttpServer.javaindex 9b6182664..920343c23 100644--- a/source/net/yacy/http/HttpServer.java+++ b/source/net/yacy/http/HttpServer.java@@ -35,6 +35,11 @@ import org.eclipse.jetty.server.handler.DefaultHandler;import org.eclipse.jetty.server.handler.HandlerList;import org.eclipse.jetty.server.handler.ResourceHandler;import org.eclipse.jetty.server.nio.SelectChannelConnector;+import org.eclipse.jetty.servlet.ServletContextHandler;+import org.eclipse.jetty.servlet.ServletHolder;+import org.eclipse.jetty.servlets.ProxyServlet;++import de.anomic.search.Switchboard;/*** class to embedded jetty http server into YaCy@@ -47,12 +52,20 @@ public class HttpServer {* @param port TCP Port to listen for http requests*/public HttpServer(int port) {+ Switchboard sb = Switchboard.getSwitchboard();server = new Server();SelectChannelConnector connector = new SelectChannelConnector();connector.setPort(port);//connector.setThreadPool(new QueuedThreadPool(20));server.addConnector(connector);+ YacyDomainHandler domainHandler = new YacyDomainHandler();+ domainHandler.setAlternativeResolver(sb.peers);++ ServletContextHandler proxyContext = new ServletContextHandler();+ proxyContext.setContextPath("/");+ proxyContext.addServlet(new ServletHolder(new ProxyServlet()), "/*");+ResourceHandler resource_handler = new ResourceHandler();resource_handler.setDirectoriesListed(true);resource_handler.setWelcomeFiles(new String[]{ "index.html" });@@ -60,16 +73,10 @@ public class HttpServer {resource_handler.setResourceBase("htroot/");HandlerList handlers = new HandlerList();- handlers.setHandlers(new Handler[] {new SSIHandler(new TemplateHandler()), resource_handler, new DefaultHandler() });-- Constraint constraint = new Constraint();- constraint.setName(Constraint.__BASIC_AUTH);;- constraint.setRoles(new String[]{"admin"});- constraint.setAuthenticate(true);-- ConstraintMapping cm = new ConstraintMapping();- cm.setConstraint(constraint);- cm.setPathSpec("/authenticate_me");+ handlers.setHandlers(new Handler[]+ { domainHandler, new RewriteHandler(),+ new SSIHandler(new TemplateHandler()),+ resource_handler, new DefaultHandler() });YaCySecurityHandler securityHandler = new YaCySecurityHandler();securityHandler.setLoginService(new YaCyLoginService());diff --git a/source/net/yacy/http/ProxyHandler.java b/source/net/yacy/http/ProxyHandler.javanew file mode 100644index 000000000..d64fe425e--- /dev/null+++ b/source/net/yacy/http/ProxyHandler.java@@ -0,0 +1,52 @@+//+// ProxyHandler+// Copyright 2004 by Michael Peter Christen; [email protected], Frankfurt a. M., Germany+// Copyright 2011 by Florian Richter+// First released 2011 at http://yacy.net+//+// $LastChangedDate$+// $LastChangedRevision$+// $LastChangedBy$+//+// This library is free software; you can redistribute it and/or+// modify it under the terms of the GNU Lesser General Public+// License as published by the Free Software Foundation; either+// version 2.1 of the License, or (at your option) any later version.+//+// This library is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU+// Lesser General Public License for more details.+//+// You should have received a copy of the GNU Lesser General Public License+// along with this program in the file lgpl21.txt+// If not, see <http://www.gnu.org/licenses/>.+//++package net.yacy.http;++import java.io.IOException;++import javax.servlet.ServletException;+import javax.servlet.http.HttpServletRequest;+import javax.servlet.http.HttpServletResponse;++import net.yacy.cora.protocol.http.HTTPClient;++import org.eclipse.jetty.server.Handler;+import org.eclipse.jetty.server.Request;+import org.eclipse.jetty.server.handler.AbstractHandler;++public class ProxyHandler extends AbstractHandler implements Handler {++ @Override+ public void handle(String target, Request baseRequest, HttpServletRequest request,+ HttpServletResponse response) throws IOException, ServletException {+ final HTTPClient client = new HTTPClient();+ int timeout = 10;+ client.setTimout(timeout);+ //client.setHeader();+ client.setRedirecting(false);+ }++}diff --git a/source/net/yacy/http/RewriteHandler.java b/source/net/yacy/http/RewriteHandler.javanew file mode 100644index 000000000..b80ff5372--- /dev/null+++ b/source/net/yacy/http/RewriteHandler.java@@ -0,0 +1,49 @@+//+// RewriteHandler+// Copyright 2011 by Florian Richter+// First released 2011 at http://yacy.net+//+// $LastChangedDate$+// $LastChangedRevision$+// $LastChangedBy$+//+// This library is free software; you can redistribute it and/or+// modify it under the terms of the GNU Lesser General Public+// License as published by the Free Software Foundation; either+// version 2.1 of the License, or (at your option) any later version.+//+// This library is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU+// Lesser General Public License for more details.+//+// You should have received a copy of the GNU Lesser General Public License+// along with this program in the file lgpl21.txt+// If not, see <http://www.gnu.org/licenses/>.+//++package net.yacy.http;++import java.io.IOException;++import javax.servlet.RequestDispatcher;+import javax.servlet.ServletException;+import javax.servlet.http.HttpServletRequest;+import javax.servlet.http.HttpServletResponse;++import org.eclipse.jetty.server.Handler;+import org.eclipse.jetty.server.Request;+import org.eclipse.jetty.server.handler.AbstractHandler;++public class RewriteHandler extends AbstractHandler implements Handler {++ @Override+ public void handle(String target, Request baseRequest, HttpServletRequest request,+ HttpServletResponse response) throws IOException, ServletException {+ if(target.equals("/")) {+ RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");+ dispatcher.forward(request, response);+ }+ }++}diff --git a/source/net/yacy/http/TemplateHandler.java b/source/net/yacy/http/TemplateHandler.javaindex 026b00076..c19d1b4fa 100644--- a/source/net/yacy/http/TemplateHandler.java+++ b/source/net/yacy/http/TemplateHandler.java@@ -259,7 +259,7 @@ public class TemplateHandler extends AbstractHandler implements Handler {}final String mimeType = MimeTable.ext2mime(targetExt, "text/html");- response.setContentType(targetExt);+ response.setContentType(mimeType);response.setContentLength(result.length());response.setStatus(HttpServletResponse.SC_OK);diff --git a/source/net/yacy/http/YacyDomainHandler.java b/source/net/yacy/http/YacyDomainHandler.javanew file mode 100644index 000000000..27fc8c6be--- /dev/null+++ b/source/net/yacy/http/YacyDomainHandler.java@@ -0,0 +1,91 @@+//+// YacyDomainHandler+// Copyright 2005 by Michael Peter Christen; [email protected], Frankfurt a. M., Germany+// Copyright 2011 by Florian Richter+// First released 13.04.2011 at http://yacy.net+//+// $LastChangedDate$+// $LastChangedRevision$+// $LastChangedBy$+//+// This library is free software; you can redistribute it and/or+// modify it under the terms of the GNU Lesser General Public+// License as published by the Free Software Foundation; either+// version 2.1 of the License, or (at your option) any later version.+//+// This library is distributed in the hope that it will be useful,+// but WITHOUT ANY WARRANTY; without even the implied warranty of+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU+// Lesser General Public License for more details.+//+// You should have received a copy of the GNU Lesser General Public License+// along with this program in the file lgpl21.txt+// If not, see <http://www.gnu.org/licenses/>.+//++package net.yacy.http;++import java.io.IOException;++import javax.servlet.RequestDispatcher;+import javax.servlet.ServletException;+import javax.servlet.http.HttpServletRequest;+import javax.servlet.http.HttpServletRequestWrapper;+import javax.servlet.http.HttpServletResponse;++import org.eclipse.jetty.server.Handler;+import org.eclipse.jetty.server.Request;+import org.eclipse.jetty.server.handler.AbstractHandler;++import de.anomic.http.server.AlternativeDomainNames;++public class YacyDomainHandler extends AbstractHandler implements Handler {++ private AlternativeDomainNames alternativeResolvers;++ public void setAlternativeResolver(AlternativeDomainNames resolver) {+ this.alternativeResolvers = resolver;+ }++ @Override+ public void handle(String target, Request baseRequest, HttpServletRequest request,+ HttpServletResponse response) throws IOException, ServletException {+ String host = request.getServerName();+ String resolved = alternativeResolvers.resolve(host);+ if(resolved != null) {+ // split resolved into host, port and path+ int posPath = resolved.indexOf('/');+ String path = resolved.substring(posPath);+ String hostPort = resolved.substring(0, posPath);+ int posPort = hostPort.lastIndexOf(':');+ String newHost = hostPort.substring(0, posPort);+ int newPort = Integer.parseInt(hostPort.substring(posPort + 1));++ RequestDispatcher dispatcher = request.getRequestDispatcher(path + target);+ dispatcher.forward(new DomainRequestWrapper(request, newHost, newPort), response);+ }+ }++ private class DomainRequestWrapper extends HttpServletRequestWrapper {++ private String newServerName;+ private int newServerPort;++ public DomainRequestWrapper(HttpServletRequest request, String serverName, int serverPort) {+ super(request);+ this.newServerName = serverName;+ this.newServerPort = serverPort;+ }++ @Override+ public String getServerName() {+ return newServerName;+ }++ public int getServerPort() {+ return newServerPort;+ }++ }++}