httplite: commit source archive from FELIX-3084. Also see FELIX-538.
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1182650 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/httplite/LICENSE-2.0.txt b/httplite/LICENSE-2.0.txt
new file mode 100644
index 0000000..59fa6ca
--- /dev/null
+++ b/httplite/LICENSE-2.0.txt
@@ -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 2011 Ken Gilmer
+
+ 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/META-INF/LICENSE b/httplite/META-INF/LICENSE
new file mode 100644
index 0000000..d645695
--- /dev/null
+++ b/httplite/META-INF/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/META-INF/MANIFEST.MF b/httplite/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..d93263d
--- /dev/null
+++ b/httplite/META-INF/MANIFEST.MF
@@ -0,0 +1,14 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Apache Felix Lightweight Http Service Bundle
+Bundle-SymbolicName: org.apache.felix.http.lightweight
+Bundle-Activator: org.apache.felix.http.lightweight.osgi.Activator
+Bundle-Version: 0.2.0
+Import-Package:
+ org.osgi.framework,
+ org.osgi.service.http;version="1.2.1",
+ javax.servlet;version="2.4",
+ javax.servlet.http;version="2.4",
+ javax.servlet.resources;version="2.4"
+Bundle-RequiredExecutionEnvironment: J2SE-1.4
+
diff --git a/httplite/META-INF/NOTICE b/httplite/META-INF/NOTICE
new file mode 100644
index 0000000..9ea90ec
--- /dev/null
+++ b/httplite/META-INF/NOTICE
@@ -0,0 +1,5 @@
+Apache Felix HTTP Server
+Copyright 2006 The Apache Software Foundation
+
+This product includes software developed at
+The Apache Software Foundation (http://www.apache.org/).
diff --git a/httplite/build.xml b/httplite/build.xml
new file mode 100644
index 0000000..9a4ecfb
--- /dev/null
+++ b/httplite/build.xml
@@ -0,0 +1,138 @@
+<?xml version="1.0"?>
+<project name="org.apache.felix.http.lightweight" default="jar">
+ <property name="src.dir" location="src/" />
+ <property name="project.description" value="Lightweight HTTP Service for Apache Felix" />
+ <property name="external.dir" location="external" />
+ <property name="doc.dir" location="${basedir}/docs" />
+ <property name="build.dir" location="${basedir}/classes" />
+
+ <property name="wget.bin" location="/usr/bin/wget" />
+ <property name="svn.bin" location="/usr/bin/svn" />
+ <property name="git.bin" location="/usr/bin/git" />
+ <property name="rm.bin" location="/bin/rm" />
+
+ <property name="framework.fqpn" value="org.apache.felix.framework" />
+ <property name="compendium.fqpn" value="org.osgi.compendium" />
+
+ <tstamp>
+ <format property="TS_NOW" pattern="yyyy-MM-dd HH:mm:ss" />
+ </tstamp>
+
+ <target name="clean-external" unless="${skip.fetch}">
+ <delete dir="${external.dir}" />
+ </target>
+
+ <target name="clean" depends="clean-external" description="remove intermediate files">
+ <delete dir="${build.dir}" />
+ <delete dir="${build.dir}-debug" />
+ <delete dir="${doc.dir}" />
+ <delete dir="org/apache" />
+ <delete dir="org/osgi" />
+ <delete dir="bin" />
+ <delete file="${ant.project.name}-min.jar" />
+ <delete file="${ant.project.name}-all.jar" />
+ </target>
+
+ <target name="fetch" depends="clean" unless="${skip.fetch}">
+ <mkdir dir="${external.dir}" />
+ <exec executable="${wget.bin}" dir="${external.dir}">
+ <arg value="http://www.meisei-u.ac.jp/mirror/apache/dist//felix/org.apache.felix.framework-3.2.2.jar" />
+ </exec>
+
+ <exec executable="${svn.bin}">
+ <arg value="export" />
+ <arg value="http://svn.apache.org/repos/asf/felix/releases/org.osgi.compendium-1.4.0" />
+ <arg value="${external.dir}/org.osgi.compendium" />
+ </exec>
+
+ <exec executable="${svn.bin}">
+ <arg value="export" />
+ <arg value="http://svn.apache.org/repos/asf/tomcat/archive/servletapi/branches/other/servlet2.4-jsp2.0-tc5.x/ASF/jsr154" />
+ <arg value="${external.dir}/servletapi" />
+ </exec>
+ </target>
+
+ <target name="stage-dependencies" depends="fetch">
+ <copy toDir="${src.dir}">
+ <fileset dir="${external.dir}/org.osgi.compendium/src" includes="main/java/org/osgi/service/http/**/*.java" />
+ </copy>
+
+ <copy toDir="${src.dir}">
+ <fileset dir="${external.dir}/servletapi/src" />
+ </copy>
+ <copy toDir="${src.dir}/share/javax/servlet/http">
+ <fileset dir="${external.dir}/servletapi/examples/WEB-INF/classes" includes="*.properties" />
+ </copy>
+ <copy toDir="${src.dir}/share/javax/servlet/resources">
+ <fileset dir="${src.dir}/share/dtd" />
+ </copy>
+
+ <delete dir="${src.dir}/etc" />
+ <delete dir="examples" />
+ </target>
+
+ <target name="compile" depends="stage-dependencies" description="compile the Java source code to class files">
+ <mkdir dir="${build.dir}" />
+ <mkdir dir="${build.dir}-debug" />
+
+ <javac destdir="${build.dir}" source="1.4" target="1.4" encoding="utf-8" fork="true" includeantruntime="false" debug="false">
+ <src path="${src.dir}/main/java" />
+ <src path="${src.dir}/share" />
+ <classpath path="${external.dir}/org.apache.felix.framework-3.2.2.jar" />
+ </javac>
+ <javac destdir="${build.dir}-debug" source="1.4" target="1.4" encoding="utf-8" fork="true" includeantruntime="false" debug="true" debuglevel="lines,vars,source">
+ <src path="${src.dir}/main/java" />
+ <src path="${src.dir}/share" />
+ <classpath path="${external.dir}/org.apache.felix.framework-3.2.2.jar" />
+ </javac>
+ </target>
+
+ <target name="jar" depends="compile" description="create a Jar file for the application">
+ <mkdir dir="${build.dir}/META-INF" />
+ <copy toDir="${build.dir}/META-INF-MIN">
+ <fileset dir="META-INF" />
+ </copy>
+
+ <copy toDir="${build.dir}/META-INF-ALL">
+ <fileset dir="META-INF" />
+ </copy>
+
+ <manifest file="${build.dir}/META-INF-ALL/MANIFEST.MF" mode="update">
+ <attribute name="Import-Package" value="org.osgi.framework" />
+ <attribute name="Export-Package" value='org.osgi.service.http;version="1.2.1", javax.servlet;version="2.4",javax.servlet.http;version="2.4",javax.servlet.resources;version="2.4"' />
+ <attribute name="Built-By" value="${user.name}" />
+ <attribute name="Built-Date" value="${TS_NOW}" />
+ </manifest>
+
+ <manifest file="${build.dir}/META-INF-MIN/MANIFEST.MF" mode="update">
+ <attribute name="Import-Package" value="org.osgi.framework,org.osgi.service.http,javax.servlet,javax.servlet.http,javax.servlet.resources" />
+ <attribute name="Export-Package" value="" />
+ <attribute name="Built-By" value="${user.name}" />
+ <attribute name="Built-Date" value="${TS_NOW}" />
+ </manifest>
+
+ <jar destfile="${ant.project.name}-debug-all.jar" manifest="${build.dir}/META-INF-ALL/MANIFEST.MF">
+ <fileset dir="${build.dir}-debug" includes="**/*.class,**/*.properties,**/*.dtd,**/*.xsd" />
+ <fileset dir="${src.dir}/share" includes="javax/servlet/**/*.properties,javax/servlet/**/*.dtd,javax/servlet/**/*.xsd" />
+ <fileset dir="." includes="LICENSE-2.0.txt" />
+ </jar>
+
+ <jar destfile="${ant.project.name}-all.jar" manifest="${build.dir}/META-INF-ALL/MANIFEST.MF">
+ <fileset dir="${build.dir}" includes="**/*.class,**/*.properties,**/*.dtd,**/*.xsd" />
+ <fileset dir="${src.dir}/share" includes="javax/servlet/**/*.properties,javax/servlet/**/*.dtd,javax/servlet/**/*.xsd" />
+ <fileset dir="." includes="LICENSE-2.0.txt" />
+ </jar>
+
+ <jar destfile="${ant.project.name}-min.jar" manifest="${build.dir}/META-INF-MIN/MANIFEST.MF">
+ <fileset dir="${build.dir}" includes="org/apache/felix/http/lightweight/**/*.class" />
+ <fileset dir="." includes="LICENSE-2.0.txt" />
+ </jar>
+ </target>
+
+ <target name="javadoc" depends="jar">
+ <javadoc sourcepath="${src.dir}/main/java" defaultexcludes="no" destdir="${doc.dir}" version="true" use="true" windowtitle="${project.description}">
+ <classpath path="${external.dir}/org.apache.felix.framework-3.2.2.jar" />
+ <classpath path="${external.dir}/osgi.cmpn.jar" />
+ </javadoc>
+ </target>
+</project>
\ No newline at end of file
diff --git a/httplite/pom.xml b/httplite/pom.xml
new file mode 100644
index 0000000..af37a88
--- /dev/null
+++ b/httplite/pom.xml
@@ -0,0 +1,54 @@
+<!--
+ 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>
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix</artifactId>
+ <version>1.0.0</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+ <packaging>bundle</packaging>
+ <name>Apache Felix HTTP Server</name>
+ <description>A simple HTTP server.</description>
+ <artifactId>org.apache.felix.sandbox.httpserver</artifactId>
+ <dependencies>
+ <dependency>
+ <groupId>${pom.groupId}</groupId>
+ <artifactId>org.osgi.core</artifactId>
+ <version>1.0.0</version>
+ </dependency>
+ </dependencies>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>maven-bundle-plugin</artifactId>
+ <version>1.0.0</version>
+ <extensions>true</extensions>
+ <configuration>
+ <instructions>
+ <Private-Package>org.apache.felix.sandbox.httpserver.*</Private-Package>
+ <Bundle-SymbolicName>${pom.artifactId}</Bundle-SymbolicName>
+ <Bundle-Activator>org.apache.felix.sandbox.httpserver.Activator</Bundle-Activator>
+ </instructions>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Activator.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Activator.java
new file mode 100644
index 0000000..1b979ae
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Activator.java
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.osgi;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.felix.http.lightweight.server.Server;
+import org.apache.felix.http.lightweight.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
+ {
+ 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/src/main/java/org/apache/felix/http/lightweight/osgi/DefaultContextImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/DefaultContextImpl.java
new file mode 100644
index 0000000..cde587b
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.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/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceFactoryImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceFactoryImpl.java
new file mode 100644
index 0000000..6603303
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceFactoryImpl.java
@@ -0,0 +1,139 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.osgi;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.http.lightweight.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;
+
+ /**
+ * @param logger
+ * @param m_server
+ */
+ public HttpServiceFactoryImpl(final Logger logger, final Server m_server)
+ {
+ this.m_logger = logger;
+ this.m_server = m_server;
+ }
+
+ /* (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);
+
+ 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)
+ {
+ try
+ {
+ m_logger.log(Logger.LOG_INFO,
+ "Stopping http server since no clients are registered.");
+ m_server.stop();
+ }
+ catch (InterruptedException e)
+ {
+ return;
+ }
+ }
+ }
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceImpl.java
new file mode 100644
index 0000000..1a4f4f5
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/HttpServiceImpl.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.http.lightweight.osgi;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+
+import org.apache.felix.http.lightweight.server.ResourceHandler;
+import org.apache.felix.http.lightweight.server.Server;
+import org.apache.felix.http.lightweight.server.ServletHandler;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.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
+ * @throws IOException
+ */
+ public HttpServiceImpl(final Bundle bundle, final Server server, final Logger logger) throws IOException
+ {
+ this.m_bundle = bundle;
+ this.m_logger = logger;
+ this.m_server = server;
+ this.m_servletMap = new HashMap();
+ }
+
+ /*
+ * (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
+ {
+ 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
+ {
+ 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);
+ }
+ }
+
+ 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);
+ }
+
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Logger.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Logger.java
new file mode 100644
index 0000000..f9ed58f
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/Logger.java
@@ -0,0 +1,315 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.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
+{
+ public static final int LOG_ERROR = 1;
+ public static final int LOG_WARNING = 2;
+ public static final int LOG_INFO = 3;
+ 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()
+ {
+ }
+
+ public final synchronized void setLogLevel(int i)
+ {
+ m_logLevel = i;
+ }
+
+ public final synchronized int getLogLevel()
+ {
+ return m_logLevel;
+ }
+
+ protected void setSystemBundleContext(BundleContext context)
+ {
+ m_context = context;
+ startListeningForLogService();
+ }
+
+ public final void log(int level, String msg)
+ {
+ _log(null, null, level, msg, null);
+ }
+
+ public final void log(int level, String msg, Throwable throwable)
+ {
+ _log(null, null, level, msg, throwable);
+ }
+
+ public final void log(ServiceReference sr, int level, String msg)
+ {
+ _log(null, sr, level, msg, null);
+ }
+
+ public final void log(ServiceReference sr, int level, String msg, Throwable throwable)
+ {
+ _log(null, sr, level, msg, throwable);
+ }
+
+ public final void log(Bundle bundle, int level, String msg)
+ {
+ _log(bundle, null, level, msg, null);
+ }
+
+ 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/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistration.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistration.java
new file mode 100644
index 0000000..0481727
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.osgi;
+
+import java.util.Dictionary;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletConfig;
+
+import org.apache.felix.http.lightweight.servlet.ServletConfigImpl;
+import org.apache.felix.http.lightweight.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/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistrationHandler.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistrationHandler.java
new file mode 100644
index 0000000..42f52d6
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistrationHandler.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.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.
+ *
+ * @throws IOException on I/O error
+ * @throws ServletException on Servlet error
+ */
+ void handle(boolean closeConnection) throws IOException, ServletException;
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistrationResolver.java b/httplite/src/main/java/org/apache/felix/http/lightweight/osgi/ServiceRegistrationResolver.java
new file mode 100644
index 0000000..bf410e3
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.osgi;
+
+import java.io.OutputStream;
+import java.net.Socket;
+
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.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/src/main/java/org/apache/felix/http/lightweight/server/Connection.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java
new file mode 100644
index 0000000..e8c96f0
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/server/Connection.java
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.Socket;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+import org.apache.felix.http.lightweight.servlet.ConcreteServletInputStream;
+import org.apache.felix.http.lightweight.servlet.HttpConstants;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+
+/**
+ * This class represents an accepted connection between the server and
+ * a client. It supports persistent connections for both HTTP 1.0 and 1.1
+ * clients. A given persistent connection is limited in the number of
+ * consecutive requests it is allowed to make before having its connection
+ * closed as well as after a period of inactivity.
+**/
+public class Connection
+{
+ public static final int DEFAULT_CONNECTION_TIMEOUT = 10000;
+ public static final int DEFAULT_CONNECTION_REQUESTLIMIT = 50;
+
+ private final Socket m_socket;
+ private ConcreteServletInputStream m_is;
+ private OutputStream m_os;
+ private int m_requestCount = 0;
+ private final int m_requestLimit;
+ private final ServiceRegistrationResolver m_resolver;
+ private final Logger m_logger;
+
+ /**
+ * Constructs a connection with a default inactivity timeout and request limit.
+ *
+ * @param socket Socket connection
+ * @param resolver a resolver to get http request/response and handler.
+ * @param logger Logger
+ * @throws IOException If any I/O error occurs.
+ */
+ public Connection(final Socket socket, final ServiceRegistrationResolver resolver, final Logger logger) throws IOException
+ {
+ this(socket, DEFAULT_CONNECTION_TIMEOUT, DEFAULT_CONNECTION_REQUESTLIMIT,
+ resolver, logger);
+ }
+
+ /**
+ * Constructs a connection with the specified inactivity timeout and request limit.
+ * @param server The web server associated with the connection.
+ * @param socket The client socket.
+ * @param timeout The inactivity timeout of the connection in milliseconds.
+ * @param requestLimit The maximum number of consecutive requests.
+ * @param m_resolver
+ * @throws java.io.IOException If any I/O error occurs.
+ */
+ public Connection(final Socket socket, final int timeout, final int requestLimit, final ServiceRegistrationResolver resolver, final Logger logger) throws IOException
+ {
+ m_socket = socket;
+ m_resolver = resolver;
+ m_logger = logger;
+ m_socket.setSoTimeout(timeout);
+ m_socket.setTcpNoDelay(true);
+ m_requestLimit = requestLimit;
+ try
+ {
+ m_is = new ConcreteServletInputStream(new BufferedInputStream(
+ m_socket.getInputStream()));
+ m_os = new BufferedOutputStream(m_socket.getOutputStream());
+ }
+ catch (IOException ex)
+ {
+ // Make sure we close any opened socket/streams.
+ try
+ {
+ m_socket.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket.", ex);
+ }
+ if (m_is != null)
+ {
+ try
+ {
+ m_is.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket input stream.",
+ ex2);
+ }
+ }
+ if (m_os != null)
+ {
+ try
+ {
+ m_os.close();
+ }
+ catch (IOException ex2)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket output stream.",
+ ex2);
+ }
+ }
+ throw ex;
+ }
+ }
+
+ /**
+ * Performs the actual servicing of the connection and its subsequent requests.
+ * This method will be called by threads in the thread pool. This method
+ * typically exits when the connection is closed due to either an explicit
+ * connection close, the inactivity timeout expires, the maximum request
+ * limit was reached, or an I/O error occurred. When this method returns,
+ * the associated socket will be closed, regardless of whether or not an
+ * expection was thrown.
+ * @throws java.net.SocketTimeoutException If the inactivity timeout expired
+ * while trying to read from the socket.
+ * @throws java.io.IOException If any I/O error occurs.
+ * @throws ServletException on servlet errors
+ **/
+ public void process() throws IOException, ServletException
+ {
+ HttpServletRequestImpl request = m_resolver.getServletRequest(m_socket);
+ HttpServletResponseImpl response = m_resolver.getServletResponse(m_os);
+
+ try
+ {
+ // Loop until we close the connection.
+ boolean close = false;
+ while (!close)
+ {
+ // Read the next request.
+ try
+ {
+ request.parseRequestLine(m_is);
+ }
+ catch (IOException e)
+ {
+ m_logger.log(
+ Logger.LOG_ERROR,
+ "Error with request: " + request.toString() + ": "
+ + e.getMessage());
+ throw e;
+ }
+ m_requestCount++;
+
+ // Keep track of whether we have errored or not,
+ // because we still want to read the bytes to clear
+ // the input stream so we can service more requests.
+ boolean error = false;
+
+ m_logger.log(Logger.LOG_DEBUG,
+ "Processing request (" + (m_requestLimit - m_requestCount)
+ + " remaining) : " + request.getRequestURI());
+
+ // If client is HTTP/1.1, then send continue message.
+ if (request.getProtocol().equals(HttpConstants.HTTP11_VERSION))
+ {
+ response.sendContinueResponse();
+ }
+
+ // Read the header lines of the request.
+ request.parseHeader(m_is);
+
+ // If we have an HTTP/1.0 request without the connection set to
+ // keep-alive or we explicitly have a request to close the connection,
+ // then set close flag to exit the loop rather than trying to read
+ // more requests.
+ String v = request.getHeader(HttpConstants.HEADER_CONNECTION);
+ if ((request.getProtocol().equals(HttpConstants.HTTP10_VERSION) && ((v == null) || (!v.equalsIgnoreCase(HttpConstants.KEEPALIVE_CONNECTION))))
+ || ((v != null) && v.equalsIgnoreCase(HttpConstants.CLOSE_CONNECTION)))
+ {
+ close = true;
+ response.setConnectionType("close");
+ }
+ // If we have serviced the maximum number of requests for
+ // this connection, then set close flag so we exit the loop
+ // and close the connection.
+ else if (m_requestCount >= m_requestLimit)
+ {
+ close = true;
+ response.setConnectionType("close");
+ }
+
+ // We only service GET and/or HEAD requests, so send
+ // a "not implemented" error otherwise.
+ if (!HttpServletRequestImpl.isSupportedMethod(request.getMethod()))
+ {
+ error = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendNotImplementedResponse();
+ }
+
+ // Ignore if we have already errored, otherwise send error message
+ // if an HTTP/1.1 client did not include HOST header.
+ if (!error && request.getProtocol().equals(HttpConstants.HTTP11_VERSION)
+ && (request.getHeader(HttpConstants.HOST_HEADER) == null))
+ {
+ error = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendMissingHostResponse();
+ }
+
+ // Read in the request body.
+ request.parseBody(m_is);
+
+ // Only process the request if there was no error.
+ if (!error)
+ {
+ ServiceRegistrationHandler processor = m_resolver.getProcessor(
+ request, response, request.getRequestURI());
+
+ if (processor != null)
+ {
+ processor.handle(close);
+
+ m_logger.log(Logger.LOG_DEBUG, "Processed " + request.toString());
+
+ // TODO: Adding next line to make test cases pass, but not sure if it is correct
+ // and needs further investigation.
+ close = true;
+ continue;
+ }
+
+ close = true;
+ response.setConnectionType(HttpConstants.CLOSE_CONNECTION);
+ response.sendNotFoundResponse();
+ }
+ }
+ }
+ finally
+ {
+ try
+ {
+ m_is.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket input stream.", ex);
+ }
+ try
+ {
+ m_os.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket output stream.", ex);
+ }
+ try
+ {
+ m_socket.close();
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Error closing socket.", ex);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java
new file mode 100644
index 0000000..6e882da
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ResourceHandler.java
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
+
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.servlet.HttpConstants;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+import org.osgi.service.http.HttpContext;
+
+/**
+ * Handles resource processing.
+ *
+ * Encapsulates the logic in OSGI Service Platform Release 4 Compendium Version 4.2 Section 102.3
+ *
+ */
+public class ResourceHandler implements ServiceRegistrationHandler
+{
+
+ private final HttpServletRequestImpl m_request;
+ private final HttpServletResponseImpl m_response;
+
+ private final HttpContext m_httpContext;
+ private final String m_name;
+ private final String m_alias;
+
+ /**
+ * @param req HttpRequest
+ * @param res HttpResponse
+ * @param resource ServiceRegistration
+ */
+ public ResourceHandler(final HttpServletRequestImpl req, final HttpServletResponseImpl res, final ServiceRegistration resource)
+ {
+ if (resource.isServlet())
+ {
+ throw new IllegalStateException(
+ "Invalid state, ResourceHandler constructed with a Servlet.");
+ }
+
+ this.m_request = req;
+ this.m_response = res;
+ this.m_httpContext = resource.getContext();
+ this.m_name = resource.getName();
+ this.m_alias = resource.getAlias();
+ }
+
+ /* (non-Javadoc)
+ * @see org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler#process(boolean)
+ */
+ public void handle(final boolean close) throws IOException
+ {
+ if (!m_request.getMethod().equals(HttpConstants.GET_REQUEST)
+ && !m_request.getMethod().equals(HttpConstants.HEAD_REQUEST))
+ {
+
+ //POST, PUT, DELETE operations not valid on resources.
+ return;
+ }
+
+ if (m_httpContext.handleSecurity(m_request, m_response))
+ {
+ String resourceName = getResourceName(m_request.getRequestURI());
+
+ URL resource = m_httpContext.getResource(resourceName);
+
+ if (resource == null)
+ {
+ throw new IOException("Unable to find resource: " + resourceName);
+ }
+
+ InputStream inputStream = resource.openStream();
+ m_response.setContentType(m_httpContext.getMimeType(resourceName));
+
+ m_response.writeToOutputStream(inputStream, close);
+ }
+ }
+
+ /**
+ * @param path String
+ * @return resource name at given path.
+ */
+ private String getResourceName(final String path)
+ {
+ return m_name + path.substring(m_alias.length());
+ }
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java
new file mode 100644
index 0000000..b87fbb6
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/server/Server.java
@@ -0,0 +1,370 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.io.IOException;
+import java.net.InetAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.UnknownHostException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+
+/**
+ * This class implements a simple multi-threaded web server. It
+ * only supports GET/HEAD requests. The web server has various configurable
+ * properties that can be passed into the constructor; see the constructor
+ * for more information about configuration properties.
+**/
+public class Server
+{
+ public static final String CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP = "org.apache.felix.http.connection.timeout";
+ public static final String CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP = "org.apache.felix.http.connection.requestlimit";
+ public static final String CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP = "org.apache.felix.http.threadpool.timeout";
+ public static final String CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP = "org.apache.felix.http.threadpool.limit";
+ //Flag to enable debugging for this service implementation. The default is false.
+ public static final String CONFIG_PROPERTY_HTTP_DEBUG = "org.apache.felix.http.debug";
+ // Flag to enable the user of HTTPS. The default is false.
+ public static final String CONFIG_PROPERTY_HTTPS_ENABLE = "org.apache.felix.https.enable";
+ // Flag to enable the use of HTTP. The default is true.
+ public static final String CONFIG_PROPERTY_HTTP_ENABLE = "org.apache.felix.http.enable";
+ // The port used for servlets and resources available via HTTP. The default is 8080. A negative port number has the same effect as setting org.apache.felix.http.enable to false.
+ public static final String CONFIG_PROPERTY_HTTP_PORT = "org.osgi.service.http.port";
+
+ /**
+ * Default HTTP port to listen on.
+ */
+ private static final int DEFAULT_PORT = 8080;
+ /**
+ * Default number of concurrent requests.
+ */
+ private static final int DEFAULT_THREADPOOL_LIMIT = 10;
+
+ /**
+ * Server is inactive (off).
+ */
+ public static final int INACTIVE_STATE = 0;
+ /**
+ * Server is active (running)
+ */
+ public static final int ACTIVE_STATE = 1;
+ /**
+ * Server is shutting down.
+ */
+ public static final int STOPPING_STATE = 2;
+
+ private String m_hostname;
+ private final int m_port;
+
+ private int m_state;
+ private ThreadGate m_shutdownGate;
+
+ private Thread m_serverThread;
+ private ServerSocket m_serverSocket;
+ private final ThreadPool m_threadPool;
+
+ private final int m_connectionTimeout;
+ private final int m_connectionRequestLimit;
+ private ServiceRegistrationResolver m_resolver;
+ private final Logger m_logger;
+
+ /**
+ * Construct a web server with the specified configuration. The configuration
+ * map may contain the following properties:
+ * <ul>
+ * <li><tt>org.osgi.service.http.port</tt> - the port on which it listens for connections;
+ * the default is 8080.
+ * </li>
+ * <li><tt>org.apache.felix.http.threadpool.limit</tt> - the maximum number of threads in the
+ * thread pool; the default value is 10.
+ * </li>
+ * <li><tt>org.apache.felix.http.threadpool.timeout</tt> - the inactivity timeout for threads in
+ * the thread pool after which time the threads will terminate; the
+ * default value is 60000 milliseconds.
+ * </li>
+ * <li><tt>.org.apache.felix.http.connection.requestlimit</tt> - the maximum number of requests that
+ * will be accepted over a persistent connection before closing the
+ * connection; the default value is 50.
+ * </li>
+ * <li><tt>org.apache.felix.http.connection.timeout</tt> - the inactivity timeout for persistent
+ * connections after which the connection is closed; the default value
+ * is 10000 milliseconds.
+ * </li>
+ * </ul>
+ * The configuration properties cannot be changed after construction. The
+ * web server is not active until it is started.
+ * @param configMap The map of configuration properties; can be <tt>null</tt>.
+ * @param logger
+ **/
+ public Server(Map configMap, final Logger logger)
+ {
+ this.m_logger = logger;
+ m_state = INACTIVE_STATE;
+
+ configMap = (configMap == null) ? new HashMap() : configMap;
+
+ // Read in the configured properties or their default values.
+ m_port = getConfiguredPort(configMap);
+ int threadLimit = (configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP) == null) ? DEFAULT_THREADPOOL_LIMIT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_LIMIT_PROP));
+ int threadTimeout = (configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP) == null) ? ThreadPool.DEFAULT_THREAD_TIMEOUT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_THREADPOOL_TIMEOUT_PROP));
+ m_threadPool = new ThreadPool(threadLimit, threadTimeout, m_logger);
+ m_connectionTimeout = (configMap.get(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP) == null) ? Connection.DEFAULT_CONNECTION_TIMEOUT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_CONNECTION_TIMEOUT_PROP));
+ m_connectionRequestLimit = (configMap.get(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP) == null) ? Connection.DEFAULT_CONNECTION_REQUESTLIMIT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_CONNECTION_REQUESTLIMIT_PROP));
+ }
+
+ /**
+ * Get the port the HTTP server listens on based on configuration map or default value.
+ *
+ * @param configMap
+ * @return port number that server listens on.
+ */
+ public static int getConfiguredPort(Map configMap)
+ {
+ return (configMap.get(Server.CONFIG_PROPERTY_HTTP_PORT) == null) ? DEFAULT_PORT
+ : Integer.parseInt((String) configMap.get(Server.CONFIG_PROPERTY_HTTP_PORT));
+ }
+
+ /**
+ * This method returns the current state of the web server, which is one
+ * of the following values:
+ * <ul>
+ * <li><tt>HttpServer.INACTIVE_STATE</tt> - the web server is currently
+ * not active.
+ * </li>
+ * <li><tt>HttpServer.ACTIVE_STATE</tt> - the web server is active and
+ * serving files.
+ * </li>
+ * <li><tt>HttpServer.STOPPING_STATE</tt> - the web server is in the
+ * process of shutting down.
+ * </li>
+ * </li>
+ * @return The current state of the web server.
+ **/
+ public synchronized int getState()
+ {
+ return m_state;
+ }
+
+ /**
+ * Returns the hostname associated with the web server.
+ * @return The hostname associated with the web server.
+ **/
+ public synchronized String getHostname()
+ {
+ if (m_hostname == null)
+ {
+ try
+ {
+ m_hostname = InetAddress.getLocalHost().getHostName();
+ }
+ catch (UnknownHostException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR,
+ "Unable to get hostname, setting to localhost.", ex);
+ m_hostname = "localhost";
+ }
+ }
+ return m_hostname;
+ }
+
+ /**
+ * Returns the port associated with the web server.
+ * @return The port associated with the web server.
+ **/
+ public synchronized int getPort()
+ {
+ return m_port;
+ }
+
+ /**
+ * This method starts the web server if it is not already active.
+ * @param resolver Resolver is able to get Servlet or Resource based on request URI.
+ *
+ * @throws java.io.IOException If there are any networking issues.
+ * @throws java.lang.IllegalStateException If the server is in the
+ * <tt>HttpServer.STOPPING_STATE</tt> state.
+ **/
+ public synchronized void start(final ServiceRegistrationResolver resolver)
+ throws IOException
+ {
+ m_resolver = resolver;
+ if (m_state == INACTIVE_STATE)
+ {
+ // If inactive, then create server socket, server thread, and
+ // set state to active.
+ m_serverSocket = new ServerSocket(m_port);
+ m_serverThread = new Thread(new Runnable()
+ {
+ public void run()
+ {
+ acceptConnections();
+ }
+ }, "HttpServer");
+ m_state = ACTIVE_STATE;
+ m_serverThread.start();
+ }
+ else if (m_state == STOPPING_STATE)
+ {
+ throw new IllegalStateException("Server is in process of stopping.");
+ }
+ }
+
+ /**
+ * This method stops the web server if it is currently active. This method
+ * will block the calling thread until the web server is completely stopped.
+ * This can potentially take a long time, since it allows all existing
+ * connections to be processed before shutting down. Subsequent calls to
+ * this method will also block the caller. If a blocked thread is interrupted,
+ * the method will release the blocked thread by throwing an interrupted
+ * exception. In such a case, the web server will still continue its
+ * shutdown process.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted.
+ **/
+ public void stop() throws InterruptedException
+ {
+ ThreadGate gate = null;
+
+ synchronized (this)
+ {
+ // If we are active or stopping, allow the caller to shutdown the
+ // server socket and grab a local copy of the shutdown gate to
+ // wait for the server to stop.
+ if (m_state != INACTIVE_STATE)
+ {
+ m_logger.log(Logger.LOG_INFO,
+ "Shutting down, be patient...waiting for all threads to finish.");
+
+ // If there is no shutdown gate, create one and save its
+ // reference both in the field and locally. All threads
+ // that call stop() while the server is stopping will wait
+ // on this gate.
+ if (m_shutdownGate == null)
+ {
+ m_shutdownGate = new ThreadGate();
+ }
+ gate = m_shutdownGate;
+
+ // Close the server socket, which will cause the server thread
+ // to exit its accept() loop.
+ try
+ {
+ m_serverSocket.close();
+ }
+ catch (IOException ex)
+ {
+ }
+ }
+ }
+
+ // Wait on gate for server thread to shutdown.
+ if (gate != null)
+ {
+ gate.await();
+ }
+ }
+
+ /**
+ * This method is the main server loop for accepting connection. This is
+ * only ever called by the server thread.
+ **/
+ private void acceptConnections()
+ {
+ // Start the thread pool.
+ m_threadPool.start();
+
+ Socket socket;
+
+ m_logger.log(Logger.LOG_DEBUG, "Waiting for connections.");
+
+ // Now listen for connections until interrupted.
+ while (m_serverSocket.isBound() && !m_serverSocket.isClosed())
+ {
+ try
+ {
+ socket = m_serverSocket.accept();
+ try
+ {
+ // Create connection object and add it to the thread pool
+ // to be serviced.
+ Connection connection = new Connection(socket, m_connectionTimeout,
+ m_connectionRequestLimit, m_resolver, m_logger);
+ m_logger.log(Logger.LOG_DEBUG, "Accepted a new connection.");
+ m_threadPool.addConnection(connection);
+ }
+ catch (IOException ex)
+ {
+ // If we have any difficulty creating the connection
+ // then just ignore it, because the socket will be
+ // closed in the connection constructor.
+ m_logger.log(Logger.LOG_ERROR, "Error creating connection.", ex);
+ }
+ }
+ catch (IOException ex)
+ {
+ m_logger.log(Logger.LOG_ERROR,
+ "The call to accept() terminated with an exception.", ex);
+ }
+ }
+
+ // Shutdown the server.
+ shutdown();
+ }
+
+ /**
+ * This method shuts down the server; it is only ever called by the
+ * server thread.
+ **/
+ private void shutdown()
+ {
+ m_logger.log(Logger.LOG_DEBUG, "Waiting for thread pool threads to stop.");
+
+ while (true)
+ {
+ try
+ {
+ // Wait for thread pool to stop servicing connections.
+ m_threadPool.stop();
+ break;
+ }
+ catch (InterruptedException ex)
+ {
+ // Only the server thread will call this and we don't interrupt
+ // it, so this should never happen, but just in case we will loop
+ // until the thread pool is actually stopped.
+ }
+ }
+
+ synchronized (this)
+ {
+ // Now that the thread pool is stopped, open the shutdown
+ // gate and set the state to inactive.
+ m_shutdownGate.open();
+ m_shutdownGate = null;
+ m_state = INACTIVE_STATE;
+ }
+ m_logger.log(Logger.LOG_DEBUG, "Shutdown complete.");
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ServletHandler.java
new file mode 100644
index 0000000..277848a
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.server;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationHandler;
+import org.apache.felix.http.lightweight.servlet.HttpServletRequestImpl;
+import org.apache.felix.http.lightweight.servlet.HttpServletResponseImpl;
+
+/**
+ * Handles servlet processing.
+ * This class encapsulates the work done on a servlet given a request and response.
+ */
+public class ServletHandler implements ServiceRegistrationHandler
+{
+
+ private final HttpServletRequestImpl m_request;
+ private final HttpServletResponseImpl m_response;
+ private final ServiceRegistration m_servletElement;
+ private final Logger m_logger;
+
+ /**
+ * @param request Http Request
+ * @param response Http Response
+ * @param element Servlet Registration
+ * @param m_logger Logger
+ */
+ public ServletHandler(final HttpServletRequestImpl request, final HttpServletResponseImpl response, final ServiceRegistration element, final Logger m_logger)
+ {
+ this.m_request = request;
+ this.m_response = response;
+ this.m_servletElement = element;
+ this.m_logger = m_logger;
+ }
+
+ /**
+ * Process the servlet.
+ *
+ * @param close true if not keep-alive connection.
+ * @throws ServletException
+ * @throws IOException
+ */
+ public void handle(final boolean close) throws ServletException, IOException
+ {
+ //Check to see if the Servlet has been initialized, if not initialize it and set the flag.
+ synchronized (m_servletElement)
+ {
+ if (!m_servletElement.hasBeenInitialized())
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Initializing servlet " + m_servletElement.getAlias());
+ m_servletElement.getServlet().init(m_servletElement.getServletConfig());
+ m_servletElement.setInitialized();
+ }
+ }
+
+ if (m_servletElement.getContext().handleSecurity(m_request, m_response))
+ {
+ m_servletElement.getServlet().service(m_request, m_response);
+ }
+
+ if (!m_response.isCommitted())
+ {
+ m_response.flushBuffer();
+ }
+ }
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadGate.java
new file mode 100644
index 0000000..15c19d0
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.server;
+
+/**
+ * This class implements a simple one-shot gate for threads. The gate
+ * starts closed and will block any threads that try to wait on it. Once
+ * opened, all waiting threads will be released. The gate cannot be reused.
+**/
+public class ThreadGate
+{
+ private boolean m_open = false;
+
+ /**
+ * Open the gate and release any waiting threads.
+ **/
+ public synchronized void open()
+ {
+ m_open = true;
+ notifyAll();
+ }
+
+ /**
+ * Wait for the gate to open.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted;
+ * the gate still remains closed until opened.
+ **/
+ public synchronized void await() throws InterruptedException
+ {
+ while (!m_open)
+ {
+ wait();
+ }
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java
new file mode 100644
index 0000000..e4c0455
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/server/ThreadPool.java
@@ -0,0 +1,312 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.server;
+
+import java.net.SocketTimeoutException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+
+/**
+ * This class implements a simple thread pool for servicing HTTP connections.
+ * The thread pool does not create any threads initially, but waits for
+ * connections to be added to create threads. As connections are added, threads
+ * are only created if they are needed up until the thread limit. If threads
+ * are inactive for a period of time, then the threads terminate; the default
+ * is 60000 milliseconds.
+**/
+public class ThreadPool
+{
+ public static final int DEFAULT_THREAD_TIMEOUT = 60000;
+ private int m_threadTimeout;
+
+ private ThreadGroup m_group = new ThreadGroup("ThreadPoolGroup");
+ private int m_state;
+ private ThreadGate m_shutdownGate;
+ private int m_threadName = 0;
+ private int m_threadLimit = 0;
+ private int m_threadCount = 0;
+ private int m_threadAvailable = 0;
+ private List m_connectionList = new ArrayList();
+ private final Logger m_logger;
+
+ /**
+ * Constructs a thread pool with the specified thread limit and with
+ * the default inactivity timeout.
+ * @param threadLimit The maximum number of threads in the pool.
+ **/
+ public ThreadPool(final int threadLimit, final Logger logger)
+ {
+ this(threadLimit, DEFAULT_THREAD_TIMEOUT, logger);
+ }
+
+ /**
+ * Constructs a thread pool with the specified thread limit and inactivity
+ * timeout.
+ * @param threadLimit The maximum number of threads in the pool.
+ * @param threadTimeout The inactivity timeout for threads in milliseconds.
+ **/
+ public ThreadPool(int threadLimit, int threadTimeout, Logger logger)
+ {
+ m_threadLimit = threadLimit;
+ m_threadTimeout = threadTimeout;
+ m_logger = logger;
+ m_state = Server.INACTIVE_STATE;
+ }
+
+ /**
+ * This method returns the current state of the thread pool, which is one
+ * of the following values:
+ * <ul>
+ * <li><tt>ThreadPool.INACTIVE_STATE</tt> - the thread pool is currently
+ * not active.
+ * </li>
+ * <li><tt>ThreadPool.ACTIVE_STATE</tt> - the thread pool is active and
+ * servicing connections.
+ * </li>
+ * <li><tt>ThreadPool.STOPPING_STATE</tt> - the thread pool is in the
+ * process of shutting down.
+ * </li>
+ * </li>
+ * @return The current state of the thread pool.
+ **/
+ public synchronized int getState()
+ {
+ return m_state;
+ }
+
+ /**
+ * Starts the thread pool if it is not already active, allowing it to
+ * service connections.
+ * @throws java.lang.IllegalStateException If the thread pool is in the
+ * <tt>ThreadPool.STOPPING_STATE</tt> state.
+ **/
+ public synchronized void start()
+ {
+ if (m_state != Server.STOPPING_STATE)
+ {
+ m_state = Server.ACTIVE_STATE;
+ }
+ else
+ {
+ throw new IllegalStateException("Thread pool is in process of stopping.");
+ }
+ }
+
+ /**
+ * This method stops the thread pool if it is currently active. This method
+ * will block the calling thread until the thread pool is completely stopped.
+ * This can potentially take a long time, since it allows all existing
+ * connections to be processed before shutting down. Subsequent calls to
+ * this method will also block the caller. If a blocked thread is interrupted,
+ * the method will release the blocked thread by throwing an interrupted
+ * exception. In such a case, the thread pool will still continue its
+ * shutdown process.
+ * @throws java.lang.InterruptedException If the calling thread is interrupted.
+ **/
+ public void stop() throws InterruptedException
+ {
+ ThreadGate gate = null;
+
+ synchronized (this)
+ {
+ if (m_state != Server.INACTIVE_STATE)
+ {
+ // If there is no shutdown gate, create one and save its
+ // reference both in the field and locally. All threads
+ // that call stop() while the server is stopping will wait
+ // on this gate.
+ if ((m_shutdownGate == null) && (m_threadCount > 0))
+ {
+ m_shutdownGate = new ThreadGate();
+ }
+ gate = m_shutdownGate;
+ m_state = Server.STOPPING_STATE;
+ // Interrupt all threads that have been created by the
+ // thread pool.
+ m_group.interrupt();
+ }
+ }
+
+ // Wait on gate for thread pool shutdown to complete.
+ if (gate != null)
+ {
+ gate.await();
+ }
+ }
+
+ /**
+ * This method adds an HTTP connection to the thread pool for servicing.
+ * @param connection
+ * @throws java.lang.IllegalStateException If the thread pool is not in the
+ * <tt>ThreadPool.ACTIVE_STATE</tt> state.
+ **/
+ public synchronized void addConnection(final Connection connection)
+ {
+ if (m_state == Server.ACTIVE_STATE)
+ {
+ // Add the new connection to the connection list.
+ m_connectionList.add(connection);
+ notify();
+
+ // If there are not enough available threads to handle all outstanding
+ // connections and we still haven't reached our thread limit, then
+ // add another thread.
+ if ((m_threadAvailable < m_connectionList.size())
+ && (m_threadCount < m_threadLimit))
+ {
+ // Increase our thread count, but not number of available threads,
+ // since the new thread will be used to service the new connection
+ // and thus is not available.
+ m_threadCount++;
+ // Use simple integer for thread name for logging purposes.
+ if (m_threadName == Integer.MAX_VALUE)
+ {
+ m_threadName = 1;
+ }
+ else
+ {
+ m_threadName++;
+ }
+ // Create and start thread into our thread group.
+ new Thread(m_group, new Runnable()
+ {
+ public void run()
+ {
+ processConnections();
+ }
+ }, Integer.toString(m_threadName)).start();
+ m_logger.log(Logger.LOG_DEBUG, "Created new thread for pool; count = "
+ + m_threadCount + ", max = " + m_threadLimit + ".");
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("The thread pool is not active.");
+ }
+ }
+
+ /**
+ * This method is the main loop for all threads servicing connections.
+ **/
+ private void processConnections()
+ {
+ Connection connection;
+ while (true)
+ {
+ synchronized (this)
+ {
+ // Any new threads entering this region are now available to
+ // process a connection, so increment the available count.
+ m_threadAvailable++;
+
+ try
+ {
+ // Keep track of when we start to wait so that we
+ // know if our timeout expires.
+ long start = System.currentTimeMillis();
+ long current = start;
+ // Wait until there is a connection to service or until
+ // the timeout expires; if the timeout is zero, then there
+ // is no timeout.
+ while (m_state == Server.ACTIVE_STATE
+ && (m_connectionList.size() == 0)
+ && ((m_threadTimeout == 0) || ((current - start) < m_threadTimeout)))
+ {
+ // Try to wait for another connection, but our timeout
+ // expires then commit suicide.
+ wait(m_threadTimeout - (current - start));
+ current = System.currentTimeMillis();
+ }
+ }
+ catch (InterruptedException ex)
+ {
+ // This generally happens when we are shutting down.
+ Thread.currentThread().interrupt();
+ }
+
+ // Set connection to null if we are going to commit suicide;
+ // otherwise get the first available connection for servicing.
+ if (m_connectionList.size() == 0)
+ {
+ connection = null;
+ }
+ else
+ {
+ connection = (Connection) m_connectionList.remove(0);
+ }
+
+ // Decrement number of available threads, since we will either
+ // start to service a connection at this point or we will commit
+ // suicide.
+ m_threadAvailable--;
+
+ // If we do not have a connection, then we are committing
+ // suicide due to inactivity or because we were interrupted
+ // and are stopping the thread pool.
+ if (connection == null)
+ {
+ // One less thread in use.
+ m_threadCount--;
+ if (Thread.interrupted())
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Pool thread dying due to interrupt.");
+ }
+ else
+ {
+ m_logger.log(Logger.LOG_DEBUG,
+ "Pool thread dying due to inactivity.");
+ }
+ // If we are stopping and the last thread is dead, then
+ // open the shutdown gate to release all threads waiting
+ // for us to stop.
+ if ((m_state == Server.STOPPING_STATE) && (m_threadCount == 0))
+ {
+ m_shutdownGate.open();
+ m_shutdownGate = null;
+ m_state = Server.INACTIVE_STATE;
+ }
+ // Return to kill the thread by exiting our run method.
+ return;
+ }
+ }
+
+ // Otherwise, we have a connection so process it.
+ // Note, we might have outstanding connections to
+ // process even if we are stopping, so we cleaning
+ // service those remaining connections before stopping.
+ try
+ {
+ connection.process();
+ m_logger.log(Logger.LOG_DEBUG, "Connection closed normally.");
+ }
+ catch (SocketTimeoutException ex)
+ {
+ m_logger.log(Logger.LOG_INFO, "Connection closed due to inactivity.");
+ }
+ catch (Exception ex)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Connection close due to unknown reason.",
+ ex);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ConcreteServletInputStream.java
new file mode 100644
index 0000000..925311c
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletInputStream;
+
+/**
+ * Simple utility input stream class that provides a method for reading
+ * a line of characters, where a "line" is leniently defined as anything
+ * ending in '\n' or '\r\n'.
+ *
+ * Extends ServletInputStream
+**/
+public class ConcreteServletInputStream extends ServletInputStream
+{
+ private InputStream m_is;
+ private StringBuffer m_sb = new StringBuffer();
+
+ /**
+ * @param is InputStream
+ */
+ public ConcreteServletInputStream(final InputStream is)
+ {
+ m_is = is;
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#available()
+ */
+ public int available() throws IOException
+ {
+ return m_is.available();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#close()
+ */
+ public void close() throws IOException
+ {
+ m_is.close();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#mark(int)
+ */
+ public void mark(int readlimit)
+ {
+ m_is.mark(readlimit);
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#markSupported()
+ */
+ public boolean markSupported()
+ {
+ return m_is.markSupported();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read()
+ */
+ public int read() throws IOException
+ {
+ return m_is.read();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read(byte[])
+ */
+ public int read(final byte[] b) throws IOException
+ {
+ return m_is.read(b);
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#read(byte[], int, int)
+ */
+ public int read(final byte[] b, final int off, final int len) throws IOException
+ {
+ return m_is.read(b, off, len);
+ }
+
+ /**
+ * @return The next line in the input stream or null for EOF
+ * @throws IOException on I/O error
+ */
+ public String readLine() throws IOException
+ {
+ m_sb.delete(0, m_sb.length());
+ int bytesRead = 0;
+ for (int i = m_is.read(); i >= 0; i = m_is.read())
+ {
+ bytesRead++;
+ if ('\n' == (char) i)
+ {
+ break;
+ }
+ else if ('\r' != (char) i)
+ {
+ m_sb.append((char) i);
+ }
+ }
+ if (bytesRead == 0)
+ {
+ return null;
+ }
+
+ return m_sb.toString();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#reset()
+ */
+ public void reset() throws IOException
+ {
+ m_is.reset();
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.InputStream#skip(long)
+ */
+ public long skip(final long n) throws IOException
+ {
+ return m_is.skip(n);
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpConstants.java
new file mode 100644
index 0000000..f321aa1
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.servlet;
+
+import java.util.Collections;
+import java.util.Enumeration;
+
+/**
+ * Defines some commonly used HTTP constants and headers.
+ */
+public class HttpConstants
+{
+ /**
+ * HTTP line delimiter
+ */
+ public static final String HEADER_DELEMITER = "\r\n";
+ /**
+ * HTTP header delimiter
+ */
+ public static final String HEADER_TERMINATOR = HEADER_DELEMITER + HEADER_DELEMITER;
+
+ /**
+ * Content-Length header
+ */
+ public static final String HEADER_CONTENT_LENGTH = "Content-Length";
+ /**
+ * Location header
+ */
+ public static final String HEADER_LOCATION = "Location";
+ /**
+ * Content-Type header
+ */
+ public static final String HEADER_CONTENT_TYPE = "Content-Type";
+ /**
+ * Connection header
+ */
+ public static final String HEADER_CONNECTION = "Connection";
+
+ /**
+ * For building HTML error messages, this value is the default start of the html document for error message responses.
+ */
+ public static final String DEFAULT_HTML_HEADER = "<html>";
+
+ /**
+ * HTTP header delimiter.
+ */
+ public static final String HEADER_VALUE_DELIMITER = ": ";
+
+ /**
+ * HTTP GET Method
+ */
+ public static final String GET_REQUEST = "GET";
+ /**
+ * HTTP HEAD Method
+ */
+ public static final String HEAD_REQUEST = "HEAD";
+ /**
+ * HTTP POST Method
+ */
+ public static final String POST_REQUEST = "POST";
+ /**
+ * HTTP PUT Method
+ */
+ public static final String PUT_REQUEST = "PUT";
+ /**
+ * HTTP DELETE Method
+ */
+ public static final String DELETE_REQUEST = "DELETE";
+ /**
+ * HTTP OPTIONS Method
+ */
+ public static final Object OPTIONS_REQUEST = "OPTIONS";
+
+ /**
+ * HTTP v 1.0
+ */
+ public static final String HTTP10_VERSION = "HTTP/1.0";
+ /**
+ * HTTP v 1.1
+ */
+ public static final String HTTP11_VERSION = "HTTP/1.1";
+ /**
+ * Host header
+ */
+ public static final String HOST_HEADER = "Host";
+
+ /**
+ * Keep-alive value for Connection header.
+ */
+ public static final String KEEPALIVE_CONNECTION = "keep-alive";
+ /**
+ * Close value for Connection header.
+ */
+ public static final String CLOSE_CONNECTION = "close";
+ /**
+ * Date format for HTTP
+ */
+ public static final String HTTP_DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss z";
+ /**
+ * Timezone specified for HTTP
+ */
+ public static final String HTTP_TIMEZONE = "GMT";
+
+ /**
+ * Felix HTTP service property to enable HTTP server
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTP_ENABLE = "org.apache.felix.http.enable";
+ /**
+ * Felix HTTP service property to enable HTTPS server (unimplemented)
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTPS_ENABLE = "org.apache.felix.https.enable";
+ /**
+ * Felix HTTP property to configure server port.
+ */
+ public static final Object SERVICE_PROPERTY_KEY_HTTP_PORT = "org.osgi.service.http.port";
+
+ /**
+ * HTTP response code 100
+ */
+ public static final int HTTP_RESPONSE_CONTINUE = 100;
+ /**
+ * Servlet implementation name.
+ */
+ public static final String SERVER_INFO = "Apache Felix Lightweight HTTP Service";
+ /**
+ * HTTP Scheme
+ */
+ public static final String HTTP_SCHEME = "http";
+
+ /**
+ * Servlet API requires passing empty enumerations.
+ */
+ public static final Enumeration EMPTY_ENUMERATION = Collections.enumeration(Collections.EMPTY_SET);
+
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java
new file mode 100644
index 0000000..358c5de
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletRequestImpl.java
@@ -0,0 +1,977 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.UnsupportedEncodingException;
+import java.net.Socket;
+import java.net.URLDecoder;
+import java.security.Principal;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.felix.http.lightweight.osgi.Logger;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistration;
+import org.apache.felix.http.lightweight.osgi.ServiceRegistrationResolver;
+
+/**
+ * This class represents an HTTP request, which is parses from a given input
+ * stream, and implements HttpServletRequest for servlet processing.
+ **/
+public class HttpServletRequestImpl implements HttpServletRequest
+{
+ /**
+ * HTTP Method
+ */
+ private String m_method;
+ /**
+ * Host info of URI
+ */
+ private String m_uriHost;
+ /**
+ * URI of HTTP request
+ */
+ private String m_uri;
+ /**
+ * HTTP version
+ */
+ private String m_version;
+ /**
+ * Headers in HTTP request
+ */
+ private final Map m_headers = new HashMap();
+ private final Socket m_socket;
+ private Cookie[] m_cookies;
+ private final Locale m_locale = new Locale(System.getProperty("user.language"));
+ private Map m_attributes;
+ private final ServiceRegistrationResolver m_resolver;
+ private String m_servletPath;
+ /**
+ * Map of the parameters of the request.
+ */
+ private Map m_parameters;
+
+ /**
+ * When the body is parsed this value will be set to a non-null value
+ * regardless of the body content. As such it serves as a flag for parsing
+ * the body.
+ */
+ private byte[] m_requestBody = null;
+ private final static String m_encoding = "UTF-8";
+ private final Logger m_logger;
+ private String m_queryString;
+ /**
+ * Used to enforce the servlet API getInputStream()/getReader() calls.
+ */
+ private boolean m_getInputStreamCalled = false;
+ /**
+ * Used to enforce the servlet API getInputStream()/getReader() calls.
+ */
+ private boolean m_getReaderCalled = false;
+
+ /**
+ * @param socket Socket assocated with request
+ * @param serviceRegistrationResolver
+ * @param logger
+ */
+ public HttpServletRequestImpl(final Socket socket, final ServiceRegistrationResolver serviceRegistrationResolver, final Logger logger)
+ {
+ this.m_socket = socket;
+ this.m_resolver = serviceRegistrationResolver;
+ this.m_logger = logger;
+ }
+
+ /**
+ * @return The socket this request is associated with.
+ */
+ protected Socket getSocket()
+ {
+ return m_socket;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getInputStream()
+ */
+ public ServletInputStream getInputStream() throws IOException
+ {
+
+ if (m_getReaderCalled)
+ {
+ throw new IllegalStateException("getReader() has already been called.");
+ }
+
+ if (m_requestBody == null)
+ {
+ parseBody(new BufferedInputStream(m_socket.getInputStream()));
+ }
+
+ m_getInputStreamCalled = true;
+
+ return new ConcreteServletInputStream(new ByteArrayInputStream(m_requestBody));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getReader()
+ */
+ public BufferedReader getReader() throws IOException
+ {
+ if (m_getInputStreamCalled)
+ {
+ throw new IllegalStateException("getInputStream() has already been called.");
+ }
+ if (m_requestBody == null)
+ {
+ parseBody(new BufferedInputStream(m_socket.getInputStream()));
+ }
+
+ m_getReaderCalled = true;
+
+ return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(
+ m_requestBody)));
+ }
+
+ /**
+ * This method parses the HTTP request line from the specified input stream
+ * and stores the result.
+ *
+ * @param is
+ * The input stream from which to read the HTTP request.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseRequestLine(final ConcreteServletInputStream is) throws IOException
+ {
+ String requestLine = is.readLine();
+ if (requestLine == null)
+ {
+ throw new IOException("Unexpected end of file when reading request line.");
+ }
+ StringTokenizer st = new StringTokenizer(requestLine, " ");
+ if (st.countTokens() != 3)
+ {
+ throw new IOException("Malformed HTTP request: " + requestLine);
+ }
+ m_method = st.nextToken();
+ m_uri = st.nextToken();
+ m_version = st.nextToken();
+
+ // If the URI is absolute, break into host and path.
+ m_uriHost = "";
+ int hostIdx = m_uri.indexOf("//");
+ if (hostIdx > 0)
+ {
+ int pathIdx = m_uri.indexOf("/", hostIdx + 2);
+ m_uriHost = m_uri.substring(hostIdx + 2, pathIdx);
+ m_uri = m_uri.substring(pathIdx);
+ }
+
+ // If the URI has query string, parse it.
+ int qsIdx = m_uri.indexOf("?");
+ if (qsIdx > 0)
+ {
+ m_queryString = m_uri.substring(qsIdx + 1);
+ m_uri = m_uri.substring(0, qsIdx);
+ }
+ }
+
+ /**
+ * This method parses the HTTP header lines from the specified input stream
+ * and stores the results.
+ *
+ * The map m_headers is populated with two types of values, Strings if the
+ * header occurs once or a List in the case that the same header is
+ * specified multiple times.
+ *
+ * @param is
+ * The input stream from which to read the HTTP header lines.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseHeader(final ConcreteServletInputStream is) throws IOException
+ {
+ for (String s = is.readLine(); (s != null) && (s.length() != 0); s = is.readLine())
+ {
+ int idx = s.indexOf(":");
+ if (idx > 0)
+ {
+ String header = s.substring(0, idx).trim();
+ String value = s.substring(idx + 1).trim();
+
+ String key = header.toLowerCase();
+
+ if (!m_headers.containsKey(key))
+ {
+ m_headers.put(key, value);
+ }
+ else
+ {
+ Object originalValue = m_headers.get(key);
+
+ if (originalValue instanceof String)
+ {
+ List headerList = new ArrayList();
+ headerList.add(originalValue);
+ headerList.add(value);
+ m_headers.put(key, headerList);
+ }
+ else if (originalValue instanceof List)
+ {
+ ((List) originalValue).add(value);
+ }
+ else
+ {
+ throw new RuntimeException("Unexpected type in m_headers: "
+ + originalValue.getClass().getName());
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * This method parses the HTTP body from the specified input stream and
+ * ignores the result.
+ *
+ * @param is
+ * The input stream from which to read the HTTP body.
+ * @throws java.io.IOException
+ * If any I/O error occurs.
+ **/
+ public void parseBody(final InputStream is) throws IOException
+ {
+ int length = getContentLength();
+
+ if (length > 0)
+ {
+ ByteArrayOutputStream baos = null;
+
+ byte[] buf = new byte[length];
+ int left = length;
+
+ do
+ {
+ left = left - is.read(buf);
+ if (left > 0)
+ {
+ if (baos == null)
+ {
+ baos = new ByteArrayOutputStream(length);
+ }
+ baos.write(buf);
+ }
+ }
+ while (left > 0);
+
+ if (baos != null)
+ {
+ m_requestBody = baos.toByteArray();
+ }
+ else
+ {
+ m_requestBody = buf;
+ }
+ }
+ else
+ {
+ // Set this to a non-null value so we know that the body has been
+ // parsed.
+ m_requestBody = new byte[0];
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getMethod()
+ */
+ public String getMethod()
+ {
+ return m_method;
+ }
+
+ /**
+ * Returns the value of the specified header, if present.
+ *
+ * @param header
+ * The header value to retrieve.
+ * @return The value of the specified header or <tt>null</tt>.
+ **/
+
+ public String getHeader(final String header)
+ {
+ Object value = m_headers.get(header.toLowerCase());
+
+ if (value == null)
+ {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ public Enumeration getHeaders(final String name)
+ {
+ Object v = m_headers.get(name);
+
+ if (v == null)
+ {
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ if (v instanceof String)
+ {
+ return Collections.enumeration(Arrays.asList(new String[] { (String) v }));
+ }
+
+ if (v instanceof List)
+ {
+ return Collections.enumeration((List) v);
+ }
+
+ throw new RuntimeException("Unexpected type in m_headers: "
+ + v.getClass().getName());
+ }
+
+ public Enumeration getHeaderNames()
+ {
+ if (m_headers.isEmpty())
+ {
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ return Collections.enumeration(m_headers.keySet());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
+ */
+ public Object getAttribute(final String arg0)
+ {
+ if (m_attributes != null)
+ {
+ return m_attributes.get(arg0);
+ }
+
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getAttributeNames()
+ */
+
+ public Enumeration getAttributeNames()
+ {
+ if (m_attributes != null)
+ {
+ return Collections.enumeration(m_attributes.keySet());
+ }
+
+ return HttpConstants.EMPTY_ENUMERATION;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getCharacterEncoding()
+ */
+
+ public String getCharacterEncoding()
+ {
+ return getHeader("Accept-Encoding");
+ }
+
+ public int getContentLength()
+ {
+ int len = 0;
+
+ try
+ {
+ len = Integer.parseInt(getHeader("Content-Length"));
+ }
+ catch (NumberFormatException e)
+ {
+ // Ignore this exception intentionally.
+ }
+
+ return len;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getContentType()
+ */
+
+ public String getContentType()
+ {
+ return getHeader("Content-Type");
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getLocale()
+ */
+
+ public Locale getLocale()
+ {
+ return m_locale;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getLocales()
+ */
+
+ public Enumeration getLocales()
+ {
+ return Collections.enumeration(Arrays.asList(new Object[] { m_locale }));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
+ */
+
+ public String getParameter(final String arg0)
+ {
+ if (m_parameters == null)
+ {
+ try
+ {
+ m_parameters = parseParameters();
+ }
+ catch (UnsupportedEncodingException e)
+ {
+ m_logger.log(Logger.LOG_ERROR, "Failed to parse request parameters.", e);
+ return null;
+ }
+ }
+
+ return (String) m_parameters.get(arg0);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterMap()
+ */
+
+ public Map getParameterMap()
+ {
+ return m_parameters;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterNames()
+ */
+
+ public Enumeration getParameterNames()
+ {
+ return Collections.enumeration(m_parameters.keySet());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
+ */
+
+ public String[] getParameterValues(String arg0)
+ {
+ return (String[]) m_parameters.values().toArray(new String[m_parameters.size()]);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getProtocol()
+ */
+ public String getProtocol()
+ {
+ return m_version;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
+ */
+ public String getRealPath(final String arg0)
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletRequest#getRemoteAddr()
+ */
+ public String getRemoteAddr()
+ {
+ return getSocket().getRemoteSocketAddress().toString();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getRemoteHost()
+ */
+ public String getRemoteHost()
+ {
+ return getSocket().getRemoteSocketAddress().toString();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
+ */
+ public RequestDispatcher getRequestDispatcher(String arg0)
+ {
+ return null;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getScheme()
+ */
+ public String getScheme()
+ {
+ return HttpConstants.HTTP_SCHEME;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getServerName()
+ */
+ public String getServerName()
+ {
+ return HttpConstants.SERVER_INFO;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#getServerPort()
+ */
+ public int getServerPort()
+ {
+ return getSocket().getLocalPort();
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#isSecure()
+ */
+ public boolean isSecure()
+ {
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
+ */
+ public void removeAttribute(String arg0)
+ {
+ if (m_attributes != null)
+ {
+ m_attributes.remove(arg0);
+ }
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
+ */
+ public void setAttribute(String arg0, Object arg1)
+ {
+ if (m_attributes == null)
+ {
+ m_attributes = new HashMap();
+ }
+
+ m_attributes.put(arg0, arg1);
+ }
+
+ /* (non-Javadoc)
+ * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
+ */
+ public void setCharacterEncoding(String arg0) throws UnsupportedEncodingException
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getAuthType()
+ */
+
+ public String getAuthType()
+ {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getCookies()
+ */
+
+ public Cookie[] getCookies()
+ {
+ if (m_cookies == null)
+ {
+
+ String cookieHeader = getHeader("Cookie");
+
+ if (cookieHeader == null)
+ {
+ return null;
+ }
+
+ List cookieList = new ArrayList();
+
+ for (Iterator i = Arrays.asList(cookieHeader.split(";")).iterator(); i.hasNext();)
+ {
+ String[] nvp = i.next().toString().split("=");
+
+ if (nvp.length != 2)
+ {
+ //Ignore invalid cookie and and continue.
+ continue;
+ }
+
+ cookieList.add(new Cookie(nvp[0].trim(), nvp[1].trim()));
+ }
+ m_cookies = (Cookie[]) cookieList.toArray(new Cookie[cookieList.size()]);
+ }
+
+ return m_cookies;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
+ */
+
+ public long getDateHeader(final String name)
+ {
+ String headerValue = getHeader(name);
+
+ if (headerValue == null)
+ {
+ return -1;
+ }
+
+ try
+ {
+ SimpleDateFormat sdf = new SimpleDateFormat();
+
+ return sdf.parse(headerValue).getTime();
+ }
+ catch (ParseException e)
+ {
+ throw new IllegalArgumentException("Unable to convert to date: "
+ + headerValue);
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
+ */
+
+ public int getIntHeader(final String name)
+ {
+ String value = getHeader(name);
+
+ if (value == null)
+ {
+ return -1;
+ }
+
+ return Integer.parseInt(value);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getPathInfo()
+ */
+
+ public String getPathInfo()
+ {
+ String alias = getAlias();
+
+ if (m_uri != null && alias.length() > 0)
+ {
+ if (m_uri.length() == alias.length())
+ {
+ return null;
+ }
+
+ return m_uri.substring(alias.length());
+ }
+
+ return null;
+ }
+
+ public String getPathTranslated()
+ {
+ // TODO: Always returning null may be incorrect.
+ return null;
+ }
+
+ public String getContextPath()
+ {
+ return "";
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getQueryString()
+ */
+
+ public String getQueryString()
+ {
+ return m_queryString;
+ }
+
+ public String getRemoteUser()
+ {
+ return null;
+ }
+
+ public boolean isUserInRole(String role)
+ {
+ return false;
+ }
+
+ public Principal getUserPrincipal()
+ {
+ return null;
+ }
+
+ public String getRequestedSessionId()
+ {
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getRequestURI()
+ */
+ public String getRequestURI()
+ {
+ return m_uri;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getRequestURL()
+ */
+ public StringBuffer getRequestURL()
+ {
+ StringBuffer sb = new StringBuffer();
+ sb.append(m_uriHost);
+ sb.append(m_uri);
+
+ return sb;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.http.HttpServletRequest#getServletPath()
+ */
+
+ public String getServletPath()
+ {
+ if (m_servletPath == null)
+ {
+ ServiceRegistration element = m_resolver.getServiceRegistration(m_uri);
+
+ if (element == null)
+ {
+ throw new IllegalStateException(
+ "Unable to get ServletElement for HttpRequest.");
+ }
+
+ m_servletPath = element.getAlias();
+ }
+
+ return m_servletPath;
+ }
+
+ /**
+ * @return Alias associated with this request
+ */
+ private String getAlias()
+ {
+ ServiceRegistration element = m_resolver.getServiceRegistration(m_uri);
+
+ if (element == null)
+ {
+ throw new IllegalStateException(
+ "Unable to get ServletElement for HttpRequest.");
+ }
+
+ return element.getAlias();
+ }
+
+ public HttpSession getSession(boolean create)
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public HttpSession getSession()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdValid()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromCookie()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromURL()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ public boolean isRequestedSessionIdFromUrl()
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /**
+ * Parse the parameters in the request and return as a Map of <String,
+ * String>.
+ *
+ * @return
+ * @throws UnsupportedEncodingException
+ */
+ private Map parseParameters() throws UnsupportedEncodingException
+ {
+ Map params = new HashMap();
+
+ String queryString = getQueryString();
+
+ if (queryString != null && queryString.length() > 0)
+ {
+ parseParameterString(queryString, params);
+ }
+
+ if (m_requestBody != null)
+ {
+ parseParameterString(new String(m_requestBody), params);
+ }
+
+ return params;
+ }
+
+ /**
+ *
+ * @param queryString
+ * A String formatted like: 'home=Cosby&favorite+flavor=flies'
+ * @param params
+ * Map of <String, String> of existing parameters to be added to.
+ *
+ * @throws UnsupportedEncodingException
+ * if encoding type is unsupported
+ */
+ private void parseParameterString(final String queryString, final Map params)
+ throws UnsupportedEncodingException
+ {
+ for (Iterator i = Arrays.asList(queryString.split("&")).iterator(); i.hasNext();)
+ {
+ String[] nva = i.next().toString().split("=");
+
+ if (nva.length == 2)
+ {
+ params.put(URLDecoder.decode(nva[0].trim(), m_encoding), nva[1].trim());
+ }
+ }
+ }
+
+ /**
+ * @param method
+ * HTTP method
+ * @return true if the psased HTTP method is supported by this server.
+ */
+ public static boolean isSupportedMethod(final String method)
+ {
+ if (method.equals(HttpConstants.OPTIONS_REQUEST))
+ {
+ return false;
+ }
+
+ return true;
+ }
+
+ /* (non-Javadoc)
+ * @see java.lang.Object#toString()
+ */
+ public String toString()
+ {
+ if (m_method != null && m_uri != null)
+ {
+ return m_method + m_uri;
+ }
+
+ return super.toString();
+ }
+}
\ No newline at end of file
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletResponseImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletResponseImpl.java
new file mode 100644
index 0000000..7990014
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/HttpServletResponseImpl.java
@@ -0,0 +1,743 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.io.BufferedInputStream;
+import java.io.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 ServletPrintWriter m_servletPrintWriter;
+
+ 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 server The web server associated with the response.
+ * @param request The HTTP request associated with the response.
+ **/
+ 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));
+ 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_servletPrintWriter.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_servletPrintWriter == null)
+ {
+ m_buffer = new ByteArrayOutputStream(m_bufferSize);
+ m_servletPrintWriter = new ServletPrintWriter(m_buffer,
+ getCharacterEncoding());
+ }
+
+ return m_servletPrintWriter;
+ }
+
+ /* (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_servletPrintWriter = 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_servletPrintWriter = 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)
+ {
+ throw new UnimplementedAPIException();
+ }
+
+ /* (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 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(' ');
+ 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)
+ {
+ 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();
+ }
+
+ /**
+ * 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/src/main/java/org/apache/felix/http/lightweight/servlet/ServletConfigImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletConfigImpl.java
new file mode 100644
index 0000000..75d7f2a
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.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/src/main/java/org/apache/felix/http/lightweight/servlet/ServletContextImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletContextImpl.java
new file mode 100644
index 0000000..ca325a1
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletContextImpl.java
@@ -0,0 +1,285 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.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.http.lightweight.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;
+ }
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletOutputStreamImpl.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletOutputStreamImpl.java
new file mode 100644
index 0000000..86774b2
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.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/src/main/java/org/apache/felix/http/lightweight/servlet/ServletPrintWriter.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletPrintWriter.java
new file mode 100644
index 0000000..6b288ef
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/ServletPrintWriter.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.http.lightweight.servlet;
+
+import java.io.CharConversionException;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.nio.charset.Charset;
+
+/**
+ * ServletPrintWriter encodes characters with supplied encoding specification.
+ *
+ */
+public class ServletPrintWriter extends PrintWriter
+{
+ private final Charset charset;
+
+ /**
+ * @param outputStream OutputStream
+ * @param characterEncoding character encoding
+ * @throws UnknownCharacterException if the character encoding is not supported.
+ */
+ public ServletPrintWriter(OutputStream outputStream, String characterEncoding) throws CharConversionException
+ {
+ super(outputStream);
+ if (!Charset.isSupported(characterEncoding))
+ {
+ throw new CharConversionException("Unsupported character encoding: "
+ + characterEncoding);
+ }
+
+ this.charset = Charset.forName(characterEncoding);
+ }
+
+ /* (non-Javadoc)
+ * @see java.io.PrintWriter#print(java.lang.String)
+ */
+ public void print(String s)
+ {
+ super.print(charset.encode(s).toString());
+ }
+}
diff --git a/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/UnimplementedAPIException.java b/httplite/src/main/java/org/apache/felix/http/lightweight/servlet/UnimplementedAPIException.java
new file mode 100644
index 0000000..d867527
--- /dev/null
+++ b/httplite/src/main/java/org/apache/felix/http/lightweight/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.http.lightweight.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;
+
+}