incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r964790 - in /sling/trunk/contrib/extensions/bgservlets: ./ src/main/java/org/apache/sling/bgservlets/impl/
Date Fri, 16 Jul 2010 12:56:48 GMT
Author: bdelacretaz
Date: Fri Jul 16 12:56:47 2010
New Revision: 964790

URL: http://svn.apache.org/viewvc?rev=964790&view=rev
Log:
SLING-550, use new SlingServlet service (requires SLING-1603 patch, no other patches)

Added:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
  (with props)
Removed:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java
Modified:
    sling/trunk/contrib/extensions/bgservlets/pom.xml
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java

Modified: sling/trunk/contrib/extensions/bgservlets/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/pom.xml?rev=964790&r1=964789&r2=964790&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/pom.xml (original)
+++ sling/trunk/contrib/extensions/bgservlets/pom.xml Fri Jul 16 12:56:47 2010
@@ -81,7 +81,13 @@
     <dependency>
       <groupId>org.apache.sling</groupId>
       <artifactId>org.apache.sling.api</artifactId>
-      <version>2.0.8</version>
+      <version>2.0.9-SNAPSHOT</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.commons.auth</artifactId>
+      <version>0.9.0-SNAPSHOT</version>
       <scope>provided</scope>
     </dependency>
     <dependency>

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java?rev=964790&r1=964789&r2=964790&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
Fri Jul 16 12:56:47 2010
@@ -27,7 +27,6 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Locale;
 import java.util.Map;
-import java.util.ResourceBundle;
 
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletInputStream;
@@ -35,21 +34,27 @@ import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpSession;
 
-import org.apache.sling.api.SlingHttpServletRequest;
-import org.apache.sling.api.request.RequestDispatcherOptions;
-import org.apache.sling.api.request.RequestParameter;
-import org.apache.sling.api.request.RequestParameterMap;
-import org.apache.sling.api.request.RequestPathInfo;
-import org.apache.sling.api.request.RequestProgressTracker;
-import org.apache.sling.api.resource.Resource;
-import org.apache.sling.api.resource.ResourceResolver;
-
-public class BackgroundHttpServletRequest implements SlingHttpServletRequest {
+public class BackgroundHttpServletRequest implements HttpServletRequest {
 
 	private final String contextPath;
 	private final String method;
 	private final String pathInfo;
-	private final RequestProgressTracker requestProgressTracker;
+	private final String servletPath;
+	private final String queryString;
+	private final String requestURI;
+	private final StringBuffer requestURL;
+	private final String characterEncoding; 
+	private final int contentLength;
+	private final String contentType;
+	private final Locale locale;
+	private final String protocol;
+	private final String remoteAddr;
+	private final String remoteHost;
+	private final int remotePort;
+	private final int serverPort;
+	private final String scheme;
+	private final String remoteUser;
+	private final String serverName;
 	
 	private final Map<String, Object> attributes;
 	private final Map<String, ?> parameters;
@@ -80,9 +85,7 @@ public class BackgroundHttpServletReques
 	}
 	
 	@SuppressWarnings("unchecked")
-	BackgroundHttpServletRequest(HttpServletRequest r) {
-		
-		final SlingHttpServletRequest sr = (r instanceof SlingHttpServletRequest ? (SlingHttpServletRequest)r
: null);
+	BackgroundHttpServletRequest(HttpServletRequest r,String [] parametersToRemove) {
 		
 		// Store objects which are safe to use outside
 		// of the container's request/response cycle - the
@@ -91,18 +94,37 @@ public class BackgroundHttpServletReques
 		contextPath = r.getContextPath();
 		method = r.getMethod();
 		pathInfo = r.getPathInfo();
-		
-		requestProgressTracker = (sr == null ? null : sr.getRequestProgressTracker());
-		
+		servletPath = r.getServletPath();
+		queryString = r.getQueryString();
+		requestURI = r.getRequestURI();
+		requestURL = r.getRequestURL();
+		characterEncoding = r.getCharacterEncoding();
+		contentLength = r.getContentLength();
+		contentType = r.getContentType();
+		locale = r.getLocale();
+		protocol = r.getProtocol();
+		remoteAddr = r.getRemoteAddr();
+		remoteHost = r.getRemoteHost();
+		remotePort = r.getRemotePort();
+		serverPort = r.getServerPort();
+		scheme = r.getScheme();
+		remoteUser = r.getRemoteUser();
+		serverName = r.getServerName();
+
 		attributes = new HashMap<String, Object>();
+		/* Don't copy attributes, we consider this to be a "fresh" request
 		final Enumeration<?> e = r.getAttributeNames();
 		while(e.hasMoreElements()) {
 			final String key = (String)e.nextElement();
 			attributes.put(key, r.getAttribute(key));
 		}
+		*/
 		
 		parameters = new HashMap<String, String>();
 		parameters.putAll(r.getParameterMap());
+		for(String key : parametersToRemove) {
+			parameters.remove(key);
+		}
 	}
 	
 	public String getAuthType() {
@@ -150,11 +172,11 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getQueryString() {
-		throw new UnsupportedBackgroundOperationException();
+		return queryString;
 	}
 
 	public String getRemoteUser() {
-		throw new UnsupportedBackgroundOperationException();
+		return remoteUser;
 	}
 
 	public String getRequestedSessionId() {
@@ -162,15 +184,15 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getRequestURI() {
-		throw new UnsupportedBackgroundOperationException();
+		return requestURI;
 	}
 
 	public StringBuffer getRequestURL() {
-		throw new UnsupportedBackgroundOperationException();
+		return requestURL;
 	}
 
 	public String getServletPath() {
-		throw new UnsupportedBackgroundOperationException();
+		return servletPath;
 	}
 
 	public HttpSession getSession() {
@@ -214,15 +236,15 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getCharacterEncoding() {
-		throw new UnsupportedBackgroundOperationException();
+		return characterEncoding;
 	}
 
 	public int getContentLength() {
-		throw new UnsupportedBackgroundOperationException();
+		return contentLength;
 	}
 
 	public String getContentType() {
-		throw new UnsupportedBackgroundOperationException();
+		return contentType;
 	}
 
 	public ServletInputStream getInputStream() throws IOException {
@@ -234,10 +256,10 @@ public class BackgroundHttpServletReques
 	}
 
 	public Locale getLocale() {
-		throw new UnsupportedBackgroundOperationException();
+		return locale;
 	}
 
-	public Enumeration getLocales() {
+	public Enumeration<?> getLocales() {
 		throw new UnsupportedBackgroundOperationException();
 	}
 
@@ -270,7 +292,7 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getProtocol() {
-		throw new UnsupportedBackgroundOperationException();
+		return protocol;
 	}
 
 	public BufferedReader getReader() throws IOException {
@@ -282,15 +304,15 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getRemoteAddr() {
-		throw new UnsupportedBackgroundOperationException();
+		return remoteAddr;
 	}
 
 	public String getRemoteHost() {
-		throw new UnsupportedBackgroundOperationException();
+		return remoteHost;
 	}
 
 	public int getRemotePort() {
-		throw new UnsupportedBackgroundOperationException();
+		return remotePort;
 	}
 
 	public RequestDispatcher getRequestDispatcher(String arg0) {
@@ -298,24 +320,23 @@ public class BackgroundHttpServletReques
 	}
 
 	public String getScheme() {
-		throw new UnsupportedBackgroundOperationException();
+		return scheme;
 	}
 
 	public String getServerName() {
-		throw new UnsupportedBackgroundOperationException();
+		return serverName;
 	}
 
 	public int getServerPort() {
-		throw new UnsupportedBackgroundOperationException();
+		return serverPort;
 	}
 
 	public boolean isSecure() {
-		throw new UnsupportedBackgroundOperationException();
+		return false;
 	}
 
-	public void removeAttribute(String arg0) {
-		throw new UnsupportedBackgroundOperationException();
-		
+	public void removeAttribute(String name) {
+		attributes.remove(name);
 	}
 
 	public void setAttribute(String key, Object value) {
@@ -327,70 +348,4 @@ public class BackgroundHttpServletReques
 		throw new UnsupportedBackgroundOperationException();
 		
 	}
-
-	public Cookie getCookie(String arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestDispatcher getRequestDispatcher(Resource arg0,
-			RequestDispatcherOptions arg1) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestDispatcher getRequestDispatcher(Resource arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestDispatcher getRequestDispatcher(String arg0,
-			RequestDispatcherOptions arg1) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestParameter getRequestParameter(String arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestParameterMap getRequestParameterMap() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestParameter[] getRequestParameters(String arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestPathInfo getRequestPathInfo() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public RequestProgressTracker getRequestProgressTracker() {
-		return requestProgressTracker;
-	}
-
-	public Resource getResource() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public ResourceBundle getResourceBundle(Locale arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public ResourceBundle getResourceBundle(String arg0, Locale arg1) {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public ResourceResolver getResourceResolver() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public String getResponseContentType() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public Enumeration<String> getResponseContentTypes() {
-		throw new UnsupportedBackgroundOperationException();
-	}
-
-	public <AdapterType> AdapterType adaptTo(Class<AdapterType> arg0) {
-		throw new UnsupportedBackgroundOperationException();
-	}
 }

Added: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java?rev=964790&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
(added)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
Fri Jul 16 12:56:47 2010
@@ -0,0 +1,104 @@
+/*
+ * 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.bgservlets.impl;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.servlets.SlingServlet;
+import org.apache.sling.bgservlets.JobStatus;
+import org.apache.sling.commons.auth.impl.SlingAuthenticator;
+import org.apache.sling.commons.auth.spi.AuthenticationInfo;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Runnable that executes a FilterChain, using 
+ * 	a ServletResponseWrapper to capture the output.
+ */
+class BackgroundRequestExecutionJob implements Runnable, JobStatus {
+	private final Logger log = LoggerFactory.getLogger(getClass());
+	private final HttpServletRequest request;
+	private final BackgroundHttpServletResponse response;
+	private final SuspendableOutputStream stream;
+	private final SlingServlet slingServlet;
+	private final String path;
+	
+	BackgroundRequestExecutionJob(SlingServlet slingServlet, ResourceResolverFactory rrf, HttpServletRequest
request, 
+			HttpServletResponse hsr, String [] parametersToRemove) throws IOException, LoginException
{
+		this.request = new BackgroundHttpServletRequest(request, parametersToRemove);
+		this.slingServlet = slingServlet;
+		
+		// TODO we might 
+		// In a normal request the ResourceResolver is added to the request attributes
+		// by the authentication service, need to do the same here as we can't reuse the
+		// original one which is closed once main request is done
+		final AuthenticationInfo aa = (AuthenticationInfo)request.getAttribute(AuthenticationInfo.class.getName());
+		if(aa == null) {
+			throw new IllegalArgumentException("Missing AuthenticationInfo attribute");
+		}
+		final ResourceResolver rr = rrf.getResourceResolver(aa);
+		this.request.setAttribute(SlingAuthenticator.REQUEST_ATTRIBUTE_RESOLVER, rr);
+		
+		// TODO write output to the Sling repository. For now: just a temp file
+		final File output = File.createTempFile(getClass().getSimpleName(), ".data");
+		output.deleteOnExit();
+		path = output.getAbsolutePath();
+		stream = new SuspendableOutputStream(new FileOutputStream(output));
+		response  = new BackgroundHttpServletResponse(hsr, stream);
+	}
+	
+	public String toString() {
+		return getClass().getSimpleName() + ", state=" + getState() + ", path=" + path;
+	}
+	
+	public void run() {
+		try {
+			slingServlet.processRequest(request, response);
+		} catch(Exception e) {
+			// TODO report errors in the background job's output
+			log.error("Exception in background request processing", e);
+		} finally {
+			try {
+				response.cleanup();
+			} catch(IOException ioe) {
+				// TODO report errors in the background job's output
+				log.error("ServletResponseWrapper cleanup failed", ioe);
+			}
+		}
+	}
+
+	public String getPath() {
+		return path;
+	}
+
+	public State getState() {
+		return stream.getState();
+	}
+
+	public void requestStateChange(State s) {
+		stream.requestStateChange(s);
+	}
+}

Propchange: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundRequestExecutionJob.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java?rev=964790&r1=964789&r2=964790&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundServletStarterFilter.java
Fri Jul 16 12:56:47 2010
@@ -34,6 +34,9 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Property;
 import org.apache.felix.scr.annotations.Reference;
 import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.api.servlets.SlingServlet;
 import org.apache.sling.bgservlets.ExecutionEngine;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -56,10 +59,17 @@ public class BackgroundServletStarterFil
 	@Reference
 	private ExecutionEngine executionEngine;
 	
+	@Reference
+	private SlingServlet slingServlet;
+	
+	@Reference
+	private ResourceResolverFactory resourceResolverFactory;
+	
 	/** 
 	 * Request runs in the background if this request parameter is present 
 	 * TODO should be configurable, and maybe use other decision methods */
 	public static final String BG_PARAM = "sling:bg";
+	private static final String [] PARAM_TO_REMOVE = { BG_PARAM };
 	
 	public void doFilter(final ServletRequest sreq, final ServletResponse sresp, 
 			final FilterChain chain) throws IOException, ServletException {
@@ -70,14 +80,26 @@ public class BackgroundServletStarterFil
 			throw new ServletException("response is not an HttpServletResponse: " + sresp.getClass().getName());
 		}
 		final HttpServletRequest request = (HttpServletRequest)sreq;
+		final SlingHttpServletRequest slingRequest = 
+			(request instanceof SlingHttpServletRequest ? (SlingHttpServletRequest)request : null);

 		final HttpServletResponse response = (HttpServletResponse)sresp; 
-		if(sreq.getParameter(BG_PARAM) != null) {
-			final FilterChainExecutionJob job = new FilterChainExecutionJob(chain, request, response);
-			log.debug("{} request parameter present, running request in the background using {}",
BG_PARAM, job);
-			executionEngine.queueForExecution(job);
-			
-			// TODO not really an error, should send a nicer message
-			response.sendError(HttpServletResponse.SC_ACCEPTED, "Running request in the background
using " + job);
+		final String bgParam = sreq.getParameter(BG_PARAM); 
+		if(Boolean.valueOf(bgParam)) {
+			try {
+				final BackgroundRequestExecutionJob job = new BackgroundRequestExecutionJob(
+						slingServlet, resourceResolverFactory, request, response, PARAM_TO_REMOVE);
+				log.debug("{} parameter true, running request in the background ({})", BG_PARAM, job);
+				if(slingRequest != null) {
+					slingRequest.getRequestProgressTracker().log(
+							BG_PARAM + " parameter true, running request in background (" + job + ")");
+				}
+				executionEngine.queueForExecution(job);
+				
+				// TODO not really an error, should send a nicer message
+				response.sendError(HttpServletResponse.SC_ACCEPTED, "Running request in the background
using " + job);
+			} catch (org.apache.sling.api.resource.LoginException e) {
+				throw new ServletException("LoginException in doFilter", e);
+			}
 		} else {
 			chain.doFilter(sreq, sresp);
 		}



Mime
View raw message