Example usage for javax.servlet ServletRequest getParameterValues

List of usage examples for javax.servlet ServletRequest getParameterValues

Introduction

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

Prototype

public String[] getParameterValues(String name);

Source Link

Document

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

Usage

From source file:org.rhq.enterprise.gui.util.WebUtility.java

public static int[] getResourceIds(ServletRequest request) {
    String[] resourceIdStrings = request.getParameterValues(ParamConstants.RESOURCE_ID_PARAM);
    int[] ids;/*from w w w.ja  v  a  2s. c o m*/
    if (resourceIdStrings == null) {
        ids = new int[0];
    } else {
        ids = new int[resourceIdStrings.length];
        for (int i = 0; i < resourceIdStrings.length; i++) {
            ids[i] = Integer.parseInt(resourceIdStrings[i]);
        }
    }

    return ids;
}

From source file:org.rhq.enterprise.gui.util.WebUtility.java

private static void logWarningIfParameterHasMultipleValues(ServletRequest request, String name) {
    if ((request.getParameterValues(name) != null) && (request.getParameterValues(name).length > 1)) {
        LOG.warn("More than one '" + name + "' request parameter exists - exactly one is required (values are "
                + Arrays.asList(request.getParameterValues(name)) + ").");
    }//from  w w  w.  jav a 2s  .  co  m
}

From source file:org.soaplab.clients.spinet.filters.RequestDumperFilter.java

/**
 * Time the processing that is performed by all subsequent filters in the
 * current filter stack, including the ultimately invoked servlet.
 *
 * @param request The servlet request we are processing
 * @param result 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
 *///from  ww  w . j ava 2s. c  o m
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    if (filterConfig == null)
        return;

    // Render the generic servlet request properties
    StringWriter sw = new StringWriter();
    PrintWriter writer = new PrintWriter(sw);
    writer.println("Request Received at " + (new Timestamp(System.currentTimeMillis())));
    writer.println(" characterEncoding=" + request.getCharacterEncoding());
    writer.println("     contentLength=" + request.getContentLength());
    writer.println("       contentType=" + request.getContentType());
    writer.println("            locale=" + request.getLocale());
    writer.print("           locales=");
    Enumeration locales = request.getLocales();
    boolean first = true;
    while (locales.hasMoreElements()) {
        Locale locale = (Locale) locales.nextElement();
        if (first)
            first = false;
        else
            writer.print(", ");
        writer.print(locale.toString());
    }
    writer.println();
    Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        writer.print("         parameter=" + name + "=");
        String values[] = request.getParameterValues(name);
        for (int i = 0; i < values.length; i++) {
            if (i > 0)
                writer.print(", ");
            writer.print(values[i]);
        }
        writer.println();
    }
    writer.println("          protocol=" + request.getProtocol());
    writer.println("        remoteAddr=" + request.getRemoteAddr());
    writer.println("        remoteHost=" + request.getRemoteHost());
    writer.println("            scheme=" + request.getScheme());
    writer.println("        serverName=" + request.getServerName());
    writer.println("        serverPort=" + request.getServerPort());
    writer.println("          isSecure=" + request.isSecure());

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

    // Log the resulting string
    writer.flush();
    filterConfig.getServletContext().log(sw.getBuffer().toString());
    log.info(sw.getBuffer().toString());

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

}

From source file:org.xwiki.wysiwyg.filter.ConversionFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    // Take the list of request parameters that require HTML conversion.
    String[] parametersRequiringHTMLConversion = req.getParameterValues(REQUIRES_HTML_CONVERSION);
    if (parametersRequiringHTMLConversion != null) {
        MutableServletRequestFactory mreqFactory = Utils.getComponent((Type) MutableServletRequestFactory.class,
                req.getProtocol());/*from  w  ww .  j  av  a2  s.  c o  m*/
        // Wrap the current request in order to be able to change request parameters.
        MutableServletRequest mreq = mreqFactory.newInstance(req);
        // Remove the list of request parameters that require HTML conversion to avoid recurrency.
        mreq.removeParameter(REQUIRES_HTML_CONVERSION);
        // Try to convert each parameter from the list and save caught exceptions.
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
        // Save also the output to prevent loosing data in case of conversion exceptions.
        Map<String, String> output = new HashMap<String, String>();
        for (int i = 0; i < parametersRequiringHTMLConversion.length; i++) {
            String parameterName = parametersRequiringHTMLConversion[i];
            String html = req.getParameter(parameterName);
            // Remove the syntax parameter from the request to avoid interference with further request processing.
            String syntax = mreq.removeParameter(parameterName + "_syntax");
            if (html == null || syntax == null) {
                continue;
            }
            try {
                HTMLConverter converter = Utils.getComponent((Type) HTMLConverter.class);
                mreq.setParameter(parameterName, converter.fromHTML(html, syntax));
            } catch (Exception e) {
                LOGGER.error(e.getLocalizedMessage(), e);
                errors.put(parameterName, e);
            }
            // If the conversion fails the output contains the value before the conversion.
            output.put(parameterName, mreq.getParameter(parameterName));
        }

        if (!errors.isEmpty()) {
            handleConversionErrors(errors, output, mreq, res);
        } else {
            chain.doFilter(mreq, res);
        }
    } else {
        chain.doFilter(req, res);
    }
}

From source file:org.xwiki.wysiwyg.server.filter.ConversionFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
        throws IOException, ServletException {
    // Take the list of request parameters that require HTML conversion.
    String[] parametersRequiringHTMLConversion = req.getParameterValues(REQUIRES_HTML_CONVERSION);
    if (parametersRequiringHTMLConversion != null) {
        MutableServletRequestFactory mreqFactory = (MutableServletRequestFactory) Utils
                .getComponent(MutableServletRequestFactory.class, req.getProtocol());
        // Wrap the current request in order to be able to change request parameters.
        MutableServletRequest mreq = mreqFactory.newInstance(req);
        // Remove the list of request parameters that require HTML conversion to avoid recurrency.
        mreq.removeParameter(REQUIRES_HTML_CONVERSION);
        // Try to convert each parameter from the list and save caught exceptions.
        Map<String, Throwable> errors = new HashMap<String, Throwable>();
        // Save also the output to prevent loosing data in case of conversion exceptions.
        Map<String, String> output = new HashMap<String, String>();
        for (int i = 0; i < parametersRequiringHTMLConversion.length; i++) {
            String parameterName = parametersRequiringHTMLConversion[i];
            if (StringUtils.isEmpty(parameterName)) {
                continue;
            }/*  w  w w .java 2s. c  o  m*/
            // Remove the syntax parameter from the request to avoid interference with further request processing.
            String syntax = mreq.removeParameter(parameterName + "_syntax");
            try {
                HTMLConverter converter = Utils.getComponent(HTMLConverter.class);
                mreq.setParameter(parameterName, converter.fromHTML(req.getParameter(parameterName), syntax));
            } catch (Exception e) {
                LOGGER.error(e.getLocalizedMessage(), e);
                errors.put(parameterName, e);
            }
            // If the conversion fails the output contains the value before the conversion.
            output.put(parameterName, mreq.getParameter(parameterName));
        }

        if (!errors.isEmpty()) {
            // In case of a conversion exception we have to redirect the request back and provide a key to access
            // the exception and the value before the conversion from the session.
            // Redirect to the error page specified on the request.
            String redirectURL = mreq.getParameter("xerror");
            if (redirectURL == null) {
                // Redirect to the referrer page.
                redirectURL = mreq.getReferer();
            }
            // Extract the query string.
            String queryString = StringUtils.substringAfterLast(redirectURL, String.valueOf('?'));
            // Remove the query string.
            redirectURL = StringUtils.substringBeforeLast(redirectURL, String.valueOf('?'));
            // Remove the previous key from the query string. We have to do this since this might not be the first
            // time the conversion fails for this redirect URL.
            queryString = queryString.replaceAll("key=.*&?", "");
            if (queryString.length() > 0 && !queryString.endsWith(String.valueOf('&'))) {
                queryString += '&';
            }
            // Save the output and the caught exceptions on the session.
            queryString += "key=" + save(mreq, output, errors);
            mreq.sendRedirect(res, redirectURL + '?' + queryString);
        } else {
            chain.doFilter(mreq, res);
        }
    } else {
        chain.doFilter(req, res);
    }
}

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.//from  ww  w  . j ava2 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);

}