Author: reto
Date: Thu Nov 29 07:33:51 2012
New Revision: 1415064

URL: http://svn.apache.org/viewvc?rev=1415064&view=rev
Log:
STANBOL-822:
- Extracted jersey specific parts to core.jersey
- Updates jersey to 1.15
- Removed dependendency embedding in ontologies and rule (second part of the 
commit)

Added:
    stanbol/trunk/commons/web/base.jersey/
    stanbol/trunk/commons/web/base.jersey/pom.xml
    stanbol/trunk/commons/web/base.jersey/src/
    stanbol/trunk/commons/web/base.jersey/src/license/
    stanbol/trunk/commons/web/base.jersey/src/license/THIRD-PARTY.properties
    stanbol/trunk/commons/web/base.jersey/src/main/
    stanbol/trunk/commons/web/base.jersey/src/main/java/
    stanbol/trunk/commons/web/base.jersey/src/main/java/org/
    stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/
    stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/
    
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/
    
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/
    
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/
    
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/
    
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java

Added: stanbol/trunk/commons/web/base.jersey/pom.xml
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/base.jersey/pom.xml?rev=1415064&view=auto
==============================================================================
--- stanbol/trunk/commons/web/base.jersey/pom.xml (added)
+++ stanbol/trunk/commons/web/base.jersey/pom.xml Thu Nov 29 07:33:51 2012
@@ -0,0 +1,161 @@
+<?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.stanbol</groupId>
+    <artifactId>stanbol-parent</artifactId>
+    <version>2-SNAPSHOT</version>
+    <relativePath>../../../parent</relativePath>
+  </parent>
+
+  <groupId>org.apache.stanbol</groupId>
+  <artifactId>org.apache.stanbol.commons.web.base.jersey</artifactId>
+  <version>0.10.1-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+
+  <name>Apache Stanbol Commons Web Base Jersey</name>
+  <description>Stanbol Jersey based implementation of HTTP application 
bundle.</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>
+    
+  <scm>
+    <connection>
+      
scm:svn:http://svn.apache.org/repos/asf/stanbol/trunk/commons/web/base.jersey
+    </connection>
+    <developerConnection>
+      
scm:svn:https://svn.apache.org/repos/asf/stanbol/trunk/commons/web/base.jersey
+    </developerConnection>
+    <url>http://stanbol.apache.org/</url>
+  </scm>
+
+  <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>
+            <Export-Package>
+              
org.apache.stanbol.commons.web.base.jersey;version=${project.version}
+            </Export-Package>
+            <Import-Package>
+              !javax.servlet.jsp.*,
+              !org.apache.tools.*,
+              !org.mortbay.*,
+              !org.apache.xpath.*,
+              !org.jdom.*,
+              !org.jaxen.*,
+              !org.apache.xml.*,
+              !org.mozilla.javascript.*,
+              !org.python.*,
+              !org.dom4j.*,
+              !org.apache.log.*,
+              !com.sun.org.apache.*,
+              !javax.el,
+              !org.slf4j.spi,
+              !org.zeroturnaround.javarebel,
+              org.apache.stanbol.commons.web.base,
+              org.apache.stanbol.commons.web.base.*,
+              *
+            </Import-Package>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+      <dependency>
+      <groupId>org.apache.stanbol</groupId>
+      <artifactId>org.apache.stanbol.commons.web.base</artifactId>
+      <version>0.10.1-SNAPSHOT</version>
+    </dependency>
+    
+    <!-- Jersey -->
+<dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-server</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-servlet</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-core</artifactId>
+    </dependency>
+    <!-- <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-json</artifactId>
+    </dependency>  -->
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+    </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>
+
+    <!-- Jetty -->
+    <dependency>
+      <groupId>org.mortbay.jetty</groupId>
+      <artifactId>jetty</artifactId>
+      <scope>provided</scope>
+    </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>

Added: stanbol/trunk/commons/web/base.jersey/src/license/THIRD-PARTY.properties
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/base.jersey/src/license/THIRD-PARTY.properties?rev=1415064&view=auto
==============================================================================
--- stanbol/trunk/commons/web/base.jersey/src/license/THIRD-PARTY.properties 
(added)
+++ stanbol/trunk/commons/web/base.jersey/src/license/THIRD-PARTY.properties 
Thu Nov 29 07:33:51 2012
@@ -0,0 +1,28 @@
+# Generated by org.codehaus.mojo.license.AddThirdPartyMojo
+#-------------------------------------------------------------------------------
+# Already used licenses in project :
+# - Apache License
+# - Apache License Version 2.0
+# - Apache Software License - Version 2.0
+# - BSD
+# - BSD-style license
+# - CDDL 1.1
+# - Common Development and Distribution License (CDDL) v1.0
+# - Common Public License Version 1.0
+# - Eclipse Public License - Version 1.0
+# - GPL2 w/ CPE
+# - ICU License
+# - MIT License
+# - The Apache Software License, Version 2.0
+# - The Apache Software License, Version 2.0
+#-------------------------------------------------------------------------------
+# Please fill the missing licenses for dependencies :
+#
+#
+#Thu Feb 16 11:15:59 CET 2012
+asm--asm--3.1=BSD License
+javax.servlet--servlet-api--2.4=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

Added: 
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java?rev=1415064&view=auto
==============================================================================
--- 
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
 (added)
+++ 
stanbol/trunk/commons/web/base.jersey/src/main/java/org/apache/stanbol/commons/web/base/jersey/JerseyEndpoint.java
 Thu Nov 29 07:33:51 2012
@@ -0,0 +1,255 @@
+/*
+ * 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.Arrays;
+import java.util.Collections;
+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.apache.stanbol.commons.web.base.resource.BaseStanbolResource;
+import org.osgi.framework.Bundle;
+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 com.sun.jersey.spi.container.servlet.ServletContainer;
+import org.apache.stanbol.commons.web.base.BundleHttpContext;
+import org.apache.stanbol.commons.web.base.DefaultApplication;
+import org.apache.stanbol.commons.web.base.LinkResource;
+import org.apache.stanbol.commons.web.base.NavigationLink;
+import org.apache.stanbol.commons.web.base.ScriptResource;
+import org.apache.stanbol.commons.web.base.WebFragment;
+
+/**
+ * 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)
+@Reference(name = "webFragment", referenceInterface = WebFragment.class, 
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";
+
+    /**
+     * The origins allowed for multi-host requests
+     */
+    @Property(cardinality = 100, value = {"*"})
+    public static final String CORS_ORIGIN = 
"org.apache.stanbol.commons.web.cors.origin";
+
+    @Property(cardinality = 100, value = {"Location"})
+    public static final String CORS_ACCESS_CONTROL_EXPOSE_HEADERS = 
"org.apache.stanbol.commons.web.cors.access_control_expose_headers";
+
+    @Reference
+    HttpService httpService;
+
+    protected ComponentContext componentContext;
+
+    protected ServletContext servletContext;
+
+    protected final List<WebFragment> webFragments = new 
ArrayList<WebFragment>();
+
+    protected final List<String> registeredAliases = new ArrayList<String>();
+
+    protected Set<String> corsOrigins;
+
+    protected Set<String> exposedHeaders;
+
+    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;
+        // init corsOrigins
+        Object values = componentContext.getProperties().get(CORS_ORIGIN);
+        if (values instanceof String && !((String) values).isEmpty()) {
+            corsOrigins = Collections.singleton((String) values);
+        } else if (values instanceof String[]) {
+            corsOrigins = new HashSet<String>(Arrays.asList((String[]) 
values));
+        } else if (values instanceof Iterable<?>) {
+            corsOrigins = new HashSet<String>();
+            for (Object value : (Iterable<?>) values) {
+                if (value != null && !value.toString().isEmpty()) {
+                    corsOrigins.add(value.toString());
+                }
+            }
+        } else {
+            throw new ConfigurationException(CORS_ORIGIN,
+                    "CORS origin(s) MUST be a String, String[], 
Iterable<String> (value:" + values + ")");
+        }
+
+        // parse headers to be exposed
+        values = 
componentContext.getProperties().get(CORS_ACCESS_CONTROL_EXPOSE_HEADERS);
+        if (values instanceof String && !((String) values).isEmpty()) {
+            exposedHeaders = Collections.singleton((String) values);
+        } else if (values instanceof String[]) {
+            exposedHeaders = new HashSet<String>(Arrays.asList((String[]) 
values));
+        } else if (values instanceof Iterable<?>) {
+            exposedHeaders = new HashSet<String>();
+            for (Object value : (Iterable<?>) values) {
+                if (value != null && !value.toString().isEmpty()) {
+                    exposedHeaders.add(value.toString());
+                }
+            }
+        } else {
+            exposedHeaders = new HashSet<String>();
+        }
+
+        if (!webFragments.isEmpty()) {
+            initJersey();
+        }
+    }
+
+    /** Initialize the Jersey subsystem */
+    private synchronized void initJersey() throws NamespaceException, 
ServletException {
+
+        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 staticUrlRoot = (String) componentContext.getProperties().get(
+            STATIC_RESOURCES_URL_ROOT_PROPERTY);
+        String applicationAlias = (String) 
componentContext.getProperties().get(ALIAS_PROPERTY);
+
+        // incrementally contribute fragment resources
+        List<LinkResource> linkResources = new ArrayList<LinkResource>();
+        List<ScriptResource> scriptResources = new ArrayList<ScriptResource>();
+        List<NavigationLink> navigationLinks = new ArrayList<NavigationLink>();
+        for (WebFragment fragment : webFragments) {
+            log.debug("Registering web fragment '{}' into jaxrs application", 
fragment.getName());
+            linkResources.addAll(fragment.getLinkResources());
+            scriptResources.addAll(fragment.getScriptResources());
+            navigationLinks.addAll(fragment.getNavigationLinks());
+            app.contributeClasses(fragment.getJaxrsResourceClasses());
+            app.contributeSingletons(fragment.getJaxrsResourceSingletons());
+            String staticPath = fragment.getStaticResourceClassPath();
+            if (staticPath != null) {
+                String resourceAlias = (applicationAlias.endsWith("/") ? 
applicationAlias.substring(0,
+                    applicationAlias.length() - 1) : applicationAlias)
+                                       + staticUrlRoot
+                                       + '/'
+                                       + fragment.getName();
+                httpService.registerResources(resourceAlias, staticPath, new 
BundleHttpContext(fragment));
+                registeredAliases.add(resourceAlias);
+            }
+        }
+        Collections.sort(linkResources);
+        Collections.sort(scriptResources);
+        Collections.sort(navigationLinks);
+
+        // bind the aggregate JAX-RS application to a dedicated servlet
+        ServletContainer container = new ServletContainer(app);
+        Bundle appBundle = componentContext.getBundleContext().getBundle();
+        httpService.registerServlet(applicationAlias, container, 
getInitParams(), new BundleHttpContext(
+                appBundle));
+        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());
+        servletContext.setAttribute(BaseStanbolResource.ROOT_URL, 
applicationAlias);
+        
servletContext.setAttribute(BaseStanbolResource.STATIC_RESOURCES_ROOT_URL, 
staticUrlRoot);
+        servletContext.setAttribute(BaseStanbolResource.LINK_RESOURCES, 
linkResources);
+        servletContext.setAttribute(BaseStanbolResource.SCRIPT_RESOURCES, 
scriptResources);
+        servletContext.setAttribute(BaseStanbolResource.NAVIGATION_LINKS, 
navigationLinks);
+        servletContext.setAttribute(CORS_ORIGIN, corsOrigins);
+        servletContext.setAttribute(CORS_ACCESS_CONTROL_EXPOSE_HEADERS, 
exposedHeaders);
+
+        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 bindWebFragment(WebFragment webFragment) throws IOException,
+                                                           ServletException,
+                                                           NamespaceException {
+        // TODO: support some ordering for jax-rs resource and template 
overrides?
+        webFragments.add(webFragment);
+        initJersey();
+    }
+
+    protected void unbindWebFragment(WebFragment webFragment) throws 
IOException,
+                                                             ServletException,
+                                                             
NamespaceException {
+        webFragments.remove(webFragment);
+        initJersey();
+    }
+
+    public List<WebFragment> getWebFragments() {
+        return webFragments;
+    }
+}


Reply via email to