You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2012/01/05 14:00:36 UTC

svn commit: r1227578 - in /sling/whiteboard/fmeschbe/httpcontext: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/extensions/ src/main/java/org/apache/sling/ex...

Author: fmeschbe
Date: Thu Jan  5 13:00:35 2012
New Revision: 1227578

URL: http://svn.apache.org/viewvc?rev=1227578&view=rev
Log:
Sample of a shared HttpContext service

Added:
    sling/whiteboard/fmeschbe/httpcontext/
    sling/whiteboard/fmeschbe/httpcontext/pom.xml
    sling/whiteboard/fmeschbe/httpcontext/src/
    sling/whiteboard/fmeschbe/httpcontext/src/main/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/
    sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/SlingHttpContext.java

Added: sling/whiteboard/fmeschbe/httpcontext/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/httpcontext/pom.xml?rev=1227578&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/httpcontext/pom.xml (added)
+++ sling/whiteboard/fmeschbe/httpcontext/pom.xml Thu Jan  5 13:00:35 2012
@@ -0,0 +1,108 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+  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.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>12</version>
+        <relativePath><!-- ../../../parent/pom.xml --></relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.extensions.httpcontext</artifactId>
+    <version>0.0.1-SNAPSHOT</version>
+    <packaging>bundle</packaging>
+
+    <name>Apache Sling shared HttpContext</name>
+    <description>
+      Provides an OSGi HttpContext service implementing authentication
+      based on Sling Authentication and MIME type resolution based
+      on Sling MimeTypeService.
+    </description>
+
+    <properties>
+        <!-- <site.jira.version.id>jira-id</site.jira.version.id> -->
+        <site.javadoc.exclude>**.impl.**</site.javadoc.exclude>
+    </properties>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/httpcontext</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/sling/whiteboard/fmeschbe/httpcontext</developerConnection>
+        <url>http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/httpcontext</url>
+    </scm>
+
+    <build>
+        <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>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <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.sling</groupId>
+            <artifactId>org.apache.sling.auth.core</artifactId>
+            <version>1.0.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.commons.mime</artifactId>
+            <version>2.1.2</version>
+            <scope>provided</scope>
+        </dependency>
+
+        <!-- Commons Dependencies -->
+        <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+          <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Added: sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/SlingHttpContext.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/SlingHttpContext.java?rev=1227578&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/SlingHttpContext.java (added)
+++ sling/whiteboard/fmeschbe/httpcontext/src/main/java/org/apache/sling/extensions/httpcontext/impl/SlingHttpContext.java Thu Jan  5 13:00:35 2012
@@ -0,0 +1,91 @@
+/*
+ * 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.sling.extensions.httpcontext.impl;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.auth.core.AuthenticationSupport;
+import org.apache.sling.commons.mime.MimeTypeService;
+import org.osgi.framework.Constants;
+import org.osgi.service.http.HttpContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component
+@Service
+@Properties({ @Property(name = Constants.SERVICE_DESCRIPTION, value = "Apache Sling shared OSGi HttpContext"),
+    @Property(name = "contextId", value = "sling.httpcontext"),
+    @Property(name = "context.shared", boolValue = true) })
+public class SlingHttpContext implements HttpContext {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(policy = ReferencePolicy.DYNAMIC)
+    private AuthenticationSupport authenticator;
+
+    @Reference(policy = ReferencePolicy.DYNAMIC)
+    private MimeTypeService mimeTypeService;
+
+    public boolean handleSecurity(HttpServletRequest request, HttpServletResponse response) throws IOException {
+
+        final AuthenticationSupport localAuthenticator = this.authenticator;
+        if (localAuthenticator != null) {
+            return localAuthenticator.handleSecurity(request, response);
+        }
+
+        log.error("handleSecurity: Sling AuthenticationSupport service is not available; not ready to serve requests");
+
+        // send 503/SERVICE UNAVAILABLE, flush to ensure delivery
+        response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
+        response.flushBuffer();
+
+        // terminate this request now
+        return false;
+    }
+
+    public URL getResource(String name) {
+        // we don't serve resources for now ....
+        return null;
+    }
+
+    public String getMimeType(String name) {
+        MimeTypeService mts = this.mimeTypeService;
+        if (mts != null) {
+            final String type = mts.getMimeType(name);
+            log.debug("getMimeType({}) -> {}", name, type);
+            return type;
+        }
+
+        // no MimeTypeService
+        log.debug("getMimeType({}) -> null (Sling MimeType service is not available)", name);
+        return null;
+    }
+
+}