httplite: reorg modules to core and complete.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1233710 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httplite/core/DEPENDENCIES b/httplite/core/DEPENDENCIES
new file mode 100644
index 0000000..95baca0
--- /dev/null
+++ b/httplite/core/DEPENDENCIES
@@ -0,0 +1,21 @@
+Apache Felix Lightweight HTTP Service
+Copyright 2012 The Apache Software Foundation
+
+This software was developed at the Apache Software Foundation
+(http://www.apache.org) and may have dependencies on other
+Apache software licensed under Apache License 2.0.
+
+I. Included Third-Party Software
+
+N/A
+
+II. Used Third-Party Software
+
+This product uses software developed at
+The OSGi Alliance (http://www.osgi.org).
+Copyright (c) OSGi Alliance (2000, 2009).
+Licensed under the Apache License 2.0.
+
+III. License Summary
+- Apache License 2.0
+
diff --git a/httplite/core/LICENSE b/httplite/core/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/httplite/core/LICENSE
@@ -0,0 +1,202 @@
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed 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.
diff --git a/httplite/core/NOTICE b/httplite/core/NOTICE
new file mode 100644
index 0000000..c74e865
--- /dev/null
+++ b/httplite/core/NOTICE
@@ -0,0 +1,5 @@
+Apache Felix Lightweight HTTP Service
+Copyright 2012 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
diff --git a/httplite/core/README b/httplite/core/README
new file mode 100644
index 0000000..46e39c8
--- /dev/null
+++ b/httplite/core/README
@@ -0,0 +1,3 @@
+httplite is a minimal implementation of the OSGi HTTP Service specification. It supports a subset of the features found in the bridge and jetty HTTP Service implementations. See https://issues.apache.org/jira/browse/FELIX-538 for more details.
+
+This is the 'minimum' target which at runtime, requires the OSGi Compedium HTTP Service and Servlet API classes to be provided by external bundles.
\ No newline at end of file
diff --git a/httplite/core/doc/changelog.txt b/httplite/core/doc/changelog.txt
new file mode 100644
index 0000000..1fac81a
--- /dev/null
+++ b/httplite/core/doc/changelog.txt
@@ -0,0 +1,22 @@
+Changes from 0.1.2 to 0.1.4
+---------------------------
+
+** Bug
+ * fix regression in resource path resolution code in test cases.
+ * put 'shutdown server on last client deregistration' optimization in a thread and wait 30 seconds before checking if no clients are registered. Fixes issue in which sole client "bounces" registration service on start and causes deadlock in server.
+ * fix bad formatting of ASF license header clobbered by Eclipse formatter.
+ * Use consistent names in DEPS/NOTICE files.
+ * [FELIX-3266] - PrintWriter in HttpServletResponse does not send string content to client
+ * [FELIX-3253] - Fails to resolve urls with multiple successive path separators
+ * [FELIX-3256] - Remove Ant build file from Lightweight HTTP Service bundle.
+
+** Improvement
+ * : [FELIX-3291] - added cookie support
+ * : [FELIX-3287] - Support Java 1.3 with provided by patch from Yann Diorcet.
+ * : ignore socket exceptions when server is explicitly shutdown.
+ * : [FELIX-3276] - create 'minimal' and 'complete' targets. 'complete' target includes servlet and osgi dependencies.
+ * : javadoc additions to public methods.
+
+Version 0.1.2
+---------------------
+ * Initial Release
diff --git a/httplite/core/pom.xml b/httplite/core/pom.xml
new file mode 100644
index 0000000..d852426
--- /dev/null
+++ b/httplite/core/pom.xml
@@ -0,0 +1,127 @@
+<!--
+ 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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix-parent</artifactId>
+ <version>2.1</version>
+ <relativePath>../../pom/pom.xml</relativePath>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>bundle</packaging>
+ <name>Apache Felix Lightweight HTTP Service</name>
+ <description>A minimal HTTP Service implementation.</description>
+ <version>0.1.3-SNAPSHOT</version>
+ <artifactId>org.apache.felix.httplite.core</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>4.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.compendium</artifactId>
+ <version>4.2.0</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.servlet</groupId>
+ <artifactId>servlet-api</artifactId>
+ <version>2.4</version>
+ </dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>3.8.1</version>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>com.googlecode.pojosr</groupId>
+ <artifactId>de.kalpatec.pojosr.framework</artifactId>
+ <version>0.1.6</version>
+ <type>bundle</type>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.1</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+ <build>
+ <resources>
+ <resource>
+ <directory>${project.basedir}</directory>
+ <targetPath>META-INF/</targetPath>
+ <includes>
+ <include>LICENSE</include>
+ <include>NOTICE</include>
+ <include>DEPENDENCIES</include>
+ </includes>
+ </resource>
+ </resources>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-compiler-plugin</artifactId>
+ <version>2.3.2</version>
+ <configuration>
+ <source>1.3</source>
+ <target>1.3</target>
+ </configuration>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <version>2.3.4</version>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Export-Package />
+ <Private-Package>org.apache.felix.httplite.osgi,
+ org.apache.felix.httplite.server,
+ org.apache.felix.httplite.servlet
+ </Private-Package>
+ <Export-Service>org.osgi.service.http.HttpService</Export-Service>
+ <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
+ <Bundle-Activator>${project.artifactId}.osgi.Activator</Bundle-Activator>
+ <Bundle-Vendor>The Apache Software Foundation</Bundle-Vendor>
+ <Import-Package>
+ javax.servlet;version="2.4",
+ javax.servlet.http;version="2.4",
+ org.osgi.service.http;version="1.2",
+ *
+ </Import-Package>
+ <Import-Service>
+ org.osgi.service.log.LogService;availability:=optional;multiple:=false
+ </Import-Service>
+ <Bundle-DocURL>http://felix.apache.org/site/apache-felix-light-http-service.html</Bundle-DocURL>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/felix/trunk/httplite</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/trunk/httplite</developerConnection>
+ <url>http://svn.apache.org/repos/asf/felix/trunk/httplite</url>
+ </scm>
+ <url>http://felix.apache.org/site/apache-felix-lightweight-http-service.html</url>
+</project>
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
new file mode 100644
index 0000000..8974c24
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Activator.java
@@ -0,0 +1,183 @@
+/*
+ * 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.httplite.osgi;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.felix.httplite.server.Server;
+import org.apache.felix.httplite.servlet.HttpConstants;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.http.HttpService;
+
+/**
+ * Activator for org.apache.felix.http.lightweight HTTP Service implementation.
+ *
+ * The activator will read in system properties that are relevant to the service
+ * and register the HttpService in the service registry.
+ *
+**/
+public class Activator implements BundleActivator
+{
+ /**
+ * Felix-specific log level setting as system property.
+ */
+ private static final String FELIX_LOG_LEVEL = "felix.log.level";
+ /**
+ * HTTP Service registration.
+ */
+ private ServiceRegistration m_httpServiceReg;
+ /**
+ * Reference to socket server.
+ */
+ private Server m_server;
+
+ /* (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+ */
+ public void start(BundleContext context) throws Exception
+ {
+ Logger logger = createLogger(context);
+
+ //Set the internal logger's log level if specified.
+ if (context.getProperty(FELIX_LOG_LEVEL) != null)
+ {
+ logger.setLogLevel(Integer.parseInt(context.getProperty(FELIX_LOG_LEVEL)));
+ }
+
+ //Only enable the HTTPService if the HTTP_ENABLE property is set or undefined.
+ if (isPropertyTrue(context, Server.CONFIG_PROPERTY_HTTP_ENABLE, true))
+ {
+ Map config = createConfigMap(context);
+ m_server = new Server(config, logger);
+ m_httpServiceReg = context.registerService(HttpService.class.getName(),
+ new HttpServiceFactoryImpl(logger, m_server),
+ createHttpServiceProperties(Server.getConfiguredPort(config)));
+ }
+
+ //Warn user that HTTPS is not supported if it is specifically enabled.
+ if (isPropertyTrue(context, Server.CONFIG_PROPERTY_HTTPS_ENABLE, false))
+ {
+ logger.log(Logger.LOG_WARNING, Server.CONFIG_PROPERTY_HTTPS_ENABLE
+ + " is not implemented in this http service.");
+ }
+ }
+
+ /**
+ * Create a Dictionary intended to be used with Http Service registration.
+ *
+ * @param port Port number to add to the properties.
+ * @return A dictionary of OSGi service properties associate with the HTTP service.
+ */
+ private Dictionary createHttpServiceProperties(final int port)
+ {
+ Dictionary props = new Properties();
+
+ props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTP_ENABLE, Boolean.toString(true));
+ props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTPS_ENABLE,
+ Boolean.toString(false));
+ props.put(HttpConstants.SERVICE_PROPERTY_KEY_HTTP_PORT, Integer.toString(port));
+
+ return props;
+ }
+
+ /**
+ * Create a Map of configuration name/value pairs that the socket server requires to start.
+ *
+ * @param context BundleContext
+ * @return Map of configuration name/value pairs that the socket server requires to start.
+ */
+ private Map createConfigMap(final BundleContext context)
+ {
+ Map config = new HashMap();
+
+ config.put(Server.CONFIG_PROPERTY_HTTP_PORT,
+ context.getProperty(Server.CONFIG_PROPERTY_HTTP_PORT));
+ config.put(Server.CONFIG_PROPERTY_HTTP_ENABLE,
+ context.getProperty(Server.CONFIG_PROPERTY_HTTP_ENABLE));
+ config.put(Server.CONFIG_PROPERTY_HTTPS_ENABLE,
+ context.getProperty(Server.CONFIG_PROPERTY_HTTPS_ENABLE));
+ config.put(Server.CONFIG_PROPERTY_HTTP_DEBUG,
+ context.getProperty(Server.CONFIG_PROPERTY_HTTP_DEBUG));
+ config.put(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP,
+ context.getProperty(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP));
+ config.put(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP,
+ context.getProperty(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP));
+ config.put(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP,
+ context.getProperty(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP));
+ config.put(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP,
+ context.getProperty(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP));
+
+ return config;
+ }
+
+ /**
+ * @param context BundleContext
+ * @return Logger instance
+ */
+ private Logger createLogger(final BundleContext context)
+ {
+ Logger logger = new Logger();
+ logger.setSystemBundleContext(context);
+
+ return logger;
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+ */
+ public void stop(final BundleContext context) throws Exception
+ {
+ m_server.setStopping();
+
+ if (m_httpServiceReg != null)
+ {
+ m_httpServiceReg.unregister();
+ }
+
+ if (m_server.getState() == Server.ACTIVE_STATE)
+ {
+ m_server.stop();
+ }
+ }
+
+ /**
+ * Convenience method that returns true if Bundle property exists and is true, false if false, and defaultValue otherwise.
+ * @param context BundleContext
+ * @param name Property name
+ * @param defaultValue default value for case that the key does not exist.
+ * @return true if Bundle property exists and is true, false if false, and defaultValue otherwise.
+ */
+ private static boolean isPropertyTrue(final BundleContext context, final String name,
+ final boolean defaultValue)
+ {
+ String value = context.getProperty(name);
+
+ if (value == null)
+ {
+ return defaultValue;
+ }
+
+ return Boolean.valueOf(value).booleanValue();
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/DefaultContextImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/DefaultContextImpl.java
new file mode 100644
index 0000000..2503043
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/DefaultContextImpl.java
@@ -0,0 +1,98 @@
+/*
+ * 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.httplite.osgi;
+
+import java.net.URL;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.osgi.framework.Bundle;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * This class was adapted from the Jetty HTTP Service: http://felix.apache.org/site/apache-felix-http-service.html.
+ *
+ * Implementation of default HttpContext as per OSGi specification.
+ *
+ * Notes
+ *
+ * - no current inclusion/support for permissions
+ * - security allows all request. Spec leaves security handling to be
+ * implementation specific, but does outline some suggested handling.
+ * Deeper than my understanding of HTTP at this stage, so left for now.
+ */
+public class DefaultContextImpl implements HttpContext
+{
+ /**
+ * Reference to bundle that registered with the Http Service.
+ */
+ private Bundle m_bundle;
+
+ /**
+ * @param bundle bundle that registered with the Http Service.
+ */
+ public DefaultContextImpl(final Bundle bundle)
+ {
+ m_bundle = bundle;
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.service.http.HttpContext#getMimeType(java.lang.String)
+ */
+ public String getMimeType(final String name)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.service.http.HttpContext#getResource(java.lang.String)
+ */
+ public URL getResource(final String name)
+ {
+ //TODO: temp measure for name. Bundle classloading doesn't seem to find
+ // resources which have a leading "/". This code should be removed
+ // if the bundle classloader is changed to allow a leading "/"
+ String resource = name;
+
+ if (name.startsWith("/"))
+ {
+ resource = name.substring(1);
+ }
+
+ URL url = m_bundle.getResource(resource);
+
+ if (url == null)
+ {
+ url = this.getClass().getResource(resource);
+ }
+
+ return url;
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.service.http.HttpContext#handleSecurity(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+ */
+ public boolean handleSecurity(final HttpServletRequest request,
+ final HttpServletResponse response)
+ {
+ //By default all security is "handled".
+ return true;
+ }
+}
\ No newline at end of file
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceFactoryImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceFactoryImpl.java
new file mode 100644
index 0000000..922a8db
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceFactoryImpl.java
@@ -0,0 +1,163 @@
+/*
+ * 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.httplite.osgi;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.felix.httplite.server.Server;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.ServiceFactory;
+import org.osgi.framework.ServiceRegistration;
+
+/**
+ * HttpServiceFactory creates a separate HttpService instance for every requester, so that
+ * the requester (client) bundle has access to it's own class loader for getting resources.
+ *
+ */
+public class HttpServiceFactoryImpl implements ServiceFactory
+{
+ /**
+ * Logger instance
+ */
+ private final Logger m_logger;
+ /**
+ * Socket server reference
+ */
+ private final Server m_server;
+ /**
+ * List of service registrations, both Resource and Servlet.
+ */
+ private List m_registrations;
+ /**
+ * Map to store the servlet and resource registrations.
+ */
+ private final HashMap m_servletMap;
+
+ /**
+ * @param logger
+ * @param m_server
+ */
+ public HttpServiceFactoryImpl(final Logger logger, final Server m_server)
+ {
+ this.m_logger = logger;
+ this.m_server = m_server;
+ this.m_servletMap = new HashMap();
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.framework.ServiceFactory#getService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration)
+ */
+ public Object getService(final Bundle bundle, final ServiceRegistration registration)
+ {
+ HttpServiceImpl httpService = null;
+ try
+ {
+ httpService = new HttpServiceImpl(bundle, m_server, m_logger, m_servletMap);
+
+ if (m_server.getState() != Server.ACTIVE_STATE)
+ {
+ m_logger.log(Logger.LOG_INFO, "Starting http server.");
+ httpService.start();
+ }
+
+ if (m_registrations == null)
+ {
+ m_registrations = new ArrayList();
+ }
+
+ m_registrations.add(serializeRegistration(bundle, registration));
+ }
+ catch (IOException e)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Unable to create Http Service.", e);
+ return null;
+ }
+
+ return httpService;
+ }
+
+ /**
+ * Provide a unique string to represent a given registration.
+ *
+ * @param bundle
+ * @param registration
+ * @return
+ */
+ private String serializeRegistration(final Bundle bundle,
+ final ServiceRegistration registration)
+ {
+ return registration.toString() + bundle.getBundleId();
+ }
+
+ /* (non-Javadoc)
+ * @see org.osgi.framework.ServiceFactory#ungetService(org.osgi.framework.Bundle, org.osgi.framework.ServiceRegistration, java.lang.Object)
+ */
+ public void ungetService(final Bundle bundle, final ServiceRegistration registration,
+ final Object service)
+ {
+ if (m_registrations == null)
+ {
+ throw new IllegalStateException("m_registrations has not been initialized.");
+ }
+
+ String key = serializeRegistration(bundle, registration);
+
+ if (!m_registrations.contains(key))
+ {
+ throw new IllegalStateException("Untracked service registration.");
+ }
+
+ m_registrations.remove(key);
+
+ if (m_registrations.size() == 0 && m_server.getState() == Server.ACTIVE_STATE)
+ {
+ (new Thread(new Runnable()
+ {
+ public void run()
+ {
+ try
+ {
+ Thread.sleep( 1000 * 30 );
+
+ if (m_registrations == null || m_server == null || Thread.interrupted())
+ {
+ return;
+ }
+
+ if (m_registrations.size() == 0 && m_server.getState() == Server.ACTIVE_STATE)
+ {
+ m_logger.log(Logger.LOG_INFO,
+ "Stopping http server since no clients are registered.");
+ m_server.setStopping();
+ m_server.stop();
+ }
+ }
+ catch (InterruptedException e)
+ {
+ return;
+ }
+
+ }
+ })).start();
+ }
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceImpl.java
new file mode 100644
index 0000000..b85dded
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/HttpServiceImpl.java
@@ -0,0 +1,346 @@
+/*
+ * 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.httplite.osgi;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.Dictionary;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.server.ResourceHandler;
+import org.apache.felix.httplite.server.Server;
+import org.apache.felix.httplite.server.ServletHandler;
+import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
+import org.apache.felix.httplite.servlet.HttpServletResponseImpl;
+import org.osgi.framework.Bundle;
+import org.osgi.service.http.HttpContext;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+/**
+ * The HTTP Service implementation that also implements RegistrationResolver to
+ * provide internal server classes with OSGi service registration data.
+ */
+public class HttpServiceImpl implements HttpService, ServiceRegistrationResolver
+{
+
+ /**
+ * Socket server reference.
+ */
+ private final Server m_server;
+ /**
+ * Map of registered servlets.
+ */
+ private final Map m_servletMap;
+ /**
+ * Logger reference.
+ */
+ private final Logger m_logger;
+ /**
+ * Client bundle reference.
+ */
+ private final Bundle m_bundle;
+
+ /**
+ * @param server
+ * Map of <String, String> of configuration properties for the
+ * HTTP server.
+ * @param bundle
+ * Bundle that registered with the service
+ * @param logger
+ * instance of Logger
+ * @param servletMap Map of servlet instances.
+ * @throws IOException
+ */
+ public HttpServiceImpl(final Bundle bundle, final Server server, final Logger logger, Map servletMap) throws IOException
+ {
+ this.m_bundle = bundle;
+ this.m_logger = logger;
+ this.m_server = server;
+ this.m_servletMap = servletMap;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.osgi.service.http.HttpService#registerResources(java.lang.String,
+ * java.lang.String, org.osgi.service.http.HttpContext)
+ */
+ public void registerResources(final String alias, final String name,
+ final HttpContext context) throws NamespaceException
+ {
+ validateAlias(alias);
+
+ synchronized (m_servletMap)
+ {
+ if (m_servletMap.containsKey(alias))
+ {
+ throw new NamespaceException("Alias " + alias
+ + " has already been registered.");
+ }
+ }
+
+ if (context == null)
+ {
+ m_servletMap.put(alias, new ServiceRegistration(alias, name,
+ createDefaultHttpContext(), m_logger));
+ }
+ else
+ {
+ m_servletMap.put(alias, new ServiceRegistration(alias, name, context,
+ m_logger));
+ }
+
+ m_logger.log(Logger.LOG_DEBUG, "Registered resource for alias: " + alias);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.osgi.service.http.HttpService#unregister(java.lang.String)
+ */
+ public void unregister(String alias)
+ {
+ ServiceRegistration reg = null;
+ synchronized (m_servletMap)
+ {
+ reg = (ServiceRegistration) m_servletMap.get(alias);
+ if (reg != null)
+ {
+ m_servletMap.remove(alias);
+ m_logger.log(Logger.LOG_DEBUG, "Unregistered resource for alias: "
+ + alias);
+ }
+ }
+
+ if (reg != null && reg.isServlet() && reg.hasBeenInitialized())
+ {
+ reg.getServlet().destroy();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.osgi.service.http.HttpService#createDefaultHttpContext()
+ */
+ public HttpContext createDefaultHttpContext()
+ {
+ return new DefaultContextImpl(m_bundle);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.osgi.service.http.HttpService#registerServlet(java.lang.String,
+ * javax.servlet.Servlet, java.util.Dictionary,
+ * org.osgi.service.http.HttpContext)
+ */
+ public void registerServlet(final String alias, final Servlet servlet,
+ final Dictionary initparams, final HttpContext context) throws ServletException,
+ NamespaceException
+ {
+ validateAlias(alias);
+
+ if (m_servletMap.containsKey(alias))
+ {
+ throw new NamespaceException("Alias " + alias
+ + " has already been registered.");
+ }
+
+ if (context == null)
+ {
+ m_servletMap.put(alias, new ServiceRegistration(alias, servlet, initparams,
+ new DefaultContextImpl(m_bundle), m_logger));
+ }
+ else
+ {
+ m_servletMap.put(alias, new ServiceRegistration(alias, servlet, initparams,
+ context, m_logger));
+ }
+
+ m_logger.log(Logger.LOG_DEBUG, "Registered servlet for alias: " + alias);
+ }
+
+ /**
+ * Start the HTTP server.
+ *
+ * @throws IOException on I/O error
+ */
+ protected final void start() throws IOException
+ {
+ if (m_server.getState() != Server.INACTIVE_STATE)
+ {
+ throw new IllegalStateException("Attempted to start already-running server.");
+ }
+
+ m_server.start(this);
+ }
+
+ /**
+ * Stop the HTTP server
+ *
+ * @throws InterruptedException on thread interruption.
+ */
+ protected final void stop() throws InterruptedException
+ {
+ if (m_server.getState() != Server.ACTIVE_STATE)
+ {
+ throw new IllegalStateException("Attempted to stop an inactive server.");
+ }
+
+ for (Iterator i = m_servletMap.values().iterator(); i.hasNext();)
+ {
+ ServiceRegistration sr = (ServiceRegistration) i.next();
+
+ try
+ {
+ m_logger.log(Logger.LOG_DEBUG, "Cleaning up servlet " + sr.getAlias());
+ sr.getServlet().destroy();
+ }
+ catch (Exception e)
+ {
+ m_logger.log(Logger.LOG_ERROR,
+ "Servlet threw exception during destroy(): " + sr.getAlias());
+ }
+ }
+
+ m_server.stop();
+ }
+
+ /**
+ * Iterate through all service registrations and return the registration
+ * which matches the longest alias, or null if no matches are found.
+ *
+ * TODO: consider caching if a lot of time is spent resolving registrations.
+ *
+ * @param requestPath the URI of the request
+ * @return the service registration with the deepest match to the request
+ * path.
+ */
+ public final ServiceRegistration getServiceRegistration(final String requestPath)
+ {
+ ServiceRegistration sr = null;
+ int maxLength = 0;
+ synchronized (m_servletMap)
+ {
+ for (Iterator i = m_servletMap.keySet().iterator(); i.hasNext();)
+ {
+ String alias = (String) i.next();
+
+ if (requestPath.startsWith(alias))
+ {
+ if (sr == null)
+ {
+ sr = (ServiceRegistration) m_servletMap.get(alias);
+ maxLength = alias.length();
+ }
+ else if (alias.length() > maxLength)
+ {
+ sr = (ServiceRegistration) m_servletMap.get(alias);
+ maxLength = alias.length();
+ }
+ }
+ }
+ }
+
+ return sr;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.apache.felix.http.lightweight.osgi.RegistrationResolver#getProcessor
+ * (org.apache.felix.http.lightweight.http.HttpRequest,
+ * org.apache.felix.http.lightweight.http.HttpResponse, java.lang.String)
+ */
+ public ServiceRegistrationHandler getProcessor(final HttpServletRequestImpl request,
+ final HttpServletResponseImpl response, final String requestPath)
+ {
+ ServiceRegistration element = getServiceRegistration(requestPath);
+
+ if (element != null)
+ {
+ if (element.isServlet())
+ {
+ return new ServletHandler(request, response, element, m_logger);
+ }
+ else
+ {
+ return new ResourceHandler(request, response, element, m_logger);
+ }
+ }
+
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver#
+ * getServletRequest(java.net.Socket)
+ */
+ public HttpServletRequestImpl getServletRequest(final Socket socket)
+ {
+ return new HttpServletRequestImpl(socket, this, m_logger);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver#
+ * getServletResponse(org.apache.felix.http.lightweight.servlet.HttpRequest,
+ * java.io.OutputStream)
+ */
+ public HttpServletResponseImpl getServletResponse(final OutputStream output)
+ {
+ return new HttpServletResponseImpl(output);
+ }
+
+ /**
+ * Validate that a given alias is legal.
+ *
+ * @param alias input alias
+ * @throws NamespaceException is thrown if alias is illegal
+ */
+ private void validateAlias( String alias ) throws NamespaceException
+ {
+ if (alias == null)
+ {
+ throw new NamespaceException( "Alias is null." );
+ }
+
+ if (alias.trim().length() == 0)
+ {
+ throw new NamespaceException( "Alias is an empty string." );
+ }
+
+ if (!alias.startsWith( "/" ))
+ {
+ throw new NamespaceException( "Alias must begin with '/'." );
+ }
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Logger.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Logger.java
new file mode 100644
index 0000000..0d18a23
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/Logger.java
@@ -0,0 +1,367 @@
+/*
+ * 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.httplite.osgi;
+
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * <p>
+ * This class mimics the standard OSGi <tt>LogService</tt> interface. An
+ * instance of this class is used by the framework for all logging. By default
+ * this class logs messages to standard out. The log level can be set to
+ * control the amount of logging performed, where a higher number results in
+ * more logging. A log level of zero turns off logging completely.
+ * </p>
+ * <p>
+ * The log levels match those specified in the OSGi Log Service (i.e., 1 = error,
+ * 2 = warning, 3 = information, and 4 = debug). The default value is 1.
+ * </p>
+ * <p>
+ * This class also uses the System Bundle's context to track log services
+ * and will use the highest ranking log service, if present, as a back end
+ * instead of printing to standard out. The class uses reflection to invoking
+ * the log service's method to avoid a dependency on the log interface.
+ * </p>
+**/
+public class Logger implements ServiceListener
+{
+ /**
+ * ERROR level
+ */
+ public static final int LOG_ERROR = 1;
+ /**
+ * WARNING level
+ */
+ public static final int LOG_WARNING = 2;
+ /**
+ * INFO level
+ */
+ public static final int LOG_INFO = 3;
+ /**
+ * DEBUG level
+ */
+ public static final int LOG_DEBUG = 4;
+
+ private int m_logLevel = 1;
+ private BundleContext m_context = null;
+
+ private final static int LOGGER_OBJECT_IDX = 0;
+ private final static int LOGGER_METHOD_IDX = 1;
+ private ServiceReference m_logRef = null;
+ private Object[] m_logger = null;
+
+ /**
+ *
+ */
+ public Logger()
+ {
+ }
+
+ /**
+ * @param i log level
+ */
+ public final synchronized void setLogLevel(int i)
+ {
+ m_logLevel = i;
+ }
+
+ /**
+ * @return current log level
+ */
+ public final synchronized int getLogLevel()
+ {
+ return m_logLevel;
+ }
+
+ protected void setSystemBundleContext(BundleContext context)
+ {
+ m_context = context;
+ startListeningForLogService();
+ }
+
+ /**
+ * @param level log level
+ * @param msg message to log
+ */
+ public final void log(int level, String msg)
+ {
+ _log(null, null, level, msg, null);
+ }
+
+ /**
+ * @param level log level
+ * @param msg message to log
+ * @param throwable error to log
+ */
+ public final void log(int level, String msg, Throwable throwable)
+ {
+ _log(null, null, level, msg, throwable);
+ }
+
+ /**
+ * @param sr service reference to log
+ * @param level level to log
+ * @param msg message to log
+ */
+ public final void log(ServiceReference sr, int level, String msg)
+ {
+ _log(null, sr, level, msg, null);
+ }
+
+ /**
+ * @param sr service reference to log
+ * @param level level to log
+ * @param msg message to log
+ * @param throwable error to log
+ */
+ public final void log(ServiceReference sr, int level, String msg, Throwable throwable)
+ {
+ _log(null, sr, level, msg, throwable);
+ }
+
+ /**
+ * @param bundle bundle to log
+ * @param level level to log
+ * @param msg message to log
+ */
+ public final void log(Bundle bundle, int level, String msg)
+ {
+ _log(bundle, null, level, msg, null);
+ }
+
+ /**
+ * @param bundle source of log event
+ * @param level level to log
+ * @param msg message to log
+ * @param throwable error to log
+ */
+ public final void log(Bundle bundle, int level, String msg, Throwable throwable)
+ {
+ _log(bundle, null, level, msg, throwable);
+ }
+
+ protected void doLog(Bundle bundle, ServiceReference sr, int level, String msg,
+ Throwable throwable)
+ {
+ String s = "";
+ if (sr != null)
+ {
+ s = s + "SvcRef " + sr + " ";
+ }
+ else if (bundle != null)
+ {
+ s = s + "Bundle " + bundle.toString() + " ";
+ }
+ s = s + msg;
+ if (throwable != null)
+ {
+ s = s + " (" + throwable + ")";
+ }
+ switch (level)
+ {
+ case LOG_DEBUG:
+ System.out.println("DEBUG: " + s);
+ break;
+ case LOG_ERROR:
+ System.out.println("ERROR: " + s);
+ if (throwable != null)
+ {
+ if ((throwable instanceof BundleException)
+ && (((BundleException) throwable).getNestedException() != null))
+ {
+ throwable = ((BundleException) throwable).getNestedException();
+ }
+ throwable.printStackTrace();
+ }
+ break;
+ case LOG_INFO:
+ System.out.println("INFO: " + s);
+ break;
+ case LOG_WARNING:
+ System.out.println("WARNING: " + s);
+ break;
+ default:
+ System.out.println(s);
+ }
+ }
+
+ private void _log(Bundle bundle, ServiceReference sr, int level, String msg,
+ Throwable throwable)
+ {
+ // Save our own copy just in case it changes. We could try to do
+ // more conservative locking here, but let's be optimistic.
+ Object[] logger = m_logger;
+
+ if (m_logLevel >= level)
+ {
+ // Use the log service if available.
+ if (logger != null)
+ {
+ _logReflectively(logger, sr, level, msg, throwable);
+ }
+ // Otherwise, default logging action.
+ else
+ {
+ doLog(bundle, sr, level, msg, throwable);
+ }
+ }
+ }
+
+ private void _logReflectively(Object[] logger, ServiceReference sr, int level,
+ String msg, Throwable throwable)
+ {
+ if (logger != null)
+ {
+ Object[] params = { sr, new Integer(level), msg, throwable };
+ try
+ {
+ ((Method) logger[LOGGER_METHOD_IDX]).invoke(logger[LOGGER_OBJECT_IDX],
+ params);
+ }
+ catch (InvocationTargetException ex)
+ {
+ System.err.println("Logger: " + ex);
+ }
+ catch (IllegalAccessException ex)
+ {
+ System.err.println("Logger: " + ex);
+ }
+ }
+ }
+
+ /**
+ * This method is called when the system bundle context is set;
+ * it simply adds a service listener so that the system bundle can track
+ * log services to be used as the back end of the logging mechanism. It also
+ * attempts to get an existing log service, if present, but in general
+ * there will never be a log service present since the system bundle is
+ * started before every other bundle.
+ **/
+ private synchronized void startListeningForLogService()
+ {
+ // Add a service listener for log services.
+ try
+ {
+ m_context.addServiceListener(this,
+ "(objectClass=org.osgi.service.log.LogService)");
+ }
+ catch (InvalidSyntaxException ex)
+ {
+ // This will never happen since the filter is hard coded.
+ }
+ // Try to get an existing log service.
+ m_logRef = m_context.getServiceReference("org.osgi.service.log.LogService");
+ // Get the service object if available and set it in the logger.
+ if (m_logRef != null)
+ {
+ setLogger(m_context.getService(m_logRef));
+ }
+ }
+
+ /**
+ * This method implements the callback for the ServiceListener interface.
+ * It is public as a byproduct of implementing the interface and should
+ * not be called directly. This method tracks run-time changes to log
+ * service availability. If the log service being used by the framework's
+ * logging mechanism goes away, then this will try to find an alternative.
+ * If a higher ranking log service is registered, then this will switch
+ * to the higher ranking log service.
+ **/
+ public final synchronized void serviceChanged(ServiceEvent event)
+ {
+ // If no logger is in use, then grab this one.
+ if ((event.getType() == ServiceEvent.REGISTERED) && (m_logRef == null))
+ {
+ m_logRef = event.getServiceReference();
+ // Get the service object and set it in the logger.
+ setLogger(m_context.getService(m_logRef));
+ }
+ // If a logger is in use, but this one has a higher ranking, then swap
+ // it for the existing logger.
+ else if ((event.getType() == ServiceEvent.REGISTERED) && (m_logRef != null))
+ {
+ ServiceReference ref = m_context.getServiceReference("org.osgi.service.log.LogService");
+ if (!ref.equals(m_logRef))
+ {
+ m_context.ungetService(m_logRef);
+ m_logRef = ref;
+ setLogger(m_context.getService(m_logRef));
+ }
+
+ }
+ // If the current logger is going away, release it and try to
+ // find another one.
+ else if ((event.getType() == ServiceEvent.UNREGISTERING)
+ && m_logRef.equals(event.getServiceReference()))
+ {
+ // Unget the service object.
+ m_context.ungetService(m_logRef);
+ // Try to get an existing log service.
+ m_logRef = m_context.getServiceReference("org.osgi.service.log.LogService");
+ // Get the service object if available and set it in the logger.
+ if (m_logRef != null)
+ {
+ setLogger(m_context.getService(m_logRef));
+ }
+ else
+ {
+ setLogger(null);
+ }
+ }
+ }
+
+ /**
+ * This method sets the new log service object. It also caches the method to
+ * invoke. The service object and method are stored in array to optimistically
+ * eliminate the need to locking when logging.
+ **/
+ private void setLogger(Object logObj)
+ {
+ if (logObj == null)
+ {
+ m_logger = null;
+ }
+ else
+ {
+ Class[] formalParams = { ServiceReference.class, Integer.TYPE, String.class,
+ Throwable.class };
+
+ try
+ {
+ Method logMethod = logObj.getClass().getMethod("log", formalParams);
+ logMethod.setAccessible(true);
+ m_logger = new Object[] { logObj, logMethod };
+ }
+ catch (NoSuchMethodException ex)
+ {
+ System.err.println("Logger: " + ex);
+ m_logger = null;
+ }
+ }
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistration.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistration.java
new file mode 100644
index 0000000..5753614
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistration.java
@@ -0,0 +1,190 @@
+/*
+ * 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.httplite.osgi;
+
+import java.util.Dictionary;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+
+import org.apache.felix.httplite.servlet.ServletConfigImpl;
+import org.apache.felix.httplite.servlet.ServletContextImpl;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * This class stores the state from a client that registered a Servlet
+ * or resource with the HTTP Service.
+ */
+public class ServiceRegistration
+{
+ /**
+ * Servlet instance
+ */
+ private final Servlet m_servlet;
+ /**
+ * Dictionary of parameters
+ */
+ private final Dictionary m_initparams;
+ /**
+ * Reference to HttpContext
+ */
+ private final HttpContext m_context;
+ /**
+ * Alias
+ */
+ private final String m_alias;
+ /**
+ * Logger instance
+ */
+ private final Logger m_logger;
+ /**
+ * true if the servlet has been initialized. This should happen on the first request only.
+ */
+ private boolean m_initialized;
+ /**
+ * Servlet instance
+ */
+ private boolean m_isServlet;
+ /**
+ * Name of registration
+ */
+ private final String m_name;
+ private ServletConfigImpl m_servletConfigImpl;
+
+ /**
+ * @param alias Alias that the service is registered with.
+ * @param servlet Instance of servlet corresponding to alias.
+ * @param initparams initial configuration parameters
+ * @param context HTTP context
+ * @param logger Logger instance
+ */
+ public ServiceRegistration(final String alias, final Servlet servlet, final Dictionary initparams, final HttpContext context, final Logger logger)
+ {
+ this.m_alias = alias;
+ this.m_name = null;
+ this.m_servlet = servlet;
+ this.m_initparams = initparams;
+ this.m_context = context;
+ this.m_logger = logger;
+ this.m_initialized = false;
+ this.m_isServlet = true;
+ }
+
+ /**
+ * @param alias Alias that the service is registered with.
+ * @param name name of the resource
+ * @param context HTTP context
+ * @param logger Logger instance
+ */
+ public ServiceRegistration(final String alias, final String name, final HttpContext context, final Logger logger)
+ {
+ this.m_alias = alias;
+ this.m_name = name;
+ this.m_logger = logger;
+ this.m_servlet = null;
+ this.m_initparams = null;
+ this.m_context = context;
+ this.m_isServlet = false;
+ }
+
+ /**
+ * @return true if this registration represents a servlet, false for a resource.
+ */
+ public final boolean isServlet()
+ {
+ return m_isServlet;
+ }
+
+ /**
+ * @return Alias of resource
+ */
+ public final String getAlias()
+ {
+ return m_alias;
+ }
+
+ /**
+ * @return Name of resource
+ */
+ public final String getName()
+ {
+ return m_name;
+ }
+
+ /**
+ * @return true if the init() method has been called
+ * on the Servlet at some point in the past, false otherwise.
+ */
+ public final boolean hasBeenInitialized()
+ {
+ return m_initialized;
+ }
+
+ /**
+ * Set the initialized flat to true. Will throw
+ * IllegalStateException() if called multiple times on the same instance.
+ */
+ public final void setInitialized()
+ {
+ if (m_initialized)
+ {
+ throw new IllegalStateException("Servlet has already been initialized.");
+ }
+
+ m_initialized = true;
+ }
+
+ /**
+ * @return the Servlet instance.
+ */
+ public final Servlet getServlet()
+ {
+ return m_servlet;
+ }
+
+ /**
+ * @return Dictionary of init params.
+ */
+ public final Dictionary getInitparams()
+ {
+ return m_initparams;
+ }
+
+ /**
+ * @return HttpContext
+ */
+ public final HttpContext getContext()
+ {
+ return m_context;
+ }
+
+ /**
+ * @return the ServletConfig for this servlet.
+ */
+ public final ServletConfig getServletConfig()
+ {
+ if (m_servletConfigImpl == null)
+ {
+ m_servletConfigImpl = new ServletConfigImpl(m_alias, m_initparams,
+ new ServletContextImpl(m_alias, m_context, m_initparams, m_logger));
+ }
+
+ return m_servletConfigImpl;
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationHandler.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationHandler.java
new file mode 100644
index 0000000..98b9ff3
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationHandler.java
@@ -0,0 +1,40 @@
+/*
+ * 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.httplite.osgi;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+/**
+ * Represents either a Resource or a Servlet that should be processed
+ * with a valid client connection provided by the server.
+ *
+ */
+public interface ServiceRegistrationHandler
+{
+ /**
+ * Process the request against the registered provider.
+ *
+ * @param closeConnection if true close connection after handling request
+ * @throws IOException on I/O error
+ * @throws ServletException on Servlet error
+ */
+ void handle(boolean closeConnection) throws IOException, ServletException;
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationResolver.java b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationResolver.java
new file mode 100644
index 0000000..7d9112e
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/osgi/ServiceRegistrationResolver.java
@@ -0,0 +1,69 @@
+/*
+ * 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.httplite.osgi;
+
+import java.io.OutputStream;
+import java.net.Socket;
+
+import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
+import org.apache.felix.httplite.servlet.HttpServletResponseImpl;
+
+/**
+ * An interface to provide internal classes with access to client service registration state.
+ * This interface serves as the connection between the internal server and the HTTP Service.
+ *
+ */
+public interface ServiceRegistrationResolver
+{
+ /**
+ * Given a socket connection return a HttpServletRequestImpl.
+ *
+ * @param socket socket connection to be associated with HTTP Request
+ * @return HttpServletRequestImpl instance
+ */
+ HttpServletRequestImpl getServletRequest(Socket socket);
+
+ /**
+ * Resolve the requestPath to a Resource or Servlet registration with the closest (deepest) match.
+ *
+ * @param requestPath URI of request
+ * @return A ServiceRegistration or null if no match was found.
+ */
+ ServiceRegistration getServiceRegistration(String requestPath);
+
+ /**
+ * Given a HttpRequest and an output stream, return a HttpServletResponseImpl.
+ *
+ * @param request HttpRequest
+ * @param output output stream associated with socket connection.
+ * @return A HttpServletResponseImpl instance
+ */
+ HttpServletResponseImpl getServletResponse(OutputStream output);
+
+ /**
+ * For a request, response, and requestPath, return a ServiceRegistrationHandler.
+ *
+ * @param request HttpRequest
+ * @param response HttpResponse
+ * @param requestPath The request URI
+ * @return A ServiceRegistrationHandler corresponding to the requestPath, or null if no match.
+ */
+ ServiceRegistrationHandler getProcessor(HttpServletRequestImpl request,
+ HttpServletResponseImpl response, String requestPath);
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/Connection.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/Connection.java
new file mode 100644
index 0000000..4b1d95d
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/Connection.java
@@ -0,0 +1,290 @@
+/*
+ * 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.httplite.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 org.apache.felix.httplite.osgi.Logger;
+import org.apache.felix.httplite.osgi.ServiceRegistrationHandler;
+import org.apache.felix.httplite.osgi.ServiceRegistrationResolver;
+import org.apache.felix.httplite.servlet.ConcreteServletInputStream;
+import org.apache.felix.httplite.servlet.HttpConstants;
+import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
+import org.apache.felix.httplite.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
+{
+ /**
+ * Connection timeout
+ */
+ public static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
+ /**
+ * Requests per request
+ */
+ 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 socket The client socket.
+ * @param timeout The inactivity timeout of the connection in milliseconds.
+ * @param requestLimit The maximum number of consecutive requests.
+ * @param resolver resolves a request URI to a client or servlet registration via the HTTP Service.
+ * @param logger logger instance.
+ * @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 failed 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.getRequestURI() + " (" + (m_requestLimit - m_requestCount)
+ + " remaining)");
+
+ // 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 do not support OPTIONS method so send
+ // a "not implemented" error in that case.
+ if (!HttpServletRequestImpl.isSupportedMethod(request.getMethod()))
+ {
+ error = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendNotImplementedResponse();
+ }
+
+ // Ignore if we have already failed, 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
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/ResourceHandler.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/ResourceHandler.java
new file mode 100644
index 0000000..b730224
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/ResourceHandler.java
@@ -0,0 +1,135 @@
+/*
+ * 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.httplite.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.httplite.osgi.Logger;
+import org.apache.felix.httplite.osgi.ServiceRegistration;
+import org.apache.felix.httplite.osgi.ServiceRegistrationHandler;
+import org.apache.felix.httplite.servlet.HttpConstants;
+import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
+import org.apache.felix.httplite.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 static final String INDEX_HTML = "index.html";
+ 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;
+ private final Logger m_logger;
+ private final int m_aliasIndex;
+
+ /**
+ * @param req HttpRequest
+ * @param res HttpResponse
+ * @param resource ServiceRegistration
+ * @param logger Log reference
+ */
+ public ResourceHandler(final HttpServletRequestImpl req, final HttpServletResponseImpl res, final ServiceRegistration resource, final Logger logger)
+ {
+ 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();
+ this.m_aliasIndex = m_alias.length();
+ this.m_logger = logger;
+ }
+
+ /* (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.
+ m_logger.log(Logger.LOG_WARNING, "Ignored client " + m_request.getMethod() + " on static resource.");
+ return;
+ }
+
+ if (m_httpContext.handleSecurity(m_request, m_response))
+ {
+ String resourceName = getResourceName(m_request.getRequestURI());
+
+ URL resource = null;
+
+ if (resourceName.endsWith("/"))
+ {
+ m_logger.log(Logger.LOG_DEBUG, "Appending " + INDEX_HTML + " to request " + resourceName);
+ resource = m_httpContext.getResource(resourceName + INDEX_HTML);
+ }
+ else
+ {
+ resource = m_httpContext.getResource(resourceName);
+ }
+
+ if (resource == null)
+ {
+ m_logger.log(Logger.LOG_INFO, "Returning HTTP 404 for request for " + resourceName);
+ m_response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ return;
+ }
+
+ 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)
+ {
+ if (path.startsWith( "/" ))
+ {
+ return m_name + path.substring(m_aliasIndex);
+ } else
+ {
+ return m_name + "/" + path.substring(m_aliasIndex);
+ }
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
new file mode 100644
index 0000000..cef90fb
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/Server.java
@@ -0,0 +1,405 @@
+/*
+ * 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.httplite.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.httplite.osgi.Logger;
+import org.apache.felix.httplite.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
+{
+ /**
+ * Connection timeout property
+ */
+ public static final String CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP = "org.apache.felix.http.connection.timeout";
+ /**
+ * Connection request limit property
+ */
+ public static final String CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP = "org.apache.felix.http.connection.requestlimit";
+ /**
+ * Thread pool timeout property
+ */
+ public static final String CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP = "org.apache.felix.http.threadpool.timeout";
+ /**
+ * Thread pool limit property
+ */
+ 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;
+
+ /**
+ * Set to true when bundle is in a stopping state. Socket errors will be ignored.
+ */
+ private boolean m_stopping = false;
+
+ /**
+ * 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)
+ {
+ if (!m_stopping)
+ {
+ 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.");
+ }
+
+ /**
+ * Sets the stopping flag to true, meaning that further socket exceptions will not be logged as errors.
+ */
+ public void setStopping() {
+ m_stopping = true;
+ }
+}
\ No newline at end of file
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/ServletHandler.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/ServletHandler.java
new file mode 100644
index 0000000..141212c
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/ServletHandler.java
@@ -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.httplite.server;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.Logger;
+import org.apache.felix.httplite.osgi.ServiceRegistration;
+import org.apache.felix.httplite.osgi.ServiceRegistrationHandler;
+import org.apache.felix.httplite.servlet.HttpServletRequestImpl;
+import org.apache.felix.httplite.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();
+ }
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadGate.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadGate.java
new file mode 100644
index 0000000..1086051
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadGate.java
@@ -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.httplite.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
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadPool.java b/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadPool.java
new file mode 100644
index 0000000..0af541d
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/server/ThreadPool.java
@@ -0,0 +1,319 @@
+/*
+ * 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.httplite.server;
+
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.httplite.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
+{
+ /**
+ * Default thread timeout
+ */
+ public static final int DEFAULT_THREAD_TIMEOUT = 60000;
+ private final int m_threadTimeout;
+
+ private final 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 final 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.
+ * @param logger Logger instance.
+ **/
+ 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.
+ * @param logger Logger instance.
+ **/
+ 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;
+ m_logger.log(Logger.LOG_DEBUG,
+ "Server shutdown complete.");
+ }
+ // 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
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ConcreteServletInputStream.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ConcreteServletInputStream.java
new file mode 100644
index 0000000..f938a46
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ConcreteServletInputStream.java
@@ -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.httplite.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
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpConstants.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpConstants.java
new file mode 100644
index 0000000..940aac7
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpConstants.java
@@ -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.httplite.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);
+
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletRequestImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletRequestImpl.java
new file mode 100644
index 0000000..a1c9d9a
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletRequestImpl.java
@@ -0,0 +1,1168 @@
+/*
+ * 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.httplite.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.httplite.osgi.Logger;
+import org.apache.felix.httplite.osgi.ServiceRegistration;
+import org.apache.felix.httplite.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 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 );
+ }
+
+ // If path contains multiple successive path separators (a//b/c a/b////c, etc.), strip them.
+ if ( m_uri.indexOf( "//" ) > -1 )
+ {
+ // separator
+
+ m_uri = stripRedundantSeparators( m_uri );
+ }
+ }
+
+
+ /**
+ * Remove successive '/' characters.
+ *
+ * @param in input string
+ * @return stripped string
+ */
+ private String stripRedundantSeparators( String in )
+ {
+ StringBuffer sb = new StringBuffer();
+ boolean lastIsSeparator = false;
+
+ for ( int i = 0; i < in.length(); ++i )
+ {
+ char c = in.charAt( i );
+
+ if ( lastIsSeparator && c == '/' )
+ {
+ continue;
+ }
+
+ sb.append( c );
+
+ if ( c == '/' )
+ {
+ lastIsSeparator = true;
+ }
+ else
+ {
+ lastIsSeparator = false;
+ }
+ }
+
+ return sb.toString();
+ }
+
+
+ /**
+ * 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() );
+ }
+ }
+ }
+ }
+
+ if ( m_headers.containsKey( "Host" ) )
+ {
+ m_uriHost = m_headers.get( "Host" ).toString();
+ }
+ }
+
+
+ /**
+ * 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()
+ {
+ 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 m_parameters;
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterNames()
+ */
+ public Enumeration getParameterNames()
+ {
+ 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 Collections.enumeration( m_parameters.keySet() );
+ }
+
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
+ */
+ public String[] getParameterValues( 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.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( splitString( cookieHeader, ';' ) ).iterator(); i.hasNext(); )
+ {
+ String[] nvp = splitString( i.next().toString(), '=' );
+
+ 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();
+ if ( m_uriHost != null )
+ {
+ 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 && m_requestBody.length > 0 )
+ {
+ parseParameterString( new String( m_requestBody ), params );
+ }
+
+ return Collections.unmodifiableMap( 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
+ {
+ String[] parameters = splitString( queryString, '&' );
+ for ( Iterator i = Arrays.asList( parameters ).iterator(); i.hasNext(); )
+ {
+ String[] nva = splitString( i.next().toString(), '=' );
+
+ 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();
+ }
+
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getLocalAddr()
+ */
+ public String getLocalAddr()
+ {
+ return m_socket.getLocalAddress().getHostAddress();
+ }
+
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getLocalName()
+ */
+ public String getLocalName()
+ {
+ return m_socket.getLocalAddress().getHostName();
+ }
+
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getLocalPort()
+ */
+ public int getLocalPort()
+ {
+ return m_socket.getLocalPort();
+ }
+
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getRemotePort()
+ */
+ public int getRemotePort()
+ {
+ return m_socket.getPort();
+ }
+
+
+ /**
+ * Split a string into substrings based on delimiter character.
+ *
+ * @param instr input string
+ * @param separator separator char
+ * @return array of substrings
+ * @throws IllegalArgumentException if instr parameter is null.
+ */
+ private static String[] splitString( String instr, char separator )
+ {
+ if ( instr == null )
+ {
+ throw new IllegalArgumentException( "Input string must not be null." );
+ }
+
+ StringTokenizer tokenizer = new StringTokenizer(instr, Character.toString(separator));
+ int length = tokenizer.countTokens();
+ String[] str_array = new String[length];
+ for ( int i = 0; i < length; i++ )
+ {
+ str_array[i] = tokenizer.nextToken();
+ }
+ return str_array;
+ }
+}
\ No newline at end of file
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
new file mode 100644
index 0000000..6f97b98
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/HttpServletResponseImpl.java
@@ -0,0 +1,794 @@
+/*
+ * 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.httplite.servlet;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
+import java.net.HttpURLConnection;
+import java.net.URLEncoder;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.TimeZone;
+
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * This class represents an HTTP response and handles sending properly
+ * formatted responses to HTTP requests.
+**/
+public class HttpServletResponseImpl implements HttpServletResponse
+{
+ private static final int COPY_BUFFER_SIZE = 1024 * 4;
+
+ private final SimpleDateFormat m_dateFormat;
+ private final OutputStream m_out;
+ private int m_bufferSize = COPY_BUFFER_SIZE;
+ private ByteArrayOutputStream m_buffer;
+ private final Map m_headers = new HashMap();
+ private String m_characterEncoding = "UTF-8";
+ private Locale m_locale = new Locale(System.getProperty("user.language"));
+ private boolean m_getOutputStreamCalled = false;
+ private boolean m_getWriterCalled = false;
+ private ServletOutputStreamImpl m_servletOutputStream;
+ private PrintWriter m_printWriter;
+ private List m_cookies = null;
+
+ private int m_statusCode = HttpURLConnection.HTTP_OK;
+ private String m_customStatusMessage = null;
+ private boolean m_headersWritten = false;
+
+ /**
+ * Constructs an HTTP response for the specified server and request.
+ * @param outputStream The output stream for the client.
+ **/
+ public HttpServletResponseImpl(OutputStream outputStream)
+ {
+ m_out = outputStream;
+ m_dateFormat = new SimpleDateFormat(HttpConstants.HTTP_DATE_FORMAT);
+ m_dateFormat.setTimeZone(TimeZone.getTimeZone(HttpConstants.HTTP_TIMEZONE));
+ }
+
+ /**
+ * Write HTTP headers to output stream.
+ *
+ * @param close if true, should save state, only allow headers to be written once. Does not close the stream.
+ * @throws IOException on I/O error
+ */
+ void writeHeaders(boolean close) throws IOException
+ {
+ if (m_headersWritten)
+ {
+ throw new IllegalStateException("Headers have already been written.");
+ }
+
+ if (!m_headers.containsKey(HttpConstants.HEADER_CONTENT_LENGTH)
+ && m_buffer != null)
+ {
+ setContentLength(m_buffer.size());
+ }
+
+ m_out.write(buildResponse(m_statusCode, m_headers, m_customStatusMessage, null));
+
+ if (m_cookies != null)
+ {
+ m_out.write( "Set-Cookie: ".getBytes() );
+ for (Iterator i = m_cookies.iterator(); i.hasNext();)
+ {
+ Cookie cookie = (Cookie) i.next();
+ m_out.write( cookieToHeader(cookie) );
+
+ if (i.hasNext()) {
+ m_out.write( ';' );
+ }
+ }
+ }
+ m_out.write(HttpConstants.HEADER_DELEMITER.getBytes());
+ m_out.flush();
+
+ if (close)
+ {
+ m_headersWritten = true;
+ }
+ else
+ {
+ m_headers.clear();
+ }
+ }
+
+ /**
+ * Copy the contents of the input to the output stream, then close the input stream.
+ * @param inputStream input stream
+ * @param close if connection should be closed
+ * @throws IOException on I/O error
+ */
+ public void writeToOutputStream(final InputStream inputStream, final boolean close)
+ throws IOException
+ {
+ InputStream bufferedInput = new BufferedInputStream(inputStream);
+
+ if (!m_headers.containsKey(HttpConstants.HEADER_CONTENT_LENGTH))
+ {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ copy(bufferedInput, baos);
+ byte[] outputBuffer = baos.toByteArray();
+
+ setContentLength(outputBuffer.length);
+ bufferedInput = new ByteArrayInputStream(outputBuffer);
+ }
+
+ if (!m_headersWritten)
+ {
+ writeHeaders(close);
+ }
+
+ try
+ {
+ copy(bufferedInput, m_out);
+
+ m_out.flush();
+ }
+ finally
+ {
+ if (bufferedInput != null)
+ {
+ bufferedInput.close();
+ }
+ }
+ }
+
+ /**
+ * Copy an input stream to an output stream.
+ *
+ * @param input InputStream
+ * @param output OutputStream
+ * @throws IOException on I/O error.
+ */
+ public static void copy(final InputStream input, final OutputStream output)
+ throws IOException
+ {
+
+ byte[] buf = new byte[COPY_BUFFER_SIZE];
+ for (int len = input.read(buf); len >= 0; len = input.read(buf))
+ {
+ output.write(buf, 0, len);
+ }
+ }
+
+ /**
+ * Static utility method to send a continue response.
+ * @throws java.io.IOException If any I/O error occurs.
+ **/
+ public void sendContinueResponse() throws IOException
+ {
+ m_out.write(buildResponse(HttpConstants.HTTP_RESPONSE_CONTINUE));
+ m_out.flush();
+ }
+
+ /**
+ * Static utility method to send a missing host response.
+ * @throws java.io.IOException If any I/O error occurs.
+ **/
+ public void sendMissingHostResponse() throws IOException
+ {
+ m_out.write(buildResponse(HttpURLConnection.HTTP_BAD_REQUEST));
+ m_out.flush();
+ }
+
+ /**
+ * Static utility method to send a not implemented response.
+ * @throws java.io.IOException If any I/O error occurs.
+ **/
+ public void sendNotImplementedResponse() throws IOException
+ {
+ m_out.write(buildResponse(HttpURLConnection.HTTP_NOT_IMPLEMENTED));
+ m_out.flush();
+ }
+
+ /**
+ * Static utility method to send a moved permanently response.
+ * @param hostname The hostname of the new location.
+ * @param port The port of the new location.
+ * @param newURI The path of the new location.
+ * @throws java.io.IOException If any I/O error occurs.
+ **/
+ public void sendMovedPermanently(final String hostname, final int port,
+ final String newURI) throws IOException
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append(HttpConstants.HEADER_LOCATION);
+ sb.append(HttpConstants.HEADER_VALUE_DELIMITER);
+ sb.append(HttpConstants.HTTP_SCHEME);
+ sb.append("://");
+ sb.append(hostname);
+ if (port != 80)
+ {
+ sb.append(':');
+ sb.append(Integer.toString(port));
+ }
+ sb.append(newURI);
+ sb.append(HttpConstants.HEADER_DELEMITER);
+
+ m_out.write(buildResponse(301, null, sb.toString(), null));
+ m_out.flush();
+ }
+
+ /**
+ * Static utility method to send a Not Found (404) response.
+ * @throws java.io.IOException If any I/O error occurs.
+ **/
+ public void sendNotFoundResponse() throws IOException
+ {
+ m_out.write(buildResponse(HttpURLConnection.HTTP_NOT_FOUND));
+ m_out.flush();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#flushBuffer()
+ */
+ synchronized public void flushBuffer() throws IOException
+ {
+ if (m_getOutputStreamCalled)
+ {
+ m_servletOutputStream.flush();
+ }
+ else if (m_getWriterCalled)
+ {
+ m_printWriter.flush();
+ }
+
+ if (!m_headersWritten)
+ {
+ writeHeaders(true);
+ }
+
+ if (m_buffer != null)
+ {
+ byte[] content = m_buffer.toByteArray();
+ copy(new ByteArrayInputStream(content), m_out);
+ m_out.flush();
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getBufferSize()
+ */
+ public int getBufferSize()
+ {
+ if (m_buffer != null)
+ {
+ return m_buffer.size();
+ }
+
+ return m_bufferSize;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getCharacterEncoding()
+ */
+ public String getCharacterEncoding()
+ {
+ return m_characterEncoding;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getContentType()
+ */
+ public String getContentType()
+ {
+ Object contentType = m_headers.get(HttpConstants.HEADER_CONTENT_TYPE);
+
+ if (contentType != null)
+ {
+ return contentType.toString();
+ }
+
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getLocale()
+ */
+ public Locale getLocale()
+ {
+ return m_locale;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getOutputStream()
+ */
+ public ServletOutputStream getOutputStream() throws IOException
+ {
+ m_getOutputStreamCalled = true;
+
+ if (m_getWriterCalled)
+ throw new IllegalStateException(
+ "getWriter method has already been called for this response object.");
+
+ if (m_servletOutputStream == null)
+ {
+ m_buffer = new ByteArrayOutputStream(m_bufferSize);
+ m_servletOutputStream = new ServletOutputStreamImpl(m_buffer);
+ }
+ return m_servletOutputStream;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#getWriter()
+ */
+ public PrintWriter getWriter() throws IOException
+ {
+ m_getWriterCalled = true;
+
+ if (m_getOutputStreamCalled)
+ throw new IllegalStateException(
+ "getOutputStream method has already been called for this response object.");
+
+ if (m_printWriter == null)
+ {
+ m_buffer = new ByteArrayOutputStream(m_bufferSize);
+ m_printWriter = new PrintWriter(m_buffer);
+ }
+
+ return m_printWriter;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#isCommitted()
+ */
+ public boolean isCommitted()
+ {
+ return m_headersWritten;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#reset()
+ */
+ public void reset()
+ {
+ if (isCommitted())
+ {
+ throw new IllegalStateException("Response has already been committed.");
+ }
+ m_buffer.reset();
+ m_printWriter = null;
+ m_servletOutputStream = null;
+ m_getOutputStreamCalled = false;
+ m_getWriterCalled = false;
+ m_headers.clear();
+ m_statusCode = HttpURLConnection.HTTP_OK;
+ m_customStatusMessage = null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#resetBuffer()
+ */
+ public void resetBuffer()
+ {
+ if (isCommitted())
+ {
+ throw new IllegalStateException("Response has already been committed.");
+ }
+
+ m_buffer.reset();
+ m_printWriter = null;
+ m_servletOutputStream = null;
+ m_getOutputStreamCalled = false;
+ m_getWriterCalled = false;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#setBufferSize(int)
+ */
+ public void setBufferSize(final int arg0)
+ {
+ if (isCommitted())
+ {
+ throw new IllegalStateException("Response has already been committed.");
+ }
+
+ m_bufferSize = arg0;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#setCharacterEncoding(java.lang.String)
+ */
+ public void setCharacterEncoding(final String arg0)
+ {
+ m_characterEncoding = arg0;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#setContentLength(int)
+ */
+ public void setContentLength(final int arg0)
+ {
+ m_headers.put(HttpConstants.HEADER_CONTENT_LENGTH, Integer.toString(arg0));
+ }
+
+ /**
+ * Can be 'close' or 'Keep-Alive'.
+ * @param type
+ */
+ public void setConnectionType(final String type)
+ {
+ setHeader(HttpConstants.HEADER_CONNECTION, type);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#setContentType(java.lang.String)
+ */
+ public void setContentType(final String arg0)
+ {
+ setHeader(HttpConstants.HEADER_CONTENT_TYPE, arg0);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletResponse#setLocale(java.util.Locale)
+ */
+ public void setLocale(final Locale arg0)
+ {
+ m_locale = arg0;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#addCookie(javax.servlet.http.Cookie)
+ */
+ public void addCookie(final Cookie cookie)
+ {
+ if (m_cookies == null)
+ {
+ m_cookies = new ArrayList();
+ }
+
+ if (!m_cookies.contains( cookie ))
+ {
+ m_cookies.add( cookie );
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#containsHeader(java.lang.String)
+ */
+ public boolean containsHeader(final String name)
+ {
+ return m_headers.get(name) != null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#encodeURL(java.lang.String)
+ */
+ public String encodeURL(final String url)
+ {
+ // TODO Re-examing if/when sessions are supported.
+ return url;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#encodeRedirectURL(java.lang.String)
+ */
+ public String encodeRedirectURL(final String url)
+ {
+ return encodeUrl(url);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#encodeUrl(java.lang.String)
+ */
+ public String encodeUrl(final String url)
+ {
+ try
+ {
+ return URLEncoder.encode(url, m_characterEncoding);
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ return url;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#encodeRedirectUrl(java.lang.String)
+ */
+ public String encodeRedirectUrl(String url)
+ {
+ return encodeURL(url);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#sendError(int, java.lang.String)
+ */
+
+ public void sendError(final int sc, final String msg) throws IOException
+ {
+ if (m_headersWritten)
+ throw new IllegalStateException(
+ "Response has already been committed, unable to send error.");
+
+ m_out.write(buildResponse(sc, msg));
+ m_out.flush();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#sendError(int)
+ */
+ public void sendError(final int sc) throws IOException
+ {
+ sendError(sc, null);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#sendRedirect(java.lang.String)
+ */
+ public void sendRedirect(final String location) throws IOException
+ {
+ if (m_headersWritten)
+ {
+ throw new IllegalStateException("Response has already been committed.");
+ }
+
+ Map map = new HashMap();
+ map.put("Location", location);
+ m_out.write(buildResponse(307, map, null, null));
+ m_out.flush();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#setDateHeader(java.lang.String, long)
+ */
+ public void setDateHeader(final String name, final long date)
+ {
+ setHeader(name, m_dateFormat.format(new Date(date)));
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#addDateHeader(java.lang.String, long)
+ */
+ public void addDateHeader(final String name, final long date)
+ {
+ addHeader(name, m_dateFormat.format(new Date(date)));
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#setHeader(java.lang.String, java.lang.String)
+ */
+
+ public void setHeader(final String name, final String value)
+ {
+ if (value != null)
+ {
+ m_headers.put(name, value);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#addHeader(java.lang.String, java.lang.String)
+ */
+
+ public void addHeader(final String name, final String value)
+ {
+ if (value != null && m_headers.containsKey(name))
+ {
+ Object pvalue = m_headers.get(name);
+
+ if (pvalue instanceof List)
+ {
+ ((List) pvalue).add(value);
+ }
+ else
+ {
+ List vlist = new ArrayList();
+ vlist.add(pvalue);
+ vlist.add(value);
+ }
+ }
+ else
+ {
+ setHeader(name, value);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#setIntHeader(java.lang.String, int)
+ */
+ public void setIntHeader(final String name, final int value)
+ {
+ setHeader(name, Integer.toString(value));
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#addIntHeader(java.lang.String, int)
+ */
+ public void addIntHeader(final String name, final int value)
+ {
+ addHeader(name, Integer.toString(value));
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#setStatus(int)
+ */
+ public void setStatus(final int sc)
+ {
+ m_statusCode = sc;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.http.HttpServletResponse#setStatus(int, java.lang.String)
+ */
+ public void setStatus(int sc, String sm)
+ {
+ m_statusCode = sc;
+ m_customStatusMessage = sm;
+ }
+
+ /**
+ * @param code HTTP code
+ * @return byte array of response
+ */
+ public static byte[] buildResponse(int code)
+ {
+ return buildResponse(code, null, null, HttpConstants.DEFAULT_HTML_HEADER);
+ }
+
+ /**
+ * @param code HTTP code
+ * @param userMessage user message
+ * @return byte array of response
+ */
+ public static byte[] buildResponse(int code, String userMessage)
+ {
+ return buildResponse(code, null, userMessage, HttpConstants.DEFAULT_HTML_HEADER);
+ }
+
+ /**
+ * Build a response given input parameters.
+ *
+ * @param code HTTP code
+ * @param headers Map of HTTP headers
+ * @param userMessage user message
+ * @param htmlStartTag custom HTML document start
+ * @return byte array of response.
+ */
+ public static byte[] buildResponse(int code, Map headers, String userMessage,
+ String htmlStartTag)
+ {
+ StringBuffer buffer = new StringBuffer();
+
+ buffer.append(HttpConstants.HTTP11_VERSION);
+ buffer.append(' ');
+ buffer.append(code);
+ buffer.append(' ');
+
+ if (code > 399)
+ {
+ buffer.append("HTTP Error ");
+ buffer.append(code);
+ }
+ buffer.append(HttpConstants.HEADER_DELEMITER);
+ if (code == 100)
+ {
+ buffer.append(HttpConstants.HEADER_DELEMITER);
+ }
+ else if (headers != null)
+ {
+ if (headers.containsKey(HttpConstants.HEADER_CONTENT_TYPE))
+ {
+ appendHeader(buffer, HttpConstants.HEADER_CONTENT_TYPE,
+ headers.get(HttpConstants.HEADER_CONTENT_TYPE).toString());
+ }
+
+ for (Iterator i = headers.entrySet().iterator(); i.hasNext();)
+ {
+ Map.Entry entry = (Map.Entry) i.next();
+
+ if (entry.getValue() == null)
+ {
+ throw new IllegalStateException(
+ "Header map contains value with null value: " + entry.getKey());
+ }
+
+ appendHeader(buffer, entry.getKey().toString(),
+ entry.getValue().toString());
+ }
+ }
+
+ //Only append error HTML messages if the return code is in the error range.
+ if (code > 399)
+ {
+ //TODO: Consider disabling the HTML generation, optionally, so clients have full control of the response content.
+ if (htmlStartTag == null)
+ {
+ htmlStartTag = HttpConstants.DEFAULT_HTML_HEADER;
+ }
+ buffer.append(htmlStartTag);
+ buffer.append("<h1>");
+ buffer.append(code);
+ buffer.append(' ');
+ buffer.append("HTTP Error ");
+ buffer.append(code);
+
+ if (userMessage != null)
+ {
+ buffer.append("</h1><p>");
+ buffer.append(userMessage);
+ buffer.append("</p>");
+ }
+ else
+ {
+ buffer.append("</h1>");
+ }
+
+ buffer.append("<h3>" + HttpConstants.SERVER_INFO + "</h3></html>");
+ }
+
+ return buffer.toString().getBytes();
+ }
+
+ /**
+ * Convert a cookie into the HTTP header in response.
+ *
+ * @param cookie Cookie
+ * @return String as byte array of cookie as header
+ */
+ private byte[] cookieToHeader( Cookie cookie )
+ {
+ if (cookie == null || cookie.getName() == null || cookie.getValue() == null)
+ {
+ throw new IllegalArgumentException( "Invalid cookie" );
+ }
+
+ StringBuffer sb = new StringBuffer();
+
+ sb.append( cookie.getName() );
+ sb.append( '=' );
+ sb.append( cookie.getValue() );
+
+ //TODO: Implement all Cookie fields
+
+ return sb.toString().getBytes();
+ }
+
+ /**
+ * Append name and value as an HTTP header to a StringBuffer
+ *
+ * @param sb StringBuffer
+ * @param name Name
+ * @param value Value
+ */
+ private static void appendHeader(StringBuffer sb, String name, String value)
+ {
+ sb.append(name);
+ sb.append(HttpConstants.HEADER_VALUE_DELIMITER);
+ sb.append(value);
+ sb.append(HttpConstants.HEADER_DELEMITER);
+ }
+}
\ No newline at end of file
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletConfigImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletConfigImpl.java
new file mode 100644
index 0000000..3a24840
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletConfigImpl.java
@@ -0,0 +1,91 @@
+/*
+ * 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.httplite.servlet;
+
+import java.util.Dictionary;
+import java.util.Enumeration;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+/**
+ * ServletConfig implementation.
+ *
+ */
+public class ServletConfigImpl implements ServletConfig
+{
+ private final String m_alias;
+ private final Dictionary m_initparams;
+ private final ServletContext m_servletContext;
+
+ /**
+ * @param alias Alias
+ * @param initparams Dictionary
+ * @param servletContext ServletContext
+ */
+ public ServletConfigImpl(String alias, Dictionary initparams, ServletContext servletContext)
+ {
+ this.m_alias = alias;
+ this.m_initparams = initparams;
+ this.m_servletContext = servletContext;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletConfig#getServletName()
+ */
+ public String getServletName()
+ {
+ return m_alias;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletConfig#getServletContext()
+ */
+ public ServletContext getServletContext()
+ {
+ return m_servletContext;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletConfig#getInitParameterNames()
+ */
+ public Enumeration getInitParameterNames()
+ {
+ if (m_initparams != null)
+ {
+ return m_initparams.keys();
+ }
+
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletConfig#getInitParameter(java.lang.String)
+ */
+ public String getInitParameter(String arg0)
+ {
+ if (m_initparams != null)
+ {
+ return m_initparams.get(arg0).toString();
+ }
+
+ return null;
+ }
+
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletContextImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletContextImpl.java
new file mode 100644
index 0000000..ad705b3
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletContextImpl.java
@@ -0,0 +1,301 @@
+/*
+ * 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.httplite.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collections;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.Logger;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * ServletContext implementation.
+ *
+ */
+public class ServletContextImpl implements ServletContext
+{
+
+ private final HttpContext m_httpContext;
+ private final Logger m_logger;
+ private final Dictionary m_initparams;
+ private Map m_attributes;
+ private final String m_name;
+
+ /**
+ * @param name Name of Servlet Context
+ * @param httpContext HttpContext
+ * @param initparams Dictionary
+ * @param logger Logger
+ */
+ public ServletContextImpl(final String name, final HttpContext httpContext, final Dictionary initparams, final Logger logger)
+ {
+ this.m_name = name;
+ this.m_httpContext = httpContext;
+ this.m_initparams = initparams;
+ this.m_logger = logger;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getContext(java.lang.String)
+ */
+ public ServletContext getContext(String uripath)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getMajorVersion()
+ */
+ public int getMajorVersion()
+ {
+ return 2;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getMinorVersion()
+ */
+ public int getMinorVersion()
+ {
+ return 4;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
+ */
+ public String getMimeType(String file)
+ {
+ return m_httpContext.getMimeType(file);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
+ */
+ public Set getResourcePaths(String path)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getResource(java.lang.String)
+ */
+ public URL getResource(String path) throws MalformedURLException
+ {
+ return m_httpContext.getResource(path);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
+ */
+ public InputStream getResourceAsStream(String path)
+ {
+ try
+ {
+ return m_httpContext.getResource(path).openStream();
+ }
+ catch (IOException e)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Unable to open stream on resource: " + path,
+ e);
+ return null;
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getRequestDispatcher(java.lang.String)
+ */
+ public RequestDispatcher getRequestDispatcher(String path)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
+ */
+
+ public RequestDispatcher getNamedDispatcher(String name)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getServlet(java.lang.String)
+ */
+ public Servlet getServlet(String name) throws ServletException
+ {
+ return null;
+ }
+
+
+ public Enumeration getServlets()
+ {
+ return null;
+ }
+
+
+ public Enumeration getServletNames()
+ {
+ return null;
+ }
+
+
+ public void log(String msg)
+ {
+ m_logger.log(Logger.LOG_INFO, msg);
+ }
+
+
+ public void log(Exception exception, String msg)
+ {
+ m_logger.log(Logger.LOG_ERROR, msg, exception);
+ }
+
+
+ public void log(String message, Throwable throwable)
+ {
+ m_logger.log(Logger.LOG_ERROR, message, throwable);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
+ */
+ public String getRealPath(String path)
+ {
+ return path;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getServerInfo()
+ */
+ public String getServerInfo()
+ {
+ return HttpConstants.SERVER_INFO;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
+ */
+ public String getInitParameter(String name)
+ {
+ if (m_initparams != null)
+ {
+ Object o = m_initparams.get(name);
+
+ if (o != null)
+ {
+ return o.toString();
+ }
+ }
+
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getInitParameterNames()
+ */
+ public Enumeration getInitParameterNames()
+ {
+ if (m_initparams != null)
+ {
+ return m_initparams.keys();
+ }
+
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
+ */
+ public Object getAttribute(String name)
+ {
+ if (m_attributes != null)
+ {
+ return m_attributes.get(name);
+ }
+
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getAttributeNames()
+ */
+ public Enumeration getAttributeNames()
+ {
+ if (m_attributes != null)
+ {
+ return Collections.enumeration(m_attributes.keySet());
+ }
+
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
+ */
+ public void setAttribute(String name, Object object)
+ {
+ if (m_attributes == null)
+ {
+ m_attributes = new HashMap();
+ }
+
+ m_attributes.put(name, object);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
+ */
+ public void removeAttribute(String name)
+ {
+ if (m_attributes != null)
+ {
+ m_attributes.remove(name);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getServletContextName()
+ */
+ public String getServletContextName()
+ {
+ return m_name;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletContext#getContextPath()
+ */
+ /**
+ * @return the context path.
+ */
+ public String getContextPath() {
+ return m_name;
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletOutputStreamImpl.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletOutputStreamImpl.java
new file mode 100644
index 0000000..2e1a676
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/ServletOutputStreamImpl.java
@@ -0,0 +1,50 @@
+/*
+ * 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.httplite.servlet;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.ServletOutputStream;
+
+/**
+ * ServletOutputStream Implementation.
+ *
+ */
+public class ServletOutputStreamImpl extends ServletOutputStream
+{
+
+ private final OutputStream m_outputStream;
+
+ /**
+ * @param outputStream OutputStream
+ */
+ public ServletOutputStreamImpl(OutputStream outputStream)
+ {
+ this.m_outputStream = outputStream;
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.OutputStream#write(int)
+ */
+ public void write(int i) throws IOException
+ {
+ m_outputStream.write(i);
+ }
+}
diff --git a/httplite/core/src/main/java/org/apache/felix/httplite/servlet/UnimplementedAPIException.java b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/UnimplementedAPIException.java
new file mode 100644
index 0000000..d3faa1c
--- /dev/null
+++ b/httplite/core/src/main/java/org/apache/felix/httplite/servlet/UnimplementedAPIException.java
@@ -0,0 +1,30 @@
+/*
+ * 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.httplite.servlet;
+
+/**
+ * A specific exception for unimplemented features. Used rather than RuntimeException()
+ * to make it easier to find where unimplemented features are in the source code.
+ *
+ */
+public class UnimplementedAPIException extends RuntimeException
+{
+ private static final long serialVersionUID = -8213749667381059522L;
+
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractHttpliteTestCase.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractHttpliteTestCase.java
new file mode 100644
index 0000000..db185c0
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractHttpliteTestCase.java
@@ -0,0 +1,101 @@
+/*
+ * 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.httplite.osgi.test;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.apache.commons.io.IOUtils;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.http.HttpService;
+
+
+/**
+ * Base class with commong HTTP testing methods. Depends on Apache Commons-IO.
+ *
+ */
+public abstract class AbstractHttpliteTestCase extends AbstractPojoSRTestCase
+{
+
+ protected static final int DEFAULT_PORT = 8080;
+ protected static final String DEFAULT_BASE_URL = "http://localhost:" + DEFAULT_PORT;
+
+ protected static String readInputAsString( InputStream in ) throws IOException
+ {
+ return IOUtils.toString( in );
+ }
+
+
+ /**
+ * Create an array of bytesfrom the complete contents of an InputStream.
+ *
+ * @param in
+ * InputStream to turn into a byte array
+ * @return byte array (byte[]) w/ contents of input stream, or null if inputstream is null.
+ * @throws IOException
+ * on I/O error
+ */
+ protected static byte[] readInputAsByteArray( InputStream in ) throws IOException
+ {
+ return IOUtils.toByteArray( in );
+ }
+
+
+ /**
+ * Create a HttpURLConnection for specified url.
+ *
+ * @param urlStr
+ * @param method
+ * @return
+ * @throws IOException
+ */
+ protected static HttpURLConnection getConnection( String urlStr, String method ) throws IOException
+ {
+ URL url = new URL( urlStr );
+ HttpURLConnection connection = ( HttpURLConnection ) url.openConnection();
+ connection.setRequestMethod( method );
+ connection.setConnectTimeout( 1000 );
+
+ return connection;
+
+ }
+
+
+ /**
+ * @param context
+ * @return instance of HTTP service
+ */
+ protected HttpService getHTTPService( BundleContext context )
+ {
+ ServiceReference sr = registry.getServiceReference( HttpService.class.getName() );
+
+ assertNotNull( sr );
+
+ Object svc = registry.getService( sr );
+
+ assertNotNull( svc );
+ assertTrue( svc instanceof HttpService );
+
+ return ( HttpService ) svc;
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractPojoSRTestCase.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractPojoSRTestCase.java
new file mode 100644
index 0000000..6bcc12e
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/AbstractPojoSRTestCase.java
@@ -0,0 +1,72 @@
+/*
+ * 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.httplite.osgi.test;
+
+
+import java.util.HashMap;
+import java.util.ServiceLoader;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.httplite.osgi.Activator;
+
+import de.kalpatec.pojosr.framework.launch.PojoServiceRegistry;
+import de.kalpatec.pojosr.framework.launch.PojoServiceRegistryFactory;
+
+
+/**
+ * Common functionality for PojoSR-based tests.
+ *
+ */
+public abstract class AbstractPojoSRTestCase extends TestCase
+{
+
+ protected PojoServiceRegistry registry;
+ protected Activator activator;
+
+
+ /* (non-Javadoc)
+ * @see junit.framework.TestCase#setUp()
+ */
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+
+ //Initialize service registry
+ ServiceLoader loader = ServiceLoader.load( PojoServiceRegistryFactory.class );
+
+ registry = ( ( PojoServiceRegistryFactory ) loader.iterator().next() ).newPojoServiceRegistry( new HashMap() );
+
+ assertNotNull( registry );
+
+ //Initialize bundle
+ activator = new Activator();
+ activator.start( registry.getBundleContext() );
+ }
+
+
+ protected void tearDown() throws Exception
+ {
+ if ( activator != null && registry != null )
+ {
+ activator.stop( registry.getBundleContext() );
+ }
+ super.tearDown();
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/BasicTestingServlet.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/BasicTestingServlet.java
new file mode 100644
index 0000000..6e5b405
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/BasicTestingServlet.java
@@ -0,0 +1,218 @@
+/*
+ * 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.httplite.osgi.test;
+
+
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+
+/**
+ * Servlet for tests.
+ *
+ */
+public class BasicTestingServlet extends HttpServlet implements Servlet
+{
+
+ boolean m_getCalled, m_postCalled, m_putCalled, m_deleteCalled = false;
+ private final String m_responseStringContent;
+ private final boolean m_asWriter;
+ private final byte[] m_responseBinaryContent;
+ private Map m_requestParams;
+ private Map m_queryStringMap;
+ private String m_pathInfo;
+ private HttpServletRequest m_request;
+
+
+ /**
+ * Most basic constructor.
+ */
+ public BasicTestingServlet()
+ {
+ this.m_responseStringContent = null;
+ this.m_responseBinaryContent = null;
+ this.m_asWriter = false;
+ }
+
+
+ /**
+ * Pass back content in GET.
+ * @param content
+ * @param asWriter
+ */
+ public BasicTestingServlet( String content, boolean asWriter )
+ {
+ this.m_responseStringContent = content;
+ this.m_responseBinaryContent = null;
+ this.m_asWriter = asWriter;
+ }
+
+
+ public BasicTestingServlet( byte[] content, boolean asWriter )
+ {
+ this.m_responseStringContent = null;
+ this.m_responseBinaryContent = content;
+ this.m_asWriter = asWriter;
+ }
+
+
+ protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
+ {
+ m_getCalled = true;
+ m_requestParams = req.getParameterMap();
+ m_queryStringMap = parseQueryStringMap( req.getQueryString() );
+ m_pathInfo = req.getPathInfo();
+ m_request = req;
+
+ if ( m_responseStringContent != null )
+ {
+ if ( m_asWriter )
+ {
+ resp.getWriter().print( m_responseStringContent );
+ }
+ else
+ {
+ resp.getOutputStream().print( m_responseStringContent );
+ }
+ }
+ else if ( m_responseBinaryContent != null )
+ {
+
+ resp.getOutputStream().write( m_responseBinaryContent );
+
+ }
+ }
+
+
+ private Map parseQueryStringMap( String queryString )
+ {
+ if ( queryString == null || queryString.length() == 0 )
+ {
+ return Collections.EMPTY_MAP;
+ }
+
+ Map m = new HashMap();
+
+ String[] kvp = queryString.split( "&" );
+
+ for ( int i = 0; i < kvp.length; ++i )
+ {
+ String elem[] = kvp[i].split( "=" );
+ m.put( elem[0], elem[1] );
+ }
+
+ return m;
+ }
+
+
+ protected void doPost( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
+ {
+ m_postCalled = true;
+ m_requestParams = req.getParameterMap();
+ m_queryStringMap = parseQueryStringMap( req.getQueryString() );
+ m_pathInfo = req.getPathInfo();
+ m_request = req;
+ }
+
+
+ protected void doDelete( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
+ {
+ m_deleteCalled = true;
+ m_requestParams = req.getParameterMap();
+ m_queryStringMap = parseQueryStringMap( req.getQueryString() );
+ m_pathInfo = req.getPathInfo();
+ m_request = req;
+ }
+
+
+ protected void doPut( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
+ {
+ m_putCalled = true;
+ m_requestParams = req.getParameterMap();
+ m_queryStringMap = parseQueryStringMap( req.getQueryString() );
+ m_pathInfo = req.getPathInfo();
+ m_request = req;
+ }
+
+
+ public boolean isGetCalled()
+ {
+ return m_getCalled;
+ }
+
+
+ public boolean isPostCalled()
+ {
+ return m_postCalled;
+ }
+
+
+ public boolean isPutCalled()
+ {
+ return m_putCalled;
+ }
+
+
+ public boolean isDeleteCalled()
+ {
+ return m_deleteCalled;
+ }
+
+
+ public Map getRequestParameters()
+ {
+ return m_requestParams;
+ }
+
+
+ public Map getQueryStringMap()
+ {
+ return m_queryStringMap;
+ }
+
+
+ public String getPathInfo()
+ {
+ return m_pathInfo;
+ }
+
+ public Enumeration getHeaderNames()
+ {
+ return m_request.getHeaderNames();
+ }
+
+ public String getHeader(String name)
+ {
+ return m_request.getHeader( name );
+ }
+
+ public Enumeration getHeaders(String name)
+ {
+ return m_request.getHeaders( name );
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
new file mode 100644
index 0000000..3892853
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestCookies.java
@@ -0,0 +1,226 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.List;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Test that servlet container handles cookies correctly.
+ *
+ */
+public class TestCookies extends AbstractHttpliteTestCase
+{
+ private static final int MODE_NONE_SET = 1;
+ private static final int MODE_SIMPLE = 2;
+ private static final int MODE_MULTI = 0;
+
+
+ //TODO: Test cookie expiration
+
+ /**
+ * Test that no cookies are sent by default.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testNoCookiesSentByDefault() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+ CookieServlet servlet = new CookieServlet( MODE_NONE_SET );
+ httpService.registerServlet( "/test", servlet, null, null );
+
+ //Test that no cookies are currently set.
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+
+ }
+
+
+ /**
+ * Test creating and sending one cookie.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testSimpleCookie() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+ CookieServlet servlet = new CookieServlet( MODE_SIMPLE );
+ httpService.registerServlet( "/test", servlet, null, null );
+
+ //Test that no cookies are currently set.
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ //Set the cookie in the response
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ assertNotNull( client.getHeaderFields().get( "Set-Cookie" ) );
+ List l = ( List ) client.getHeaderFields().get( "Set-Cookie" );
+ assertTrue( l.contains( "testcookie=testvalue" ) );
+
+ for ( int i = 0; i < 10; ++i )
+ {
+ //Confirm the cookie in the request
+ client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+ client.addRequestProperty( "Cookie", "testcookie=testvalue" );
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ }
+
+ }
+
+
+ /**
+ * Test sending and recieving multiple cookies.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testMultipleCookies() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+ CookieServlet servlet = new CookieServlet( MODE_MULTI );
+ httpService.registerServlet( "/test", servlet, null, null );
+
+ //Test that no cookies are currently set.
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ //Set the cookie in the response
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ assertNotNull( client.getHeaderFields().get( "Set-Cookie" ) );
+ List l = ( List ) client.getHeaderFields().get( "Set-Cookie" );
+ assertTrue( l.contains( "testcookie=testvalue;testcookie2=testvalue2" ) );
+
+ for ( int i = 0; i < 10; ++i )
+ {
+ //Confirm the cookie in the request
+ client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+ client.addRequestProperty( "Cookie", "testcookie=testvalue;testcookie2=testvalue2" );
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ }
+
+ }
+
+ /**
+ * Servlet to test cookie support.
+ *
+ */
+ private class CookieServlet extends HttpServlet implements Servlet
+ {
+
+ private final int m_mode;
+ private int requestCount = 0;
+ private Cookie[] m_cookies;
+
+
+ public CookieServlet( int mode )
+ {
+ this.m_mode = mode;
+ }
+
+
+ public Cookie[] getCookies()
+ {
+ return m_cookies;
+ }
+
+
+ protected void doGet( HttpServletRequest req, HttpServletResponse resp ) throws ServletException, IOException
+ {
+ m_cookies = req.getCookies();
+
+ switch ( m_mode )
+ {
+ case MODE_NONE_SET:
+ Cookie[] cookies = req.getCookies();
+
+ assertNull( cookies );
+
+ break;
+ case MODE_SIMPLE:
+ requestCount++;
+ if ( requestCount == 1 )
+ {
+ assertNull( req.getCookies() );
+ Cookie c = new Cookie( "testcookie", "testvalue" );
+ resp.addCookie( c );
+
+ }
+ else if ( requestCount > 1 )
+ {
+ Cookie[] c = req.getCookies();
+
+ assertNotNull( c );
+ assertTrue( c.length == 1 );
+ assertTrue( c[0].getName().equals( "testcookie" ) );
+ assertTrue( c[0].getValue().equals( "testvalue" ) );
+ }
+
+ break;
+ case MODE_MULTI:
+ requestCount++;
+ if ( requestCount == 1 )
+ {
+ assertNull( req.getCookies() );
+ Cookie c = new Cookie( "testcookie", "testvalue" );
+ resp.addCookie( c );
+ c = new Cookie( "testcookie2", "testvalue2" );
+ resp.addCookie( c );
+ }
+ else if ( requestCount > 1 )
+ {
+ Cookie[] c = req.getCookies();
+
+ assertNotNull( c );
+ assertTrue( c.length == 2 );
+ assertTrue( c[0].getName().equals( "testcookie" ) );
+ assertTrue( c[0].getValue().equals( "testvalue" ) );
+ assertTrue( c[1].getName().equals( "testcookie2" ) );
+ assertTrue( c[1].getValue().equals( "testvalue2" ) );
+ }
+
+ break;
+ default:
+ throw new ServletException( "Invalid test mode." );
+ }
+ }
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestOSGiService.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestOSGiService.java
new file mode 100644
index 0000000..542a9bc
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestOSGiService.java
@@ -0,0 +1,147 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.apache.felix.httplite.osgi.test.BasicTestingServlet;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Tests related to OSGi service registry.
+ *
+ * @author kgilmer
+ *
+ */
+public class TestOSGiService extends AbstractHttpliteTestCase
+{
+
+ /**
+ * HTTPService is available
+ */
+ public void testHTTPServiceAvailability()
+ {
+ assertNotNull( getHTTPService( registry.getBundleContext() ) );
+ }
+
+
+ /**
+ * Can register a servlet with the HTTPService
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ */
+ public void testCanRegisterServlet() throws ServletException, NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ httpService.registerServlet( "/", new BasicTestingServlet(), null, null );
+ }
+
+
+ /**
+ * Test that HTTP Service does not allow same alias to be registered twice.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ */
+ public void testCannotRegisterSameAliasTwice() throws ServletException, NamespaceException
+ {
+
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ boolean namespaceExceptionThrown = false;
+ httpService.registerServlet( "/alias", new BasicTestingServlet(), null, null );
+
+ try
+ {
+ httpService.registerServlet( "/alias", new BasicTestingServlet(), null, null );
+ }
+ catch ( NamespaceException e )
+ {
+ namespaceExceptionThrown = true;
+ }
+
+ assertTrue( namespaceExceptionThrown );
+ }
+
+
+ /**
+ * Test invalid aliases throw NamespaceException.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ */
+ public void testCannotRegisterInvalidAlias() throws ServletException, NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+ String[] badAliases =
+ { "noslash" };
+
+ for ( int i = 0; i < badAliases.length; ++i )
+ {
+ boolean namespaceExceptionThrown = false;
+ try
+ {
+ httpService.registerServlet( badAliases[i], new BasicTestingServlet(), null, null );
+ }
+ catch ( NamespaceException e )
+ {
+ namespaceExceptionThrown = true;
+ }
+
+ }
+ }
+
+
+ /**
+ * Test that an alias can be registered after it's been previously registered and then unregistered.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ */
+ public void testCanReregisterAlias() throws ServletException, NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ httpService.registerServlet( "/alias", new BasicTestingServlet(), null, null );
+
+ httpService.unregister( "/alias" );
+
+ httpService.registerServlet( "/alias", new BasicTestingServlet(), null, null );
+ }
+
+
+ /**
+ * Test resources can be registered.
+ *
+ * @throws NamespaceException
+ */
+ public void testCanRegisterResources() throws NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ httpService.registerResources( "/restest", "/webroot/", null );
+ }
+
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestParameters.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestParameters.java
new file mode 100644
index 0000000..62b57c5
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestParameters.java
@@ -0,0 +1,119 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.apache.felix.httplite.osgi.test.BasicTestingServlet;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Test request parameter handling by container.
+ *
+ */
+public class TestParameters extends AbstractHttpliteTestCase
+{
+
+ //TODO: test unicode parameters
+ //TODO: test parameters with empty values
+ //TODO: test parameter name collision
+
+ /**
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testCorrectParameterCount() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ int parameterCount = 16;
+ for ( int i = 0; i < parameterCount; ++i )
+ {
+ client.addRequestProperty( "k" + i, "v" + i );
+ }
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+
+ int headerCount = 0;
+ Enumeration enum = testServlet.getHeaderNames();
+ while (enum.hasMoreElements())
+ {
+ headerCount++;
+ System.out.println("header: " + enum.nextElement().toString());
+ }
+
+
+ assertTrue( headerCount >= parameterCount );
+ }
+
+
+ /**
+ * Test the parameter contents.
+ *
+ * @throws IOException
+ * @throws ServletException
+ * @throws NamespaceException
+ */
+ public void testParameterContents() throws IOException, ServletException, NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ int parameterCount = 16;
+ for ( int i = 0; i < parameterCount; ++i )
+ {
+ client.addRequestProperty( "k" + i, "v" + i );
+ }
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ Map rp = new HashMap();
+
+ Enumeration e = testServlet.getHeaderNames();
+ while (e.hasMoreElements())
+ {
+ String key = e.nextElement().toString();
+ rp.put( key , testServlet.getHeader( key ) );
+ }
+
+ for ( int i = 0; i < parameterCount; ++i )
+ {
+ assertTrue( rp.get( "k" + i ).equals( "v" + i ) );
+ }
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestQueryString.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestQueryString.java
new file mode 100644
index 0000000..425785e
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestQueryString.java
@@ -0,0 +1,84 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.Map;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.apache.felix.httplite.osgi.test.BasicTestingServlet;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Tests for query string handling by the container.
+ *
+ */
+public class TestQueryString extends AbstractHttpliteTestCase
+{
+
+ //TODO: test unicode keys and values
+ //TODO: test invalid query string in request
+
+ /**
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testQueryString() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ StringBuffer qs = new StringBuffer( "?" );
+ int parameterCount = 16;
+ for ( int i = 0; i < parameterCount; ++i )
+ {
+ qs.append( "k" + i );
+ qs.append( "=" );
+ qs.append( "v" + i );
+ if ( i != ( parameterCount - 1 ) )
+ {
+ qs.append( '&' );
+ }
+ }
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test" + qs.toString(), "GET" );
+
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+
+ Map qsm = testServlet.getQueryStringMap();
+
+ assertTrue( qsm.size() == parameterCount );
+
+ for ( int i = 0; i < parameterCount; ++i )
+ {
+ assertTrue( qsm.containsKey( "k" + i ) );
+ assertTrue( qsm.get( "k" + i ).equals( "v" + i ) );
+ }
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestRequestPath.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestRequestPath.java
new file mode 100644
index 0000000..7d64f5f
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestRequestPath.java
@@ -0,0 +1,68 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.apache.felix.httplite.osgi.test.BasicTestingServlet;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Tests for handling of client request path.
+ *
+ */
+public class TestRequestPath extends AbstractHttpliteTestCase
+{
+
+ public void testSimpleRequestPath() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test/a/b/c", "GET" );
+
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ assertTrue( testServlet.getPathInfo().equals( "/a/b/c" ) );
+ }
+
+
+ public void testMultipleSeperatorsRequestPath() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test/a/b//c", "GET" );
+
+ client.connect();
+ assertTrue( client.getResponseCode() == 200 );
+ assertTrue( testServlet.getPathInfo().equals( "/a/b/c" ) );
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestResources.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestResources.java
new file mode 100644
index 0000000..9810a7c
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestResources.java
@@ -0,0 +1,76 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Tests for resources.
+ *
+ */
+public class TestResources extends AbstractHttpliteTestCase
+{
+
+ //TODO: test GET binary file
+
+ /**
+ * Test that a resource can be retrieved from client.
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testCanGetResource() throws NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ httpService.registerResources( "/", "/webroot/", null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/index.html", "GET" );
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+ String response = readInputAsString( client.getInputStream() );
+ assertNotNull( response );
+ assertTrue( response.indexOf( "boo" ) > -1 );
+ }
+
+
+ /**
+ * Test that non-existent resource returns 404.
+ * @throws IOException
+ * @throws NamespaceException
+ */
+ public void testCannotGetInvalidResource() throws IOException, NamespaceException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ httpService.registerResources( "/", "/webroot/", null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/index2.html", "GET" );
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 404 );
+ }
+}
diff --git a/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestServletContainer.java b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestServletContainer.java
new file mode 100644
index 0000000..239a71c
--- /dev/null
+++ b/httplite/core/src/test/java/org/apache/felix/httplite/osgi/test/cases/TestServletContainer.java
@@ -0,0 +1,252 @@
+/*
+ * 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.httplite.osgi.test.cases;
+
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.util.Random;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.httplite.osgi.test.AbstractHttpliteTestCase;
+import org.apache.felix.httplite.osgi.test.BasicTestingServlet;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+
+
+/**
+ * Tests related to OSGi service registry.
+ *
+ * @author kgilmer
+ *
+ */
+public class TestServletContainer extends AbstractHttpliteTestCase
+{
+ /**
+ * Test calling GET enters TestServlet doGet() method.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testExecuteGET() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+ assertTrue( testServlet.isGetCalled() );
+ assertFalse( testServlet.isDeleteCalled() );
+ assertFalse( testServlet.isPostCalled() );
+ assertFalse( testServlet.isPutCalled() );
+ }
+
+
+ /**
+ * Test can execute POST method.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testExecutePOST() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "POST" );
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+ assertFalse( testServlet.isGetCalled() );
+ assertFalse( testServlet.isDeleteCalled() );
+ assertTrue( testServlet.isPostCalled() );
+ assertFalse( testServlet.isPutCalled() );
+ }
+
+
+ /**
+ * Test can execute PUT method.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testExecutePUT() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "PUT" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+ assertFalse( testServlet.isGetCalled() );
+ assertFalse( testServlet.isDeleteCalled() );
+ assertFalse( testServlet.isPostCalled() );
+ assertTrue( testServlet.isPutCalled() );
+ }
+
+
+ /**
+ * Test can execute DELETE method.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testExecuteDELETE() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ BasicTestingServlet testServlet = new BasicTestingServlet();
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "DELETE" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+ assertFalse( testServlet.isGetCalled() );
+ assertTrue( testServlet.isDeleteCalled() );
+ assertFalse( testServlet.isPostCalled() );
+ assertFalse( testServlet.isPutCalled() );
+ }
+
+
+ /**
+ * Test that container returns exact content as specified in servlet.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testGETResponseStringContent() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ String content = "test content";
+
+ BasicTestingServlet testServlet = new BasicTestingServlet( content, false );
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+
+ String response = readInputAsString( client.getInputStream() );
+
+ printBytes( content.getBytes() );
+ printBytes( response.getBytes() );
+ assertTrue( content.equals( response ) );
+
+ httpService.unregister( "/test" );
+
+ content = "test content";
+ testServlet = new BasicTestingServlet( content, true );
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+
+ response = readInputAsString( client.getInputStream() );
+ printBytes( content.getBytes() );
+ printBytes( response.getBytes() );
+ assertTrue( content.length() == response.length() );
+ assertTrue( content.equals( response ) );
+ }
+
+
+ /**
+ * Test that container returns exact content as specified in servlet.
+ *
+ * @throws ServletException
+ * @throws NamespaceException
+ * @throws IOException
+ */
+ public void testGETResponseBinaryContent() throws ServletException, NamespaceException, IOException
+ {
+ HttpService httpService = getHTTPService( registry.getBundleContext() );
+
+ byte[] content = generateRandomBinaryContent();
+
+ BasicTestingServlet testServlet = new BasicTestingServlet( content, false );
+ httpService.registerServlet( "/test", testServlet, null, null );
+
+ HttpURLConnection client = getConnection( DEFAULT_BASE_URL + "/test", "GET" );
+
+ client.connect();
+
+ assertTrue( client.getResponseCode() == 200 );
+
+ byte[] response = readInputAsByteArray( client.getInputStream() );
+
+ printBytes( content );
+ printBytes( response );
+ assertTrue( content.length == response.length );
+ for (int i = 0; i < content.length; ++i)
+ {
+ assertTrue( content[i] == response[i] );
+ }
+
+ httpService.unregister( "/test" );
+ }
+
+
+ private byte[] generateRandomBinaryContent()
+ {
+ Random rnd = new Random();
+
+ int l = rnd.nextInt( 40 ) + 20;
+
+ byte[] buf = new byte[l];
+
+ rnd.nextBytes( buf );
+
+ return buf;
+ }
+
+
+ private static void printBytes( byte[] b )
+ {
+ for ( int i = 0; i < b.length; ++i )
+ {
+ System.out.print( b[i] );
+ System.out.print( ", " );
+ }
+ System.out.println();
+ }
+}
diff --git a/httplite/core/src/test/resources/webroot/index.html b/httplite/core/src/test/resources/webroot/index.html
new file mode 100644
index 0000000..0cb4d79
--- /dev/null
+++ b/httplite/core/src/test/resources/webroot/index.html
@@ -0,0 +1,24 @@
+<!--
+ 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.
+-->
+<html>
+<head></head>
+<body>
+<p>boo</p>
+</body>
+</html>
\ No newline at end of file