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:com.mtgi.analytics.servlet.ServletRequestBehaviorTrackingAdapter.java

public BehaviorEvent start(ServletRequest request) {

    HttpServletRequest req = (HttpServletRequest) request;
    if (!match(req))
        return null;

    String eventName = getEventName(req);
    BehaviorEvent event = manager.createEvent(eventType, eventName);

    //log relevant request data and parameters to the event.
    EventDataElement data = event.addData();
    data.add("uri", req.getRequestURI());
    data.add("protocol", req.getProtocol());
    data.add("method", req.getMethod());
    data.add("remote-address", req.getRemoteAddr());
    data.add("remote-host", req.getRemoteHost());

    if (this.parameters != null) {
        EventDataElement parameters = data.addElement(PARAMETERS_ELEMENT);
        //include only configured parameters
        for (String name : this.parameters) {
            String[] values = request.getParameterValues(name);
            if (values != null)
                addParameter(parameters, name, values);
        }/*from  ww  w .  j ava 2s  . c  om*/
    } else {
        EventDataElement parameters = data.addElement(PARAMETERS_ELEMENT);
        //include all parameters
        for (Enumeration<?> params = request.getParameterNames(); params.hasMoreElements();) {
            String name = (String) params.nextElement();
            String[] values = request.getParameterValues(name);
            addParameter(parameters, name, values);
        }
    }

    manager.start(event);
    return event;
}

From source file:gov.nih.nci.cabig.caaers.web.filters.BadInputFilter.java

/**
 * Stops requests that contain forbidden string patterns in parameter
 * names and parameter values./*from w  w  w .  j  a  v  a2  s.co  m*/
 *
 * @param request  The servlet request to be processed
 * @param response The servlet response to be created
 * @return false if the request is forbidden, true otherwise.
 * @throws IOException      if an input/output error occurs
 * @throws ServletException if a servlet error occurs
 */
public boolean processAllowsAndDenies(ServletRequest request, ServletResponse response)
        throws IOException, ServletException {

    @SuppressWarnings("unchecked")
    Map<String, String[]> paramMap = request.getParameterMap();
    // Loop through the list of parameters.
    Iterator<String> y = paramMap.keySet().iterator();
    while (y.hasNext()) {
        String name = y.next();
        String[] values = request.getParameterValues(name);

        // See if the name contains a forbidden pattern.
        if (!checkAllowsAndDenies(name, response)) {
            return false;
        }

        // Check the parameter's values for the pattern.
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                String value = values[i];
                if (!checkAllowsAndDenies(value, response)) {
                    return false;
                }
            }
        }
    }

    // No parameter caused a deny.  The request should continue.
    return true;

}

From source file:com.netspective.sparx.form.DialogContext.java

public String getUrlParamsToRecreateDialog(String prefix) {
    StringBuffer sb = new StringBuffer();

    ServletRequest request = getRequest();
    sb.append(prefix);/* w  w  w  .  java2 s. co m*/
    sb.append(fieldStates.getAsUrlParams());

    Set retainedParams = null;
    if (retainReqParams != null) {
        retainedParams = new HashSet();
        for (int i = 0; i < retainReqParams.length; i++) {
            String paramName = retainReqParams[i];
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues != null)
                exportParamAsUrlParam(sb, paramName, paramValues);
            retainedParams.add(paramName);
        }
    }
    boolean retainedAnyParams = retainedParams != null;

    if (dialog.retainRequestParams()) {
        if (dialog.getDialogFlags().flagIsSet(DialogFlags.RETAIN_ALL_REQUEST_PARAMS)) {
            for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
                String paramName = (String) e.nextElement();
                if (paramName.startsWith(Dialog.PARAMNAME_DIALOGPREFIX)
                        || paramName.startsWith(Dialog.PARAMNAME_CONTROLPREFIX)
                        || (retainedAnyParams && retainedParams.contains(paramName)))
                    continue;

                exportParamAsUrlParam(sb, paramName, request.getParameterValues(paramName));
            }
        } else {
            String[] retainParams = dialog.getRetainParams();
            int retainParamsCount = retainParams.length;

            for (int i = 0; i < retainParamsCount; i++) {
                String paramName = retainParams[i];
                if (retainedAnyParams && retainedParams.contains(paramName))
                    continue;

                exportParamAsUrlParam(sb, paramName, request.getParameterValues(paramName));
            }
        }
    }

    return sb.toString();
}

From source file:com.netspective.sparx.form.DialogContext.java

public void exportToXml(Element parent) {
    ServletRequest request = getRequest();
    Element dcElem = parent.getOwnerDocument().createElement("dialog-context");
    dcElem.setAttribute("name", dialog.getName());
    dcElem.setAttribute("transaction", state.getIdentifier());
    fieldStates.exportToXml(dcElem);/*from ww w  . j av a  2 s  .  co m*/

    Set retainedParams = null;
    if (retainReqParams != null) {
        retainedParams = new HashSet();
        for (int i = 0; i < retainReqParams.length; i++) {
            String paramName = retainReqParams[i];
            String[] paramValues = request.getParameterValues(paramName);
            if (paramValues != null)
                exportParamToXml(dcElem, paramName, paramValues);
            retainedParams.add(paramName);
        }
    }
    boolean retainedAnyParams = retainedParams != null;

    if (dialog.retainRequestParams()) {
        if (dialog.getDialogFlags().flagIsSet(DialogFlags.RETAIN_ALL_REQUEST_PARAMS)) {
            for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
                String paramName = (String) e.nextElement();
                if (paramName.startsWith(Dialog.PARAMNAME_DIALOGPREFIX)
                        || paramName.startsWith(Dialog.PARAMNAME_CONTROLPREFIX)
                        || (retainedAnyParams && retainedParams.contains(paramName)))
                    continue;

                exportParamToXml(dcElem, paramName, request.getParameterValues(paramName));
            }
        } else {
            String[] retainParams = dialog.getRetainParams();
            int retainParamsCount = retainParams.length;

            for (int i = 0; i < retainParamsCount; i++) {
                String paramName = retainParams[i];
                if (retainedAnyParams && retainedParams.contains(paramName))
                    continue;

                exportParamToXml(dcElem, paramName, request.getParameterValues(paramName));
            }
        }
    }

    parent.appendChild(dcElem);
}

From source file:edu.vt.middleware.servlet.filter.RequestDumperFilter.java

/** {@inheritDoc} */
@SuppressWarnings(value = "unchecked")
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
        throws IOException, ServletException {
    if (this.config == null) {
        return;//from   w  w w . ja va 2  s.  c  o m
    }

    // Just pass through to next filter if we're not at TRACE level
    if (!logger.isTraceEnabled()) {
        chain.doFilter(request, response);
        return;
    }

    // Create a variable to hold the (possibly different) request
    // passed to downstream filters
    ServletRequest downstreamRequest = request;

    // Render the generic servlet request properties
    final StringWriter sw = new StringWriter();
    final PrintWriter writer = new PrintWriter(sw);
    writer.println("Dumping request...");
    writer.println("-----------------------------------------------------");
    writer.println("REQUEST received " + Calendar.getInstance().getTime());
    writer.println(" characterEncoding=" + request.getCharacterEncoding());
    writer.println("     contentLength=" + request.getContentLength());
    writer.println("       contentType=" + request.getContentType());
    writer.println("            locale=" + request.getLocale());
    writer.print("           locales=");

    final Enumeration<Locale> locales = request.getLocales();
    for (int i = 0; locales.hasMoreElements(); i++) {
        if (i > 0) {
            writer.print(", ");
        }
        writer.print(locales.nextElement());
    }
    writer.println();

    final Enumeration<String> paramNames = request.getParameterNames();
    while (paramNames.hasMoreElements()) {
        final String name = paramNames.nextElement();
        writer.print("         parameter=" + name + "=");

        final 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) {
        final 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());
        }

        final Enumeration<String> headerNames = hrequest.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            final String name = headerNames.nextElement();
            final 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());

        // Create a wrapped request that contains the request body
        // and that we will pass to downstream filters
        final ByteArrayRequestWrapper wrappedRequest = new ByteArrayRequestWrapper(hrequest);
        downstreamRequest = wrappedRequest;
        writer.println(wrappedRequest.getRequestBodyAsString());
    }
    writer.println("-----------------------------------------------------");

    // Log the resulting string
    writer.flush();
    logger.trace(sw.getBuffer().toString());

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

From source file:net.turnbig.pandora.web.Servlets.java

/**
 * ???Request Parameters, copy from spring WebUtils.
 * //from w w w .j  ava  2s  . co m
 * Parameter???.
 */
public static Map<String, Object> getParametersStartingWith(ServletRequest request, String prefix) {
    Enumeration paramNames = request.getParameterNames();
    Map<String, Object> params = new TreeMap<String, Object>();

    String prefix_ = prefix == null ? "" : prefix;
    while ((paramNames != null) && paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        if ("".equals(prefix_) || paramName.startsWith(prefix_)) {
            String unprefixed = paramName.substring(prefix_.length());
            String[] values = request.getParameterValues(paramName);
            if ((values == null) || (values.length == 0)) {
                // Do nothing, no values found at all.
            } else if (values.length > 1) {
                params.put(unprefixed, values);
            } else {
                params.put(unprefixed, values[0]);
            }
        }
    }
    return params;
}

From source file:net.turnbig.pandora.web.Servlets.java

/**
 * ?Request Parameters, copy from spring WebUtils.
 * /*ww  w  . j  a  v  a2 s .c  o  m*/
 * Parameter
 */
@SuppressWarnings("rawtypes")
public static Map<String, Object> getParameters(ServletRequest request) {
    Enumeration paramNames = request.getParameterNames();
    Map<String, Object> params = new TreeMap<String, Object>();
    while ((paramNames != null) && paramNames.hasMoreElements()) {
        String paramName = (String) paramNames.nextElement();
        String[] values = request.getParameterValues(paramName);
        if ((values == null) || (values.length == 0)) {
            // Do nothing, no values found at all.
        } else if (values.length > 1) {
            params.put(paramName, values);
        } else {
            params.put(paramName, values[0]);
        }
    }
    return params;
}

From source file:org.apache.beehive.netui.pageflow.scoping.internal.ScopedRequestImpl.java

public String[] getParameterValues(String paramName) {
    ServletRequest request = getRequest();
    String[] retVals = request.getParameterValues(_scopedContainer.getScopedName(paramName));

    if (retVals == null && _isActiveRequest && paramName.startsWith(AUTOSCOPE_PREFIX)) {
        retVals = request.getParameterValues(paramName);
    }/*from w w  w  .ja  v  a  2  s. c o  m*/

    if (retVals == null && _listenScopes != null) {
        for (int i = 0, len = _listenScopes.size(); retVals == null && i < len; ++i) {
            String key = ScopedServletUtils.getScopedName(paramName, _listenScopes.get(i));
            retVals = request.getParameterValues(key);
        }
    }

    return retVals;
}

From source file:org.hyperic.hq.ui.util.RequestUtils.java

public static String dumpRequestParamsToString(ServletRequest request, boolean html) {
    StringBuffer output = new StringBuffer();
    if (html)// ww  w.  j a  va  2  s. c  o  m
        output.append("<ol>\n");
    for (Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
        String key = (String) e.nextElement();
        String[] values = request.getParameterValues(key);
        if (html)
            output.append("<li>");
        output.append(key + " = ");
        for (int i = 0; i < values.length; i++) {
            output.append(values[i] + "\n ");
        }
    }
    if (html)
        output.append("</ol>\n");
    return output.toString();
}

From source file:org.infoscoop.admin.web.PreviewImpersonationFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {
    request.setAttribute(IS_PREVIEW, Boolean.TRUE);

    Subject previewUser = new Subject();

    List<String> principals = new ArrayList<String>();

    String uidParam = request.getParameter(ISPrincipal.UID_PRINCIPAL);
    if (uidParam != null) {
        principals.add(ISPrincipal.UID_PRINCIPAL);
        principals.add(uidParam);// w w  w . j av a2  s.  c  o  m
        previewUser.getPrincipals().add(new ISPrincipal(ISPrincipal.UID_PRINCIPAL, uidParam));
    }
    for (PrincipalDef def : SessionCreateConfig.getInstance().getPrincipalDefs()) {
        String[] principalValues = request.getParameterValues(def.getType());
        if (principalValues != null) {
            for (int i = 0; i < principalValues.length; i++) {
                if (log.isInfoEnabled())
                    log.info("Set preview principal: PrincipalType=" + def.getType() + ", name="
                            + principalValues[i] + ".");

                principals.add(def.getType());
                principals.add(principalValues[i]);
                previewUser.getPrincipals().add(new ISPrincipal(def.getType(), principalValues[i]));
            }
        }
    }

    // Principal retrieved from AccountManager set AuthenticationService
    AuthenticationService service = AuthenticationService.getInstance();
    IAccountManager manager = null;
    if (service != null)
        manager = service.getAccountManager();
    if (manager != null) {
        for (PrincipalDef def : manager.getPrincipalDefs()) {
            String roleType = def.getType();
            String[] principalValues = request.getParameterValues(roleType);

            for (int i = 0; principalValues != null && i < principalValues.length; i++) {
                if (log.isInfoEnabled())
                    log.info("Set preview principal: PrincipalType=" + roleType + ", name=" + principalValues[i]
                            + ".");
                principals.add(def.getType());
                principals.add(principalValues[i]);
                previewUser.getPrincipals().add(new ISPrincipal(roleType, principalValues[i]));
            }
        }
    }

    request.setAttribute(PRINCIPAL_PARAMS, principals);

    SetPrincipalHttpServletRequest reqwrapper = new SetPrincipalHttpServletRequest((HttpServletRequest) request,
            previewUser);
    filterChain.doFilter(reqwrapper, response);
}