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 2007/11/09 16:41:20 UTC
svn commit: r593572 [2/2] - in
/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core:
auth/ impl/ impl/auth/ impl/filter/ impl/helper/ impl/resolver/
impl/services/ resolver/
Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/RequestData.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/RequestData.java?rev=593572&r1=593571&r2=593572&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/RequestData.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/RequestData.java Fri Nov 9 07:41:18 2007
@@ -18,6 +18,9 @@
*/
package org.apache.sling.core.impl.helper;
+import static org.apache.sling.api.SlingConstants.ATTR_REQUEST_CONTENT;
+import static org.apache.sling.api.SlingConstants.ATTR_REQUEST_SERVLET;
+
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
@@ -26,6 +29,8 @@
import java.util.Locale;
import javax.jcr.Session;
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletRequest;
@@ -35,6 +40,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.sling.api.HttpStatusCodeException;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
@@ -43,16 +49,16 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceManager;
import org.apache.sling.api.services.ServiceLocator;
+import org.apache.sling.api.servlets.ServletResolver;
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper;
import org.apache.sling.api.wrappers.SlingHttpServletResponseWrapper;
-import org.apache.sling.core.impl.SlingHttpServletRequestImpl;
-import org.apache.sling.core.impl.SlingHttpServletResponseImpl;
+import org.apache.sling.core.impl.SlingMainServlet;
import org.apache.sling.core.impl.adapter.SlingServletRequestAdapter;
import org.apache.sling.core.impl.output.BufferProvider;
import org.apache.sling.core.impl.parameters.ParameterSupport;
import org.apache.sling.core.impl.request.SlingRequestProgressTracker;
-import org.apache.sling.core.impl.resolver.ResolvedURLImpl;
import org.apache.sling.core.theme.Theme;
+import org.apache.sling.jcr.resource.JcrResourceManagerFactory;
import org.osgi.service.component.ComponentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -69,15 +75,24 @@
/** default log */
private final Logger log = LoggerFactory.getLogger(RequestData.class);
+ /** The SlingMainServlet used for request dispatching and other stuff */
+ private final SlingMainServlet slingMainServlet;
+
/** The original servlet Servlet Request Object */
private HttpServletRequest servletRequest;
- /** The parameter support class */
- private ParameterSupport parameterSupport;
-
/** The original servlet Servlet Response object */
private HttpServletResponse servletResponse;
+ /** The original servlet Servlet Request Object */
+ private SlingHttpServletRequest slingRequest;
+
+ /** The original servlet Servlet Response object */
+ private SlingHttpServletResponse slingResponse;
+
+ /** The parameter support class */
+ private ParameterSupport parameterSupport;
+
/**
* <code>true</code> if the servlet is
* <code>RequestDispatcher.include()</code>-ed
@@ -104,17 +119,10 @@
/** Caches the real query string returned by {@link #getRealQueryString()} */
private String queryString;
- /** Caches the real method name returned by {@link #getRealMethod()} */
- private String method;
-
- private Session session;
-
private ResourceManager resourceManager;
private RequestProgressTracker requestProgressTracker;
- private ServiceLocator serviceLocator;
-
private Locale locale;
private Theme theme;
@@ -125,54 +133,87 @@
/** the stack of ContentData objects */
private LinkedList<ContentData> contentDataStack;
- public RequestData(HttpServletRequest request, HttpServletResponse response) {
+ public RequestData(SlingMainServlet slingMainServlet, Session session,
+ HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+
+ this.slingMainServlet = slingMainServlet;
+
this.servletRequest = request;
this.servletResponse = response;
- requestProgressTracker = new SlingRequestProgressTracker();
+ this.slingRequest = new SlingHttpServletRequestImpl(this, servletRequest);
+ this.slingResponse = new SlingHttpServletResponseImpl(this, servletResponse);
- // some more preparation
+ this.requestProgressTracker = new SlingRequestProgressTracker();
this.included = request.getAttribute(SlingConstants.INCLUDE_REQUEST_URI) != null;
+
+ // the resource manager factory may be missing
+ JcrResourceManagerFactory rmf = slingMainServlet.getResourceManagerFactory();
+ if (rmf == null) {
+ log.error("RequestData: Missing JcrResourceManagerFactory");
+ throw new HttpStatusCodeException(HttpServletResponse.SC_NOT_FOUND,
+ "No resource can be found");
+ }
+
+ // officially, getting the manager may fail, but not i this implementation
+ this.resourceManager = rmf.getResourceManager(session);
+
+ // resolve the resource and the request path info, will never be null
+ Resource resource = resourceManager.resolve(request);
+ RequestPathInfo requestPathInfo = new SlingRequestPathInfo(resource,
+ request.getPathInfo());
+ ContentData contentData = pushContent(resource, requestPathInfo);
+
+ // finally resolve the servlet for the resource
+ ServletResolver sr = slingMainServlet.getServletResolver();
+ Servlet servlet = sr.resolveServlet(slingRequest);
+ contentData.setServlet(servlet);
}
- /* package */void dispose() {
+ public void dispose() {
// make sure our request attributes do not exist anymore
- this.servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_CONTENT);
- this.servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_SERVLET);
+ servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_CONTENT);
+ servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_SERVLET);
// clear the content data stack
- if (this.contentDataStack != null) {
- while (!this.contentDataStack.isEmpty()) {
- ContentData cd = this.contentDataStack.removeLast();
+ if (contentDataStack != null) {
+ while (!contentDataStack.isEmpty()) {
+ ContentData cd = contentDataStack.removeLast();
cd.dispose();
}
}
// dispose current content data, if any
- if (this.currentContentData != null) {
- this.currentContentData.dispose();
- }
-
- // logout the session
- if (this.session != null) {
- this.session.logout();
+ if (currentContentData != null) {
+ currentContentData.dispose();
}
// clear fields
- this.contentDataStack = null;
- this.currentContentData = null;
- this.servletRequest = null;
- this.servletResponse = null;
- this.resourceManager = null;
- this.session = null;
+ contentDataStack = null;
+ currentContentData = null;
+ servletRequest = null;
+ servletResponse = null;
+ resourceManager = null;
+ }
+
+ public SlingMainServlet getSlingMainServlet() {
+ return slingMainServlet;
}
public HttpServletRequest getServletRequest() {
- return this.servletRequest;
+ return servletRequest;
}
public HttpServletResponse getServletResponse() {
- return this.servletResponse;
+ return servletResponse;
+ }
+
+ public SlingHttpServletRequest getSlingRequest() {
+ return slingRequest;
+ }
+
+ public SlingHttpServletResponse getSlingResponse() {
+ return slingResponse;
}
// ---------- Request Helper
@@ -334,66 +375,55 @@
// ---------- Content inclusion stacking -----------------------------------
- public void pushContent(Resource resource, RequestPathInfo requestPathInfo) {
+ public ContentData pushContent(Resource resource,
+ RequestPathInfo requestPathInfo) {
BufferProvider parent;
- if (this.currentContentData != null) {
- if (this.contentDataStack == null) {
- this.contentDataStack = new LinkedList<ContentData>();
- }
-
- // ensure the selectors, extension and suffix are inherited
- // from the parent if none have been declared on inclusion
- if (requestPathInfo.getExtension() == null
- || requestPathInfo.getExtension().length() == 0) {
- ResolvedURLImpl copy = new ResolvedURLImpl(requestPathInfo);
- RequestPathInfo current = currentContentData.getRequestPathInfo();
- copy.setSelectorString(current.getSelectorString());
- copy.setExtension(current.getExtension());
- copy.setSuffix(current.getSuffix());
- requestPathInfo = copy;
+ if (currentContentData != null) {
+ if (contentDataStack == null) {
+ contentDataStack = new LinkedList<ContentData>();
}
// remove the request attributes if the stack is empty now
- this.servletRequest.setAttribute(
- SlingConstants.ATTR_REQUEST_CONTENT,
- this.currentContentData.getResource());
- this.servletRequest.setAttribute(
- SlingConstants.ATTR_REQUEST_SERVLET,
- this.currentContentData.getServlet());
+ servletRequest.setAttribute(ATTR_REQUEST_CONTENT,
+ currentContentData.getResource());
+ servletRequest.setAttribute(ATTR_REQUEST_SERVLET,
+ currentContentData.getServlet());
- this.contentDataStack.add(this.currentContentData);
- parent = this.currentContentData;
+ contentDataStack.add(currentContentData);
+ parent = currentContentData;
} else {
parent = this;
}
- this.currentContentData = new ContentData(resource, requestPathInfo,
- parent);
+ currentContentData = new ContentData(resource, requestPathInfo, parent);
+ return currentContentData;
}
- public void popContent() {
+ public ContentData popContent() {
// dispose current content data before replacing it
- if (this.currentContentData != null) {
- this.currentContentData.dispose();
+ if (currentContentData != null) {
+ currentContentData.dispose();
}
- if (this.contentDataStack != null && !this.contentDataStack.isEmpty()) {
+ if (contentDataStack != null && !contentDataStack.isEmpty()) {
// remove the topmost content data object
- this.currentContentData = this.contentDataStack.removeLast();
+ currentContentData = contentDataStack.removeLast();
// remove the request attributes if the stack is empty now
- if (this.contentDataStack.isEmpty()) {
- this.servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_CONTENT);
- this.servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_SERVLET);
+ if (contentDataStack.isEmpty()) {
+ servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_CONTENT);
+ servletRequest.removeAttribute(SlingConstants.ATTR_REQUEST_SERVLET);
}
} else {
- this.currentContentData = null;
+ currentContentData = null;
}
+
+ return currentContentData;
}
public ContentData getContentData() {
- return this.currentContentData;
+ return currentContentData;
}
/**
@@ -402,8 +432,7 @@
* <code>SlingHttpServletRequestDispatcher.include</code>.
*/
public boolean isContentIncluded() {
- return this.contentDataStack != null
- && !this.contentDataStack.isEmpty();
+ return contentDataStack != null && !contentDataStack.isEmpty();
}
// ---------- parameters differing in included servlets --------------------
@@ -416,7 +445,7 @@
* <code>RequestDispatcher.include()</code>.
*/
public boolean isIncluded() {
- return this.included;
+ return included;
}
/**
@@ -431,18 +460,18 @@
* context path removed.
*/
public String getRequestURI() {
- if (this.requestURI == null) {
+ if (requestURI == null) {
// get the unmodified request URI and context information
- this.requestURI = this.included
- ? (String) this.servletRequest.getAttribute(SlingConstants.INCLUDE_REQUEST_URI)
- : this.servletRequest.getRequestURI();
+ requestURI = included
+ ? (String) servletRequest.getAttribute(SlingConstants.INCLUDE_REQUEST_URI)
+ : servletRequest.getRequestURI();
- String ctxPrefix = this.getContextPath();
+ String ctxPrefix = getContextPath();
if (log.isDebugEnabled()) {
log.debug("getRequestURI: Servlet request URI is {}",
- this.requestURI);
+ requestURI);
}
// check to remove the context prefix
@@ -453,25 +482,25 @@
if (log.isDebugEnabled()) {
log.debug("getRequestURI: Default root context, no change to uri");
}
- } else if (ctxPrefix.length() < this.requestURI.length()
- && this.requestURI.startsWith(ctxPrefix)
- && this.requestURI.charAt(ctxPrefix.length()) == '/') {
+ } else if (ctxPrefix.length() < requestURI.length()
+ && requestURI.startsWith(ctxPrefix)
+ && requestURI.charAt(ctxPrefix.length()) == '/') {
// some path below context root
if (log.isDebugEnabled()) {
log.debug("getRequestURI: removing '{}' from '{}'",
- ctxPrefix, this.requestURI);
+ ctxPrefix, requestURI);
}
- this.requestURI = this.requestURI.substring(ctxPrefix.length());
- } else if (ctxPrefix.equals(this.requestURI)) {
+ requestURI = requestURI.substring(ctxPrefix.length());
+ } else if (ctxPrefix.equals(requestURI)) {
// context root
if (log.isDebugEnabled()) {
log.debug("getRequestURI: URI equals context prefix, assuming '/'");
}
- this.requestURI = "/";
+ requestURI = "/";
}
}
- return this.requestURI;
+ return requestURI;
}
/**
@@ -482,13 +511,13 @@
* @return The relevant context path according to environment.
*/
public String getContextPath() {
- if (this.contextPath == null) {
- this.contextPath = this.included
- ? (String) this.servletRequest.getAttribute(SlingConstants.INCLUDE_CONTEXT_PATH)
- : this.servletRequest.getContextPath();
+ if (contextPath == null) {
+ contextPath = included
+ ? (String) servletRequest.getAttribute(SlingConstants.INCLUDE_CONTEXT_PATH)
+ : servletRequest.getContextPath();
}
- return this.contextPath;
+ return contextPath;
}
/**
@@ -503,13 +532,13 @@
* @return The relevant servlet path according to environment.
*/
public String getServletPath() {
- if (this.servletPath == null) {
- this.servletPath = this.included
- ? (String) this.servletRequest.getAttribute(SlingConstants.INCLUDE_SERVLET_PATH)
- : this.servletRequest.getServletPath();
+ if (servletPath == null) {
+ servletPath = included
+ ? (String) servletRequest.getAttribute(SlingConstants.INCLUDE_SERVLET_PATH)
+ : servletRequest.getServletPath();
}
- return this.servletPath;
+ return servletPath;
}
/**
@@ -523,13 +552,13 @@
* @return The relevant path info according to environment.
*/
public String getPathInfo() {
- if (this.pathInfo == null) {
- this.pathInfo = this.included
- ? (String) this.servletRequest.getAttribute(SlingConstants.INCLUDE_PATH_INFO)
- : this.servletRequest.getPathInfo();
+ if (pathInfo == null) {
+ pathInfo = included
+ ? (String) servletRequest.getAttribute(SlingConstants.INCLUDE_PATH_INFO)
+ : servletRequest.getPathInfo();
}
- return this.pathInfo;
+ return pathInfo;
}
/**
@@ -540,20 +569,20 @@
* @return The relevant query string according to environment.
*/
public String getQueryString() {
- if (this.queryString == null) {
- this.queryString = this.included
- ? (String) this.servletRequest.getAttribute(SlingConstants.INCLUDE_QUERY_STRING)
- : this.servletRequest.getQueryString();
+ if (queryString == null) {
+ queryString = included
+ ? (String) servletRequest.getAttribute(SlingConstants.INCLUDE_QUERY_STRING)
+ : servletRequest.getQueryString();
}
- return this.queryString;
+ return queryString;
}
/**
* @return the locale
*/
public Locale getLocale() {
- return this.locale;
+ return locale;
}
/**
@@ -567,44 +596,19 @@
return resourceManager;
}
- /**
- * @param persistenceManager the persistenceManager to set
- */
- public void setResourceManager(ResourceManager resourceManager) {
- this.resourceManager = resourceManager;
- }
-
public RequestProgressTracker getRequestProgressTracker() {
return requestProgressTracker;
}
- public void setServiceLocator(ServiceLocator serviceLocator) {
- this.serviceLocator = serviceLocator;
- }
-
public ServiceLocator getServiceLocator() {
- return serviceLocator;
- }
-
- /**
- * @return the session
- */
- public Session getSession() {
- return this.session;
- }
-
- /**
- * @param session the session to set
- */
- public void setSession(Session session) {
- this.session = session;
+ return slingMainServlet.getServiceLocator();
}
/**
* @return the theme
*/
public Theme getTheme() {
- return this.theme;
+ return theme;
}
/**
@@ -614,56 +618,54 @@
this.theme = theme;
// provide the current theme to components as a request attribute
// TODO - We should define a well known constant for this
- this.servletRequest.setAttribute(Theme.class.getName(), theme);
+ servletRequest.setAttribute(Theme.class.getName(), theme);
}
// ---------- BufferProvider -----------------------------------------
public BufferProvider getBufferProvider() {
- return (this.currentContentData != null)
- ? (BufferProvider) this.currentContentData
+ return (currentContentData != null)
+ ? (BufferProvider) currentContentData
: this;
}
public ServletOutputStream getOutputStream() throws IOException {
- return this.getServletResponse().getOutputStream();
+ return getServletResponse().getOutputStream();
}
public PrintWriter getWriter() throws IOException {
- return this.getServletResponse().getWriter();
+ return getServletResponse().getWriter();
}
// ---------- Parameter support -------------------------------------------
ServletInputStream getInputStream() throws IOException {
- if (this.parameterSupport != null
- && this.parameterSupport.requestDataUsed()) {
+ if (parameterSupport != null && parameterSupport.requestDataUsed()) {
throw new IllegalStateException(
"Request Data has already been read");
}
// may throw IllegalStateException if the reader has already been
// acquired
- return this.getServletRequest().getInputStream();
+ return getServletRequest().getInputStream();
}
BufferedReader getReader() throws UnsupportedEncodingException, IOException {
- if (this.parameterSupport != null
- && this.parameterSupport.requestDataUsed()) {
+ if (parameterSupport != null && parameterSupport.requestDataUsed()) {
throw new IllegalStateException(
"Request Data has already been read");
}
// may throw IllegalStateException if the input stream has already been
// acquired
- return this.getServletRequest().getReader();
+ return getServletRequest().getReader();
}
ParameterSupport getParameterSupport() {
- if (this.parameterSupport == null) {
- this.parameterSupport = new ParameterSupport(this /* getServletRequest() */);
+ if (parameterSupport == null) {
+ parameterSupport = new ParameterSupport(this /* getServletRequest() */);
}
- return this.parameterSupport;
+ return parameterSupport;
}
}
Copied: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletRequestImpl.java (from r593170, incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletRequestImpl.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletRequestImpl.java?p2=incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletRequestImpl.java&p1=incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletRequestImpl.java&r1=593170&r2=593572&rev=593572&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletRequestImpl.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletRequestImpl.java Fri Nov 9 07:41:18 2007
@@ -16,35 +16,23 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.core.impl;
+package org.apache.sling.core.impl.helper;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
-import java.lang.reflect.Method;
import java.util.Collections;
import java.util.Enumeration;
-import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
-import java.util.NoSuchElementException;
import java.util.ResourceBundle;
-import javax.jcr.Item;
-import javax.jcr.Node;
-import javax.jcr.NodeIterator;
-import javax.jcr.RepositoryException;
-import javax.jcr.Session;
import javax.servlet.RequestDispatcher;
-import javax.servlet.ServletContext;
import javax.servlet.ServletInputStream;
-import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
-import javax.servlet.http.HttpSession;
-
-import org.apache.jackrabbit.ocm.exception.ObjectContentManagerException;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.request.RequestDispatcherOptions;
import org.apache.sling.api.request.RequestParameter;
@@ -54,23 +42,19 @@
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.services.ServiceLocator;
-import org.apache.sling.core.RequestUtil;
-import org.apache.sling.core.impl.helper.ContentData;
-import org.apache.sling.core.impl.helper.RequestData;
import org.apache.sling.core.impl.parameters.ParameterSupport;
-import org.apache.sling.core.objects.SelectableContent;
-import org.apache.sling.core.objects.Selector;
/**
* The <code>SlingHttpServletRequestImpl</code> TODO
*/
-class SlingHttpServletRequestImpl extends HttpServletRequestWrapper implements
+public class SlingHttpServletRequestImpl extends HttpServletRequestWrapper implements
SlingHttpServletRequest {
private final RequestData requestData;
- protected SlingHttpServletRequestImpl(RequestData requestData) {
- super(requestData.getServletRequest());
+ public SlingHttpServletRequestImpl(RequestData requestData,
+ HttpServletRequest servletRequest) {
+ super(servletRequest);
this.requestData = requestData;
}
@@ -110,28 +94,14 @@
public RequestDispatcher getRequestDispatcher(Resource resource,
RequestDispatcherOptions options) {
- ContentData cd = getRequestData().getContentData();
- if (cd == null) {
- // in case of issue, this may happen, but should, just taking care
- return null;
- }
-
- ServletContext ctx = cd.getServlet().getServletConfig().getServletContext();
- return ctx.getRequestDispatcher(resource);
+ return new SlingRequestDispatcher(resource, options);
}
/**
* @see javax.servlet.ServletRequestWrapper#getRequestDispatcher(java.lang.String)
*/
public RequestDispatcher getRequestDispatcher(String path) {
- ContentData cd = this.getRequestData().getContentData();
- if (cd == null) {
- // in case of issue, this may happen, but should, just taking care
- return null;
- }
-
- ServletContext ctx = cd.getServlet().getServletConfig().getServletContext();
- return ctx.getRequestDispatcher(path);
+ return new SlingRequestDispatcher(path);
}
/**
@@ -204,11 +174,15 @@
*/
public Cookie getCookie(String name) {
Cookie[] cookies = getCookies();
- for (int i = 0; cookies != null && i < cookies.length; i++) {
- if (cookies[i].getName().equals(name)) {
- return cookies[i];
+
+ if (cookies != null) {
+ for (int i = 0; i < cookies.length; i++) {
+ if (cookies[i].getName().equals(name)) {
+ return cookies[i];
+ }
}
}
+
return null;
}
Copied: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletResponseImpl.java (from r593170, incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletResponseImpl.java)
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletResponseImpl.java?p2=incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletResponseImpl.java&p1=incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletResponseImpl.java&r1=593170&r2=593572&rev=593572&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/SlingHttpServletResponseImpl.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingHttpServletResponseImpl.java Fri Nov 9 07:41:18 2007
@@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
-package org.apache.sling.core.impl;
+package org.apache.sling.core.impl.helper;
import java.io.IOException;
import java.io.PrintWriter;
@@ -24,29 +24,28 @@
import java.util.Map;
import javax.servlet.ServletOutputStream;
+import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.core.RequestUtil;
-import org.apache.sling.core.impl.helper.RequestData;
+import org.apache.sling.core.impl.filter.ErrorHandler;
+import org.apache.sling.jcr.resource.PathResolver;
/**
* The <code>SlingHttpServletResponseImpl</code> TODO
*/
-class SlingHttpServletResponseImpl extends HttpServletResponseWrapper implements SlingHttpServletResponse {
+public class SlingHttpServletResponseImpl extends HttpServletResponseWrapper implements SlingHttpServletResponse {
private final RequestData requestData;
+
private String contentType = "text/html";
private String characterEncoding = null;
- protected SlingHttpServletResponseImpl(RequestData requestData) {
- super(requestData.getServletResponse());
- this.requestData = requestData;
- }
-
- protected SlingHttpServletResponseImpl(SlingHttpServletResponseImpl response) {
+ public SlingHttpServletResponseImpl(RequestData requestData,
+ HttpServletResponse response) {
super(response);
- requestData = response.getRequestData();
+ this.requestData = requestData;
}
protected final RequestData getRequestData() {
@@ -149,5 +148,86 @@
// set the content type with charset on the underlying response
super.setContentType(getContentType());
+ }
+
+ // ---------- Redirection support through PathResolver --------------------
+
+ @Override
+ public String encodeURL(String url) {
+ // make the path absolute
+ url = makeAbsolutePath(url);
+
+ // resolve the url to as if it would be a resource path
+ url = getPathResolver().pathToURL(url);
+
+ // have the servlet container to further encodings
+ return super.encodeURL(url);
+ }
+
+ @Override
+ public String encodeRedirectURL(String url) {
+ // make the path absolute
+ url = makeAbsolutePath(url);
+
+ // resolve the url to as if it would be a resource path
+ url = getPathResolver().pathToURL(url);
+
+ // have the servlet container to further encodings
+ return super.encodeRedirectURL(url);
+ }
+
+ @Override
+ @Deprecated
+ public String encodeUrl(String url) {
+ return encodeURL(url);
+ }
+
+ @Override
+ @Deprecated
+ public String encodeRedirectUrl(String url) {
+ return encodeRedirectURL(url);
+ }
+
+ // ---------- Error handling through Sling Error Resolver -----------------
+
+ @Override
+ public void sendError(int sc) throws IOException {
+ sendError(sc, null);
+ }
+
+ @Override
+ public void sendError(int sc, String msg) throws IOException {
+ checkCommitted();
+
+ ErrorHandler eh = getRequestData().getSlingMainServlet().getErrorHandler();
+ eh.handleError(sc, msg, requestData.getSlingRequest(), this);
+ }
+
+ // ---------- Internal helper ---------------------------------------------
+
+ private void checkCommitted() {
+ if (isCommitted()) {
+ throw new IllegalStateException(
+ "Response has already been committed");
+ }
+ }
+
+ private String makeAbsolutePath(String path) {
+ if (path.startsWith("/")) {
+ return path;
+ }
+
+ String base = getRequestData().getContentData().getResource().getURI();
+ int lastSlash = base.lastIndexOf('/');
+ if (lastSlash >= 0) {
+ path = base.substring(0, lastSlash+1) + path;
+ } else {
+ path = "/" + path;
+ }
+
+ return path;
+ }
+ private PathResolver getPathResolver() {
+ return (PathResolver) getRequestData().getResourceManager();
}
}
Added: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestDispatcher.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestDispatcher.java?rev=593572&view=auto
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestDispatcher.java (added)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestDispatcher.java Fri Nov 9 07:41:18 2007
@@ -0,0 +1,149 @@
+/*
+ * 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.core.impl.helper;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+import org.apache.sling.api.resource.ResourceResolver;
+
+class SlingRequestDispatcher implements RequestDispatcher {
+
+ private Resource resource;
+
+ private RequestDispatcherOptions options;
+
+ private String path;
+
+ SlingRequestDispatcher(String path) {
+ this.path = path;
+
+ this.resource = null;
+ this.options = null;
+ }
+
+ SlingRequestDispatcher(Resource resource, RequestDispatcherOptions options) {
+ this.resource = resource;
+ this.options = options;
+ this.path = resource.getURI();
+ }
+
+ public void include(ServletRequest request, ServletResponse response)
+ throws ServletException, IOException {
+
+ // this may throw an exception in case loading fails, which is
+ // ok here, if no content is available at that path null is
+ // return, which results in using the servlet container
+ SlingHttpServletRequest cRequest = RequestData.unwrap(request);
+ RequestData rd = RequestData.getRequestData(cRequest);
+ String absPath = getAbsolutePath(cRequest, path);
+
+ if (resource == null) {
+ ResourceResolver rr = cRequest.getResourceResolver();
+ resource = rr.getResource(absPath);
+ }
+
+ if (resource == null) {
+
+ rd.getSlingMainServlet().includeServlet(request, response, path);
+
+ } else {
+
+ // ensure request path info and optional merges
+ SlingRequestPathInfo info = new SlingRequestPathInfo(resource, path);
+ info = info.merge(cRequest.getRequestPathInfo());
+ info = info.merge(options);
+
+ // ensure overwritten resource type
+ String rtOverwrite = options.get(RequestDispatcherOptions.OPT_FORCE_RESOURCE_TYPE);
+ if (rtOverwrite != null && !rtOverwrite.equals(resource.getResourceType())) {
+ resource = new ResourceWrapper(resource, rtOverwrite);
+ }
+
+ rd.getSlingMainServlet().includeContent(request, response,
+ resource, info);
+ }
+ }
+
+ public void forward(ServletRequest request, ServletResponse response)
+ throws ServletException, IOException {
+ // TODO Auto-generated method stub
+ // TODO, use servlet container dispatcher !!
+ }
+
+ private String getAbsolutePath(SlingHttpServletRequest request, String path) {
+ // path is already absolute
+ if (path.startsWith("/")) {
+ return path;
+ }
+
+ // get parent of current request
+ String uri = request.getResource().getURI();
+ int lastSlash = uri.lastIndexOf('/');
+ if (lastSlash >= 0) {
+ uri = uri.substring(0, lastSlash);
+ }
+
+ // append relative path to parent
+ return uri + '/' + path;
+ }
+
+ private static class ResourceWrapper implements Resource {
+ private final Resource delegatee;
+ private final String resourceType;
+
+ ResourceWrapper(Resource delegatee, String resourceType) {
+ this.delegatee = delegatee;
+ this.resourceType = resourceType;
+ }
+
+ public String getResourceType() {
+ return resourceType;
+ }
+
+ public InputStream getInputStream() throws IOException {
+ return delegatee.getInputStream();
+ }
+
+ public Object getObject() {
+ return delegatee.getObject();
+ }
+
+ public Object getRawData() {
+ return delegatee.getRawData();
+ }
+
+ public ResourceMetadata getResourceMetadata() {
+ return delegatee.getResourceMetadata();
+ }
+
+ public String getURI() {
+ return delegatee.getURI();
+ }
+ }
+}
\ No newline at end of file
Added: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestPathInfo.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestPathInfo.java?rev=593572&view=auto
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestPathInfo.java (added)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingRequestPathInfo.java Fri Nov 9 07:41:18 2007
@@ -0,0 +1,199 @@
+/*
+ * 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.core.impl.helper;
+
+import org.apache.sling.api.request.RequestDispatcherOptions;
+import org.apache.sling.api.request.RequestPathInfo;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceMetadata;
+
+/**
+ * microsling request URI parser that provides SlingRequestPathInfo for the
+ * current request, based on the path of the Resource. The values provided by
+ * this depend on the Resource.getURI() value, as the ResourceResolver might use
+ * all or only part of the request URI path to locate the resource (see also
+ * SLING-60 ). What we're after is the remainder of the path, the part that was
+ * not used to locate the Resource, and we split that part in different
+ * subparts: selectors, extension and suffix.
+ *
+ * @see MicroslingRequestPathInfoTest for a number of examples.
+ */
+public class SlingRequestPathInfo implements RequestPathInfo {
+
+ private final String selectorString;
+
+ private final String[] selectors;
+
+ private final String extension;
+
+ private final String suffix;
+
+ private final String resourcePath;
+
+ private final static String[] NO_SELECTORS = new String[0];
+
+ /** break requestPath as required by SlingRequestPathInfo */
+ public SlingRequestPathInfo(Resource r, String requestPath) {
+
+ // ensure the resource
+ if (r == null) {
+ throw new NullPointerException("resource");
+ }
+
+ String pathToParse = requestPath;
+ if (pathToParse == null) {
+ pathToParse = "";
+ }
+
+ resourcePath = (String) r.getResourceMetadata().get(
+ ResourceMetadata.RESOLUTION_PATH);
+ if (resourcePath != null && !"/".equals(resourcePath)
+ && pathToParse.length() >= resourcePath.length()) {
+ pathToParse = pathToParse.substring(resourcePath.length());
+ }
+
+ if (pathToParse.startsWith("/")) {
+
+ // only a suffix exists
+ selectorString = null;
+ selectors = NO_SELECTORS;
+ extension = null;
+ suffix = pathToParse;
+
+ } else {
+
+ // separate selectors/ext from the suffix
+ int firstSlash = pathToParse.indexOf('/');
+ String pathToSplit;
+ if (firstSlash < 0) {
+ pathToSplit = pathToParse;
+ suffix = null;
+ } else {
+ pathToSplit = pathToParse.substring(0, firstSlash);
+ suffix = pathToParse.substring(firstSlash);
+ }
+
+
+ int lastDot = pathToSplit.lastIndexOf('.');
+
+ if (lastDot <= 1) {
+
+ // no selectors if only extension exists or selectors is empty
+ selectorString = null;
+ selectors = NO_SELECTORS;
+
+ } else {
+
+ // no selectors if splitting would give an empty array
+ String tmpSel = pathToSplit.substring(1, lastDot);
+ selectors = tmpSel.split("\\.");
+ selectorString = (selectors.length > 0) ? tmpSel : null;
+
+ }
+
+ // extension only if lastDot is not trailing
+ extension = (lastDot + 1 < pathToSplit.length())
+ ? pathToSplit.substring(lastDot + 1)
+ : null;
+ }
+ }
+
+ private SlingRequestPathInfo(String resourcePath, String selectorString, String extension, String suffix) {
+ this.resourcePath = resourcePath;
+ this.selectorString = selectorString;
+ this.selectors = (selectorString != null) ? selectorString.split("\\.") : NO_SELECTORS;
+ this.extension = extension;
+ this.suffix = suffix;
+ }
+
+ public SlingRequestPathInfo merge(RequestPathInfo baseInfo) {
+ if (getExtension() == null) {
+ return new SlingRequestPathInfo(getResourcePath(),
+ baseInfo.getSelectorString(), baseInfo.getExtension(),
+ baseInfo.getSuffix());
+ }
+
+ return this;
+ }
+
+ public SlingRequestPathInfo merge(RequestDispatcherOptions options) {
+
+ // set to true if any option is set
+ boolean needCreate = false;
+
+ // replacement selectors
+ String selectors = options.get(RequestDispatcherOptions.OPT_REPLACE_SELECTORS);
+ if (selectors != null) {
+ needCreate = true;
+ } else {
+ selectors = getSelectorString();
+ }
+
+ // additional selectors
+ String selectorsAdd = options.get(RequestDispatcherOptions.OPT_ADD_SELECTORS);
+ if (selectorsAdd != null) {
+ if (selectors != null) {
+ selectors += "." + selectorsAdd;
+ } else {
+ selectors = selectorsAdd;
+ }
+ needCreate = true;
+ }
+
+ // suffix replacement
+ String suffix = options.get(RequestDispatcherOptions.REPLACE_SUFFIX);
+ if (suffix != null) {
+ needCreate = true;
+ } else {
+ suffix = getSuffix();
+ }
+
+ if (needCreate) {
+ return new SlingRequestPathInfo(getResourcePath(), selectors,
+ getExtension(), suffix);
+ }
+
+ return this;
+ }
+
+ @Override
+ public String toString() {
+ return "SlingRequestPathInfoParser:" + ", path='" + resourcePath + "'"
+ + ", selectorString='" + selectorString + "'" + ", extension='"
+ + extension + "'" + ", suffix='" + suffix + "'";
+ }
+
+ public String getExtension() {
+ return extension;
+ }
+
+ public String[] getSelectors() {
+ return selectors;
+ }
+
+ public String getSelectorString() {
+ return selectorString;
+ }
+
+ public String getSuffix() {
+ return suffix;
+ }
+
+ public String getResourcePath() {
+ return resourcePath;
+ }
+}
Added: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java?rev=593572&view=auto
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java (added)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/helper/SlingServletContext.java Fri Nov 9 07:41:18 2007
@@ -0,0 +1,197 @@
+/*
+ * 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.core.impl.helper;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+
+import org.apache.sling.core.impl.SlingMainServlet;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The <code>SlingServletContext</code> TODO
+ */
+public class SlingServletContext implements ServletContext {
+
+ /** default log */
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ SlingMainServlet requestHandler;
+
+ public SlingServletContext(SlingMainServlet requestHandler) {
+ this.requestHandler = requestHandler;
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
+ */
+ public Object getAttribute(String name) {
+ return getServletContext().getAttribute(name);
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getAttributeNames()
+ */
+ @SuppressWarnings("unchecked")
+ public Enumeration<String> getAttributeNames() {
+ return getServletContext().getAttributeNames();
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
+ */
+ public String getInitParameter(String name) {
+ return getServletContext().getInitParameter(name);
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getInitParameterNames()
+ */
+ @SuppressWarnings("unchecked")
+ public Enumeration<String> getInitParameterNames() {
+ return getServletContext().getInitParameterNames();
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getMajorVersion()
+ */
+ public int getMajorVersion() {
+ return getServletContext().getMajorVersion();
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
+ */
+ public String getMimeType(String file) {
+ return getServletContext().getMimeType(file);
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#getMinorVersion()
+ */
+ public int getMinorVersion() {
+ return getServletContext().getMinorVersion();
+ }
+
+ public RequestDispatcher getRequestDispatcher(String path) {
+ // return no dispatcher if content is null
+ if (path == null) {
+ log.error("getRequestDispatcher: No path, cannot create request dispatcher");
+ return null;
+ }
+
+ return new SlingRequestDispatcher(path);
+ }
+
+ public URL getResource(String path) throws MalformedURLException {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public InputStream getResourceAsStream(String path) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public Set getResourcePaths(String arg0) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.ServletContext#getServerInfo()
+ */
+ public String getServerInfo() {
+ return requestHandler.getServerInfo();
+ }
+
+ public void log(String message, Throwable throwable) {
+ log.error(message, throwable);
+ }
+
+ public void log(String msg) {
+ log.info(msg);
+ }
+
+ public void log(Exception exception, String msg) {
+ log(msg, exception);
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
+ */
+ public void removeAttribute(String name) {
+ getServletContext().removeAttribute(name);
+ }
+
+ /**
+ * @see javax.servlet.ServletContext#setAttribute(java.lang.String,
+ * java.lang.Object)
+ */
+ public void setAttribute(String name, Object object) {
+ getServletContext().removeAttribute(name);
+ }
+
+ private ServletContext getServletContext() {
+ return requestHandler.getServletContext();
+ }
+
+ public ServletContext getContext(String uripath) {
+ // check whether to return ComponentContext ??
+ return getServletContext().getContext(uripath);
+ }
+
+ public RequestDispatcher getNamedDispatcher(String name) {
+ return getServletContext().getNamedDispatcher(name);
+ }
+
+ public String getRealPath(String path) {
+ return getServletContext().getRealPath(path);
+ }
+
+ public String getServletContextName() {
+ return getServletContext().getServletContextName();
+ }
+
+ @Deprecated
+ public Servlet getServlet(String name) {
+ return null;
+ }
+
+ @Deprecated
+ public Enumeration<?> getServletNames() {
+ return null;
+ }
+
+ @Deprecated
+ public Enumeration<?> getServlets() {
+ return null;
+ }
+
+}
Modified: incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java?rev=593572&r1=593571&r2=593572&view=diff
==============================================================================
--- incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java (original)
+++ incubator/sling/trunk/sling/core/src/main/java/org/apache/sling/core/impl/services/ServiceLocatorImpl.java Fri Nov 9 07:41:18 2007
@@ -114,7 +114,7 @@
}
}
- public void clear() {
+ public void dispose() {
final Iterator<ServiceReference> i = this.references.iterator();
while (i.hasNext()) {
final ServiceReference ref = i.next();