| 1 |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
<html><head><title>OSGi HttpService Example</title></head> |
| 44 |
|
| 45 |
|
| 46 |
<body> |
| 47 |
<h1>OSGi HttpService Example</h1> |
| 48 |
|
| 49 |
<p>This example demonstrates how to develop a simple OSGi bundle registering |
| 50 |
a Jersey based RESTful web service as an <a href="http://www.osgi.org/javadoc/r4v42/org/osgi/service/http/HttpService.html">OSGi HttpService</a></p> |
| 51 |
|
| 52 |
<h2>Contents</h2> |
| 53 |
<p>The example bundle (see the <tt>bundle</tt> module) consists of just one Jersey resource:</p> |
| 54 |
<dl> |
| 55 |
<dt><code>com.sun.jersey.samples.osgihttpservice.StatusResource</code></dt> |
| 56 |
<dd>that produces a textual response to an HTTP GET</dd> |
| 57 |
</dl> |
| 58 |
<p>The mapping of the URI path space is presented in the following table:</p> |
| 59 |
<table border="1"> |
| 60 |
<tr> |
| 61 |
<th>URI path</th> |
| 62 |
<th>Resource class</th> |
| 63 |
<th>HTTP methods</th> |
| 64 |
</tr> |
| 65 |
<tr> |
| 66 |
<td>/status</td> |
| 67 |
<td>StatusResource</td> |
| 68 |
<td>GET</td> |
| 69 |
</tr> |
| 70 |
</table> |
| 71 |
|
| 72 |
<h2>Running the Example</h2> |
| 73 |
<p>To run the example, you would need to build the OSGi bundle in the <tt>bundle</tt> module |
| 74 |
and install it to an OSGi runtime (e.g. Apache Felix) together with other OSGi bundles. |
| 75 |
Look at the attached <tt>functional-test</tt> module for details on the programmatical runtime configuration</p> |
| 76 |
To build the bundle jar file and run the tests, you can just launch |
| 77 |
<pre>%mvn clean install |
| 78 |
</pre> |
| 79 |
</p> |
| 80 |
<p>After <a href="http://felix.apache.org/site/downloads.cgi">downloading</a> |
| 81 |
and installing <a href="http://felix.apache.org/site/index.html">Felix</a>, |
| 82 |
you can also start the HTTP service manually as shown below. |
| 83 |
The following steps are known to work with the Felix version 3.0.9. You will need to replace <b><version></b> |
| 84 |
with the current Jersey version, and <b><repository></b> with either <code>snapshots</code> or <code>releases</code> based on whether |
| 85 |
you depend on a snapshot or stable release version of Jersey respectively, in the install commands below. |
| 86 |
<pre> |
| 87 |
%unzip ~/Downloads/org.apache.felix.main.distribution-3.0.9.zip |
| 88 |
%cp bundle/target/bundle*jar felix-framework-3.0.9 |
| 89 |
%cd felix-framework-3.0.9 |
| 90 |
%java -jar bin/felix.jar |
| 91 |
____________________________ |
| 92 |
Welcome to Apache Felix Gogo |
| 93 |
|
| 94 |
g! lb |
| 95 |
START LEVEL 1 |
| 96 |
ID|State |Level|Name |
| 97 |
0|Active | 0|System Bundle (3.0.9) |
| 98 |
1|Active | 1|Apache Felix Bundle Repository (1.6.2) |
| 99 |
2|Active | 1|Apache Felix Gogo Command (0.8.0) |
| 100 |
3|Active | 1|Apache Felix Gogo Runtime (0.8.0) |
| 101 |
4|Active | 1|Apache Felix Gogo Shell (0.8.0) |
| 102 |
g! install http://repo2.maven.org/maven2/org/apache/felix/org.apache.felix.configadmin/1.2.8/org.apache.felix.configadmin-1.2.8.jar |
| 103 |
Bundle ID: 5 |
| 104 |
g! install http://repo1.maven.org/maven2/org/apache/felix/org.apache.felix.eventadmin/1.2.10/org.apache.felix.eventadmin-1.2.10.jar |
| 105 |
Bundle ID: 6 |
| 106 |
g! install http://repo2.maven.org/maven2/org/ops4j/pax/web/pax-web-jetty-bundle/0.7.1/pax-web-jetty-bundle-0.7.1.jar |
| 107 |
Bundle ID: 7 |
| 108 |
g! install https://maven.java.net/service/local/artifact/maven/redirect?r=<b><repository></b>&g=com.sun.jersey&a=jersey-core&v=<b><version></b>&e=jar |
| 109 |
Bundle ID: 8 |
| 110 |
g! install https://maven.java.net/service/local/artifact/maven/redirect?r=<b><repository></b>&g=com.sun.jersey&a=jersey-server&v=<b><version></b>&e=jar |
| 111 |
Bundle ID: 9 |
| 112 |
g! install https://maven.java.net/service/local/artifact/maven/redirect?r=<b><repository></b>&g=com.sun.jersey&a=jersey-servlet&v=<b><version></b>&e=jar |
| 113 |
Bundle ID: 10 |
| 114 |
g! install file:bundle-1.9.jar |
| 115 |
Bundle ID: 11 |
| 116 |
g! lb |
| 117 |
START LEVEL 1 |
| 118 |
ID|State |Level|Name |
| 119 |
0|Active | 0|System Bundle (3.0.9) |
| 120 |
1|Active | 1|Apache Felix Bundle Repository (1.6.2) |
| 121 |
2|Active | 1|Apache Felix Gogo Command (0.8.0) |
| 122 |
3|Active | 1|Apache Felix Gogo Runtime (0.8.0) |
| 123 |
4|Active | 1|Apache Felix Gogo Shell (0.8.0) |
| 124 |
5|Installed | 1|Apache Felix Configuration Admin Service (1.2.8) |
| 125 |
6|Installed | 1|Apache Felix EventAdmin (1.2.10) |
| 126 |
7|Installed | 1|OPS4J Pax Web - Jetty Bundle (0.7.1) |
| 127 |
8|Installed | 1|jersey-core (1.9.0) |
| 128 |
9|Installed | 1|jersey-server (1.9.0) |
| 129 |
10|Installed | 1|jersey-servlet (1.9.0) |
| 130 |
11|Installed | 1|OSGi HttpService - Jersey Sample Bundle (1.9.0) |
| 131 |
g! start 5 6 7 8 9 10 11 |
| 132 |
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator start |
| 133 |
INFO: STARTING HTTP SERVICE BUNDLE |
| 134 |
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets |
| 135 |
INFO: JERSEY BUNDLE: REGISTERING SERVLETS |
| 136 |
Mar 22, 2011 10:04:30 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets |
| 137 |
INFO: JERSEY BUNDLE: HTTP SERVICE = org.ops4j.pax.web.service.internal.HttpServiceProxy@1d25d06e |
| 138 |
Mar 22, 2011 10:04:30 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate |
| 139 |
INFO: Initiating Jersey application, version 'Jersey: 1.9 09/02/2011 12:48 PM' |
| 140 |
Mar 22, 2011 10:04:30 PM com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder <init> |
| 141 |
INFO: Instantiated the Application class com.sun.jersey.samples.osgihttpservice.JerseyApplication |
| 142 |
Mar 22, 2011 10:04:31 PM com.sun.jersey.samples.osgihttpservice.Activator rawRegisterServlets |
| 143 |
INFO: JERSEY BUNDLE: SERVLETS REGISTERED |
| 144 |
Mar 22, 2011 10:04:31 PM com.sun.jersey.samples.osgihttpservice.Activator start |
| 145 |
INFO: HTTP SERVICE BUNDLE STARTED |
| 146 |
</pre> |
| 147 |
|
| 148 |
Now the Jersey resource should become |
| 149 |
available at <a href="http://localhost:8080/jersey-http-service/status">http://localhost:8080/jersey-http-service/status</a> |
| 150 |
</p> |
| 151 |
<h2>Troubleshooting</h2> |
| 152 |
<h3>HTTP Proxy</h3> |
| 153 |
<p>Behind a HTTP proxy, Apache Felix returns <tt>java.net.ConnectException: Connection timed out</tt> when |
| 154 |
installing bundles from the Internet. You will need to setup the following system properties: |
| 155 |
<ul> |
| 156 |
<li>http.proxyHost - the name of the proxy host.</li> |
| 157 |
<li>http.proxyPort - the port of the proxy host.</li> |
| 158 |
<li>http.proxyAuth - the user name and password to use when connecting to the proxy; this string should be the user name and password separated by a colon (e.g., rickhall:mypassword).</li> |
| 159 |
</ul> |
| 160 |
|
| 161 |
These system properties can be set directly on the command line when starting the JVM using the standard "-D<prop>=<value>" syntax or you can put them in the lib/system.properties file of your Felix installation; see documentation on configuring Felix for more information.</p> |
| 162 |
|
| 163 |
</body></html> |