You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by kg...@apache.org on 2011/10/13 07:01:43 UTC
svn commit: r1182650 [2/3] - in /felix/trunk/httplite: ./ META-INF/ src/
src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/
src/main/java/org/apache/felix/ src/main/java/org/apache/felix/http/
src/main/java/org/apache/felix/http/ligh...
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+import org.apache.felix.http.lightweight.servlet.ConcreteServletInputStream;
+import org.apache.felix.http.lightweight.servlet.HttpConstants;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+
+/**
+ * This class represents an accepted connection between the server and
+ * a client. It supports persistent connections for both HTTP 1.0 and 1.1
+ * clients. A given persistent connection is limited in the number of
+ * consecutive requests it is allowed to make before having its connection
+ * closed as well as after a period of inactivity.
+**/
+public class Connection
+{
+ public static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
+ public static final int DEFAULT_CONNECTION_REQUESTLIMIT = 50;
+
+ private final Socket m_socket;
+ private ConcreteServletInputStream m_is;
+ private OutputStream m_os;
+ private int m_requestCount = 0;
+ private final int m_requestLimit;
+ private final ServiceRegistrationResolver m_resolver;
+ private final Logger m_logger;
+
+ /**
+ * Constructs a connection with a default inactivity timeout and request limit.
+ *
+ * @param socket Socket connection
+ * @param resolver a resolver to get http request/response and handler.
+ * @param logger Logger
+ * @throws IOException If any I/O error occurs.
+ */
+ public Connection(final Socket socket, final ServiceRegistrationResolver resolver, final Logger logger) throws IOException
+ {
+ this(socket, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_REQUESTLIMIT,
+ resolver, logger);
+ }
+
+ /**
+ * Constructs a connection with the specified inactivity timeout and request limit.
+ * @param server The web server associated with the connection.
+ * @param socket The client socket.
+ * @param timeout The inactivity timeout of the connection in milliseconds.
+ * @param requestLimit The maximum number of consecutive requests.
+ * @param m_resolver
+ * @throws java.io.IOException If any I/O error occurs.
+ */
+ public Connection(final Socket socket, final int timeout, final int requestLimit, final ServiceRegistrationResolver resolver, final Logger logger) throws IOException
+ {
+ m_socket = socket;
+ m_resolver = resolver;
+ m_logger = logger;
+ m_socket.setSoTimeout(timeout);
+ m_socket.setTcpNoDelay(true);
+ m_requestLimit = requestLimit;
+ try
+ {
+ m_is = new ConcreteServletInputStream(new BufferedInputStream(
+ m_socket.getInputStream()));
+ m_os = new BufferedOutputStream(m_socket.getOutputStream());
+ }
+ catch (IOException ex)
+ {
+ // Make sure we close any opened socket/streams.
+ try
+ {
+ m_socket.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket.", ex);
+ }
+ if (m_is != null)
+ {
+ try
+ {
+ m_is.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket input stream.",
+ ex2);
+ }
+ }
+ if (m_os != null)
+ {
+ try
+ {
+ m_os.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket output stream.",
+ ex2);
+ }
+ }
+ throw ex;
+ }
+ }
+
+ /**
+ * Performs the actual servicing of the connection and its subsequent requests.
+ * This method will be called by threads in the thread pool. This method
+ * typically exits when the connection is closed due to either an explicit
+ * connection close, the inactivity timeout expires, the maximum request
+ * limit was reached, or an I/O error occurred. When this method returns,
+ * the associated socket will be closed, regardless of whether or not an
+ * expection was thrown.
+ * @throws java.net.SocketTimeoutException If the inactivity timeout expired
+ * while trying to read from the socket.
+ * @throws java.io.IOException If any I/O error occurs.
+ * @throws ServletException on servlet errors
+ **/
+ public void process() throws IOException, ServletException
+ {
+ HttpServletRequestImpl request = m_resolver.getServletRequest(m_socket);
+ HttpServletResponseImpl response = m_resolver.getServletResponse(m_os);
+
+ try
+ {
+ // Loop until we close the connection.
+ boolean close = false;
+ while (!close)
+ {
+ // Read the next request.
+ try
+ {
+ request.parseRequestLine(m_is);
+ }
+ catch (IOException e)
+ {
+ m_logger.log(
+ Logger.LOG_ERROR,
+ "Error with request: " + request.toString() + ": "
+ + e.getMessage());
+ throw e;
+ }
+ m_requestCount++;
+
+ // Keep track of whether we have errored or not,
+ // because we still want to read the bytes to clear
+ // the input stream so we can service more requests.
+ boolean error = false;
+
+ m_logger.log(Logger.LOG_DEBUG,
+ "Processing request (" + (m_requestLimit - m_requestCount)
+ + " remaining) : " + request.getRequestURI());
+
+ // If client is HTTP/1.1, then send continue message.
+ if (request.getProtocol().equals(HttpConstants.HTTP11_VERSION))
+ {
+ response.sendContinueResponse();
+ }
+
+ // Read the header lines of the request.
+ request.parseHeader(m_is);
+
+ // If we have an HTTP/1.0 request without the connection set to
+ // keep-alive or we explicitly have a request to close the connection,
+ // then set close flag to exit the loop rather than trying to read
+ // more requests.
+ String v = request.getHeader(HttpConstants.HEADER_CONNECTION);
+ if ((request.getProtocol().equals(HttpConstants.HTTP10_VERSION) && ((v == null) || (!v.equalsIgnoreCase(HttpConstants.KEEPALIVE_CONNECTION))))
+ || ((v != null) && v.equalsIgnoreCase(HttpConstants.CLOSE_CONNECTION)))
+ {
+ close = true;
+ response.setConnectionType("close");
+ }
+ // If we have serviced the maximum number of requests for
+ // this connection, then set close flag so we exit the loop
+ // and close the connection.
+ else if (m_requestCount >= m_requestLimit)
+ {
+ close = true;
+ response.setConnectionType("close");
+ }
+
+ // We only service GET and/or HEAD requests, so send
+ // a "not implemented" error otherwise.
+ if (!HttpServletRequestImpl.isSupportedMethod(request.getMethod()))
+ {
+ error = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendNotImplementedResponse();
+ }
+
+ // Ignore if we have already errored, otherwise send error message
+ // if an HTTP/1.1 client did not include HOST header.
+ if (!error && request.getProtocol().equals(HttpConstants.HTTP11_VERSION)
+ && (request.getHeader(HttpConstants.HOST_HEADER) == null))
+ {
+ error = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendMissingHostResponse();
+ }
+
+ // Read in the request body.
+ request.parseBody(m_is);
+
+ // Only process the request if there was no error.
+ if (!error)
+ {
+ ServiceRegistrationHandler processor = m_resolver.getProcessor(
+ request, response, request.getRequestURI());
+
+ if (processor != null)
+ {
+ processor.handle(close);
+
+ m_logger.log(Logger.LOG_DEBUG, "Processed " + request.toString());
+
+ // TODO: Adding next line to make test cases pass, but not sure if it is correct
+ // and needs further investigation.
+ close = true;
+ continue;
+ }
+
+ close = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendNotFoundResponse();
+ }
+ }
+ }
+ finally
+ {
+ try
+ {
+ m_is.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket input stream.", ex);
+ }
+ try
+ {
+ m_os.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket output stream.", ex);
+ }
+ try
+ {
+ m_socket.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket.", ex);
+ }
+ }
+ }
+}
\ No newline at end of file
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.servlet.HttpConstants;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * Handles resource processing.
+ *
+ * Encapsulates the logic in OSGI Service Platform Release 4 Compendium Version 4.2 Section 102.3
+ *
+ */
+public class ResourceHandler implements ServiceRegistrationHandler
+{
+
+ private final HttpServletRequestImpl m_request;
+ private final HttpServletResponseImpl m_response;
+
+ private final HttpContext m_httpContext;
+ private final String m_name;
+ private final String m_alias;
+
+ /**
+ * @param req HttpRequest
+ * @param res HttpResponse
+ * @param resource ServiceRegistration
+ */
+ public ResourceHandler(final HttpServletRequestImpl req, final HttpServletResponseImpl res, final ServiceRegistration resource)
+ {
+ if (resource.isServlet())
+ {
+ throw new IllegalStateException(
+ "Invalid state, ResourceHandler constructed with a Servlet.");
+ }
+
+ this.m_request = req;
+ this.m_response = res;
+ this.m_httpContext = resource.getContext();
+ this.m_name = resource.getName();
+ this.m_alias = resource.getAlias();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler#process(boolean)
+ */
+ public void handle(final boolean close) throws IOException
+ {
+ if (!m_request.getMethod().equals(HttpConstants.GET_REQUEST)
+ && !m_request.getMethod().equals(HttpConstants.HEAD_REQUEST))
+ {
+
+ //POST, PUT, DELETE operations not valid on resources.
+ return;
+ }
+
+ if (m_httpContext.handleSecurity(m_request, m_response))
+ {
+ String resourceName = getResourceName(m_request.getRequestURI());
+
+ URL resource = m_httpContext.getResource(resourceName);
+
+ if (resource == null)
+ {
+ throw new IOException("Unable to find resource: " + resourceName);
+ }
+
+ InputStream inputStream = resource.openStream();
+ m_response.setContentType(m_httpContext.getMimeType(resourceName));
+
+ m_response.writeToOutputStream(inputStream, close);
+ }
+ }
+
+ /**
+ * @param path String
+ * @return resource name at given path.
+ */
+ private String getResourceName(final String path)
+ {
+ return m_name + path.substring(m_alias.length());
+ }
+}
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,370 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+
+/**
+ * This class implements a simple multi-threaded web server. It
+ * only supports GET/HEAD requests. The web server has various configurable
+ * properties that can be passed into the constructor; see the constructor
+ * for more information about configuration properties.
+**/
+public class Server
+{
+ public static final String CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP = "org.apache.felix.http.connection.timeout";
+ public static final String CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP = "org.apache.felix.http.connection.requestlimit";
+ public static final String CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP = "org.apache.felix.http.threadpool.timeout";
+ public static final String CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP = "org.apache.felix.http.threadpool.limit";
+ //Flag to enable debugging for this service implementation. The default is false.
+ public static final String CONFIG_PROPERTY_HTTP_DEBUG = "org.apache.felix.http.debug";
+ // Flag to enable the user of HTTPS. The default is false.
+ public static final String CONFIG_PROPERTY_HTTPS_ENABLE = "org.apache.felix.https.enable";
+ // Flag to enable the use of HTTP. The default is true.
+ public static final String CONFIG_PROPERTY_HTTP_ENABLE = "org.apache.felix.http.enable";
+ // The port used for servlets and resources available via HTTP. The default is 8080. A negative port number has the same effect as setting org.apache.felix.http.enable to false.
+ public static final String CONFIG_PROPERTY_HTTP_PORT = "org.osgi.service.http.port";
+
+ /**
+ * Default HTTP port to listen on.
+ */
+ private static final int DEFAULT_PORT = 8080;
+ /**
+ * Default number of concurrent requests.
+ */
+ private static final int DEFAULT_THREADPOOL_LIMIT = 10;
+
+ /**
+ * Server is inactive (off).
+ */
+ public static final int INACTIVE_STATE = 0;
+ /**
+ * Server is active (running)
+ */
+ public static final int ACTIVE_STATE = 1;
+ /**
+ * Server is shutting down.
+ */
+ public static final int STOPPING_STATE = 2;
+
+ private String m_hostname;
+ private final int m_port;
+
+ private int m_state;
+ private ThreadGate m_shutdownGate;
+
+ private Thread m_serverThread;
+ private ServerSocket m_serverSocket;
+ private final ThreadPool m_threadPool;
+
+ private final int m_connectionTimeout;
+ private final int m_connectionRequestLimit;
+ private ServiceRegistrationResolver m_resolver;
+ private final Logger m_logger;
+
+ /**
+ * Construct a web server with the specified configuration. The configuration
+ * map may contain the following properties:
+ * <ul>
+ * <li><tt>org.osgi.service.http.port</tt> - the port on which it listens for connections;
+ * the default is 8080.
+ * </li>
+ * <li><tt>org.apache.felix.http.threadpool.limit</tt> - the maximum number of threads in the
+ * thread pool; the default value is 10.
+ * </li>
+ * <li><tt>org.apache.felix.http.threadpool.timeout</tt> - the inactivity timeout for threads in
+ * the thread pool after which time the threads will terminate; the
+ * default value is 60000 milliseconds.
+ * </li>
+ * <li><tt>.org.apache.felix.http.connection.requestlimit</tt> - the maximum number of requests that
+ * will be accepted over a persistent connection before closing the
+ * connection; the default value is 50.
+ * </li>
+ * <li><tt>org.apache.felix.http.connection.timeout</tt> - the inactivity timeout for persistent
+ * connections after which the connection is closed; the default value
+ * is 10000 milliseconds.
+ * </li>
+ * </ul>
+ * The configuration properties cannot be changed after construction. The
+ * web server is not active until it is started.
+ * @param configMap The map of configuration properties; can be <tt>null</tt>.
+ * @param logger
+ **/
+ public Server(Map configMap, final Logger logger)
+ {
+ this.m_logger = logger;
+ m_state = INACTIVE_STATE;
+
+ configMap = (configMap == null) ? new HashMap() : configMap;
+
+ // Read in the configured properties or their default values.
+ m_port = getConfiguredPort(configMap);
+ int threadLimit = (configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP) == null) ? DEFAULT_THREADPOOL_LIMIT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP));
+ int threadTimeout = (configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP) == null) ? ThreadPool.DEFAULT_THREAD_TIMEOUT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP));
+ m_threadPool = new ThreadPool(threadLimit, threadTimeout, m_logger);
+ m_connectionTimeout = (configMap.get(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP) == null) ? Connection.DEFAULT_CONNECTION_TIMEOUT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP));
+ m_connectionRequestLimit = (configMap.get(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP) == null) ? Connection.DEFAULT_CONNECTION_REQUESTLIMIT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP));
+ }
+
+ /**
+ * Get the port the HTTP server listens on based on configuration map or default value.
+ *
+ * @param configMap
+ * @return port number that server listens on.
+ */
+ public static int getConfiguredPort(Map configMap)
+ {
+ return (configMap.get(Server.CONFIG_PROPERTY_HTTP_PORT) == null) ? DEFAULT_PORT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_HTTP_PORT));
+ }
+
+ /**
+ * This method returns the current state of the web server, which is one
+ * of the following values:
+ * <ul>
+ * <li><tt>HttpServer.INACTIVE_STATE</tt> - the web server is currently
+ * not active.
+ * </li>
+ * <li><tt>HttpServer.ACTIVE_STATE</tt> - the web server is active and
+ * serving files.
+ * </li>
+ * <li><tt>HttpServer.STOPPING_STATE</tt> - the web server is in the
+ * process of shutting down.
+ * </li>
+ * </li>
+ * @return The current state of the web server.
+ **/
+ public synchronized int getState()
+ {
+ return m_state;
+ }
+
+ /**
+ * Returns the hostname associated with the web server.
+ * @return The hostname associated with the web server.
+ **/
+ public synchronized String getHostname()
+ {
+ if (m_hostname == null)
+ {
+ try
+ {
+ m_hostname = InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR,
+ "Unable to get hostname, setting to localhost.", ex);
+ m_hostname = "localhost";
+ }
+ }
+ return m_hostname;
+ }
+
+ /**
+ * Returns the port associated with the web server.
+ * @return The port associated with the web server.
+ **/
+ public synchronized int getPort()
+ {
+ return m_port;
+ }
+
+ /**
+ * This method starts the web server if it is not already active.
+ * @param resolver Resolver is able to get Servlet or Resource based on request URI.
+ *
+ * @throws java.io.IOException If there are any networking issues.
+ * @throws java.lang.IllegalStateException If the server is in the
+ * <tt>HttpServer.STOPPING_STATE</tt> state.
+ **/
+ public synchronized void start(final ServiceRegistrationResolver resolver)
+ throws IOException
+ {
+ m_resolver = resolver;
+ if (m_state == INACTIVE_STATE)
+ {
+ // If inactive, then create server socket, server thread, and
+ // set state to active.
+ m_serverSocket = new ServerSocket(m_port);
+ m_serverThread = new Thread(new Runnable()
+ {
+ public void run()
+ {
+ acceptConnections();
+ }
+ }, "HttpServer");
+ m_state = ACTIVE_STATE;
+ m_serverThread.start();
+ }
+ else if (m_state == STOPPING_STATE)
+ {
+ throw new IllegalStateException("Server is in process of stopping.");
+ }
+ }
+
+ /**
+ * This method stops the web server if it is currently active. This method
+ * will block the calling thread until the web server is completely stopped.
+ * This can potentially take a long time, since it allows all existing
+ * connections to be processed before shutting down. Subsequent calls to
+ * this method will also block the caller. If a blocked thread is interrupted,
+ * the method will release the blocked thread by throwing an interrupted
+ * exception. In such a case, the web server will still continue its
+ * shutdown process.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted.
+ **/
+ public void stop() throws InterruptedException
+ {
+ ThreadGate gate = null;
+
+ synchronized (this)
+ {
+ // If we are active or stopping, allow the caller to shutdown the
+ // server socket and grab a local copy of the shutdown gate to
+ // wait for the server to stop.
+ if (m_state != INACTIVE_STATE)
+ {
+ m_logger.log(Logger.LOG_INFO,
+ "Shutting down, be patient...waiting for all threads to finish.");
+
+ // If there is no shutdown gate, create one and save its
+ // reference both in the field and locally. All threads
+ // that call stop() while the server is stopping will wait
+ // on this gate.
+ if (m_shutdownGate == null)
+ {
+ m_shutdownGate = new ThreadGate();
+ }
+ gate = m_shutdownGate;
+
+ // Close the server socket, which will cause the server thread
+ // to exit its accept() loop.
+ try
+ {
+ m_serverSocket.close();
+ }
+ catch (IOException ex)
+ {
+ }
+ }
+ }
+
+ // Wait on gate for server thread to shutdown.
+ if (gate != null)
+ {
+ gate.await();
+ }
+ }
+
+ /**
+ * This method is the main server loop for accepting connection. This is
+ * only ever called by the server thread.
+ **/
+ private void acceptConnections()
+ {
+ // Start the thread pool.
+ m_threadPool.start();
+
+ Socket socket;
+
+ m_logger.log(Logger.LOG_DEBUG, "Waiting for connections.");
+
+ // Now listen for connections until interrupted.
+ while (m_serverSocket.isBound() && !m_serverSocket.isClosed())
+ {
+ try
+ {
+ socket = m_serverSocket.accept();
+ try
+ {
+ // Create connection object and add it to the thread pool
+ // to be serviced.
+ Connection connection = new Connection(socket, m_connectionTimeout,
+ m_connectionRequestLimit, m_resolver, m_logger);
+ m_logger.log(Logger.LOG_DEBUG, "Accepted a new connection.");
+ m_threadPool.addConnection(connection);
+ }
+ catch (IOException ex)
+ {
+ // If we have any difficulty creating the connection
+ // then just ignore it, because the socket will be
+ // closed in the connection constructor.
+ m_logger.log(Logger.LOG_ERROR, "Error creating connection.", ex);
+ }
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR,
+ "The call to accept() terminated with an exception.", ex);
+ }
+ }
+
+ // Shutdown the server.
+ shutdown();
+ }
+
+ /**
+ * This method shuts down the server; it is only ever called by the
+ * server thread.
+ **/
+ private void shutdown()
+ {
+ m_logger.log(Logger.LOG_DEBUG, "Waiting for thread pool threads to stop.");
+
+ while (true)
+ {
+ try
+ {
+ // Wait for thread pool to stop servicing connections.
+ m_threadPool.stop();
+ break;
+ }
+ catch (InterruptedException ex)
+ {
+ // Only the server thread will call this and we don't interrupt
+ // it, so this should never happen, but just in case we will loop
+ // until the thread pool is actually stopped.
+ }
+ }
+
+ synchronized (this)
+ {
+ // Now that the thread pool is stopped, open the shutdown
+ // gate and set the state to inactive.
+ m_shutdownGate.open();
+ m_shutdownGate = null;
+ m_state = INACTIVE_STATE;
+ }
+ m_logger.log(Logger.LOG_DEBUG, "Shutdown complete.");
+ }
+}
\ No newline at end of file
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+
+/**
+ * Handles servlet processing.
+ * This class encapsulates the work done on a servlet given a request and response.
+ */
+public class ServletHandler implements ServiceRegistrationHandler
+{
+
+ private final HttpServletRequestImpl m_request;
+ private final HttpServletResponseImpl m_response;
+ private final ServiceRegistration m_servletElement;
+ private final Logger m_logger;
+
+ /**
+ * @param request Http Request
+ * @param response Http Response
+ * @param element Servlet Registration
+ * @param m_logger Logger
+ */
+ public ServletHandler(final HttpServletRequestImpl request, final HttpServletResponseImpl response, final ServiceRegistration element, final Logger m_logger)
+ {
+ this.m_request = request;
+ this.m_response = response;
+ this.m_servletElement = element;
+ this.m_logger = m_logger;
+ }
+
+ /**
+ * Process the servlet.
+ *
+ * @param close true if not keep-alive connection.
+ * @throws ServletException
+ * @throws IOException
+ */
+ public void handle(final boolean close) throws ServletException, IOException
+ {
+ //Check to see if the Servlet has been initialized, if not initialize it and set the flag.
+ synchronized (m_servletElement)
+ {
+ if (!m_servletElement.hasBeenInitialized())
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Initializing servlet " + m_servletElement.getAlias());
+ m_servletElement.getServlet().init(m_servletElement.getServletConfig());
+ m_servletElement.setInitialized();
+ }
+ }
+
+ if (m_servletElement.getContext().handleSecurity(m_request, m_response))
+ {
+ m_servletElement.getServlet().service(m_request, m_response);
+ }
+
+ if (!m_response.isCommitted())
+ {
+ m_response.flushBuffer();
+ }
+ }
+}
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+/**
+ * This class implements a simple one-shot gate for threads. The gate
+ * starts closed and will block any threads that try to wait on it. Once
+ * opened, all waiting threads will be released. The gate cannot be reused.
+**/
+public class ThreadGate
+{
+ private boolean m_open = false;
+
+ /**
+ * Open the gate and release any waiting threads.
+ **/
+ public synchronized void open()
+ {
+ m_open = true;
+ notifyAll();
+ }
+
+ /**
+ * Wait for the gate to open.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted;
+ * the gate still remains closed until opened.
+ **/
+ public synchronized void await() throws InterruptedException
+ {
+ while (!m_open)
+ {
+ wait();
+ }
+ }
+}
\ No newline at end of file
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,312 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+
+/**
+ * This class implements a simple thread pool for servicing HTTP connections.
+ * The thread pool does not create any threads initially, but waits for
+ * connections to be added to create threads. As connections are added, threads
+ * are only created if they are needed up until the thread limit. If threads
+ * are inactive for a period of time, then the threads terminate; the default
+ * is 60000 milliseconds.
+**/
+public class ThreadPool
+{
+ public static final int DEFAULT_THREAD_TIMEOUT = 60000;
+ private int m_threadTimeout;
+
+ private ThreadGroup m_group = new ThreadGroup("ThreadPoolGroup");
+ private int m_state;
+ private ThreadGate m_shutdownGate;
+ private int m_threadName = 0;
+ private int m_threadLimit = 0;
+ private int m_threadCount = 0;
+ private int m_threadAvailable = 0;
+ private List m_connectionList = new ArrayList();
+ private final Logger m_logger;
+
+ /**
+ * Constructs a thread pool with the specified thread limit and with
+ * the default inactivity timeout.
+ * @param threadLimit The maximum number of threads in the pool.
+ **/
+ public ThreadPool(final int threadLimit, final Logger logger)
+ {
+ this(threadLimit, DEFAULT_THREAD_TIMEOUT, logger);
+ }
+
+ /**
+ * Constructs a thread pool with the specified thread limit and inactivity
+ * timeout.
+ * @param threadLimit The maximum number of threads in the pool.
+ * @param threadTimeout The inactivity timeout for threads in milliseconds.
+ **/
+ public ThreadPool(int threadLimit, int threadTimeout, Logger logger)
+ {
+ m_threadLimit = threadLimit;
+ m_threadTimeout = threadTimeout;
+ m_logger = logger;
+ m_state = Server.INACTIVE_STATE;
+ }
+
+ /**
+ * This method returns the current state of the thread pool, which is one
+ * of the following values:
+ * <ul>
+ * <li><tt>ThreadPool.INACTIVE_STATE</tt> - the thread pool is currently
+ * not active.
+ * </li>
+ * <li><tt>ThreadPool.ACTIVE_STATE</tt> - the thread pool is active and
+ * servicing connections.
+ * </li>
+ * <li><tt>ThreadPool.STOPPING_STATE</tt> - the thread pool is in the
+ * process of shutting down.
+ * </li>
+ * </li>
+ * @return The current state of the thread pool.
+ **/
+ public synchronized int getState()
+ {
+ return m_state;
+ }
+
+ /**
+ * Starts the thread pool if it is not already active, allowing it to
+ * service connections.
+ * @throws java.lang.IllegalStateException If the thread pool is in the
+ * <tt>ThreadPool.STOPPING_STATE</tt> state.
+ **/
+ public synchronized void start()
+ {
+ if (m_state != Server.STOPPING_STATE)
+ {
+ m_state = Server.ACTIVE_STATE;
+ }
+ else
+ {
+ throw new IllegalStateException("Thread pool is in process of stopping.");
+ }
+ }
+
+ /**
+ * This method stops the thread pool if it is currently active. This method
+ * will block the calling thread until the thread pool is completely stopped.
+ * This can potentially take a long time, since it allows all existing
+ * connections to be processed before shutting down. Subsequent calls to
+ * this method will also block the caller. If a blocked thread is interrupted,
+ * the method will release the blocked thread by throwing an interrupted
+ * exception. In such a case, the thread pool will still continue its
+ * shutdown process.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted.
+ **/
+ public void stop() throws InterruptedException
+ {
+ ThreadGate gate = null;
+
+ synchronized (this)
+ {
+ if (m_state != Server.INACTIVE_STATE)
+ {
+ // If there is no shutdown gate, create one and save its
+ // reference both in the field and locally. All threads
+ // that call stop() while the server is stopping will wait
+ // on this gate.
+ if ((m_shutdownGate == null) && (m_threadCount > 0))
+ {
+ m_shutdownGate = new ThreadGate();
+ }
+ gate = m_shutdownGate;
+ m_state = Server.STOPPING_STATE;
+ // Interrupt all threads that have been created by the
+ // thread pool.
+ m_group.interrupt();
+ }
+ }
+
+ // Wait on gate for thread pool shutdown to complete.
+ if (gate != null)
+ {
+ gate.await();
+ }
+ }
+
+ /**
+ * This method adds an HTTP connection to the thread pool for servicing.
+ * @param connection
+ * @throws java.lang.IllegalStateException If the thread pool is not in the
+ * <tt>ThreadPool.ACTIVE_STATE</tt> state.
+ **/
+ public synchronized void addConnection(final Connection connection)
+ {
+ if (m_state == Server.ACTIVE_STATE)
+ {
+ // Add the new connection to the connection list.
+ m_connectionList.add(connection);
+ notify();
+
+ // If there are not enough available threads to handle all outstanding
+ // connections and we still haven't reached our thread limit, then
+ // add another thread.
+ if ((m_threadAvailable < m_connectionList.size())
+ && (m_threadCount < m_threadLimit))
+ {
+ // Increase our thread count, but not number of available threads,
+ // since the new thread will be used to service the new connection
+ // and thus is not available.
+ m_threadCount++;
+ // Use simple integer for thread name for logging purposes.
+ if (m_threadName == Integer.MAX_VALUE)
+ {
+ m_threadName = 1;
+ }
+ else
+ {
+ m_threadName++;
+ }
+ // Create and start thread into our thread group.
+ new Thread(m_group, new Runnable()
+ {
+ public void run()
+ {
+ processConnections();
+ }
+ }, Integer.toString(m_threadName)).start();
+ m_logger.log(Logger.LOG_DEBUG, "Created new thread for pool; count = "
+ + m_threadCount + ", max = " + m_threadLimit + ".");
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("The thread pool is not active.");
+ }
+ }
+
+ /**
+ * This method is the main loop for all threads servicing connections.
+ **/
+ private void processConnections()
+ {
+ Connection connection;
+ while (true)
+ {
+ synchronized (this)
+ {
+ // Any new threads entering this region are now available to
+ // process a connection, so increment the available count.
+ m_threadAvailable++;
+
+ try
+ {
+ // Keep track of when we start to wait so that we
+ // know if our timeout expires.
+ long start = System.currentTimeMillis();
+ long current = start;
+ // Wait until there is a connection to service or until
+ // the timeout expires; if the timeout is zero, then there
+ // is no timeout.
+ while (m_state == Server.ACTIVE_STATE
+ && (m_connectionList.size() == 0)
+ && ((m_threadTimeout == 0) || ((current - start) < m_threadTimeout)))
+ {
+ // Try to wait for another connection, but our timeout
+ // expires then commit suicide.
+ wait(m_threadTimeout - (current - start));
+ current = System.currentTimeMillis();
+ }
+ }
+ catch (InterruptedException ex)
+ {
+ // This generally happens when we are shutting down.
+ Thread.currentThread().interrupt();
+ }
+
+ // Set connection to null if we are going to commit suicide;
+ // otherwise get the first available connection for servicing.
+ if (m_connectionList.size() == 0)
+ {
+ connection = null;
+ }
+ else
+ {
+ connection = (Connection) m_connectionList.remove(0);
+ }
+
+ // Decrement number of available threads, since we will either
+ // start to service a connection at this point or we will commit
+ // suicide.
+ m_threadAvailable--;
+
+ // If we do not have a connection, then we are committing
+ // suicide due to inactivity or because we were interrupted
+ // and are stopping the thread pool.
+ if (connection == null)
+ {
+ // One less thread in use.
+ m_threadCount--;
+ if (Thread.interrupted())
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Pool thread dying due to interrupt.");
+ }
+ else
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Pool thread dying due to inactivity.");
+ }
+ // If we are stopping and the last thread is dead, then
+ // open the shutdown gate to release all threads waiting
+ // for us to stop.
+ if ((m_state == Server.STOPPING_STATE) && (m_threadCount == 0))
+ {
+ m_shutdownGate.open();
+ m_shutdownGate = null;
+ m_state = Server.INACTIVE_STATE;
+ }
+ // Return to kill the thread by exiting our run method.
+ return;
+ }
+ }
+
+ // Otherwise, we have a connection so process it.
+ // Note, we might have outstanding connections to
+ // process even if we are stopping, so we cleaning
+ // service those remaining connections before stopping.
+ try
+ {
+ connection.process();
+ m_logger.log(Logger.LOG_DEBUG, "Connection closed normally.");
+ }
+ catch (SocketTimeoutException ex)
+ {
+ m_logger.log(Logger.LOG_INFO, "Connection closed due to inactivity.");
+ }
+ catch (Exception ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Connection close due to unknown reason.",
+ ex);
+ }
+ }
+ }
+}
\ No newline at end of file
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletInputStream;
+
+/**
+ * Simple utility input stream class that provides a method for reading
+ * a line of characters, where a "line" is leniently defined as anything
+ * ending in '\n' or '\r\n'.
+ *
+ * Extends ServletInputStream
+**/
+public class ConcreteServletInputStream extends ServletInputStream
+{
+ private InputStream m_is;
+ private StringBuffer m_sb = new StringBuffer();
+
+ /**
+ * @param is InputStream
+ */
+ public ConcreteServletInputStream(final InputStream is)
+ {
+ m_is = is;
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#available()
+ */
+ public int available() throws IOException
+ {
+ return m_is.available();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#close()
+ */
+ public void close() throws IOException
+ {
+ m_is.close();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#mark(int)
+ */
+ public void mark(int readlimit)
+ {
+ m_is.mark(readlimit);
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#markSupported()
+ */
+ public boolean markSupported()
+ {
+ return m_is.markSupported();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read()
+ */
+ public int read() throws IOException
+ {
+ return m_is.read();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read(byte[])
+ */
+ public int read(final byte[] b) throws IOException
+ {
+ return m_is.read(b);
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read(byte[], int, int)
+ */
+ public int read(final byte[] b, final int off, final int len) throws IOException
+ {
+ return m_is.read(b, off, len);
+ }
+
+ /**
+ * @return The next line in the input stream or null for EOF
+ * @throws IOException on I/O error
+ */
+ public String readLine() throws IOException
+ {
+ m_sb.delete(0, m_sb.length());
+ int bytesRead = 0;
+ for (int i = m_is.read(); i >= 0; i = m_is.read())
+ {
+ bytesRead++;
+ if ('\n' == (char) i)
+ {
+ break;
+ }
+ else if ('\r' != (char) i)
+ {
+ m_sb.append((char) i);
+ }
+ }
+ if (bytesRead == 0)
+ {
+ return null;
+ }
+
+ return m_sb.toString();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#reset()
+ */
+ public void reset() throws IOException
+ {
+ m_is.reset();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#skip(long)
+ */
+ public long skip(final long n) throws IOException
+ {
+ return m_is.skip(n);
+ }
+}
\ No newline at end of file
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.util.Collections;
+import java.util.Enumeration;
+
+/**
+ * Defines some commonly used HTTP constants and headers.
+ */
+public class HttpConstants
+{
+ /**
+ * HTTP line delimiter
+ */
+ public static final String HEADER_DELEMITER = "\r\n";
+ /**
+ * HTTP header delimiter
+ */
+ public static final String HEADER_TERMINATOR = HEADER_DELEMITER + HEADER_DELEMITER;
+
+ /**
+ * Content-Length header
+ */
+ public static final String HEADER_CONTENT_LENGTH = "Content-Length";
+ /**
+ * Location header
+ */
+ public static final String HEADER_LOCATION = "Location";
+ /**
+ * Content-Type header
+ */
+ public static final String HEADER_CONTENT_TYPE = "Content-Type";
+ /**
+ * Connection header
+ */
+ public static final String HEADER_CONNECTION = "Connection";
+
+ /**
+ * For building HTML error messages, this value is the default start of the html document for error message responses.
+ */
+ public static final String DEFAULT_HTML_HEADER = "<html>";
+
+ /**
+ * HTTP header delimiter.
+ */
+ public static final String HEADER_VALUE_DELIMITER = ": ";
+
+ /**
+ * HTTP GET Method
+ */
+ public static final String GET_REQUEST = "GET";
+ /**
+ * HTTP HEAD Method
+ */
+ public static final String HEAD_REQUEST = "HEAD";
+ /**
+ * HTTP POST Method
+ */
+ public static final String POST_REQUEST = "POST";
+ /**
+ * HTTP PUT Method
+ */
+ public static final String PUT_REQUEST = "PUT";
+ /**
+ * HTTP DELETE Method
+ */
+ public static final String DELETE_REQUEST = "DELETE";
+ /**
+ * HTTP OPTIONS Method
+ */
+ public static final Object OPTIONS_REQUEST = "OPTIONS";
+
+ /**
+ * HTTP v 1.0
+ */
+ public static final String HTTP10_VERSION = "HTTP/1.0";
+ /**
+ * HTTP v 1.1
+ */
+ public static final String HTTP11_VERSION = "HTTP/1.1";
+ /**
+ * Host header
+ */
+ public static final String HOST_HEADER = "Host";
+
+ /**
+ * Keep-alive value for Connection header.
+ */
+ public static final String KEEPALIVE_CONNECTION = "keep-alive";
+ /**
+ * Close value for Connection header.
+ */
+ public static final String CLOSE_CONNECTION = "close";
+ /**
+ * Date format for HTTP
+ */
+ public static final String HTTP_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss z";
+ /**
+ * Timezone specified for HTTP
+ */
+ public static final String HTTP_TIMEZONE = "GMT";
+
+ /**
+ * Felix HTTP service property to enable HTTP server
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTP_ENABLE = "org.apache.felix.http.enable";
+ /**
+ * Felix HTTP service property to enable HTTPS server (unimplemented)
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTPS_ENABLE = "org.apache.felix.https.enable";
+ /**
+ * Felix HTTP property to configure server port.
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTP_PORT = "org.osgi.service.http.port";
+
+ /**
+ * HTTP response code 100
+ */
+ public static final int HTTP_RESPONSE_CONTINUE = 100;
+ /**
+ * Servlet implementation name.
+ */
+ public static final String SERVER_INFO = "Apache Felix Lightweight HTTP Service";
+ /**
+ * HTTP Scheme
+ */
+ public static final String HTTP_SCHEME = "http";
+
+ /**
+ * Servlet API requires passing empty enumerations.
+ */
+ public static final Enumeration EMPTY_ENUMERATION = Collections.enumeration(Collections.EMPTY_SET);
+
+}
Added: felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java?rev=1182650&view=auto
==============================================================================
--- felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java (added)
+++ felix/trunk/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java Thu Oct 13 05:01:42 2011
@@ -0,0 +1,977 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.Socket;
+import java.net.URLDecoder;
+import java.security.Principal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+
+/**
+ * This class represents an HTTP request, which is parses from a given input
+ * stream, and implements HttpServletRequest for servlet processing.
+ **/
+public class HttpServletRequestImpl implements HttpServletRequest
+{
+ /**
+ * HTTP Method
+ */
+ private String m_method;
+ /**
+ * Host info of URI
+ */
+ private String m_uriHost;
+ /**
+ * URI of HTTP request
+ */
+ private String m_uri;
+ /**
+ * HTTP version
+ */
+ private String m_version;
+ /**
+ * Headers in HTTP request
+ */
+ private final Map m_headers = new HashMap();
+ private final Socket m_socket;
+ private Cookie[] m_cookies;
+ private final Locale m_locale = new Locale(System.getProperty("user.language"));
+ private Map m_attributes;
+ private final ServiceRegistrationResolver m_resolver;
+ private String m_servletPath;
+ /**
+ * Map of the parameters of the request.
+ */
+ private Map m_parameters;
+
+ /**
+ * When the body is parsed this value will be set to a non-null value
+ * regardless of the body content. As such it serves as a flag for parsing
+ * the body.
+ */
+ private byte[] m_requestBody = null;
+ private final static String m_encoding = "UTF-8";
+ private final Logger m_logger;
+ private String m_queryString;
+ /**
+ * Used to enforce the servlet API getInputStream()/getReader() calls.
+ */
+ private boolean m_getInputStreamCalled = false;
+ /**
+ * Used to enforce the servlet API getInputStream()/getReader() calls.
+ */
+ private boolean m_getReaderCalled = false;
+
+ /**
+ * @param socket Socket assocated with request
+ * @param serviceRegistrationResolver
+ * @param logger
+ */
+ public HttpServletRequestImpl(final Socket socket, final ServiceRegistrationResolver serviceRegistrationResolver, final Logger logger)
+ {
+ this.m_socket = socket;
+ this.m_resolver = serviceRegistrationResolver;
+ this.m_logger = logger;
+ }
+
+ /**
+ * @return The socket this request is associated with.
+ */
+ protected Socket getSocket()
+ {
+ return m_socket;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getInputStream()
+ */
+ public ServletInputStream getInputStream() throws IOException
+ {
+
+ if (m_getReaderCalled)
+ {
+ throw new IllegalStateException("getReader() has already been called.");
+ }
+
+ if (m_requestBody == null)
+ {
+ parseBody(new BufferedInputStream(m_socket.getInputStream()));
+ }
+
+ m_getInputStreamCalled = true;
+
+ return new ConcreteServletInputStream(new ByteArrayInputStream(m_requestBody));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getReader()
+ */
+ public BufferedReader getReader() throws IOException
+ {
+ if (m_getInputStreamCalled)
+ {
+ throw new IllegalStateException("getInputStream() has already been called.");
+ }
+ if (m_requestBody == null)
+ {
+ parseBody(new BufferedInputStream(m_socket.getInputStream()));
+ }
+
+ m_getReaderCalled = true;
+
+ return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
+ m_requestBody)));
+ }
+
+ /**
+ * This method parses the HTTP request line from the specified input stream
+ * and stores the result.
+ *
+ * @param is
+ * The input stream from which to read the HTTP request.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseRequestLine(final ConcreteServletInputStream is) throws IOException
+ {
+ String requestLine = is.readLine();
+ if (requestLine == null)
+ {
+ throw new IOException("Unexpected end of file when reading request line.");
+ }
+ StringTokenizer st = new StringTokenizer(requestLine, " ");
+ if (st.countTokens() != 3)
+ {
+ throw new IOException("Malformed HTTP request: " + requestLine);
+ }
+ m_method = st.nextToken();
+ m_uri = st.nextToken();
+ m_version = st.nextToken();
+
+ // If the URI is absolute, break into host and path.
+ m_uriHost = "";
+ int hostIdx = m_uri.indexOf("//");
+ if (hostIdx > 0)
+ {
+ int pathIdx = m_uri.indexOf("/", hostIdx + 2);
+ m_uriHost = m_uri.substring(hostIdx + 2, pathIdx);
+ m_uri = m_uri.substring(pathIdx);
+ }
+
+ // If the URI has query string, parse it.
+ int qsIdx = m_uri.indexOf("?");
+ if (qsIdx > 0)
+ {
+ m_queryString = m_uri.substring(qsIdx + 1);
+ m_uri = m_uri.substring(0, qsIdx);
+ }
+ }
+
+ /**
+ * This method parses the HTTP header lines from the specified input stream
+ * and stores the results.
+ *
+ * The map m_headers is populated with two types of values, Strings if the
+ * header occurs once or a List in the case that the same header is
+ * specified multiple times.
+ *
+ * @param is
+ * The input stream from which to read the HTTP header lines.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseHeader(final ConcreteServletInputStream is) throws IOException
+ {
+ for (String s = is.readLine(); (s != null) && (s.length() != 0); s = is.readLine())
+ {
+ int idx = s.indexOf(":");
+ if (idx > 0)
+ {
+ String header = s.substring(0, idx).trim();
+ String value = s.substring(idx + 1).trim();
+
+ String key = header.toLowerCase();
+
+ if (!m_headers.containsKey(key))
+ {
+ m_headers.put(key, value);
+ }
+ else
+ {
+ Object originalValue = m_headers.get(key);
+
+ if (originalValue instanceof String)
+ {
+ List headerList = new ArrayList();
+ headerList.add(originalValue);
+ headerList.add(value);
+ m_headers.put(key, headerList);
+ }
+ else if (originalValue instanceof List)
+ {
+ ((List) originalValue).add(value);
+ }
+ else
+ {
+ throw new RuntimeException("Unexpected type in m_headers: "
+ + originalValue.getClass().getName());
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * This method parses the HTTP body from the specified input stream and
+ * ignores the result.
+ *
+ * @param is
+ * The input stream from which to read the HTTP body.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseBody(final InputStream is) throws IOException
+ {
+ int length = getContentLength();
+
+ if (length > 0)
+ {
+ ByteArrayOutputStream baos = null;
+
+ byte[] buf = new byte[length];
+ int left = length;
+
+ do
+ {
+ left = left - is.read(buf);
+ if (left > 0)
+ {
+ if (baos == null)
+ {
+ baos = new ByteArrayOutputStream(length);
+ }
+ baos.write(buf);
+ }
+ }
+ while (left > 0);
+
+ if (baos != null)
+ {
+ m_requestBody = baos.toByteArray();
+ }
+ else
+ {
+ m_requestBody = buf;
+ }
+ }
+ else
+ {
+ // Set this to a non-null value so we know that the body has been
+ // parsed.
+ m_requestBody = new byte[0];
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getMethod()
+ */
+ public String getMethod()
+ {
+ return m_method;
+ }
+
+ /**
+ * Returns the value of the specified header, if present.
+ *
+ * @param header
+ * The header value to retrieve.
+ * @return The value of the specified header or <tt>null</tt>.
+ **/
+
+ public String getHeader(final String header)
+ {
+ Object value = m_headers.get(header.toLowerCase());
+
+ if (value == null)
+ {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ public Enumeration getHeaders(final String name)
+ {
+ Object v = m_headers.get(name);
+
+ if (v == null)
+ {
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ if (v instanceof String)
+ {
+ return Collections.enumeration(Arrays.asList(new String[] { (String) v }));
+ }
+
+ if (v instanceof List)
+ {
+ return Collections.enumeration((List) v);
+ }
+
+ throw new RuntimeException("Unexpected type in m_headers: "
+ + v.getClass().getName());
+ }
+
+ public Enumeration getHeaderNames()
+ {
+ if (m_headers.isEmpty())
+ {
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ return Collections.enumeration(m_headers.keySet());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
+ */
+ public Object getAttribute(final String arg0)
+ {
+ if (m_attributes != null)
+ {
+ return m_attributes.get(arg0);
+ }
+
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getAttributeNames()
+ */
+
+ public Enumeration getAttributeNames()
+ {
+ if (m_attributes != null)
+ {
+ return Collections.enumeration(m_attributes.keySet());
+ }
+
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getCharacterEncoding()
+ */
+
+ public String getCharacterEncoding()
+ {
+ return getHeader("Accept-Encoding");
+ }
+
+ public int getContentLength()
+ {
+ int len = 0;
+
+ try
+ {
+ len = Integer.parseInt(getHeader("Content-Length"));
+ }
+ catch (NumberFormatException e)
+ {
+ // Ignore this exception intentionally.
+ }
+
+ return len;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getContentType()
+ */
+
+ public String getContentType()
+ {
+ return getHeader("Content-Type");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getLocale()
+ */
+
+ public Locale getLocale()
+ {
+ return m_locale;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getLocales()
+ */
+
+ public Enumeration getLocales()
+ {
+ return Collections.enumeration(Arrays.asList(new Object[] { m_locale }));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
+ */
+
+ public String getParameter(final String arg0)
+ {
+ if (m_parameters == null)
+ {
+ try
+ {
+ m_parameters = parseParameters();
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Failed to parse request parameters.", e);
+ return null;
+ }
+ }
+
+ return (String) m_parameters.get(arg0);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterMap()
+ */
+
+ public Map getParameterMap()
+ {
+ return m_parameters;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterNames()
+ */
+
+ public Enumeration getParameterNames()
+ {
+ return Collections.enumeration(m_parameters.keySet());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
+ */
+
+ public String[] getParameterValues(String arg0)
+ {
+ return (String[]) m_parameters.values().toArray(new String[m_parameters.size()]);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getProtocol()
+ */
+ public String getProtocol()
+ {
+ return m_version;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
+ */
+ public String getRealPath(final String arg0)
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getRemoteAddr()
+ */
+ public String getRemoteAddr()
+ {
+ return getSocket().getRemoteSocketAddress().toString();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getRemoteHost()
+ */
+ public String getRemoteHost()
+ {
+ return getSocket().getRemoteSocketAddress().toString();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
+ */
+ public RequestDispatcher getRequestDispatcher(String arg0)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getScheme()
+ */
+ public String getScheme()
+ {
+ return HttpConstants.HTTP_SCHEME;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getServerName()
+ */
+ public String getServerName()
+ {
+ return HttpConstants.SERVER_INFO;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getServerPort()
+ */
+ public int getServerPort()
+ {
+ return getSocket().getLocalPort();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#isSecure()
+ */
+ public boolean isSecure()
+ {
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
+ */
+ public void removeAttribute(String arg0)
+ {
+ if (m_attributes != null)
+ {
+ m_attributes.remove(arg0);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
+ */
+ public void setAttribute(String arg0, Object arg1)
+ {
+ if (m_attributes == null)
+ {
+ m_attributes = new HashMap();
+ }
+
+ m_attributes.put(arg0, arg1);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
+ */
+ public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getAuthType()
+ */
+
+ public String getAuthType()
+ {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getCookies()
+ */
+
+ public Cookie[] getCookies()
+ {
+ if (m_cookies == null)
+ {
+
+ String cookieHeader = getHeader("Cookie");
+
+ if (cookieHeader == null)
+ {
+ return null;
+ }
+
+ List cookieList = new ArrayList();
+
+ for (Iterator i = Arrays.asList(cookieHeader.split(";")).iterator(); i.hasNext();)
+ {
+ String[] nvp = i.next().toString().split("=");
+
+ if (nvp.length != 2)
+ {
+ //Ignore invalid cookie and and continue.
+ continue;
+ }
+
+ cookieList.add(new Cookie(nvp[0].trim(), nvp[1].trim()));
+ }
+ m_cookies = (Cookie[]) cookieList.toArray(new Cookie[cookieList.size()]);
+ }
+
+ return m_cookies;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
+ */
+
+ public long getDateHeader(final String name)
+ {
+ String headerValue = getHeader(name);
+
+ if (headerValue == null)
+ {
+ return -1;
+ }
+
+ try
+ {
+ SimpleDateFormat sdf = new SimpleDateFormat();
+
+ return sdf.parse(headerValue).getTime();
+ }
+ catch (ParseException e)
+ {
+ throw new IllegalArgumentException("Unable to convert to date: "
+ + headerValue);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
+ */
+
+ public int getIntHeader(final String name)
+ {
+ String value = getHeader(name);
+
+ if (value == null)
+ {
+ return -1;
+ }
+
+ return Integer.parseInt(value);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getPathInfo()
+ */
+
+ public String getPathInfo()
+ {
+ String alias = getAlias();
+
+ if (m_uri != null && alias.length() > 0)
+ {
+ if (m_uri.length() == alias.length())
+ {
+ return null;
+ }
+
+ return m_uri.substring(alias.length());
+ }
+
+ return null;
+ }
+
+ public String getPathTranslated()
+ {
+ // TODO: Always returning null may be incorrect.
+ return null;
+ }
+
+ public String getContextPath()
+ {
+ return "";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getQueryString()
+ */
+
+ public String getQueryString()
+ {
+ return m_queryString;
+ }
+
+ public String getRemoteUser()
+ {
+ return null;
+ }
+
+ public boolean isUserInRole(String role)
+ {
+ return false;
+ }
+
+ public Principal getUserPrincipal()
+ {
+ return null;
+ }
+
+ public String getRequestedSessionId()
+ {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getRequestURI()
+ */
+ public String getRequestURI()
+ {
+ return m_uri;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getRequestURL()
+ */
+ public StringBuffer getRequestURL()
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append(m_uriHost);
+ sb.append(m_uri);
+
+ return sb;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getServletPath()
+ */
+
+ public String getServletPath()
+ {
+ if (m_servletPath == null)
+ {
+ ServiceRegistration element = m_resolver.getServiceRegistration(m_uri);
+
+ if (element == null)
+ {
+ throw new IllegalStateException(
+ "Unable to get ServletElement for HttpRequest.");
+ }
+
+ m_servletPath = element.getAlias();
+ }
+
+ return m_servletPath;
+ }
+
+ /**
+ * @return Alias associated with this request
+ */
+ private String getAlias()
+ {
+ ServiceRegistration element = m_resolver.getServiceRegistration(m_uri);
+
+ if (element == null)
+ {
+ throw new IllegalStateException(
+ "Unable to get ServletElement for HttpRequest.");
+ }
+
+ return element.getAlias();
+ }
+
+ public HttpSession getSession(boolean create)
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public HttpSession getSession()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdValid()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromCookie()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromURL()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromUrl()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /**
+ * Parse the parameters in the request and return as a Map of <String,
+ * String>.
+ *
+ * @return
+ * @throws UnsupportedEncodingException
+ */
+ private Map parseParameters() throws UnsupportedEncodingException
+ {
+ Map params = new HashMap();
+
+ String queryString = getQueryString();
+
+ if (queryString != null && queryString.length() > 0)
+ {
+ parseParameterString(queryString, params);
+ }
+
+ if (m_requestBody != null)
+ {
+ parseParameterString(new String(m_requestBody), params);
+ }
+
+ return params;
+ }
+
+ /**
+ *
+ * @param queryString
+ * A String formatted like: 'home=Cosby&favorite+flavor=flies'
+ * @param params
+ * Map of <String, String> of existing parameters to be added to.
+ *
+ * @throws UnsupportedEncodingException
+ * if encoding type is unsupported
+ */
+ private void parseParameterString(final String queryString, final Map params)
+ throws UnsupportedEncodingException
+ {
+ for (Iterator i = Arrays.asList(queryString.split("&")).iterator(); i.hasNext();)
+ {
+ String[] nva = i.next().toString().split("=");
+
+ if (nva.length == 2)
+ {
+ params.put(URLDecoder.decode(nva[0].trim(), m_encoding), nva[1].trim());
+ }
+ }
+ }
+
+ /**
+ * @param method
+ * HTTP method
+ * @return true if the psased HTTP method is supported by this server.
+ */
+ public static boolean isSupportedMethod(final String method)
+ {
+ if (method.equals(HttpConstants.OPTIONS_REQUEST))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ public String toString()
+ {
+ if (m_method != null && m_uri != null)
+ {
+ return m_method + m_uri;
+ }
+
+ return super.toString();
+ }
+}
\ No newline at end of file