Example usage for org.apache.commons.fileupload.servlet ServletRequestContext getContentType

List of usage examples for org.apache.commons.fileupload.servlet ServletRequestContext getContentType

Introduction

In this page you can find the example usage for org.apache.commons.fileupload.servlet ServletRequestContext getContentType.

Prototype

public String getContentType() 

Source Link

Document

Retrieve the content type of the request.

Usage

From source file:com.tremolosecurity.proxy.ProxyRequest.java

public ProxyRequest(HttpServletRequest req, HttpSession session) throws Exception {
    super(req);//from w  w w .j a v a 2 s. c  om

    this.session = session;

    ServletRequestContext reqCtx = new ServletRequestContext(req);
    this.isMultiPart = "POST".equalsIgnoreCase(req.getMethod()) && reqCtx.getContentType() != null
            && reqCtx.getContentType().toLowerCase(Locale.ENGLISH).startsWith("multipart/form-data");

    this.isParamsInBody = true;
    this.isPush = false;
    this.paramList = new ArrayList<String>();

    this.reqParams = new HashMap<String, ArrayList<String>>();
    this.queryString = new ArrayList<NVP>();

    HttpServletRequest request = (HttpServletRequest) super.getRequest();

    if (request.getQueryString() != null && !request.getQueryString().isEmpty()) {
        StringTokenizer toker = new StringTokenizer(request.getQueryString(), "&");
        while (toker.hasMoreTokens()) {
            String qp = toker.nextToken();
            int index = qp.indexOf('=');
            if (index > 0) {
                String name = qp.substring(0, qp.indexOf('='));
                String val = URLDecoder.decode(qp.substring(qp.indexOf('=') + 1), "UTf-8");
                this.queryString.add(new NVP(name, val));
            }
        }
    }

    if (this.isMultiPart) {
        this.isPush = true;
        // Create a factory for disk-based file items
        FileItemFactory factory = new DiskFileItemFactory();

        // Create a new file upload handler
        ServletFileUpload upload = new ServletFileUpload(factory);

        List<FileItem> items = upload.parseRequest(req);

        this.reqFiles = new HashMap<String, ArrayList<FileItem>>();

        for (FileItem item : items) {
            //this.paramList.add(item.getName());

            if (item.isFormField()) {
                ArrayList<String> vals = this.reqParams.get(item.getFieldName());
                if (vals == null) {
                    vals = new ArrayList<String>();
                    this.reqParams.put(item.getFieldName(), vals);
                }
                this.paramList.add(item.getFieldName());

                vals.add(item.getString());
            } else {
                ArrayList<FileItem> vals = this.reqFiles.get(item.getFieldName());
                if (vals == null) {
                    vals = new ArrayList<FileItem>();
                    this.reqFiles.put(item.getFieldName(), vals);
                }

                vals.add(item);
            }
        }

    } else {
        Enumeration enumer = req.getHeaderNames();

        String contentType = null;

        while (enumer.hasMoreElements()) {
            String name = (String) enumer.nextElement();
            if (name.equalsIgnoreCase("content-type") || name.equalsIgnoreCase("content-length")) {
                this.isPush = true;
                if (name.equalsIgnoreCase("content-type")) {
                    contentType = req.getHeader(name);
                }
            }

        }

        if (this.isPush) {
            if (contentType == null || !contentType.startsWith("application/x-www-form-urlencoded")) {
                this.isParamsInBody = false;
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream in = req.getInputStream();
                int len;
                byte[] buffer = new byte[1024];
                while ((len = in.read(buffer)) > 0) {

                    baos.write(buffer, 0, len);
                }

                req.setAttribute(ProxySys.MSG_BODY, baos.toByteArray());
            } else if (contentType.startsWith("application/x-www-form-urlencoded")) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                InputStream in = req.getInputStream();
                int len;
                byte[] buffer = new byte[1024];
                while ((len = in.read(buffer)) > 0) {

                    baos.write(buffer, 0, len);
                }

                StringTokenizer toker = new StringTokenizer(new String(baos.toByteArray()), "&");
                this.orderedList = new ArrayList<NVP>();
                while (toker.hasMoreTokens()) {
                    String token = toker.nextToken();
                    int index = token.indexOf('=');

                    String name = token.substring(0, index);

                    if (name.indexOf('%') != -1) {
                        name = URLDecoder.decode(name, "UTF-8");
                    }

                    String val = "";
                    if (index < (token.length() - 1)) {
                        val = URLDecoder.decode(token.substring(token.indexOf('=') + 1), "UTF-8");
                    }

                    this.orderedList.add(new NVP(name, val));
                    this.paramList.add(name);
                    ArrayList<String> params = this.reqParams.get(name);
                    if (params == null) {
                        params = new ArrayList<String>();
                        this.reqParams.put(name, params);
                    }

                    params.add(val);
                }
            }
        }
    }

}