Skip to content

Add Http Whiteboard Publisher #6

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
40 changes: 40 additions & 0 deletions runtime/httpwhiteboard/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
24 changes: 24 additions & 0 deletions runtime/httpwhiteboard/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>httpwhiteboard</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.pde.PluginNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//src/main/resources=UTF-8
encoding//src/test/java=UTF-8
encoding//src/test/resources=UTF-8
encoding/<project>=UTF-8
8 changes: 8 additions & 0 deletions runtime/httpwhiteboard/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=17
4 changes: 4 additions & 0 deletions runtime/httpwhiteboard/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
2 changes: 2 additions & 0 deletions runtime/httpwhiteboard/.settings/org.eclipse.pde.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BUNDLE_ROOT_PATH=target/classes
eclipse.preferences.version=1
74 changes: 74 additions & 0 deletions runtime/httpwhiteboard/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?xml version="1.0"?>
<!--
/*********************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
**********************************************************************/
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.eclipse.osgi-technology.webservices</groupId>
<artifactId>org.eclipse.osgi.technology.webservices.runtime</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<artifactId>org.eclipse.osgi.technology.webservices.httpwhiteboard</artifactId>
<name>Http WHiteboard Publisher</name>
<description>Publish the Endpoints using Http Whiteboard Service</description>


<dependencies>
<dependency>
<groupId>org.eclipse.osgi-technology.webservices</groupId>
<artifactId>
org.eclipse.osgi.technology.webservices.runtime.registrar</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>jakarta.xml.ws</groupId>
<artifactId>jakarta.xml.ws-api</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.annotation.bundle</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.component</artifactId>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.service.http.whiteboard</artifactId>
<version>1.1.1</version>
<scope>provided</scope>
</dependency>
<!-- TODO can we somehow embedd the classes instead of copy the source-->
<!-- <dependency>-->
<!-- <groupId>com.sun.xml.ws</groupId>-->
<!-- <artifactId>jaxws-rt</artifactId>-->
<!-- <version>4.0.3</version>-->
<!-- <scope>provided</scope>-->
<!-- </dependency>-->
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.technology.webservices.httpwhiteboard;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.osgi.technology.webservices.spi.EndpointPublisher;
import org.eclipse.osgi.technology.webservices.spi.PublishedEndpoint;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.http.whiteboard.annotations.RequireHttpWhiteboard;
import org.osgi.service.log.Logger;
import org.osgi.service.log.LoggerFactory;
import org.osgi.service.webservice.whiteboard.WebserviceWhiteboardConstants;
import org.osgi.service.webservice.whiteboard.annotations.RequireWebserviceWhiteboard;

import jakarta.xml.ws.Endpoint;

/**
* Publishes Endpoints with the HttpWhiteboard service
*
*/
@RequireHttpWhiteboard
@RequireWebserviceWhiteboard
@Component(immediate = true, name = "org.eclipse.osgi.technology.webservices.httpwhiteboard", service = EndpointPublisher.class)
public class HttpWhiteboardPublisher implements EndpointPublisher {

private BundleContext bundleContext;
private Logger logger;
private Map<Endpoint, WhiteboardHttpContext> registrationMap = new ConcurrentHashMap<>();

/**
* Constructor
*
* @param bundleContext the context
* @param logger the logger
*/
@Activate
public HttpWhiteboardPublisher(BundleContext bundleContext,
@Reference(service = LoggerFactory.class) Logger logger) {
this.bundleContext = bundleContext;
this.logger = logger;
}

@Override
public PublishedEndpoint publishEndpoint(Endpoint endpoint) {
Map<String, Object> properties = endpoint.getProperties();
Object prefix = properties.get(WebserviceWhiteboardConstants.WEBSERVICE_HTTP_ENDPOINT_PREFIX);
if (prefix instanceof String contextPath) {
logger.info("Registering {} with http whiteboard at context path {}", endpoint, contextPath);
WhiteboardHttpContext httpContext = new WhiteboardHttpContext(contextPath, endpoint.getProperties());
registrationMap.put(endpoint, httpContext);
endpoint.publish(httpContext);
httpContext.register(bundleContext);
return httpContext;
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*******************************************************************************
* Copyright (c) 2024 Contributors to the Eclipse Foundation
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.osgi.technology.webservices.httpwhiteboard;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.eclipse.osgi.technology.webservices.httpwhiteboard.wsri.EndpointHttpExchange;
import org.eclipse.osgi.technology.webservices.spi.PublishedEndpoint;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;

import jakarta.servlet.Servlet;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.xml.ws.spi.http.HttpContext;

/**
* Http context to use with the whiteboard
*/
public class WhiteboardHttpContext extends HttpContext implements PublishedEndpoint {

private String path;
private Map<String, ?> attributes;
private Set<String> names;
private boolean closed;
private ServiceRegistration<?> serviceRegistration;

WhiteboardHttpContext(String path, Map<String, ?> attributes) {
this.path = path;
this.attributes = attributes;
names = Collections.unmodifiableSet(attributes.keySet());
}

@Override
public String getPath() {
return path;
}

@Override
public Object getAttribute(String name) {
return attributes.get(name);
}

@Override
public Set<String> getAttributeNames() {
return names;
}

synchronized void register(BundleContext bundleContext) {
if (closed) {
return;
}
HashMap<String, Object> properties = new HashMap<>(attributes);
if (!properties.containsKey(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME)) {
properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_NAME, "JAX-WS Service for path "+getPath());
}
properties.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_SERVLET_PATTERN, getPath());
serviceRegistration = bundleContext.registerService(Servlet.class, new JaxWsServlet(), FrameworkUtil.asDictionary(properties));
}

private class JaxWsServlet extends HttpServlet {

private static final long serialVersionUID = 1L;


@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (handler == null || closed) {
resp.sendError(HttpURLConnection.HTTP_UNAVAILABLE);
return;
}
handler.handle(new EndpointHttpExchange(req, resp, getServletContext(), WhiteboardHttpContext.this));
}

}

@Override
public synchronized void unpublish() {
closed = true;
if (serviceRegistration != null) {
serviceRegistration.unregister();
}
}

@Override
public String getAddress() {
// FIXME we need the full path here, how to get it from the whiteboard?!?
return path;
}

}
Loading
Loading