Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.engine.impl.request;
 
 import static org.apache.sling.api.SlingConstants.SLING_CURRENT_SERVLET_NAME;
 
 
 
The RequestData class provides access to objects which are set on a Servlet Request wide basis such as the repository session, the persistence manager, etc. The setup order is:
  1. Invoke constructor
  2. Invoke initResource()
  3. Invoke initServlet()

See also:
ContentData
 
 public class RequestData {

    
The default value for the number of recursive inclusions for a single instance of this class (value is 50).
 
     public static final int DEFAULT_MAX_INCLUSION_COUNTER = 50;

    
The default value for the number of calls to the service(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method for a single instance of this class (value is 1000).
 
     public static final int DEFAULT_MAX_CALL_COUNTER = 1000;

    
The name of the request attribute providing the resource addressed by the request URL.
 
     public static final String REQUEST_RESOURCE_PATH_ATTR = "$$sling.request.resource$$";

    
The maximum inclusion depth (default DEFAULT_MAX_INCLUSION_COUNTER). This value is compared to the number of entries in the contentDataStack when the pushContent(org.apache.sling.api.resource.Resource,org.apache.sling.api.request.RequestPathInfo) method is called.
 
     private static int maxInclusionCounter = ;

    
The maximum number of scripts which may be included through the service(org.apache.sling.api.SlingHttpServletRequest,org.apache.sling.api.SlingHttpServletResponse) method (default DEFAULT_MAX_CALL_COUNTER). This number should not be too small to prevent request aborts.
    private static int maxCallCounter = ;
    private static SlingMainServlet SLING_MAIN_SERVLET;
    private static SlingHttpServletRequestFactory REQUEST_FACTORY;

    
The SlingMainServlet used for request dispatching and other stuff
    private final long startTimestamp;

    
The original servlet Servlet Request Object
    private HttpServletRequest servletRequest;

    
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
the current ContentData
    private ContentData currentContentData;

    
    private int servletCallCounter;

    
The name of the currently active serlvet.

    private String activeServletName;

    
Recursion depth
    private int recursionDepth;

    
The peak value for the recursion depth.
    private int peakRecusionDepth;
    public static void setMaxCallCounter(int maxCallCounter) {
        . = maxCallCounter;
    }
    public static int getMaxCallCounter() {
        return ;
    }
    public static void setMaxIncludeCounter(int maxInclusionCounter) {
        . = maxInclusionCounter;
    }
    public static int getMaxIncludeCounter() {
        return ;
    }
    public static void setSlingMainServlet(final SlingMainServlet slingMainServlet) {
        . = slingMainServlet;
        . = null;
    }
    public RequestData(SlingRequestProcessorImpl slingRequestProcessor,
            HttpServletRequest requestHttpServletResponse response) {
        this. = System.currentTimeMillis();
        this. = slingRequestProcessor;
        this. = request;
        this. = response;
        this. = new SlingHttpServletResponseImpl(this,
            );
        this..log(
        		"Method={0}, PathInfo={1}",
        		this..getMethod(), this..getPathInfo()
        );
    }
    public Resource initResource(ResourceResolver resourceResolver) {
        // keep the resource resolver for request processing
        this. = resourceResolver;
        // resolve the resource
        .startTimer("ResourceResolution");
        final SlingHttpServletRequest request = getSlingRequest();
        Resource resource = resourceResolver.resolve(requestrequest.getPathInfo());
        if (request.getAttribute() == null) {
            request.setAttribute(resource.getPath());
        }
        .logTimer("ResourceResolution",
            "URI={0} resolves to Resource={1}",
            getServletRequest().getRequestURI(), resource);
        return resource;
    }
    public void initServlet(final Resource resource,
            final ServletResolver sr) {
        // the resource and the request path info, will never be null
        RequestPathInfo requestPathInfo = new SlingRequestPathInfo(resource);
        ContentData contentData = setContent(resourcerequestPathInfo);
	    .log("Resource Path Info: {0}"requestPathInfo);
        // finally resolve the servlet for the resource
        .startTimer("ServletResolution");
        Servlet servlet = sr.resolveServlet();
        .logTimer("ServletResolution",
            "URI={0} handled by Servlet={1}",
            getServletRequest().getRequestURI(), (servlet == null ? "-none-" : RequestUtil.getServletName(servlet)));
        contentData.setServlet(servlet);
    }
        return ;
    }
        return ;
    }
        return ;
    }
        return ;
    }
        return ;
    }
    // ---------- Request Helper

    
Unwraps the ServletRequest to a SlingHttpServletRequest.

Throws:
java.lang.IllegalArgumentException If the request is not a SlingHttpServletRequest and not a ServletRequestWrapper wrapping a SlingHttpServletRequest.
    public static SlingHttpServletRequest unwrap(ServletRequest request) {
        // early check for most cases
        if (request instanceof SlingHttpServletRequest) {
            return (SlingHttpServletRequestrequest;
        }
        // unwrap wrappers
        while (request instanceof ServletRequestWrapper) {
            request = ((ServletRequestWrapperrequest).getRequest();
            // immediate termination if we found one
            if (request instanceof SlingHttpServletRequest) {
                return (SlingHttpServletRequestrequest;
            }
        }
        // if we unwrapped everything and did not find a
        // SlingHttpServletRequest, we lost
        throw new IllegalArgumentException(
            "ServletRequest not wrapping SlingHttpServletRequest");
    }

    
Unwraps the SlingHttpServletRequest to a SlingHttpServletRequestImpl

Parameters:
request
Throws:
java.lang.IllegalArgumentException If request is not a SlingHttpServletRequestImpl and not SlingHttpServletRequestWrapper wrapping a SlingHttpServletRequestImpl.
    public static SlingHttpServletRequestImpl unwrap(
            SlingHttpServletRequest request) {
        while (request instanceof SlingHttpServletRequestWrapper) {
            request = ((SlingHttpServletRequestWrapperrequest).getSlingRequest();
        }
        if (request instanceof SlingHttpServletRequestImpl) {
            return (SlingHttpServletRequestImplrequest;
        }
        throw new IllegalArgumentException(
            "SlingHttpServletRequest not of correct type");
    }

    
Unwraps the ServletRequest to a SlingHttpServletRequest.

Throws:
java.lang.IllegalArgumentException If the response is not a SlingHttpServletResponse and not a ServletResponseWrapper wrapping a SlingHttpServletResponse.
    public static SlingHttpServletResponse unwrap(ServletResponse response) {
        // early check for most cases
        if (response instanceof SlingHttpServletResponse) {
            return (SlingHttpServletResponseresponse;
        }
        // unwrap wrappers
        while (response instanceof ServletResponseWrapper) {
            response = ((ServletResponseWrapperresponse).getResponse();
            // immediate termination if we found one
            if (response instanceof SlingHttpServletResponse) {
                return (SlingHttpServletResponseresponse;
            }
        }
        // if we unwrapped everything and did not find a
        // SlingHttpServletResponse, we lost
        throw new IllegalArgumentException(
            "ServletResponse not wrapping SlingHttpServletResponse");
    }

    
Unwraps a SlingHttpServletResponse to a SlingHttpServletResponseImpl

Parameters:
response
Throws:
java.lang.IllegalArgumentException If response is not a SlingHttpServletResponseImpl and not SlingHttpServletResponseWrapper wrapping a SlingHttpServletResponseImpl.
    public static SlingHttpServletResponseImpl unwrap(
            SlingHttpServletResponse response) {
        while (response instanceof SlingHttpServletResponseWrapper) {
            response = ((SlingHttpServletResponseWrapperresponse).getSlingResponse();
        }
        if (response instanceof SlingHttpServletResponseImpl) {
            return (SlingHttpServletResponseImplresponse;
        }
        throw new IllegalArgumentException(
            "SlingHttpServletResponse not of correct type");
    }

    

Parameters:
request
Throws:
java.lang.IllegalArgumentException If the request is not a SlingHttpServletRequest and not a ServletRequestWrapper wrapping a SlingHttpServletRequest.
    public static RequestData getRequestData(ServletRequest request) {
        return unwrap(unwrap(request)).getRequestData();
    }

    

Parameters:
request
Throws:
java.lang.IllegalArgumentException If request is not a SlingHttpServletRequestImpl and not SlingHttpServletRequestWrapper wrapping a SlingHttpServletRequestImpl.
    public static RequestData getRequestData(SlingHttpServletRequest request) {
        return unwrap(request).getRequestData();
    }

    

Parameters:
request
Throws:
java.lang.IllegalArgumentException if request is not a HttpServletRequest of if request is not backed by SlingHttpServletRequestImpl.
            ServletRequest request) {
        // unwrap to SlingHttpServletRequest, may throw if no
        // SlingHttpServletRequest is wrapped in request
        SlingHttpServletRequest cRequest = unwrap(request);
        // ensure the SlingHttpServletRequest is backed by
        // SlingHttpServletRequestImpl
        RequestData.unwrap(cRequest);
        // if the request is not wrapper at all, we are done
        if (cRequest == request) {
            return cRequest;
        }
        // ensure the request is a HTTP request
        if (!(request instanceof HttpServletRequest)) {
            throw new IllegalArgumentException("Request is not an HTTP request");
        }
        // otherwise, we create a new response wrapping the servlet response
        // and unwrapped component response
        return new SlingServletRequestAdapter(cRequest,
            (HttpServletRequestrequest);
    }

    

Parameters:
response
Throws:
java.lang.IllegalArgumentException if response is not a HttpServletResponse of if response is not backed by SlingHttpServletResponseImpl.
            ServletResponse response) {
        // unwrap to SlingHttpServletResponse
        SlingHttpServletResponse cResponse = unwrap(response);
        // check type of response, don't care actually for the response itself
        RequestData.unwrap(cResponse);
        // if the servlet response is actually the SlingHttpServletResponse, we
        // are done
        if (cResponse == response) {
            return cResponse;
        }
        // ensure the response is a HTTP response
        if (!(response instanceof HttpServletResponse)) {
            throw new IllegalArgumentException(
                "Response is not an HTTP response");
        }
        // otherwise, we create a new response wrapping the servlet response
        // and unwrapped component response
        return new SlingServletResponseAdapter(cResponse,
            (HttpServletResponseresponse);
    }

    
Helper method to call the servlet for the current content data. If the current content data has no servlet, NOT_FOUND (404) error is sent and the method terminates.

If the the servlet exists, the org.apache.sling.api.SlingConstants.SLING_CURRENT_SERVLET_NAME request attribute is set to the name of that servlet and that servlet name is also set as the currently active servlet. After the termination of the servlet (normal or throwing a Throwable) the request attribute is reset to the previous value. The name of the currently active servlet is only reset to the previous value if the servlet terminates normally. In case of a Throwable, the active servlet name is not reset and indicates which servlet caused the potential abort of the request.

Parameters:
request The request object for the service method
response The response object for the service method
Throws:
java.io.IOException May be thrown by the servlet's service method
javax.servlet.ServletException May be thrown by the servlet's service method
    public static void service(SlingHttpServletRequest request,
            SlingHttpServletResponse responsethrows IOException,
            ServletException {
        RequestData requestData = RequestData.getRequestData(request);
        Servlet servlet = requestData.getContentData().getServlet();
        if (servlet == null) {
            response.sendError(.,
                "No Servlet to handle request");
        } else {
            String name = RequestUtil.getServletName(servlet);
            // verify the number of service calls in this request
            if (requestData.servletCallCounter >= ) {
                throw new TooManyCallsException(name);
            }
            // replace the current servlet name in the request
            Object oldValue = request.getAttribute();
            request.setAttribute(name);
            // setup the tracker for this service call
            String timerName = name + "#" + requestData.servletCallCounter;
            requestData.servletCallCounter++;
            requestData.getRequestProgressTracker().startTimer(timerName);
            try {
                String callerServlet = requestData.setActiveServletName(name);
                servlet.service(requestresponse);
                requestData.setActiveServletName(callerServlet);
            } finally {
                request.setAttribute(oldValue);
                requestData.getRequestProgressTracker().logTimer(timerName);
            }
        }
    }
    // ---------- Content inclusion stacking -----------------------------------
    public ContentData setContent(final Resource resource,
            final RequestPathInfo requestPathInfo) {
        if ( this. >=  ) {
            throw new RecursionTooDeepException(requestPathInfo.getResourcePath());
        }
        this.++;
        if (this. > this.) {
            this. = this.;
        }
         = new ContentData(resourcerequestPathInfo);
        return ;
    }
    public void resetContent(final ContentData data) {
        this.--;
         = data;
    }
    public ContentData getContentData() {
        return ;
    }
        return ;
    }
        return ;
    }
    public int getPeakRecusionDepth() {
        return ;
    }
    public int getServletCallCount() {
        return ;
    }
    public long getElapsedTimeMsec() {
        return System.currentTimeMillis() - ;
    }

    
Sets the name of the currently active servlet and returns the name of the previously active servlet.
    public String setActiveServletName(String servletName) {
        String old = ;
         = servletName;
        return old;
    }

    
Returns the name of the currently active servlet. If this name is not null at the end of request processing, more precisly in the case of an uncaught Throwable at the end of request processing, this is the name of the servlet causing the uncaught Throwable.
    public String getActiveServletName() {
        return ;
    }
    public <Type> Type adaptTo(Object objectClass<Type> type) {
        return .adaptTo(objecttype);
    }
    public String getMimeType(String fileName) {
        return .getMimeType(fileName);
    }
    // ---------- Parameter support -------------------------------------------
    public ServletInputStream getInputStream() throws IOException {
        if ( != null && .requestDataUsed()) {
            throw new IllegalStateException(
                "Request Data has already been read");
        }
        // may throw IllegalStateException if the reader has already been
        // acquired
        return getServletRequest().getInputStream();
    }
            IOException {
        if ( != null && .requestDataUsed()) {
            throw new IllegalStateException(
                "Request Data has already been read");
        }
        // may throw IllegalStateException if the input stream has already been
        // acquired
        return getServletRequest().getReader();
    }
        if ( == null) {
             = ParameterSupport.getInstance(getServletRequest());
        }
        return ;
    }
    // SlingHttpServletRequest instance factory
        if (factory == null) {
            SlingMainServlet servlet = .;
            if (servlet == null || servlet.getServletContext() == null
                || servlet.getServletContext().getMajorVersion() < 3) {
                factory = new SlingHttpServletRequestFactory() {
                    public SlingHttpServletRequest createRequest(RequestData requestDataHttpServletRequest request) {
                        return new SlingHttpServletRequestImpl(requestDatarequest);
                    }
                };
            } else {
                factory = new SlingHttpServletRequestFactory() {
                    public SlingHttpServletRequest createRequest(RequestData requestDataHttpServletRequest request) {
                        return new SlingHttpServletRequestImpl3(requestDatarequest);
                    }
                };
            }
            . = factory;
        }
        return factory;
    }
    private static interface SlingHttpServletRequestFactory {
        SlingHttpServletRequest createRequest(RequestData requestDataHttpServletRequest request);
    }
New to GrepCode? Check out our FAQ X