Author: rwesten
Date: Tue Nov 29 17:34:21 2011
New Revision: 1207989

URL: http://svn.apache.org/viewvc?rev=1207989&view=rev
Log:
Implementation of STANBOL-401

Adds a Servlet Filter for '.*' that registers it self as @Server. This is taken 
up by the felix.http.whiteboard.

It replaces all headers in the HttpServletRequest with values parsed by a query 
parameter "header_{header}"

It supports multiple header values as well as removing headers of  
"header_{header}" is parsed with no value.

It is not yet integrated in the Build Process and also not added to the Stanbol 
launchers. I will do that tomorrow.

Added:
    incubator/stanbol/trunk/commons/httpqueryheaders/   (with props)
    incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml   (with props)
    incubator/stanbol/trunk/commons/httpqueryheaders/src/
    incubator/stanbol/trunk/commons/httpqueryheaders/src/main/
    incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/
    incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/
    incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java
   (with props)
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java
   (with props)
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/
    
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java
   (with props)

Propchange: incubator/stanbol/trunk/commons/httpqueryheaders/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Nov 29 17:34:21 2011
@@ -0,0 +1,7 @@
+.settings
+
+.project
+
+.classpath
+
+target

Added: incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml?rev=1207989&view=auto
==============================================================================
--- incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml (added)
+++ incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml Tue Nov 29 
17:34:21 2011
@@ -0,0 +1,103 @@
+<?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>0.9.0-incubating-SNAPSHOT</version>
+    <relativePath>../../parent</relativePath>
+  </parent>
+
+  <groupId>org.apache.stanbol</groupId>
+  <artifactId>org.apache.stanbol.commons.httpqueryheaders</artifactId>
+  <packaging>bundle</packaging>
+
+  <name>Apache Stanbol Commons Web Home Page</name>
+  <description>Home page and common stylesheets for documenting HTTP 
endpoints.</description>
+<!--  <scm>
+    <connection>
+      
scm:svn:http://svn.apache.org/repos/asf/incubator/stanbol/trunk/commons/httpqueryheaders
+    </connection>
+    <developerConnection>
+      
scm:svn:https://svn.apache.org/repos/asf/incubator/stanbol/trunk/commons/httpqueryheaders
+    </developerConnection>
+    <url>http://incubator.apache.org/stanbol/</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.httpqueryheaders.*
+            </Export-Package>
+            <Private-Package>
+              org.apache.stanbol.commons.httpqueryheaders.impl
+            </Private-Package>
+            <Import-Package>*</Import-Package> -->
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+
+  <dependencies>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+    </dependency>
+    
+    <!-- Jersey, servlet and freemarker -->
+    <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>
+
+  </dependencies>
+
+</project>

Propchange: incubator/stanbol/trunk/commons/httpqueryheaders/pom.xml
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java?rev=1207989&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java
 (added)
+++ 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java
 Tue Nov 29 17:34:21 2011
@@ -0,0 +1,9 @@
+package org.apache.stanbol.commons.httpqueryheaders;
+
+public final class Constants {
+    
+    private Constants(){};
+    
+    public static final String HEARDER_PREFIX = "header_";
+
+}

Propchange: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/Constants.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java?rev=1207989&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java
 (added)
+++ 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java
 Tue Nov 29 17:34:21 2011
@@ -0,0 +1,121 @@
+package org.apache.stanbol.commons.httpqueryheaders;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletRequestWrapper;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Wrapps an {@link HttpServletRequest} and allows to override headers
+ * @author Rupert Westenthaler
+ *
+ */
+public class OverwriteableHeaderHttpServletRequest extends 
HttpServletRequestWrapper {
+
+    Logger log = 
LoggerFactory.getLogger(OverwriteableHeaderHttpServletRequest.class);
+    Map<String,List<String>> overriddenHeaders = new 
HashMap<String,List<String>>();
+    
+    public OverwriteableHeaderHttpServletRequest(HttpServletRequest request) {
+        super(request);
+    }
+    
+    
+    public Collection<String> setHeader(String header, String...values){
+        Enumeration<String> e = getHeaders(header);
+        Collection<String> oldValues;
+        if(e != null && e.hasMoreElements()){
+            oldValues = new ArrayList<String>(3);
+            while(e.hasMoreElements()){
+                oldValues.add(e.nextElement());
+            }
+        } else {
+            oldValues = null;
+        }
+        List<String> headerValues;
+        if(values == null || values.length == 0){
+            headerValues = Collections.emptyList();
+        } else if(values.length>1){
+            headerValues = new ArrayList<String>(values.length);
+            for(String value : values){
+                if(value != null && !value.isEmpty()){
+                    headerValues.add(value);
+                }
+            }
+        } else {
+            if(values[0] != null && !values[0].isEmpty()){
+                headerValues = Collections.singletonList(values[0]);
+            } else { //no value found
+                return null; 
+            }
+        }
+        if(headerValues.isEmpty()){
+            if(oldValues != null){
+                log.debug("Remove Header {} (was '{}')",
+                    header,oldValues);
+                overriddenHeaders.put(header, null);
+            } //else header not there -> nothing todo
+        } else {
+            if(oldValues != null){
+                log.debug("Add Header {}={}",
+                    new Object[]{header,headerValues,oldValues});
+            } else {
+                log.debug("{} Header {}={} (was '{}')",
+                    new Object[]{header,headerValues,oldValues});
+            }
+            overriddenHeaders.put(header, headerValues);
+        }
+        return oldValues;
+    }
+    public boolean isHeaderOverridden(String name){
+        return overriddenHeaders.containsKey(name);
+    }
+    @Override
+    public String getHeader(String name) {
+        if(isHeaderOverridden(name)){
+            List<String> values = overriddenHeaders.get(name);
+            return values == null || values.isEmpty() ? null : values.get(0);
+        } else { //not overridden
+            return super.getHeader(name);
+        }
+    }
+    @Override
+    public Enumeration getHeaders(String name) {
+        if(isHeaderOverridden(name)){
+            List<String> values = overriddenHeaders.get(name);
+            return values == null ? null : Collections.enumeration(values);
+        } else {
+            return super.getHeaders(name);
+        }
+    }
+    
+    @Override
+    public Enumeration getHeaderNames() {
+        Set<String> names = new HashSet<String>();
+        Enumeration<String> e = super.getHeaderNames();
+        if(e != null){
+            while(e.hasMoreElements()){
+                names.add(e.nextElement());
+            }
+        }
+        for(Entry<String,List<String>> entry : overriddenHeaders.entrySet()){
+            if(entry.getValue() == null){
+                names.remove(entry.getKey());
+            } else {
+                names.add(entry.getKey());
+            }
+        }
+        return Collections.enumeration(names);
+    }
+}

Propchange: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/OverwriteableHeaderHttpServletRequest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java
URL: 
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java?rev=1207989&view=auto
==============================================================================
--- 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java
 (added)
+++ 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java
 Tue Nov 29 17:34:21 2011
@@ -0,0 +1,69 @@
+package org.apache.stanbol.commons.httpqueryheaders.impl;
+
+import static 
org.apache.stanbol.commons.httpqueryheaders.Constants.HEARDER_PREFIX;
+
+import java.io.IOException;
+import java.util.Enumeration;
+
+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 org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import 
org.apache.stanbol.commons.httpqueryheaders.OverwriteableHeaderHttpServletRequest;
+
+@Component(immediate=true)
+@Service(Filter.class)
+@Property(name="pattern",value=".*")
+public class QueryHeadersFilter implements Filter {
+    
+    @Override
+    public void destroy() {
+    }
+
+    @Override
+    public void doFilter(ServletRequest request, ServletResponse response, 
FilterChain chain) throws IOException,
+                                                                               
              ServletException {
+        HttpServletRequest httpRequest;
+        try {
+            httpRequest = (HttpServletRequest) request;
+        } catch (ClassCastException e) {
+            // no Http request -> ignore
+            chain.doFilter(request, response);
+            return;
+        }
+        OverwriteableHeaderHttpServletRequest wrapped = null;
+        Enumeration<String> paramNames = request.getParameterNames();
+        while(paramNames.hasMoreElements()) {
+            String param = paramNames.nextElement();
+            if(param != null && param.startsWith(HEARDER_PREFIX) && 
param.length() > HEARDER_PREFIX.length()+1){
+                String header = param.substring(HEARDER_PREFIX.length());
+                String[] values = request.getParameterValues(param);
+                if(values != null && values.length > 0){
+                    if(wrapped == null ){ //lazzy initialisation 
+                        wrapped = new 
OverwriteableHeaderHttpServletRequest(httpRequest);
+                    }
+                    wrapped.setHeader(header, values);
+                }
+            }
+        }
+        if(wrapped != null){
+            chain.doFilter(wrapped, response);
+        } else {
+            chain.doFilter(request, response);
+        }
+    }
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        // Get properties parsed to the Filter
+        //filterConfig
+    }
+
+}

Propchange: 
incubator/stanbol/trunk/commons/httpqueryheaders/src/main/java/org/apache/stanbol/commons/httpqueryheaders/impl/QueryHeadersFilter.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to