Author: mir
Date: Wed Oct 13 12:53:50 2010
New Revision: 1022084

URL: http://svn.apache.org/viewvc?rev=1022084&view=rev
Log:
CLEREZZA-324: implemented filter which sets the default accet header if the 
request has no accept header or if only the wildcard media type */* is set as 
acceptable media type

Added:
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/pom.xml
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/AcceptAddingRequest.java
    
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/DefaultAcceptHeaderSetter.java

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/pom.xml
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/pom.xml?rev=1022084&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/pom.xml
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/pom.xml
 Wed Oct 13 12:53:50 2010
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
+       <modelVersion>4.0.0</modelVersion>
+       <parent>
+               <artifactId>org.apache.clerezza.parent</artifactId>
+               <groupId>org.apache.clerezza</groupId>
+               <version>0.2-incubating-SNAPSHOT</version>
+       </parent>
+       <groupId>org.apache.clerezza</groupId>
+       
<artifactId>org.apache.clerezza.platform.defaultacceptheader</artifactId>
+       <packaging>bundle</packaging>
+       <version>0.1-incubating-SNAPSHOT</version>
+       <name>Clerezza - Default accept header setter</name>
+       <description>Provides a wrhapi filter that add a accept-header if the
+               request does not have one or if it has only the wildcard media 
type */*
+               as acceptable type.
+       </description>
+       <dependencies>
+               <dependency>
+                       <groupId>org.wymiwyg</groupId>
+                       <artifactId>wrhapi</artifactId>
+               </dependency>
+               <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>
\ No newline at end of file

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/AcceptAddingRequest.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/AcceptAddingRequest.java?rev=1022084&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/AcceptAddingRequest.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/AcceptAddingRequest.java
 Wed Oct 13 12:53:50 2010
@@ -0,0 +1,72 @@
+/*
+ * 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.clerezza.platform.defaultacceptheader;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import org.wymiwyg.wrhapi.HandlerException;
+import org.wymiwyg.wrhapi.HeaderName;
+import org.wymiwyg.wrhapi.Request;
+import org.wymiwyg.wrhapi.util.RequestWrapper;
+
+/**
+ * Adds a specified accept-header to the wrapped request.
+ *
+ * @author mir
+ */
+class AcceptAddingRequest extends RequestWrapper {
+
+       private String[] acceptHeader;
+
+       /**
+        * Constructs a <code>AcceptAddingRequest</code> wrapping the specified
+        * request. It will appear the same as the wrapped request, except its
+        * accept header will be the specified acceptHeader.
+        *
+        * @param request
+        * @param acceptHeader
+        */
+       public AcceptAddingRequest(Request request, String[] acceptHeader) {
+               super(request);
+               this.acceptHeader = acceptHeader;
+       }
+
+       @Override
+       public String[] getHeaderValues(HeaderName headerName) throws 
HandlerException {
+               if (headerName.equals(HeaderName.ACCEPT)) {
+                       return acceptHeader;
+               } else {
+                       return super.getHeaderValues(headerName);
+               }
+       }
+
+       @Override
+       public Set<HeaderName> getHeaderNames() throws HandlerException {
+               Set<HeaderName> headerNames = super.getHeaderNames();
+               if (headerNames.contains(HeaderName.ACCEPT)) {
+                       return headerNames;
+               }
+               final Set<HeaderName> headerNamesWithAcceptHeader =
+                               new HashSet<HeaderName>(headerNames);
+               headerNamesWithAcceptHeader.add(HeaderName.ACCEPT);             
+               return Collections.unmodifiableSet(headerNamesWithAcceptHeader);
+       }
+
+}

Added: 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/DefaultAcceptHeaderSetter.java
URL: 
http://svn.apache.org/viewvc/incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/DefaultAcceptHeaderSetter.java?rev=1022084&view=auto
==============================================================================
--- 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/DefaultAcceptHeaderSetter.java
 (added)
+++ 
incubator/clerezza/trunk/org.apache.clerezza.parent/org.apache.clerezza.platform.defaultacceptheader/src/main/java/org/apache/clerezza/platform/defaultacceptheader/DefaultAcceptHeaderSetter.java
 Wed Oct 13 12:53:50 2010
@@ -0,0 +1,93 @@
+/*
+ * 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.clerezza.platform.defaultacceptheader;
+
+import java.util.Iterator;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Service;
+import org.osgi.service.component.ComponentContext;
+import org.wymiwyg.wrhapi.Handler;
+import org.wymiwyg.wrhapi.HandlerException;
+import org.wymiwyg.wrhapi.Request;
+import org.wymiwyg.wrhapi.Response;
+import org.wymiwyg.wrhapi.filter.Filter;
+import org.wymiwyg.wrhapi.util.AcceptHeaderEntry;
+import org.wymiwyg.wrhapi.util.EnhancedRequest;
+import org.wymiwyg.wrhapi.util.InvalidPatternException;
+import org.wymiwyg.wrhapi.util.MediaRange;
+
+/**
+ * This Filter sets the default accet header if the request has no accept
+ * header or if only the wildcard media type "*\/*" is set as acceptable media 
type.
+ * The default accept header can be configured over the OSGi configuration 
admin
+ * or conveniently over the Felix Web Console.
+ *
+ * @author mir
+ */
+...@component(metatype=true)
+...@service(Filter.class)
+public class DefaultAcceptHeaderSetter implements Filter {
+
+       @Property(value={"text/html", "application/xhtml+xml;q=.9", "*/*;q=.1"},
+       description="The default accept header used if a request does not have 
an " +
+                       "accept header.")
+       public static final String DEFAULT_ACCEPT_HEADER = 
"defaultAcceptHeader";
+
+       private String[] defaultAcceptHeader;
+       
+       private MediaRange wildcardRange;
+
+       public DefaultAcceptHeaderSetter() throws InvalidPatternException {
+               wildcardRange = new MediaRange("*/*");
+       }
+               
+
+       @Override
+       public void handle(Request request, Response response, Handler handler)
+                       throws HandlerException {
+               if (hasNoAcceptHeaderOrOnlyWildcard(request)) {
+                       handler.handle(new AcceptAddingRequest(request, 
defaultAcceptHeader),
+                                       response);                      
+               } else {
+                       handler.handle(request, response);              
+               }
+       }
+
+       private boolean hasNoAcceptHeaderOrOnlyWildcard(final Request request) 
throws HandlerException {
+               EnhancedRequest ehRequest = new EnhancedRequest(request);
+               Iterator<AcceptHeaderEntry> iter = ehRequest.getAccept();
+               if (!iter.hasNext()) {
+                       return true;
+               }
+               AcceptHeaderEntry entry = iter.next();
+               if (entry.getRange().equals(wildcardRange) && !iter.hasNext()) {
+                       return true;
+               }
+               return false;
+       }
+
+       protected void activate(ComponentContext context) throws Exception {
+               defaultAcceptHeader = (String[]) context.getProperties().
+                               get(DEFAULT_ACCEPT_HEADER);
+       }
+
+
+
+}


Reply via email to