Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  Copyright (c) xlightweb.org, 2008 - 2010. All rights reserved.
   *
   *  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 library; if not, write to the Free Software
  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
  * The latest copy of this software may be found on http://www.xlightweb.org/
  */
 package org.xlightweb.server;
 
 
 
 import java.util.Map;
 
 
 
 
A HttpServer. The http server accepts incoming connections and forwards the request to the assigned org.xlightweb.IHttpRequestHandler. Example:
 
  // defining a request handler 
  class MyRequestHandler implements IHttpRequestHandler  {
  
     public void onRequest(IHttpExchange exchange) throws IOException {
        IHttpRequest request = exchange.getRequest();
        //...
        
        exchange.send(new HttpResponse(200, "text/plain", "it works"));
     }
  } 

 
  // creates a server
  IServer server = new HttpServer(8080, new MyRequestHandler());
 
  // setting some properties 
  server.setMaxTransactions(400);
  server.setRequestTimeoutMillis(5 * 60 * 1000);  
  //...
 
 
  // executing the server 
  server.run();  // (blocks forever)
 
  // or server.start();  (blocks until the server has been started)
  //...
 

Author(s):
grro@xlightweb.org
 
 public class XHttpServer extends HttpServer {

    
constructor

Parameters:
webHandler the webHandler
Throws:
java.io.IOException If some other I/O error occurs
java.net.UnknownHostException if the local host cannot determined
 
 	public XHttpServer(IWebHandler webHandlerthrows UnknownHostExceptionIOException {
 	    this(webHandlernew HashMap<StringObject>());
 	}




constructor

Parameters:
webHandler the webHandler
options the socket options
Throws:
java.io.IOException If some other I/O error occurs
java.net.UnknownHostException if the local host cannot determined
	public XHttpServer(IWebHandler webHandlerMap<StringObjectoptionsthrows UnknownHostExceptionIOException {
	    this(new InetSocketAddress(0), optionswebHandlernullfalse);
	}


constructor

Parameters:
port the local port
webHandler the webHandler
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(int portIWebHandler webHandlerthrows UnknownHostExceptionIOException {
		this(portwebHandler);
	}


    
constructor

Parameters:
port the local port
webHandler the webHandler
minPoolsize the min workerpool size
maxPoolsize the max workerpool size
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
    public XHttpServer(int portIWebHandler webHandlerint minPoolsizeint maxPoolsizethrows UnknownHostExceptionIOException {
        this(portwebHandlernullfalseminPoolsizemaxPoolsize);
    }	



constructor

Parameters:
port the local port
webHandler the webHandler
options the acceptor socket options
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(int portIWebHandler webHandlerMap<String , Objectoptionsthrows UnknownHostExceptionIOException {
		this(portwebHandleroptionsnullfalse);
	}


constructor

Parameters:
address the local address
port the local port
webHandler the webHandler
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(InetAddress addressint portIWebHandler webHandlerthrows UnknownHostExceptionIOException {
		this(addressportwebHandlernullfalse);
	}



constructor

Parameters:
ipAddress the local ip address
port the local port
webHandler the webHandler
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(String ipAddressint portIWebHandler webHandlerthrows UnknownHostExceptionIOException {
		this(InetAddress.getByName(ipAddress), portwebHandler);
	}



constructor

Parameters:
ipAddress the local ip address
port the local port
webHandler the webHandler
options the socket options
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(String ipAddressint portIWebHandler webHandlerMap<StringObjectoptionsthrows UnknownHostExceptionIOException {
		this(InetAddress.getByName(ipAddress), portwebHandleroptionsnullfalse);
	}




constructor

Parameters:
port local port
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(int portIWebHandler webHandlerSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(portwebHandlernew HashMap<StringObject>(), sslContextsslOn);
	}



   
constructor

Parameters:
port local port
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
minPoolsize the min workerpool size
maxPoolsize the max workerpool size
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
    public XHttpServer(int portIWebHandler webHandlerSSLContext sslContextboolean sslOnint minPoolsizeint maxPoolsizethrows UnknownHostExceptionIOException {
        this(new InetSocketAddress(port), new HashMap<StringObject>(), webHandlersslContextsslOnmaxPoolsizemaxPoolsize);
    }





constructor

Parameters:
port local port
options the acceptor socket options
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(int portIWebHandler webHandlerMap<StringObjectoptionsSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(new InetSocketAddress(port), optionswebHandlersslContextsslOn);
	}
 

constructor

Parameters:
ipAddress local ip address
port local port
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(String ipAddressint portIWebHandler webHandlerSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(ipAddressportwebHandlernew HashMap<StringObject>(), sslContextsslOn);
	}



constructor

Parameters:
ipAddress local ip address
port local port
options the acceptor socket options
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(String ipAddressint portIWebHandler webHandlerMap<StringObjectoptionsSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(InetAddress.getByName(ipAddress), portwebHandleroptionssslContextsslOn);
	}


constructor

Parameters:
address local address
port local port
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(InetAddress addressint portIWebHandler webHandlerSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(addressportwebHandlernew HashMap<StringObject>(), sslContextsslOn);
	}


constructor

Parameters:
address local address
port local port
options the socket options
webHandler the webHandler
sslOn true, is SSL should be activated
sslContext the ssl context to use
Throws:
java.net.UnknownHostException if the local host cannot determined
java.io.IOException If some other I/O error occurs
	public XHttpServer(InetAddress addressint portIWebHandler webHandlerMap<StringObjectoptionsSSLContext sslContextboolean sslOnthrows UnknownHostExceptionIOException {
		this(new InetSocketAddress(addressport), optionswebHandlersslContextsslOn);
	}
	 
    XHttpServer(InetSocketAddress addressMap<StringObjectoptionsIWebHandler webHandlerSSLContext sslContextboolean sslOnint minPoolsizeint maxPoolsizethrows UnknownHostExceptionIOException {
        super(addressoptionsgetRequestHandler(webHandler), new WebSocketUpgradeHandler(webHandler), sslContextsslOnminPoolsizemaxPoolsize);
    }
    private static IHttpRequestHandler getRequestHandler(IWebHandler webHandler) {
        if (IHttpRequestHandler.class.isAssignableFrom(webHandler.getClass())) {
            return ((IHttpRequestHandlerwebHandler);
        } else {
            return null;
        }
    }
    
    
    
	private static final class WebSocketUpgradeHandler implements IUpgradeHandler {
	    
	    private final IWebSocketHandler webSocketHandler;
	    
	    public WebSocketUpgradeHandler(IWebHandler webHandler) {
	        
	        if (IWebSocketHandler.class.isAssignableFrom(webHandler.getClass())) {
	             = (IWebSocketHandlerwebHandler;
	        } else {
	             = null;
	        }
        }
	    
	    public boolean onRequest(IHttpExchange exchangethrows IOExceptionBadMessageException {
	        IHttpRequest request = exchange.getRequest();
	        
            if (( != null) && request.getHeader("Upgrade").equalsIgnoreCase("WebSocket") && request.getProtocolVersion().endsWith("1.1")) {
                new WebSocketConnection((HttpServerConnectionexchange.getConnection(), exchange);
                return true;
            } else {
                return false;
            }
	    }
	}	
New to GrepCode? Check out our FAQ X