Example usage for javax.servlet ServletRequest getContentLength

List of usage examples for javax.servlet ServletRequest getContentLength

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getContentLength.

Prototype

public int getContentLength();

Source Link

Document

Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known ir is greater than Integer.MAX_VALUE.

Usage

From source file:org.webdavaccess.servlet.WebdavServlet.java

/**
 * Gets properties to be set from request
 * /*from w  ww .  j ava2 s  .  c  o  m*/
 * @param req
 * @return Properties
 * @throws ServletException
 */
private Properties[] getPropertiesToSetOrRemove(ServletRequest req) throws ServletException {
    Properties[] props = new Properties[] { new Properties(), new Properties() };
    if (req.getContentLength() == 0)
        return props;
    DocumentBuilder documentBuilder = getDocumentBuilder();
    try {
        Document document = documentBuilder.parse(new InputSource(req.getInputStream()));
        // Get the root element of the document
        Element rootElement = document.getDocumentElement();
        NodeList childList = rootElement.getChildNodes();

        for (int i = 0; i < childList.getLength(); i++) {
            Node currentNode = childList.item(i);
            switch (currentNode.getNodeType()) {
            case Node.ELEMENT_NODE:
                if ("set".equals(currentNode.getLocalName())) {
                    NodeList propList = currentNode.getChildNodes();
                    for (int j = 0; j < propList.getLength(); j++) {
                        Node propNode = propList.item(j);
                        if ("prop".equals(propNode.getLocalName())) {
                            NodeList propNodes = propNode.getChildNodes();
                            for (int k = 0; k < propNodes.getLength(); k++) {
                                Node node = propNodes.item(k);
                                String pname = node.getNodeName();
                                if (pname == null || pname.trim().length() == 0)
                                    continue;
                                String value = node.getTextContent();
                                props[0].setProperty(pname, value);
                            }
                        }
                    }
                } else if ("remove".equals(currentNode.getLocalName())) {
                    NodeList propList = currentNode.getChildNodes();
                    for (int j = 0; j < propList.getLength(); j++) {
                        Node propNode = propList.item(j);
                        if ("prop".equals(propNode.getLocalName())) {
                            NodeList propNodes = propNode.getChildNodes();
                            for (int k = 0; k < propNodes.getLength(); k++) {
                                Node node = propNodes.item(k);
                                String pname = node.getNodeName();
                                if (pname == null || pname.trim().length() == 0)
                                    continue;
                                String value = node.getTextContent();
                                props[1].setProperty(pname, value);
                            }
                        }
                    }
                }
                break;
            case Node.TEXT_NODE:
            default:
                break;
            }
        }
    } catch (Exception e) {
        throw new ServletException("Unable to parse properties set/remove request");
    }
    return props;
}

From source file:org.zilverline.web.RequestDumperFilter.java

/**
 * Time the processing that is performed by all subsequent filters in the current filter stack, including the ultimately invoked
 * servlet.//  w w  w .  java  2 s  .  c  o  m
 * 
 * @param request The servlet request we are processing
 * @param response The servlet response we are creating
 * @param chain The filter chain we are processing
 * 
 * @exception IOException if an input/output error occurs
 * @exception ServletException if a servlet error occurs
 */
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {

    if (filterConfig == null) {
        return;
    }

    log.debug("Request Received at " + (new Timestamp(System.currentTimeMillis())));
    log.debug(" characterEncoding=" + request.getCharacterEncoding());
    log.debug("     contentLength=" + request.getContentLength());
    log.debug("       contentType=" + request.getContentType());
    log.debug("            locale=" + request.getLocale());
    Enumeration locales = request.getLocales();
    StringBuffer localesBuffer = new StringBuffer("           locales=");
    boolean first = true;
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        if (first) {
            first = false;
        } else {
            localesBuffer.append(", ");
        }
        localesBuffer.append(locale.toString());
    }
    log.debug(localesBuffer);
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        StringBuffer paramsBuffer = new StringBuffer();
        paramsBuffer.append("         parameter=" + name + "=");
        String[] values = request.getParameterValues(name);
        for (int i = 0; i < values.length; i++) {
            if (i > 0) {
                paramsBuffer.append(", ");
            }
            paramsBuffer.append(values[i]);
        }
        log.debug(paramsBuffer);
    }
    log.debug("          protocol=" + request.getProtocol());
    log.debug("        remoteAddr=" + request.getRemoteAddr());
    log.debug("        remoteHost=" + request.getRemoteHost());
    log.debug("            scheme=" + request.getScheme());
    log.debug("        serverName=" + request.getServerName());
    log.debug("        serverPort=" + request.getServerPort());
    log.debug("          isSecure=" + request.isSecure());

    // Render the HTTP servlet request properties
    if (request instanceof HttpServletRequest) {
        log.debug("---------------------------------------------");
        HttpServletRequest hrequest = (HttpServletRequest) request;
        log.debug("       contextPath=" + hrequest.getContextPath());
        Cookie[] cookies = hrequest.getCookies();
        if (cookies == null) {
            cookies = new Cookie[0];
        }
        for (int i = 0; i < cookies.length; i++) {
            log.debug("            cookie=" + cookies[i].getName() + "=" + cookies[i].getValue());
        }
        names = hrequest.getHeaderNames();
        while (names.hasMoreElements()) {
            String name = (String) names.nextElement();
            String value = hrequest.getHeader(name);
            log.debug("            header=" + name + "=" + value);
        }
        log.debug("            method=" + hrequest.getMethod());
        log.debug("          pathInfo=" + hrequest.getPathInfo());
        log.debug("       queryString=" + hrequest.getQueryString());
        log.debug("        remoteUser=" + hrequest.getRemoteUser());
        log.debug("requestedSessionId=" + hrequest.getRequestedSessionId());
        log.debug("        requestURI=" + hrequest.getRequestURI());
        log.debug("       servletPath=" + hrequest.getServletPath());
    }
    log.debug("=============================================");

    // Pass control on to the next filter
    chain.doFilter(request, response);

}