sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jus...@apache.org
Subject svn commit: r1163675 - in /sling/trunk/contrib/extensions/security/src: main/java/org/apache/sling/security/impl/ReferrerFilter.java test/java/org/apache/sling/security/impl/ReferrerFilterTest.java
Date Wed, 31 Aug 2011 16:00:53 GMT
Author: justin
Date: Wed Aug 31 16:00:52 2011
New Revision: 1163675

URL: http://svn.apache.org/viewvc?rev=1163675&view=rev
Log:
SLING-2200 - adding a configuration printer to the referrer filter

Modified:
    sling/trunk/contrib/extensions/security/src/main/java/org/apache/sling/security/impl/ReferrerFilter.java
    sling/trunk/contrib/extensions/security/src/test/java/org/apache/sling/security/impl/ReferrerFilterTest.java

Modified: sling/trunk/contrib/extensions/security/src/main/java/org/apache/sling/security/impl/ReferrerFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/security/src/main/java/org/apache/sling/security/impl/ReferrerFilter.java?rev=1163675&r1=1163674&r2=1163675&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/security/src/main/java/org/apache/sling/security/impl/ReferrerFilter.java
(original)
+++ sling/trunk/contrib/extensions/security/src/main/java/org/apache/sling/security/impl/ReferrerFilter.java
Wed Aug 31 16:00:52 2011
@@ -17,6 +17,7 @@
 package org.apache.sling.security.impl;
 
 import java.io.IOException;
+import java.io.PrintWriter;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetAddress;
@@ -25,8 +26,10 @@ import java.net.NetworkInterface;
 import java.net.SocketException;
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Dictionary;
 import java.util.Enumeration;
 import java.util.HashSet;
+import java.util.Hashtable;
 import java.util.List;
 import java.util.Set;
 
@@ -44,6 +47,9 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.PropertyUnbounded;
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.commons.osgi.PropertiesUtil;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -81,6 +87,8 @@ public class ReferrerFilter implements F
     /** Methods to be filtered. */
     private String[] filterMethods;
 
+    private ServiceRegistration configPrinterRegistration;
+
     /**
      * Create a default list of referrers
      */
@@ -187,8 +195,28 @@ public class ReferrerFilter implements F
                 filterMethods[i] = filterMethods[i].toUpperCase();
             }
         }
+        this.configPrinterRegistration = registerConfigPrinter(ctx.getBundleContext());
+    }
+
+    protected void deactivate() {
+        this.configPrinterRegistration.unregister();
     }
 
+    private ServiceRegistration registerConfigPrinter(BundleContext bundleContext) {
+        final ConfigurationPrinter cfgPrinter = new ConfigurationPrinter();
+        final Dictionary<String, String> serviceProps = new Hashtable<String, String>();
+        serviceProps.put(Constants.SERVICE_DESCRIPTION,
+            "Apache Sling Referrer Filter Configuration Printer");
+        serviceProps.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
+        serviceProps.put("felix.webconsole.label", "slingreferrerfilter");
+        serviceProps.put("felix.webconsole.title", "Sling Referrer Filter");
+        serviceProps.put("felix.webconsole.configprinter.modes", "always");
+
+       return bundleContext.registerService(Object.class.getName(),
+                cfgPrinter, serviceProps);
+    }
+
+
     private boolean isModification(final HttpServletRequest req) {
         final String method = req.getMethod();
         if ( filterMethods != null ) {
@@ -317,4 +345,21 @@ public class ReferrerFilter implements F
     public void destroy() {
         // nothing to do
     }
+
+    public class ConfigurationPrinter {
+
+        /**
+         * Print out the allowedReferrers
+         * @see org.apache.felix.webconsole.ConfigurationPrinter#printConfiguration(java.io.PrintWriter)
+         */
+        @SuppressWarnings("unused")
+        public void printConfiguration(PrintWriter pw) {
+            pw.println("Current Apache Sling Referrer Filter Allowed Referrers:");
+            pw.println();
+            for (final URL url : allowedReferrers) {
+                pw.println(url.toString());
+            }
+        }
+
+    }
 }

Modified: sling/trunk/contrib/extensions/security/src/test/java/org/apache/sling/security/impl/ReferrerFilterTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/security/src/test/java/org/apache/sling/security/impl/ReferrerFilterTest.java?rev=1163675&r1=1163674&r2=1163675&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/security/src/test/java/org/apache/sling/security/impl/ReferrerFilterTest.java
(original)
+++ sling/trunk/contrib/extensions/security/src/test/java/org/apache/sling/security/impl/ReferrerFilterTest.java
Wed Aug 31 16:00:52 2011
@@ -16,8 +16,8 @@
  */
 package org.apache.sling.security.impl;
 
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
+import static org.mockito.Matchers.*;
 
 import java.util.Dictionary;
 import java.util.Hashtable;
@@ -27,6 +27,8 @@ import javax.servlet.http.HttpServletReq
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.service.component.ComponentContext;
 
 public class ReferrerFilterTest {
@@ -36,8 +38,13 @@ public class ReferrerFilterTest {
     @Before public void setup() {
         filter = new ReferrerFilter();
         final ComponentContext ctx = mock(ComponentContext.class);
+        final BundleContext bundleCtx = mock(BundleContext.class);
+        final ServiceRegistration reg = mock(ServiceRegistration.class);
         final Dictionary<String, Object> props = new Hashtable<String, Object>();
-        when(ctx.getProperties()).thenReturn(props);
+        doReturn(props).when(ctx).getProperties();
+        doReturn(bundleCtx).when(ctx).getBundleContext();
+        doReturn(reg).when(bundleCtx).registerService(any(String[].class), any(), any(Dictionary.class));
+        doNothing().when(reg).unregister();
         filter.activate(ctx);
     }
 



Mime
View raw message