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.parameters;
 
 import java.io.File;
 import java.util.List;
 import java.util.Map;
 
 
 
 public class ParameterSupport {

    
The name of the form encoding request parameter indicating the character encoding of submitted request parameters. This request parameter overwrites any value of the ServletRequest.getCharacterEncoding() method (which unfortunately happens to be returning null most of the time.
 
     public final static String PARAMETER_FORMENCODING = "_charset_";

    
 
     public final static String MARKER_IS_SERVICE_PROCESSING = ParameterSupport.class.getName() + "/ServiceProcessingMarker";
 
     // name of the request attribute caching the ParameterSupport instance
     // used during the request
     private static final String ATTR_NAME = ParameterSupport.class.getName();

    
Content type signaling parameters in request body
 
     private static final String WWW_FORM_URL_ENC = "application/x-www-form-urlencoded";

    
default log
 
     private final Logger log = LoggerFactory.getLogger(getClass());

    
The maximum size allowed for multipart/form-data requests

The default is -1L, which means unlimited.

 
     private static long maxRequestSize = -1L;

    
The directory location where files will be stored
 
     private static File location = null;

    
The maximum size allowed for uploaded files.

The default is -1L, which means unlimited.

 
     private static long maxFileSize = -1L;

    
The size threshold after which the file will be written to disk
 
     private static int fileSizeThreshold = 256000;
    private final HttpServletRequest servletRequest;
    private boolean requestDataUsed;

    
Returns the ParameterSupport instance supporting request parameter for the give request. For a single request only a single instance is actually used. This single instance is cached as a request attribute. If such an attribute already exists which is not an instance of this class, the request parameter is replaced.

Parameters:
request The HttpServletRequest for which to return request parameter support.
Returns:
The ParameterSupport for the given request.
    public static ParameterSupport getInstance(HttpServletRequest request) {
        Object instance = request.getAttribute();
        if (!(instance instanceof ParameterSupport)) {
            instance = new ParameterSupport(request);
            request.setAttribute(instance);
        }
        return (ParameterSupportinstance;
    }

    
Returns a HttpServletRequestWrapper which implements request parameter access backed by an instance of the ParameterSupport class.

If used in a Servlet API 3 context, this method supports the additional Part API introduced with Servlet API 3.

Parameters:
request The HttpServletRequest to wrap
Returns:
The wrapped request
        try {
            if (request.getClass().getMethod("getServletContext") != null) {
                return new ParameterSupportHttpServletRequestWrapper3(request);
            }
        } catch (Exception e) {
            // If the getServletContext method does not exist or
            // is not visible, fall back to a Servlet API 2.x wrapper
        }
        return new ParameterSupportHttpServletRequestWrapper2x(request);
    }
    static void configure(final long maxRequestSizefinal String locationfinal long maxFileSize,
            final int fileSizeThreshold) {
        . = (maxRequestSize > 0) ? maxRequestSize : -1;
        . = (location != null) ? new File(location) : null;
        . = (maxFileSize > 0) ? maxFileSize : -1;
        . = (fileSizeThreshold > 0) ? fileSizeThreshold : 256000;
    }
    private ParameterSupport(HttpServletRequest servletRequest) {
        this. = servletRequest;
    }
        return ;
    }
    public boolean requestDataUsed() {
        return this.;
    }
    public String getParameter(String name) {
        return getRequestParameterMapInternal().getStringValue(name);
    }
    public String[] getParameterValues(String name) {
        return getRequestParameterMapInternal().getStringValues(name);
    }
    public Map<StringString[]> getParameterMap() {
    }
    public Enumeration<StringgetParameterNames() {
        return new Enumeration<String>() {
            private final Iterator<Stringbase = ParameterSupport.this.getRequestParameterMapInternal().keySet().iterator();
            public boolean hasMoreElements() {
                return this..hasNext();
            }
            public String nextElement() {
                return this..next();
            }
        };
    }
        return getRequestParameterMapInternal().getValue(name);
    }
    public RequestParameter[] getRequestParameters(String name) {
        return getRequestParameterMapInternal().getValues(name);
    }
    public Object getPart(String name) {
        return getRequestParameterMapInternal().getPart(name);
    }
    public Collection<?> getParts() {
        return getRequestParameterMapInternal().getParts();
    }
        return getRequestParameterMapInternal();
    }
    }
        if (this. == null) {
            // SLING-508 Try to force servlet container to decode parameters
            // as ISO-8859-1 such that we can recode later
            String encoding = getServletRequest().getCharacterEncoding();
            if (encoding == null) {
                encoding = .;
                try {
                    getServletRequest().setCharacterEncoding(encoding);
                } catch (UnsupportedEncodingException uee) {
                    throw new SlingUnsupportedEncodingException(uee);
                }
            }
            // SLING-152 Get parameters from the servlet Container
            ParameterMap parameters = new ParameterMap();
            // fallback is only used if this request has been started by a service call
            boolean useFallback = getServletRequest().getAttribute() != null;
            // Query String
            final String query = getServletRequest().getQueryString();
            if (query != null) {
                try {
                    InputStream input = Util.toInputStream(query);
                    Util.parseQueryString(inputencodingparametersfalse);
                } catch (IllegalArgumentException e) {
                    this..error("getRequestParameterMapInternal: Error parsing request"e);
                } catch (UnsupportedEncodingException e) {
                    throw new SlingUnsupportedEncodingException(e);
                } catch (IOException e) {
                    this..error("getRequestParameterMapInternal: Error parsing request"e);
                }
                useFallback = false;
            }
            // POST requests
            final boolean isPost = "POST".equals(this.getServletRequest().getMethod());
            if (isPost) {
                // WWW URL Form Encoded POST
                if (isWWWFormEncodedContent(this.getServletRequest())) {
                    try {
                        InputStream input = this.getServletRequest().getInputStream();
                        Util.parseQueryString(inputencodingparametersfalse);
                    } catch (IllegalArgumentException e) {
                        this..error("getRequestParameterMapInternal: Error parsing request"e);
                    } catch (UnsupportedEncodingException e) {
                        throw new SlingUnsupportedEncodingException(e);
                    } catch (IOException e) {
                        this..error("getRequestParameterMapInternal: Error parsing request"e);
                    }
                    this. = true;
                    useFallback = false;
                }
                // Multipart POST
                if (ServletFileUpload.isMultipartContent(new ServletRequestContext(this.getServletRequest()))) {
                    this.parseMultiPartPost(parameters);
                    this. = true;
                    useFallback = false;
                }
            }
            if ( useFallback ) {
                getContainerParameters(parametersencoding);
            }
            // apply any form encoding (from '_charset_') in the parameter map
            Util.fixEncoding(parameters);
            this. = parameters;
        }
        return this.;
    }
    private void getContainerParameters(final ParameterMap parametersfinal String encoding) {
        final Map<?, ?> pMap = getServletRequest().getParameterMap();
        for (Map.Entry<?, ?> entry : pMap.entrySet()) {
            final String name = (Stringentry.getKey();
            final String[] values = (String[]) entry.getValue();
            for (int i = 0; i < values.lengthi++) {
                parameters.addParameter(new ContainerRequestParameter(
                    namevalues[i], encoding), false);
            }
        }
    }
    private static final boolean isWWWFormEncodedContent(HttpServletRequest request) {
        String contentType = request.getContentType();
        if (contentType == null) {
            return false;
        }
        // This check assumes the content type ends after the WWW_FORM_URL_ENC
        // or continues with blank or semicolon. It will probably break if
        // the content type is some string extension of WWW_FORM_URL_ENC
        // such as "application/x-www-form-urlencoded-bla"
        if (contentType.toLowerCase(.).startsWith()) {
            return true;
        }
        return false;
    }
    private void parseMultiPartPost(ParameterMap parameters) {
        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload();
            .));
        RequestContext rc = new ServletRequestContext(this.getServletRequest()) {
            @Override
            public String getCharacterEncoding() {
                String enc = super.getCharacterEncoding();
                return (enc != null) ? enc : .;
            }
        };
        // Parse the request
        List<?> /* FileItem */items = null;
        try {
            items = upload.parseRequest(rc);
        } catch (FileUploadException fue) {
            this..error("parseMultiPartPost: Error parsing request"fue);
        }
        if (items != null && items.size() > 0) {
            for (Iterator<?> ii = items.iterator(); ii.hasNext();) {
                FileItem fileItem = (FileItemii.next();
                RequestParameter pp = new MultipartRequestParameter(fileItem);
                parameters.addParameter(ppfalse);
            }
        }
    }
New to GrepCode? Check out our FAQ X