Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

whiteboard servlet runtime now forwards all properties #47

Merged
merged 9 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 49 additions & 25 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -77,19 +77,36 @@ You can change various server setting by using the OSGi Configurator or the Conf
{
":configurator:resource-version": 1,

"JakartarsWhiteboardComponent":
{
"jersey.port": 8081,
"jersey.jakartars.whiteboard.name" : "demo",
"jersey.context.path" : "demo"
}
"JakartarsWhiteboardComponent":
{
"jersey.port": 8081,
"jersey.jakartars.whiteboard.name" : "demo",
"jersey.context.path" : "demo"
}
}
```

This would run the server at

http://localhost:8081/demo

The following properties are supported for configuring the Whiteboard on Jersey:

| Parameter | Description | Default |
| --- | --- | -- |
|`jersey.schema`| The schema under which the services should be available. | http |
|`jersey.host`| The host under which the services should be available. | localhost |
|`jersey.port`| The port under which the services should be available. | 8181 |
|`jersey.context.path`| The base context path of the whiteboard. | /rest |
|`jersey.jakartars.whiteboard.name`| The name of the whiteboard| Jersey REST |
|`jersey.disable.sessions`| Enable/disable session handling in Jetty.<br>Disabled by default as REST services are stateless. | `true` |

The definition of these properties is located in [JerseyConstants](https://github.com/osgi/jakartarest-osgi/blob/main/org.eclipse.osgitech.rest/src/main/java/org/eclipse/osgitech/rest/provider/JerseyConstants.java).

**_Note:_**
The default value for `jersey.context.path` is `/rest`. So if you don't configure a value via the _configurator.json_ file, your services will be available via the `rest` context path. This is also the case for a custom Jakarta-RS application. If you don't want to use a context path, you explicitly have to set it to an empty value.


Please note, that the Felix Jetty implementation runs the OSGi HTTP Service by default at port 8080.

**It may come to an conflict, with the port in your configuration.**
Expand All @@ -109,27 +126,39 @@ You can change various setting by using the OSGi Configurator or the Configurati

```json
{
"org.apache.felix.http~demo":
{
"org.osgi.service.http.port": 8081,
"org.osgi.service.http.host": "localhost",
"org.apache.felix.http.context_path": "demo",
"org.apache.felix.http.name": "Demo HTTP Whiteboard",
"org.apache.felix.http.runtime.init.id": "demowb"
},
"JakartarsServletWhiteboardRuntimeComponent~demo":
{
"jersey.jakartars.whiteboard.name" : "Demo Jakarta REST Whiteboard",
"jersey.context.path" : "rest",
"osgi.http.whiteboard.target" : "(id=demowb)"
}
"org.apache.felix.http~demo":
{
"org.osgi.service.http.port": 8081,
"org.osgi.service.http.host": "localhost",
"org.apache.felix.http.context_path": "demo",
"org.apache.felix.http.name": "Demo HTTP Whiteboard",
"org.apache.felix.http.runtime.init.id": "demowb"
},
"JakartarsServletWhiteboardRuntimeComponent~demo":
{
"jersey.jakartars.whiteboard.name" : "Demo Jakarta REST Whiteboard",
"jersey.context.path" : "rest",
"osgi.http.whiteboard.target" : "(id=demowb)"
}
}
```

This would run the Jakarta REST Whiteboard implementation at:

http://localhost:8081/demo/rest

The first block `org.apache.felix.http~demo` is used to configure the _Apache Felix HTTP Service_ service factory. Details about the configuration options are available in the [Apache Felix HTTP Service Wiki](https://cwiki.apache.org/confluence/display/FELIX/Apache+Felix+HTTP+Service).

The second block `JakartarsServletWhiteboardRuntimeComponent~demo` is used to configure the whiteboard service factory with the Servlet Whiteboard. The following properties are supported for configuring the Whiteboard on Servlet Whiteboard:

| Parameter | Description | Default |
| --- | --- | -- |
|`jersey.context.path`| The base context path of the whiteboard. | / |
|`jersey.jakartars.whiteboard.name`| The name of the whiteboard| Jersey REST |
|`osgi.http.whiteboard.target`| Service property specifying the target filter to select the Http Whiteboard implementation to process the service.<br>The value is an LDAP style filter that points to the id defined in `org.apache.felix.http.runtime.init.id`. | - |

The definition of these properties is located in [JerseyConstants](https://github.com/osgi/jakartarest-osgi/blob/main/org.eclipse.osgitech.rest/src/main/java/org/eclipse/osgitech/rest/provider/JerseyConstants.java).

Please note, that the Felix Jetty implementation runs the OSGi HTTP Service by default at port 8080.

**It may come to an conflict, with the port in your configuration.**
Expand All @@ -141,7 +170,6 @@ Therefore you may set the system property `org.osgi.service.http.port=-1` to dea
When using the Jakarta REST Whiteboard, you just have to register your REST resources and extensions as a service. There are some useful Meta-Annotations, that create component properties for you.

```java
@RequireJakartarsWhiteboard
@JakartarsResource
@JakartarsName("demo")
@Component(service = DemoResource.class, scope = ServiceScope.PROTOTYPE)
Expand All @@ -157,10 +185,6 @@ public class DemoResource {
}
```

With the `@RequireJakartarsWhiteboard` annotation, you define the requirement to the Jakarta REST Whiteboard implementation. In our case it is Jersey. You then need the Whiteboard dependencies in you workspace.

Please note, that you only need to define `@RequireJakartarsWhiteboard` once in your bundle!

## Maven Example Archetype

The module *org.eclipse.osgitech.rest.archetype* contains a Maven template to create a sample project.
Expand Down
2 changes: 0 additions & 2 deletions org.eclipse.osgitech.rest.bnd.library/bnd.bnd
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@ Provide-Capability: \
bnd.library;\
bnd.library = jakartaREST; \
path = library/workspace

-includeresource: {library=library}

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import org.osgi.service.cm.Configuration;
import org.osgi.service.jakartars.runtime.JakartarsServiceRuntime;
import org.osgi.service.jakartars.whiteboard.JakartarsWhiteboardConstants;
import org.osgi.service.servlet.whiteboard.HttpWhiteboardConstants;
import org.osgi.test.common.annotation.InjectBundleContext;
import org.osgi.test.common.annotation.config.InjectConfiguration;
import org.osgi.test.common.annotation.config.WithFactoryConfiguration;
Expand Down Expand Up @@ -222,6 +221,9 @@ public void testServletWhiteboardDefaultContext(@InjectBundleContext BundleConte
properties.put(HTTP_WHITEBOARD_SERVLET_PATTERN, "/servlet");
properties.put(HTTP_WHITEBOARD_CONTEXT_SELECT, "(" + HTTP_WHITEBOARD_CONTEXT_NAME + "=" + HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME + ")");
ctx.registerService(Servlet.class, new HttpServlet() {
/** serialVersionUID */
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.getWriter().print("Hello Servlet");
Expand Down Expand Up @@ -261,5 +263,13 @@ protected String getBaseURI(ServiceReference<JakartarsServiceRuntime> runtime) {
throw new IllegalArgumentException(
"The JAXRS Service Runtime did not declare an endpoint property");
}

@Test
public void testWhiteboardPropertiesForward() throws Exception {
ServiceReference<JakartarsServiceRuntime> serviceRuntime = tracker.getServiceReference();
Object object = serviceRuntime.getProperties().get("addition.property");
assertEquals("test.property", object);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
{
"jersey.jakartars.whiteboard.name" : "JRSWB",
"jersey.context.path" : "test",
"osgi.http.whiteboard.target" : "(id=SWB)"
"osgi.http.whiteboard.target" : "(id=SWB)",
"addition.property" : "test.property"
}
}
10 changes: 5 additions & 5 deletions org.eclipse.osgitech.rest.servlet.whiteboard.tests/test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@
org.apache.felix.http.servlet-api;version='[2.1.0,2.1.1)',\
org.apache.felix.scr;version='[2.2.6,2.2.7)',\
org.glassfish.hk2.osgi-resource-locator;version='[1.0.3,1.0.4)',\
org.eclipse.osgitech.rest;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.servlet.whiteboard;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.servlet.whiteboard.tests-tests;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.sse;version='[1.0.0,1.0.1)',\
org.osgi.service.component;version='[1.5.1,1.5.2)',\
org.osgi.service.jakartars;version='[2.0.0,2.0.1)',\
org.osgi.test.common;version='[1.2.1,1.2.2)',\
Expand Down Expand Up @@ -84,4 +80,8 @@
junit-jupiter-api;version='[5.9.2,5.9.3)',\
junit-jupiter-params;version='[5.9.2,5.9.3)',\
junit-platform-commons;version='[1.9.2,1.9.3)',\
org.opentest4j;version='[1.2.0,1.2.1)'
org.opentest4j;version='[1.2.0,1.2.1)',\
org.eclipse.osgitech.rest;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.servlet.whiteboard;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.servlet.whiteboard.tests-tests;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.sse;version='[1.2.3,1.2.4)'
6 changes: 6 additions & 0 deletions org.eclipse.osgitech.rest.servlet.whiteboard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@
<artifactId>org.apache.aries.spifly.dynamic.framework.extension</artifactId>
<destFileName>org.apache.aries.spifly.dynamic.framework.extension.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<destFileName>slf4j-api.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
Expand Down Expand Up @@ -131,6 +136,7 @@
<bundle>${project.build.directory}/bundles/org.apache.felix.scr.jar</bundle>
<bundle>${project.build.directory}/bundles/org.apache.felix.http.jetty.jar</bundle>
<bundle>${project.build.directory}/bundles/org.apache.aries.spifly.dynamic.framework.extension.jar</bundle>
<bundle>${project.build.directory}/bundles/slf4j-api.jar</bundle>
<bundle>${project.build.directory}/bundles/slf4j-simple.jar</bundle>
</bundles>
</configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.eclipse.osgitech.rest.provider.JerseyConstants;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -50,11 +49,11 @@
target = JerseyConstants.JERSEY_RUNTIME_CONDITION))
public class JakartarsServletWhiteboardRuntimeComponent {

private static Logger logger = Logger.getLogger(JakartarsServletWhiteboardRuntimeComponent.class.getName());
private BundleContext context;
private String target;
private String basePath;
private ServiceTracker<HttpServiceRuntime, ServletWhiteboardBasedJerseyServiceRuntime> httpRuntimeTracker;
private Map<String, Object> props;

/**
* Called on component activation
Expand All @@ -65,6 +64,7 @@ public class JakartarsServletWhiteboardRuntimeComponent {
public void activate(BundleContext context, Map<String, Object> props) throws ConfigurationException {

this.context = context;
this.props = props;
target = (String) props.get(HttpWhiteboardConstants.HTTP_WHITEBOARD_TARGET);
basePath = (String) props.getOrDefault(JerseyConstants.JERSEY_CONTEXT_PATH, "/");
openTracker();
Expand Down Expand Up @@ -123,7 +123,7 @@ private HttpServiceTracker(BundleContext context, Filter filter,

@Override
public ServletWhiteboardBasedJerseyServiceRuntime addingService(ServiceReference<HttpServiceRuntime> reference) {
return new ServletWhiteboardBasedJerseyServiceRuntime(context, basePath, reference);
return new ServletWhiteboardBasedJerseyServiceRuntime(context, basePath, reference, props);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

import org.eclipse.osgitech.rest.annotations.ProvideRuntimeAdapter;
import org.eclipse.osgitech.rest.helper.JerseyHelper;
Expand All @@ -61,7 +60,6 @@
@RequireHttpWhiteboard
public class ServletWhiteboardBasedJerseyServiceRuntime {

private final Logger logger = Logger.getLogger(ServletWhiteboardBasedJerseyServiceRuntime.class.getName());
private final BundleContext context;
private final String basePath;
private final ServiceReference<HttpServiceRuntime> runtimeTarget;
Expand All @@ -85,16 +83,17 @@ public RestContext(ServiceRegistration<ServletContextHelper> contextHelperReg,
}

public ServletWhiteboardBasedJerseyServiceRuntime(BundleContext context, String basePath,
ServiceReference<HttpServiceRuntime> runtimeTarget) {
ServiceReference<HttpServiceRuntime> runtimeTarget, Map<String, Object> props) {
this.context = context;
this.basePath = basePath;
this.runtimeTarget = runtimeTarget;
httpId = (Long) runtimeTarget.getProperty(SERVICE_ID);
this.httpWhiteboardTarget = String.format("(%s=%s)", SERVICE_ID, httpId);
this.runtime = new JerseyServiceRuntime<>(context, this::registerContainer, this::unregisterContainer);

runtime.start(Map.of(JAKARTA_RS_SERVICE_ENDPOINT, getURLs(),
SERVICE_DESCRIPTION, "REST whiteboard for HttpServiceRuntime " + httpId));
Map<String, Object> runtimeProperties = new HashMap<String, Object>(props);
runtimeProperties.put(JAKARTA_RS_SERVICE_ENDPOINT, getURLs());
runtimeProperties.put(SERVICE_DESCRIPTION, "REST whiteboard for HttpServiceRuntime " + httpId);
runtime.start(runtimeProperties);
}


Expand Down
15 changes: 10 additions & 5 deletions org.eclipse.osgitech.rest.tck/test.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@
net.bytebuddy.byte-buddy;version='[1.12.21,1.12.22)',\
org.apache.felix.scr;version='[2.2.6,2.2.7)',\
org.osgi.service.component;version='[1.5.1,1.5.2)',\
org.eclipse.osgitech.rest;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.jetty;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.sse;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.tck-tests;version='[1.0.0,1.0.1)',\
org.glassfish.hk2.api;version='[3.0.5,3.0.6)',\
org.glassfish.hk2.external.aopalliance-repackaged;version='[3.0.5,3.0.6)',\
org.glassfish.hk2.locator;version='[3.0.5,3.0.6)',\
Expand All @@ -85,4 +81,13 @@
junit-jupiter-api;version='[5.9.2,5.9.3)',\
junit-jupiter-params;version='[5.9.2,5.9.3)',\
junit-platform-commons;version='[1.9.2,1.9.3)',\
org.opentest4j;version='[1.2.0,1.2.1)'
org.opentest4j;version='[1.2.0,1.2.1)',\
org.eclipse.jetty.http;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.io;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.security;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.server;version='[11.0.13,11.0.14)',\
org.eclipse.jetty.util;version='[11.0.13,11.0.14)',\
org.eclipse.osgitech.rest;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.jetty;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.sse;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.tck-tests;version='[1.2.3,1.2.4)'
19 changes: 12 additions & 7 deletions org.eclipse.osgitech.rest.tck/test2.bndrun
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
osgi.extender;filter:='(osgi.extender=osgi.configurator)',\
bnd.identity;id='org.eclipse.osgitech.rest.tck-tests',\
bnd.identity;id='org.osgi.test.cases.jakartars',\
bnd.identity;id='org.eclipse.osgitech.rest.servlet.whiteboard'
bnd.identity;id='org.eclipse.osgitech.rest.servlet.whiteboard',\
bnd.identity;id='org.apache.felix.gogo.command',\
bnd.identity;id='org.apache.felix.gogo.runtime'

-runblacklist: \
bnd.identity;id='org.osgi.service.cm',\
Expand Down Expand Up @@ -60,15 +62,10 @@
org.apache.felix.cm.json;version='[2.0.0,2.0.1)',\
org.apache.felix.configurator;version='[1.0.18,1.0.19)',\
org.apache.felix.http.servlet-api;version='[2.1.0,2.1.1)',\
org.eclipse.parsson.jakarta.json;version='[1.1.2,1.1.3)',\
assertj-core;version='[3.24.2,3.24.3)',\
net.bytebuddy.byte-buddy;version='[1.12.21,1.12.22)',\
org.apache.felix.scr;version='[2.2.6,2.2.7)',\
org.osgi.service.component;version='[1.5.1,1.5.2)',\
org.eclipse.osgitech.rest;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.servlet.whiteboard;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.sse;version='[1.0.0,1.0.1)',\
org.eclipse.osgitech.rest.tck-tests;version='[1.0.0,1.0.1)',\
org.glassfish.hk2.api;version='[3.0.5,3.0.6)',\
org.glassfish.hk2.external.aopalliance-repackaged;version='[3.0.5,3.0.6)',\
org.glassfish.hk2.locator;version='[3.0.5,3.0.6)',\
Expand All @@ -85,4 +82,12 @@
junit-jupiter-api;version='[5.9.2,5.9.3)',\
junit-jupiter-params;version='[5.9.2,5.9.3)',\
junit-platform-commons;version='[1.9.2,1.9.3)',\
org.opentest4j;version='[1.2.0,1.2.1)'
org.opentest4j;version='[1.2.0,1.2.1)',\
org.eclipse.osgitech.rest;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.servlet.whiteboard;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.sse;version='[1.2.3,1.2.4)',\
org.eclipse.osgitech.rest.tck-tests;version='[1.2.3,1.2.4)',\
org.eclipse.parsson.jakarta.json;version='[1.1.5,1.1.6)',\
org.osgi.service.servlet;version='[2.0.0,2.0.1)',\
org.apache.felix.gogo.command;version='[1.1.2,1.1.3)',\
org.apache.felix.gogo.runtime;version='[1.1.6,1.1.7)'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Hashtable;
Expand Down
4 changes: 0 additions & 4 deletions org.eclipse.osgitech.rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.http.jetty</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
Expand Down
Loading
Loading