You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by re...@apache.org on 2012/12/07 11:51:17 UTC

svn commit: r1418272 - in /stanbol/trunk/commons/web/resources: ./ src/ src/license/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/stanbol/ src/main/java/org/apache/stanbol/commons/ src/main/java/org/apa...

Author: reto
Date: Fri Dec  7 10:51:14 2012
New Revision: 1418272

URL: http://svn.apache.org/viewvc?rev=1418272&view=rev
Log:
STANBOL-828: added bundle to serve META-INF/resources files

Added:
    stanbol/trunk/commons/web/resources/   (with props)
    stanbol/trunk/commons/web/resources/pom.xml
    stanbol/trunk/commons/web/resources/src/
    stanbol/trunk/commons/web/resources/src/license/
    stanbol/trunk/commons/web/resources/src/license/THIRD-PARTY.properties
    stanbol/trunk/commons/web/resources/src/main/
    stanbol/trunk/commons/web/resources/src/main/java/
    stanbol/trunk/commons/web/resources/src/main/java/org/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/
    stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/ResourceServingFilter.java
    stanbol/trunk/commons/web/resources/src/main/resources/
    stanbol/trunk/commons/web/resources/src/main/resources/META-INF/
    stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/
    stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/
    stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/test.html
    stanbol/trunk/commons/web/resources/src/test/
    stanbol/trunk/commons/web/resources/src/test/java/

Propchange: stanbol/trunk/commons/web/resources/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Fri Dec  7 10:51:14 2012
@@ -0,0 +1,5 @@
+.classpath
+bin
+.settings
+.project
+target

Added: stanbol/trunk/commons/web/resources/pom.xml
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/resources/pom.xml?rev=1418272&view=auto
==============================================================================
--- stanbol/trunk/commons/web/resources/pom.xml (added)
+++ stanbol/trunk/commons/web/resources/pom.xml Fri Dec  7 10:51:14 2012
@@ -0,0 +1,94 @@
+<?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.resources</artifactId>
+  <version>0.10.1-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+
+  <name>Apache Stanbol Commons Web Resources</name>
+  <description>Service serving the resources in META-INF/resources.</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/resources
+    </connection>
+    <developerConnection>
+      scm:svn:https://svn.apache.org/repos/asf/stanbol/trunk/commons/web/resources
+    </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>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>servlet-api</artifactId>
+    </dependency>
+
+    <!-- generic tax -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-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>
+  </dependencies>
+</project>

Added: stanbol/trunk/commons/web/resources/src/license/THIRD-PARTY.properties
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/resources/src/license/THIRD-PARTY.properties?rev=1418272&view=auto
==============================================================================
--- stanbol/trunk/commons/web/resources/src/license/THIRD-PARTY.properties (added)
+++ stanbol/trunk/commons/web/resources/src/license/THIRD-PARTY.properties Fri Dec  7 10:51:14 2012
@@ -0,0 +1,2 @@
+javax.servlet--servlet-api--2.5=Common Development And Distribution License (CDDL), Version 1.0
+

Added: stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/ResourceServingFilter.java
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/ResourceServingFilter.java?rev=1418272&view=auto
==============================================================================
--- stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/ResourceServingFilter.java (added)
+++ stanbol/trunk/commons/web/resources/src/main/java/org/apache/stanbol/commons/web/resources/ResourceServingFilter.java Fri Dec  7 10:51:14 2012
@@ -0,0 +1,171 @@
+/*
+ * Copyright 2012 The Apache Software Foundation.
+ *
+ * Licensed 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.resources;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+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.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.service.component.ComponentContext;
+
+/**
+ * Serves resource in META-INF/services of any active bundle
+ */
+@Component
+@Service(Filter.class)
+@Properties(value = {
+    @Property(name = "pattern", value = ".*"),
+    @Property(name = "service.ranking", intValue = 500)
+})
+public class ResourceServingFilter implements Filter, BundleListener {
+
+    public static final String RESOURCE_PREFIX = "META-INF/resources";
+    public static final int RESOURCE_PREFIX_LENGTH = RESOURCE_PREFIX.length();
+    private Set<Bundle> resourceProvidingBundles;
+    private Map<String, Bundle> path2Bundle;
+
+    @Activate
+    protected void activate(final ComponentContext context) {
+        resourceProvidingBundles = new HashSet<Bundle>();
+        path2Bundle = new HashMap<String, Bundle>();
+        final Bundle[] registeredBundles = context.getBundleContext().getBundles();
+        for (int i = 0; i < registeredBundles.length; i++) {
+            if (registeredBundles[i].getState() == Bundle.ACTIVE) {
+                registerResources(registeredBundles[i]);
+            }
+        }
+        context.getBundleContext().addBundleListener(this);
+    }
+
+    @Deactivate
+    protected void deactivate(final ComponentContext context) {
+        context.getBundleContext().removeBundleListener(this);
+        resourceProvidingBundles = null;
+        path2Bundle = null;
+
+    }
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+    }
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
+        doFilterHttp((HttpServletRequest) request, (HttpServletResponse) response, chain);
+    }
+
+    @Override
+    public void destroy() {
+    }
+
+    private synchronized void registerResources(Bundle bundle) {
+        //TODO maybe bundle.getLastModified() could be used for modification data in http-headers, and for if-modified since negotiation
+        registerResourcesWithPathPrefix(bundle, RESOURCE_PREFIX);
+    }
+
+    private void registerResourcesWithPathPrefix(Bundle bundle, String prefix) {
+        final Enumeration<String> resourceEnum = bundle.getEntryPaths(prefix);
+        if (resourceEnum != null && resourceEnum.hasMoreElements()) {
+            resourceProvidingBundles.add(bundle);
+            while (resourceEnum.hasMoreElements()) {
+                String resourcePath = resourceEnum.nextElement();
+                if (resourcePath.endsWith("/")) {
+                    registerResourcesWithPathPrefix(bundle, resourcePath);
+                } else {
+                    path2Bundle.put(resourcePath.substring(RESOURCE_PREFIX_LENGTH), bundle);
+                }
+            }
+        }
+    }
+    
+    
+    private void doFilterHttp(HttpServletRequest request,
+            HttpServletResponse response, FilterChain chain)
+            throws IOException, ServletException {
+        final String requestPath = request.getRequestURI();
+        if (path2Bundle.containsKey(requestPath)) {
+            if (request.getMethod().equals("GET") || request.getMethod().equals("HEAD")) {
+                Bundle resourceBundle = path2Bundle.get(requestPath);
+                URL url = resourceBundle.getEntry(RESOURCE_PREFIX + requestPath);
+                String mediaType = URLConnection.guessContentTypeFromName(url.getFile());
+                response.setContentType(mediaType);
+                //TODO can we get the length of a resource without 
+                //TODO handle caching related headers
+                if (!request.getMethod().equals("HEAD")) {
+                    OutputStream os = response.getOutputStream();
+                    byte[] ba = new byte[1024];
+                    InputStream is = url.openStream();
+                    int i = is.read(ba);
+                    while (i != -1) {
+                        os.write(ba, 0, i);
+                        i = is.read(ba);
+                    }
+                    os.flush();
+                }
+            } else {
+                //TODO handle OPTIONS
+                response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
+            }
+        } else {
+            chain.doFilter(request, response);
+        }
+    }
+
+    @Override
+    public void bundleChanged(BundleEvent event) {
+        final Bundle bundle = event.getBundle();
+        if (event.getType() == BundleEvent.STARTED) {
+            registerResources(bundle);
+        } else {
+            if (resourceProvidingBundles.contains(bundle)) {
+                synchronized (this) {
+                    Iterator<Map.Entry<String, Bundle>> entryIter = path2Bundle.entrySet().iterator();
+                    while (entryIter.hasNext()) {
+                        Map.Entry<String, Bundle> entry = entryIter.next();
+                        if (entry.getValue().equals(bundle)) {
+                            entryIter.remove();
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+}

Added: stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/test.html
URL: http://svn.apache.org/viewvc/stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/test.html?rev=1418272&view=auto
==============================================================================
--- stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/test.html (added)
+++ stanbol/trunk/commons/web/resources/src/main/resources/META-INF/resources/static-example/test.html Fri Dec  7 10:51:14 2012
@@ -0,0 +1,25 @@
+<!--
+Copyright 2012 The Apache Software Foundation.
+
+Licensed 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.
+-->
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>A Static html file</title>
+        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
+    </head>
+    <body>
+        <div>This shows that commons web resources is running</div>
+    </body>
+</html>