You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@clerezza.apache.org by re...@apache.org on 2014/06/02 15:04:02 UTC

git commit: CLEREZZA-922: added jaxrs.whiteboard

Repository: clerezza
Updated Branches:
  refs/heads/master 668ba64ea -> ece56722f


CLEREZZA-922: added jaxrs.whiteboard


Project: http://git-wip-us.apache.org/repos/asf/clerezza/repo
Commit: http://git-wip-us.apache.org/repos/asf/clerezza/commit/ece56722
Tree: http://git-wip-us.apache.org/repos/asf/clerezza/tree/ece56722
Diff: http://git-wip-us.apache.org/repos/asf/clerezza/diff/ece56722

Branch: refs/heads/master
Commit: ece56722f6e0c2ca4d7c594da3dbae5ffdcd8bc7
Parents: 668ba64
Author: Reto Bachmann-Gmür <re...@apache.org>
Authored: Mon Jun 2 15:03:10 2014 +0200
Committer: Reto Bachmann-Gmür <re...@apache.org>
Committed: Mon Jun 2 15:03:10 2014 +0200

----------------------------------------------------------------------
 jaxrs.whiteboard.jersey/pom.xml                 | 133 +++++++++++++++
 .../src/license/THIRD-PARTY.properties          |  28 +++
 .../web/base/jersey/DefaultApplication.java     |  69 ++++++++
 .../commons/web/base/jersey/JerseyEndpoint.java | 169 +++++++++++++++++++
 .../OSGI-INF/metatype/metatype.properties       |  34 ++++
 5 files changed, 433 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/clerezza/blob/ece56722/jaxrs.whiteboard.jersey/pom.xml
----------------------------------------------------------------------
diff --git a/jaxrs.whiteboard.jersey/pom.xml b/jaxrs.whiteboard.jersey/pom.xml
new file mode 100644
index 0000000..80ca473
--- /dev/null
+++ b/jaxrs.whiteboard.jersey/pom.xml
@@ -0,0 +1,133 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one or more
+  contributor license agreements.  See the NOTICE file distributed with
+  this work for additional information regarding copyright ownership.
+  The ASF licenses this file to You under the Apache License, Version 2.0
+  (the "License"); you may not use this file except in compliance with
+  the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.clerezza</groupId>
+        <artifactId>clerezza</artifactId>
+        <version>0.5-SNAPSHOT</version>
+        <relativePath>../parent</relativePath>
+    </parent>    
+    <groupId>org.apache.clerezza</groupId>
+    <artifactId>jaxrs.whiteboard.jersey</artifactId>
+    <version>0.1-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+
+
+  <name>Clerezza JAX-RS Whiteboard Jersey</name>
+  <description>Providing JAX-RS OSGi Whiteboard registration of components.</description>
+  <licenses>
+    <license>
+      <name>Apache Software License, Version 2.0</name>
+      <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
+      <distribution>repo</distribution>
+      <comments>A business-friendly OSS license</comments>
+    </license>
+  </licenses>
+    
+
+  <build>
+    <!-- make it an OSGi bundle -->
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-scr-plugin</artifactId>
+      </plugin>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Private-Package>
+              org.apache.stanbol.commons.web.base.jersey;version=${project.version}
+            </Private-Package>
+            <!-- <Import-Package>
+              javax.servlet; version="[2.5.0,4.0.0)",
+              javax.servlet.http; version="[2.5.0,4.0.0)",
+              org.apache.stanbol.commons.web.base,
+              org.apache.stanbol.commons.web.base.*,
+              *
+            </Import-Package> -->
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+      <dependency>
+          <groupId>org.slf4j</groupId>
+          <artifactId>slf4j-api</artifactId>
+      </dependency>
+      
+    <!-- Jersey -->
+    <dependency>
+        <groupId>org.glassfish.jersey.containers</groupId>
+        <!-- if your container implements Servlet API older than 3.0, 
+        use "jersey-container-servlet-core", otherwise
+        "jersey-container-servlet"        -->
+        <artifactId>jersey-container-servlet-core</artifactId>
+        <version>2.8</version>
+    </dependency>
+    <!-- jersey depends on unavailable version -->
+    <dependency>
+        <groupId>javax.annotation</groupId>
+        <artifactId>javax.annotation-api</artifactId>
+        <version>1.2-b04</version>
+    </dependency>
+    <!-- <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-json</artifactId>
+    </dependency>  -->
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+      <version>2.5</version>
+    </dependency>
+
+    
+    <!-- OSGi tax -->
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.scr.annotations</artifactId>
+    </dependency>
+    <!-- for tests -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-simple</artifactId>
+      <scope>test</scope>
+    </dependency>
+
+  </dependencies>
+
+</project>

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ece56722/jaxrs.whiteboard.jersey/src/license/THIRD-PARTY.properties
----------------------------------------------------------------------
diff --git a/jaxrs.whiteboard.jersey/src/license/THIRD-PARTY.properties b/jaxrs.whiteboard.jersey/src/license/THIRD-PARTY.properties
new file mode 100644
index 0000000..6294042
--- /dev/null
+++ b/jaxrs.whiteboard.jersey/src/license/THIRD-PARTY.properties
@@ -0,0 +1,28 @@
+# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
+#-------------------------------------------------------------------------------
+# Already used licenses in project :
+# - Apache Software License
+# - Apache Software License, Version 2.0
+# - BSD License
+# - Common Development And Distribution License (CDDL), Version 1.0
+# - Common Development And Distribution License (CDDL), Version 1.1
+# - Common Public License, Version 1.0
+# - Eclipse Public License, Version 1.0
+# - GNU General Public License (GPL), Version 2 with classpath exception
+# - GNU Lesser General Public License (LGPL)
+# - GNU Lesser General Public License (LGPL), Version 2.1
+# - ICU License
+# - MIT License
+# - New BSD License
+# - Public Domain License
+#-------------------------------------------------------------------------------
+# Please fill the missing licenses for dependencies :
+#
+#
+#Thu Dec 13 13:41:05 CET 2012
+asm--asm--3.1=BSD License
+javax.servlet--servlet-api--2.5=Common Development And Distribution License (CDDL), Version 1.0
+org.codehaus.jettison--jettison--1.3=The Apache Software License, Version 2.0
+org.osgi--org.osgi.compendium--4.1.0=The Apache Software License, Version 2.0
+org.osgi--org.osgi.core--4.1.0=The Apache Software License, Version 2.0
+xerces--xercesImpl--2.7.1=The Apache Software License, Version 2.0

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ece56722/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/DefaultApplication.java
----------------------------------------------------------------------
diff --git a/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/DefaultApplication.java b/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/DefaultApplication.java
new file mode 100644
index 0000000..6d1c292
--- /dev/null
+++ b/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/DefaultApplication.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.stanbol.commons.web.base.jersey;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.ws.rs.core.Application;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Define the list of available resources and providers to be used by the Stanbol JAX-RS Endpoint.
+ */
+public class DefaultApplication extends Application {
+
+    @SuppressWarnings("unused")
+    private static final Logger log = LoggerFactory.getLogger(DefaultApplication.class);
+
+    protected final Set<Class<?>> contributedClasses = new HashSet<Class<?>>();
+
+    protected final Set<Object> contributedSingletons = new HashSet<Object>();
+
+
+    @Override
+    public Set<Class<?>> getClasses() {
+        Set<Class<?>> classes = new HashSet<Class<?>>();
+        // resources contributed buy other bundles
+        classes.addAll(contributedClasses);
+        //TODO check if clerezza rdf.jaxrs prvoder fits the purpose
+        // message body writers, hard-coded for now
+        //classes.add(GraphWriter.class);
+        //classes.add(JenaModelWriter.class);
+        //classes.add(ResultSetWriter.class);
+        return classes;
+    }
+
+    @Override
+    public Set<Object> getSingletons() {
+        Set<Object> singletons = new HashSet<Object>();
+        singletons.addAll(contributedSingletons);
+        return singletons;
+    }
+
+    public void contributeClasses(Set<Class<?>> classes) {
+        contributedClasses.addAll(classes);
+    }
+
+    public void contributeSingletons(Set<Object> singletons) {
+        contributedSingletons.addAll(singletons);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ece56722/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
----------------------------------------------------------------------
diff --git a/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java b/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
new file mode 100644
index 0000000..e35bff0
--- /dev/null
+++ b/jaxrs.whiteboard.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
@@ -0,0 +1,169 @@
+/*
+ * 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.stanbol.commons.web.base.jersey;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Dictionary;
+import java.util.HashSet;
+import java.util.Hashtable;
+import java.util.List;
+import java.util.Set;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.http.HttpService;
+import org.osgi.service.http.NamespaceException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+import org.apache.felix.scr.annotations.References;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.servlet.ServletContainer;
+
+/**
+ * Jersey-based RESTful endpoint for the Stanbol Enhancer engines and store.
+ * <p>
+ * This OSGi component serves as a bridge between the OSGi context and the Servlet context available to JAX-RS
+ * resources.
+ */
+@Component(immediate = true, metatype = true)
+@References({
+    @Reference(name="component", referenceInterface=Object.class, 
+        target="(javax.ws.rs=true)", 
+		cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, 
+        policy=ReferencePolicy.DYNAMIC)})
+public class JerseyEndpoint {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Property(value = "/")
+    public static final String ALIAS_PROPERTY = "org.apache.stanbol.commons.web.alias";
+
+    @Property(value = "/static")
+    public static final String STATIC_RESOURCES_URL_ROOT_PROPERTY = "org.apache.stanbol.commons.web.static.url";
+    
+    @Reference
+    HttpService httpService;
+
+    protected ComponentContext componentContext;
+
+    protected ServletContext servletContext;
+
+
+    protected final List<String> registeredAliases = new ArrayList<String>();
+
+    protected Set<String> exposedHeaders;
+    private Set<Object> components = new HashSet<Object>();
+
+    public Dictionary<String,String> getInitParams() {
+        Dictionary<String,String> initParams = new Hashtable<String,String>();
+        // make jersey automatically turn resources into Viewable models and
+        // hence lookup matching freemarker templates
+        initParams.put("com.sun.jersey.config.feature.ImplicitViewables", "true");
+        return initParams;
+    }
+
+    @Activate
+    protected void activate(ComponentContext ctx) throws IOException,
+                                                 ServletException,
+                                                 NamespaceException,
+                                                 ConfigurationException {
+        componentContext = ctx;
+        initJersey();
+        
+    }
+
+    /** Initialize the Jersey subsystem */
+    private synchronized void initJersey() throws NamespaceException, ServletException {
+        if (componentContext == null) {
+            //we have not yet been activated
+            return;
+        }
+        //end of STANBOL-1073 work around
+        if (componentContext == null) {
+            log.debug(" ... can not init Jersey Endpoint - Component not yet activated!");
+            //throw new IllegalStateException("Null ComponentContext, not activated?");
+            return;
+        }
+
+        shutdownJersey();
+
+        log.info("(Re)initializing the Stanbol Jersey subsystem");
+
+        // register all the JAX-RS resources into a a JAX-RS application and bind it to a configurable URL
+        // prefix
+        DefaultApplication app = new DefaultApplication();
+        String applicationAlias = (String) componentContext.getProperties().get(ALIAS_PROPERTY);
+
+        app.contributeSingletons(components);
+
+        // bind the aggregate JAX-RS application to a dedicated servlet
+        ServletContainer container = new ServletContainer(
+                ResourceConfig.forApplication(app));
+        httpService.registerServlet(applicationAlias, container, getInitParams(), null);
+        registeredAliases.add(applicationAlias);
+
+        // forward the main Stanbol OSGi runtime context so that JAX-RS resources can lookup arbitrary
+        // services
+        servletContext = container.getServletContext();
+        servletContext.setAttribute(BundleContext.class.getName(), componentContext.getBundleContext());
+        log.info("JerseyEndpoint servlet registered at {}", applicationAlias);
+    }
+
+    /** Shutdown Jersey, if there's anything to do */
+    private synchronized void shutdownJersey() {
+        log.debug("Unregistering aliases {}", registeredAliases);
+        for (String alias : registeredAliases) {
+            httpService.unregister(alias);
+        }
+        registeredAliases.clear();
+    }
+
+    @Deactivate
+    protected void deactivate(ComponentContext ctx) {
+        shutdownJersey();
+        servletContext = null;
+        componentContext = null;
+    }
+    
+    protected void bindComponent(Object component) throws IOException,
+                                                          ServletException,
+                                                          NamespaceException  {
+        components.add(component);
+        initJersey();
+    }
+
+    protected void unbindComponent(Object component) throws IOException,
+                                                          ServletException,
+                                                          NamespaceException  {
+        components.remove(component);
+        initJersey();
+    }    
+}

http://git-wip-us.apache.org/repos/asf/clerezza/blob/ece56722/jaxrs.whiteboard.jersey/src/main/resources/OSGI-INF/metatype/metatype.properties
----------------------------------------------------------------------
diff --git a/jaxrs.whiteboard.jersey/src/main/resources/OSGI-INF/metatype/metatype.properties b/jaxrs.whiteboard.jersey/src/main/resources/OSGI-INF/metatype/metatype.properties
new file mode 100644
index 0000000..2ea7a1a
--- /dev/null
+++ b/jaxrs.whiteboard.jersey/src/main/resources/OSGI-INF/metatype/metatype.properties
@@ -0,0 +1,34 @@
+# 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.
+
+#===============================================================================
+#Properties and Options used to configure 
+#===============================================================================
+
+org.apache.stanbol.commons.web.base.jersey.JerseyEndpoint.name=Apache Stanbol Web Application
+org.apache.stanbol.commons.web.base.jersey.JerseyEndpoint.description=The main Apache Stanbol Web Endpoint where all the different modules (Enhancer, Entityhub, ...) register itself.
+
+
+org.apache.stanbol.commons.web.alias.name=Alias
+org.apache.stanbol.commons.web.alias.description=The Alias for the Stanbol Web Application
+
+org.apache.stanbol.commons.web.static.url.name=Static
+org.apache.stanbol.commons.web.static.url.description=The path to the static resources
+
+org.apache.stanbol.commons.web.cors.origin.name=CORS origins
+org.apache.stanbol.commons.web.cors.origin.description=The CORS origins: This list defines the hosts content returned by Stanbol supports multi-site-scripting. Add '*' to allow any domain. Note: As soon as '*' is added all the other restrictions are ignored.
+
+org.apache.stanbol.commons.web.cors.access_control_expose_headers.name=CORS Access-Control-Expose-Headers
+org.apache.stanbol.commons.web.cors.access_control_expose_headers.description=This is a list containing the headers whose values to be accessed through the XMLHttpRequest instance in Java Script.   
\ No newline at end of file