Example usage for javax.servlet ServletException initCause

List of usage examples for javax.servlet ServletException initCause

Introduction

In this page you can find the example usage for javax.servlet ServletException initCause.

Prototype

public synchronized Throwable initCause(Throwable cause) 

Source Link

Document

Initializes the cause of this throwable to the specified value.

Usage

From source file:uk.ac.lancs.e_science.fileUpload.UploadFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (!(request instanceof HttpServletRequest)) {
        chain.doFilter(request, response);
        return;//from   w w w  . j  a  v a  2  s.c om
    }

    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String contentLength = httpRequest.getHeader("Content-Length");
    try {
        if (sizeMax != -1 && contentLength != null && Long.parseLong(contentLength) > sizeMax) {
            ServletException servletEx = new ServletException("Uploaded file size excess maximun legal");
            throw servletEx;
        }
    } catch (NumberFormatException e) {
        e.printStackTrace();
        //nothing
    }

    boolean isMultipartContent = FileUpload.isMultipartContent(httpRequest);
    if (!isMultipartContent) {
        chain.doFilter(request, response);
        return;
    }

    DiskFileUpload upload = new DiskFileUpload();
    if (repositoryPath != null)
        upload.setRepositoryPath(repositoryPath);

    try {

        // SAK-13408 - Websphere cannot properly read the request if it has already been parsed and
        // marked by the Apache Commons FileUpload library. The request needs to be buffered so that 
        // it can be reset for subsequent processing
        if ("websphere".equals(ServerConfigurationService.getString("servlet.container"))) {
            HttpServletRequest bufferedInputRequest = new BufferedHttpServletRequestWrapper(httpRequest);
            httpRequest = bufferedInputRequest;
        }

        List list = upload.parseRequest(httpRequest);

        if ("websphere".equals(ServerConfigurationService.getString("servlet.container"))) {
            httpRequest.getInputStream().reset();
        }

        final Map map = new HashMap();
        for (int i = 0; i < list.size(); i++) {
            FileItem item = (FileItem) list.get(i);
            String str = item.getString("UTF-8");
            if (item.isFormField())
                map.put(item.getFieldName(), new String[] { str });
            else
                httpRequest.setAttribute(item.getFieldName(), item);
        }

        chain.doFilter(new HttpServletRequestWrapper(httpRequest) {
            public Map getParameterMap() {
                return map;
            }

            public String[] getParameterValues(String name) {
                Map map = getParameterMap();
                return (String[]) map.get(name);
            }

            public String getParameter(String name) {
                String[] params = getParameterValues(name);
                if (params == null)
                    return null;
                return params[0];
            }

            public Enumeration getParameterNames() {
                Map map = getParameterMap();
                return Collections.enumeration(map.keySet());
            }
        }, response);
    } catch (FileUploadException ex) {
        ServletException servletEx = new ServletException();
        servletEx.initCause(ex);
        throw servletEx;
    }
}