* yacy domain handler for jetty

* rewrite from / to /index.html
pull/1/head
Florian Richter 14 years ago
parent f25cc4407d
commit 351d264a48

@ -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>

@ -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" />

@ -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 + "";
}
}
}

@ -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 response
ContentModResponseWrapper 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 Handle
public byte[] getBuffer() {
return wrappedOutputStream.getBuffer();
}
public void setContentType(String mime) {
super.setContentType(mime);
}
}
/**

@ -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());

@ -0,0 +1,52 @@
//
// ProxyHandler
// Copyright 2004 by Michael Peter Christen; mc@yacy.net, 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);
}
}

@ -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);
}
}
}

@ -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);

@ -0,0 +1,91 @@
//
// YacyDomainHandler
// Copyright 2005 by Michael Peter Christen; mc@yacy.net, 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;
}
}
}
Loading…
Cancel
Save