
org.xlightweb.server.HttpServer Maven / Gradle / Ivy
/*
* Copyright (c) xlightweb.org, 2008. 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.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.SSLContext;
import org.xlightweb.IHttpConnectionHandler;
import org.xlightweb.IHttpRequestHandler;
import org.xsocket.connection.Server;
/**
* A HttpServer. The http server accepts incoming connections and forwards the request to
* the assigned {@link 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 [email protected]
*/
public class HttpServer extends Server {
/**
* constructor
*
* @param requestHandler the requestHandler
* @throws IOException If some other I/O error occurs
* @throws UnknownHostException if the local host cannot determined
*/
public HttpServer(IHttpRequestHandler requestHandler) throws UnknownHostException, IOException {
this(new InetSocketAddress(InetAddress.getByName(DEFAULT_HOST_ADDRESS), 0), new HashMap(), requestHandler, null, false);
}
/**
* constructor
*
* @param handler the handler to use
* @param options the socket options
* @throws IOException If some other I/O error occurs
* @throws UnknownHostException if the local host cannot determined
*/
public HttpServer(IHttpRequestHandler handler, Map options) throws UnknownHostException, IOException {
this(InetAddress.getByName(DEFAULT_HOST_ADDRESS), 0, handler, options, null, false);
}
/**
* constructor
*
* @param port the local port
* @param handler the handler to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(int port, IHttpRequestHandler handler) throws UnknownHostException, IOException {
this(new InetSocketAddress(InetAddress.getByName(DEFAULT_HOST_ADDRESS), port), new HashMap(), handler, null, false);
}
/**
* constructor
*
*
* @param port the local port
* @param handler the handler to use
* @param options the acceptor socket options
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(int port, IHttpRequestHandler handler, Map options) throws UnknownHostException, IOException {
this(InetAddress.getByName(DEFAULT_HOST_ADDRESS), port, handler, options, null, false);
}
/**
* constructor
*
*
* @param address the local address
* @param port the local port
* @param handler the handler to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(InetAddress address, int port, IHttpRequestHandler handler) throws UnknownHostException, IOException {
this(address, port, handler, new HashMap(), null, false);
}
/**
* constructor
*
*
* @param ipAddress the local ip address
* @param port the local port
* @param handler the handler to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(String ipAddress, int port, IHttpRequestHandler handler) throws UnknownHostException, IOException {
this(InetAddress.getByName(ipAddress), port, handler, new HashMap(), null, false);
}
/**
* constructor
*
*
* @param ipAddress the local ip address
* @param port the local port
* @param handler the handler to use
* @param options the socket options
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(String ipAddress, int port, IHttpRequestHandler handler, Map options) throws UnknownHostException, IOException {
this(InetAddress.getByName(ipAddress), port, handler, options, null, false);
}
/**
* constructor
*
*
* @param port local port
* @param handler the handler to use
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(InetAddress.getByName(DEFAULT_HOST_ADDRESS), port, handler, new HashMap(), sslContext, sslOn);
}
/**
* constructor
*
* @param port local port
* @param options the acceptor socket options
* @param handler the handler to use
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(int port, IHttpRequestHandler handler, Map options, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(InetAddress.getByName(DEFAULT_HOST_ADDRESS), port, handler, options, sslContext, sslOn);
}
/**
* constructor
*
* @param ipAddress local ip address
* @param port local port
* @param handler the handler to use
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(String ipAddress, int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(InetAddress.getByName(ipAddress), port, handler, new HashMap(), sslContext, sslOn);
}
/**
* constructor
*
*
* @param ipAddress local ip address
* @param port local port
* @param options the acceptor socket options
* @param handler the handler to use
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(String ipAddress, int port, IHttpRequestHandler handler, Map options, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(InetAddress.getByName(ipAddress), port, handler, options, sslContext, sslOn);
}
/**
* constructor
*
*
* @param address local address
* @param port local port
* @param handler the request handler
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(InetAddress address, int port, IHttpRequestHandler handler, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(address, port, handler, new HashMap(), sslContext, sslOn);
}
/**
* constructor
*
*
* @param address local address
* @param port local port
* @param options the socket options
* @param handler the request handler
* @param sslOn true, is SSL should be activated
* @param sslContext the ssl context to use
* @throws UnknownHostException if the local host cannot determined
* @throws IOException If some other I/O error occurs
*/
public HttpServer(InetAddress address, int port, IHttpRequestHandler handler, Map options, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
this(new InetSocketAddress(address, port), options, handler, sslContext, sslOn);
}
private HttpServer(InetSocketAddress address, Map options, IHttpRequestHandler requestHandler, SSLContext sslContext, boolean sslOn) throws UnknownHostException, IOException {
super(address, options, new HttpProtocolAdapter(requestHandler), sslContext, sslOn, 0);
}
/**
* adds a connection handler
*
* @param connectionHandler the connection handler to add
*/
public void addConnectionHandler(IHttpConnectionHandler connectionHandler) {
((HttpProtocolAdapter) super.getHandler()).addConnectionHandler(connectionHandler);
}
/**
* sets the message receive timeout
*
* @param receivetimeout the message receive timeout
*/
public void setRequestTimeoutMillis(long receivetimeout) {
((HttpProtocolAdapter) super.getHandler()).setRequestTimeoutMillis(receivetimeout);
}
/**
* set the body data receive timeout
*
* @param bodyDataReceiveTimeoutSec the timeout
*/
public final void setBodyDataReceiveTimeoutMillis(long bodyDataReceiveTimeoutMillis) {
((HttpProtocolAdapter) super.getHandler()).setBodyDataReceiveTimeoutMillis(bodyDataReceiveTimeoutMillis);
}
/**
* set is if the server-side connection will closed, if an error message is sent
*
* @param isCloseOnSendingError if the connection will closed, if an error message is sent
*/
public void setCloseOnSendingError(boolean isCloseOnSendingError) {
((HttpProtocolAdapter) super.getHandler()).setCloseOnSendingError(isCloseOnSendingError);
}
/**
* returns if the server-side connection will closed, if an error message will be sent
* @return true, if the connection will closed by sending an error message
*/
public boolean isCloseOnSendingError() {
return ((HttpProtocolAdapter) super.getHandler()).isCloseOnSendingError();
}
/**
* set the max transactions per connection. Setting this filed causes that
* a keep-alive response header will be added
*
* @param maxTransactions the max transactions
*/
public void setMaxTransactions(int maxTransactions) {
((HttpProtocolAdapter) getHandler()).setMaxTransactions(maxTransactions);
}
/**
* sets the session manager
*
* @param sessionManager the session manager
*/
public void setSessionManager(ISessionManager sessionManager) {
((HttpProtocolAdapter) getHandler()).setSessionManager(sessionManager);
}
/**
* sets if cookies is used for session state management
*
* @param useCookies true, if cookies isused for session state management
*/
public void setUsingCookies(boolean useCookies) {
((HttpProtocolAdapter) getHandler()).setUsingCookies(useCookies);
}
/**
* returns true, if cookies is used for session state management
*
* @return true, if cookies is used for session state management
*/
public boolean isUsingCookies() {
return ((HttpProtocolAdapter) getHandler()).isUsingCookies();
}
/**
* returns the session manager
*
* @return the session manager
*/
public ISessionManager getSessionManager() {
return ((HttpProtocolAdapter) getHandler()).getSessionManager();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy