📄 httpqueryserver.java
字号:
package it.unimi.dsi.mg4j.query;/* * MG4J: Managing Gigabytes for Java * * Copyright (C) 2005-2007 Sebastiano Vigna * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program 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 General Public License * for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * */import it.unimi.dsi.Util;import it.unimi.dsi.mg4j.document.DocumentCollection;import java.util.List;import java.util.Properties;import javax.servlet.http.HttpServlet;import org.apache.log4j.Logger;import org.apache.velocity.app.Velocity;import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;import org.apache.velocity.runtime.resource.loader.FileResourceLoader;import org.mortbay.http.HttpContext;import org.mortbay.http.HttpServer;import org.mortbay.http.SocketListener;import org.mortbay.jetty.servlet.ServletHandler;/** A very basic HTTP server answering queries. * * <p>The main method of this class starts a very basic HTTP server answering queries. * If a matching document collection is provided, the server will also display some * intervals satisfying the queries. * * <p>Queries are served by the {@link it.unimi.dsi.mg4j.query.QueryServlet}; by * default, the servlet listen on port 4242, and the servlet is deployed under * the path <samp>/Query</samp>. A servlet displaying single documents from the collection * is deployed under the path <samp>/Item</samp>. The server and the servlet are fully multithreaded. * * <p>If you want to start this server from the command line, you must use the * main method of {@link it.unimi.dsi.mg4j.query.Query}, providing the suitable option. * Changes to the {@link it.unimi.dsi.mg4j.query.QueryEngine} made through the text interface will * be reflected in the web interface, making it possible to experiment with different * settings. * * <p><strong>Warning</strong>: this server creates a {@link Velocity} instance with some default parameter: * in particular, template resolution is performed first on the classpath, then relatively to the current directory, and * finally using absolute pathnames. Watch out for template resolution issues. */public class HttpQueryServer { private static final Logger LOGGER = Util.getLogger( HttpQueryServer.class ); /** The underlying Jetty server. Access to this field is useful to tune or stop the server. */ public final HttpServer server; /** Creates a new HTTP query server. * * @param queryEngine the query engine that will be used (actually, {@linkplain QueryEngine#copy() copied}) by the * servlets run by this query server. * @param collection the document collection (related to the indices contained in <code>queryEngine</code>) that * will be used to display documents. * @param itemClass a class implementing an {@link javax.servlet.http.HttpServlet} and responsible * for displaying documents (see, e.g., {@link GenericItem}. * @param itemMimeType the default MIME type of a displayed item. * @param port the port exposing the server. * @param titleList an optional list of titles for all documents, or <code>null</code>. */ public HttpQueryServer( final QueryEngine queryEngine, final DocumentCollection collection, final Class<? extends HttpServlet> itemClass, final String itemMimeType, final int port, final List<? extends CharSequence> titleList ) throws Exception { LOGGER.debug( "itemClass: " + itemClass ); LOGGER.debug( "itemMimeType: " + itemMimeType ); LOGGER.debug( "queryEngine: " + queryEngine ); LOGGER.debug( "port: " + port ); // Create the server server = new HttpServer(); // Create a port listener SocketListener listener = new SocketListener(); listener.setPort( port ); server.addListener( listener ); // Create a context HttpContext context = new HttpContext(); context.setContextPath( "" ); server.addContext( context ); // Create a servlet container ServletHandler servlets = new ServletHandler(); context.addHandler( servlets ); context.setAttribute( "queryEngine", queryEngine ); context.setAttribute( "collection", collection ); context.setAttribute( "titleList", titleList ); context.setAttribute( "action", "/Query" ); // TODO: very rudimentary: we should get the template from somewhere else instead... context.setAttribute( "template", System.getProperty( "it.unimi.dsi.mg4j.query.QueryServlet.template" ) ); // Maps the main servlet onto the container. servlets.addServlet( "Query", "/Query", "it.unimi.dsi.mg4j.query.QueryServlet" ); servlets.addServlet( "Help", "/Help", "it.unimi.dsi.mg4j.query.HelpPage" ); Properties properties = new Properties(); properties.setProperty( "resource.loader", "class, current, absolute" ); properties.setProperty( "class.resource.loader.class", ClasspathResourceLoader.class.getName() ); properties.setProperty( "current.resource.loader.class", FileResourceLoader.class.getName() ); properties.setProperty( "current.resource.loader.path", System.getProperty( "user.dir" ) ); properties.setProperty( "absolute.resource.loader.class", FileResourceLoader.class.getName() ); properties.setProperty( "absolute.resource.loader.path", "" ); properties.setProperty( "input.encoding", "utf-8" ); properties.setProperty( "output.encoding", "utf-8" ); properties.setProperty( "default.contentType", "text/html; charset=UTF-8" ); Velocity.init( properties ); /* If an item servlet was specified, we link it to /Item. Otherwise, * we inform the query servlet that it should generate direct URIs. */ if ( itemClass != null ) { servlets.addServlet( "Item", "/Item", itemClass.getName() ); if ( itemClass == FileSystemItem.class ) context.setAttribute( "derelativise", Boolean.TRUE ); } else context.setAttribute( "uri", Boolean.TRUE ); context.setAttribute( "mimeType", itemMimeType ); // Start the http server server.start(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -