incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r964078 - in /sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl: BackgroundHttpServletRequest.java BackgroundHttpServletResponse.java FilterChainExecutionJob.java ServletResponseWrapper.java
Date Wed, 14 Jul 2010 15:19:56 GMT
Author: bdelacretaz
Date: Wed Jul 14 15:19:44 2010
New Revision: 964078

URL: http://svn.apache.org/viewvc?rev=964078&view=rev
Log:
SLING-550 - use request/response objects that are safe to use outside of http request/response
cycle. Requires SLING-1596 patch

Added:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
  (with props)
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletResponse.java
      - copied, changed from r964041, sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ServletResponseWrapper.java
Removed:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ServletResponseWrapper.java
Modified:
    sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java

Added: 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=964078&view=auto
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
(added)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletRequest.java
Wed Jul 14 15:19:44 2010
@@ -0,0 +1,396 @@
+/*
+ * 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.BufferedReader;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+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;
+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 {
+
+	private final String contextPath;
+	private final String method;
+	private final String pathInfo;
+	private final RequestProgressTracker requestProgressTracker;
+	
+	private final Map<String, Object> attributes;
+	private final Map<String, ?> parameters;
+	
+	static class IteratorEnumeration<T> implements Enumeration<T> {
+		private final Iterator<T> it;
+		IteratorEnumeration(Iterator<T> it) {
+			this.it = it;
+		}
+		public boolean hasMoreElements() {
+			return it.hasNext();
+		}
+		public T nextElement() {
+			return it.next();
+		}
+	}
+	
+	/** We throw this for any method for which we do not have data that's
+	 * 	safe to use outside of the container's request/response cycle.
+	 * 	Start by throwing this everywhere and implement methods as needed,
+	 * 	if their data is safe to use.
+	 */
+	@SuppressWarnings("serial")
+	class UnsupportedBackgroundOperationException extends UnsupportedOperationException {
+		UnsupportedBackgroundOperationException() {
+			super("This operation is not supported for background requests");
+		}
+	}
+	
+	@SuppressWarnings("unchecked")
+	BackgroundHttpServletRequest(HttpServletRequest r) {
+		
+		final SlingHttpServletRequest sr = (r instanceof SlingHttpServletRequest ? (SlingHttpServletRequest)r
: null);
+		
+		// Store objects which are safe to use outside
+		// of the container's request/response cycle - the
+		// goal is to release r once this request starts
+		// executing in the background
+		contextPath = r.getContextPath();
+		method = r.getMethod();
+		pathInfo = r.getPathInfo();
+		
+		requestProgressTracker = (sr == null ? null : sr.getRequestProgressTracker());
+		
+		attributes = new HashMap<String, Object>();
+		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());
+	}
+	
+	public String getAuthType() {
+		return null;
+	}
+
+	public String getContextPath() {
+		return contextPath;
+	}
+
+	public Cookie[] getCookies() {
+		return null;
+	}
+
+	public long getDateHeader(String arg0) {
+		return 0;
+	}
+
+	public String getHeader(String arg0) {
+		return null;
+	}
+
+	public Enumeration<?> getHeaderNames() {
+		return null;
+	}
+
+	public Enumeration<?> getHeaders(String name) {
+		return null;
+	}
+
+	public int getIntHeader(String name) {
+		return 0;
+	}
+
+	public String getMethod() {
+		return method;
+	}
+
+	public String getPathInfo() {
+		return pathInfo;
+	}
+
+	public String getPathTranslated() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getQueryString() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRemoteUser() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRequestedSessionId() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRequestURI() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public StringBuffer getRequestURL() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getServletPath() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public HttpSession getSession() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public HttpSession getSession(boolean arg0) {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public Principal getUserPrincipal() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isRequestedSessionIdFromCookie() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isRequestedSessionIdFromUrl() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isRequestedSessionIdFromURL() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isRequestedSessionIdValid() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isUserInRole(String arg0) {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public Object getAttribute(String name) {
+		return attributes.get(name);
+	}
+
+	public Enumeration<?> getAttributeNames() {
+		return new IteratorEnumeration<String>(attributes.keySet().iterator());
+	}
+
+	public String getCharacterEncoding() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public int getContentLength() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getContentType() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public ServletInputStream getInputStream() throws IOException {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getLocalAddr() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public Locale getLocale() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public Enumeration getLocales() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getLocalName() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public int getLocalPort() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getParameter(String name) {
+		final Object obj = parameters.get(name);
+		if(obj instanceof String[]) {
+			return ((String[])obj)[0];
+		}
+		return (String)obj;
+	}
+
+	public Map<?,?> getParameterMap() {
+		return parameters;
+	}
+
+	public Enumeration<?> getParameterNames() {
+		return new IteratorEnumeration<String>(parameters.keySet().iterator());
+	}
+
+	public String[] getParameterValues(String key) {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getProtocol() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public BufferedReader getReader() throws IOException {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRealPath(String arg0) {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRemoteAddr() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getRemoteHost() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public int getRemotePort() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public RequestDispatcher getRequestDispatcher(String arg0) {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getScheme() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public String getServerName() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public int getServerPort() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public boolean isSecure() {
+		throw new UnsupportedBackgroundOperationException();
+	}
+
+	public void removeAttribute(String arg0) {
+		throw new UnsupportedBackgroundOperationException();
+		
+	}
+
+	public void setAttribute(String key, Object value) {
+		attributes.put(key, value);
+	}
+
+	public void setCharacterEncoding(String arg0)
+			throws UnsupportedEncodingException {
+		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();
+	}
+}

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

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

Copied: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletResponse.java
(from r964041, sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ServletResponseWrapper.java)
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletResponse.java?p2=sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletResponse.java&p1=sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ServletResponseWrapper.java&r1=964041&r2=964078&rev=964078&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/ServletResponseWrapper.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/BackgroundHttpServletResponse.java
Wed Jul 14 15:19:44 2010
@@ -22,16 +22,20 @@ import java.io.IOException;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
+import java.util.Locale;
 
 import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
 import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpServletResponseWrapper;
 
-/** Wraps an HttpServletResponse for background processing */
-class ServletResponseWrapper extends HttpServletResponseWrapper {
+import org.apache.sling.api.SlingHttpServletResponse;
+
+/** Minimal HttpServletResponse for background processing */
+class BackgroundHttpServletResponse implements SlingHttpServletResponse {
 
 	private final ServletOutputStream stream;
 	private final PrintWriter writer;
+	private final SlingHttpServletResponse wrappedResponse;
 	
 	static class ServletOutputStreamWrapper extends ServletOutputStream {
 
@@ -58,25 +62,136 @@ class ServletResponseWrapper extends Htt
 		
 	}
 	
-	ServletResponseWrapper(HttpServletResponse response, OutputStream os) throws IOException
{
-		super(response);
+	BackgroundHttpServletResponse(HttpServletResponse hsr, OutputStream os) throws IOException
{
 		stream = new ServletOutputStreamWrapper(os);
 		writer = new PrintWriter(new OutputStreamWriter(stream));
+		wrappedResponse = (hsr instanceof SlingHttpServletResponse ? (SlingHttpServletResponse)hsr
: null);
 	}
 	
 	void cleanup() throws IOException {
 		stream.flush();
 		stream.close();
 	}
-	
-	@Override
+
+
 	public ServletOutputStream getOutputStream() throws IOException {
 		return stream;
 	}
 
-	@Override
 	public PrintWriter getWriter() throws IOException {
 		return writer;
 	}
 
+	public void addCookie(Cookie arg0) {
+	}
+
+	public void addDateHeader(String arg0, long arg1) {
+	}
+
+	public void addHeader(String arg0, String arg1) {
+	}
+
+	public void addIntHeader(String arg0, int arg1) {
+	}
+
+	public boolean containsHeader(String arg0) {
+		return false;
+	}
+
+	public String encodeRedirectUrl(String arg0) {
+		return null;
+	}
+
+	public String encodeRedirectURL(String arg0) {
+		return null;
+	}
+
+	public String encodeUrl(String arg0) {
+		return null;
+	}
+
+	public String encodeURL(String arg0) {
+		return null;
+	}
+
+	public void sendError(int arg0, String arg1) throws IOException {
+		// TODO
+	}
+
+	public void sendError(int arg0) throws IOException {
+		// TODO
+	}
+
+	public void sendRedirect(String arg0) throws IOException {
+		// TODO
+	}
+
+	public void setDateHeader(String arg0, long arg1) {
+	}
+
+	public void setHeader(String arg0, String arg1) {
+	}
+
+	public void setIntHeader(String arg0, int arg1) {
+	}
+
+	public void setStatus(int arg0, String arg1) {
+		// TODO
+	}
+
+	public void setStatus(int arg0) {
+		// TODO
+	}
+
+	public void flushBuffer() throws IOException {
+		stream.flush();
+	}
+
+	public int getBufferSize() {
+		return 0;
+	}
+
+	public String getCharacterEncoding() {
+		return null;
+	}
+
+	public String getContentType() {
+		return null;
+	}
+
+	public Locale getLocale() {
+		return null;
+	}
+
+	public boolean isCommitted() {
+		return false;
+	}
+
+	public void reset() {
+	}
+
+	public void resetBuffer() {
+	}
+
+	public void setBufferSize(int arg0) {
+	}
+
+	public void setCharacterEncoding(String arg0) {
+	}
+
+	public void setContentLength(int arg0) {
+	}
+
+	public void setContentType(String arg0) {
+	}
+
+	public void setLocale(Locale arg0) {
+	}
+
+	public <AdapterType> AdapterType adaptTo(Class<AdapterType> t) {
+		if(wrappedResponse != null) {
+			return wrappedResponse.adaptTo(t);
+		}
+		return null;
+	}
 }
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java?rev=964078&r1=964077&r2=964078&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java
(original)
+++ sling/trunk/contrib/extensions/bgservlets/src/main/java/org/apache/sling/bgservlets/impl/FilterChainExecutionJob.java
Wed Jul 14 15:19:44 2010
@@ -26,6 +26,8 @@ import javax.servlet.FilterChain;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
 import org.apache.sling.bgservlets.JobStatus;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,23 +38,21 @@ import org.slf4j.LoggerFactory;
 class FilterChainExecutionJob implements Runnable, JobStatus {
 	private final Logger log = LoggerFactory.getLogger(getClass());
 	private final FilterChain chain;
-	private final ServletResponseWrapper response;
+	private final BackgroundHttpServletResponse response;
 	private final SuspendableOutputStream stream;
 	private final String path;
-	
-	// TODO is it ok to keep a reference to the request until run() is called??
-	private final HttpServletRequest request;
+	private final SlingHttpServletRequest request;
 	
 	FilterChainExecutionJob(FilterChain chain, HttpServletRequest request, HttpServletResponse
hsr) throws IOException {
 		this.chain = chain;
-		this.request = request;
+		this.request = new SlingHttpServletRequestWrapper(new BackgroundHttpServletRequest(request));
 		
 		// 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 ServletResponseWrapper(hsr, stream);
+		response  = new BackgroundHttpServletResponse(hsr, stream);
 	}
 	
 	public String toString() {



Mime
View raw message