Example usage for javax.servlet.http HttpServletRequest getMethod

List of usage examples for javax.servlet.http HttpServletRequest getMethod

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest getMethod.

Prototype

public String getMethod();

Source Link

Document

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

Usage

From source file:cn.newgxu.lab.core.config.MappingJacksonJsonpView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    if (request.getMethod().toUpperCase().equals("GET")) {
        if (request.getParameterMap().containsKey("callback")) {
            ServletOutputStream ostream = response.getOutputStream();
            //            try
            ostream.write(new String("try{" + request.getParameter("callback") + "(").getBytes());
            super.render(model, request, response);
            ostream.write(new String(");}catch(e){}").getBytes());
            //            ????closeflushspring?
            //            ?
            ostream.flush();/*from w  w w  .ja  v a2  s .  com*/
            ostream.close();
        } else {
            super.render(model, request, response);
        }
    } else {
        super.render(model, request, response);
    }
}

From source file:foo.domaintest.action.RequestModule.java

/** Provides the request method (GET or POST). */
@Provides/*from ww  w  . j av a  2  s  .c o m*/
@RequestData("method")
String provideMethod(HttpServletRequest request) {
    return request.getMethod();
}

From source file:com.castlemock.web.basis.web.mvc.controller.ViewControllerAdvice.java

/**
 * Handles uncaught exceptions and creates an error view that will be displayed to the user.
 * @param request The request that originally caused the exception
 * @param exception The uncaught exception
 * @return Returns a view that displays the exception message
 */// w ww .j  a va2  s .  com
@ResponseBody
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorViewHandler(HttpServletRequest request, Exception exception) {
    LOGGER.error("The following request failed: " + request.getRequestURI() + " (" + request.getMethod() + ")");
    LOGGER.error(exception.getMessage(), exception);
    ModelAndView model = createPartialModelAndView(PAGE);
    model.addObject(TITLE, "An error occurred");
    model.addObject(MESSAGE, exception.getMessage());
    return model;
}

From source file:com.github.rabid_fish.proxy.servlet.ProxySoapServlet.java

boolean sendMockResponse(HttpServletRequest request, HttpServletResponse response) {

    String method = request.getMethod();
    String url = request.getRequestURI();

    // Don't break parent functionality
    if ("CONNECT".equalsIgnoreCase(method)) {
        return false;
    }/*w  w  w  .j  a  va2  s .c o m*/

    if (url.startsWith("/favicon.ico")) {
        return false;
    }

    try {
        String mockBody;

        if (method.equals("POST")) {

            ServletInputStream stream = request.getInputStream();
            String requestBody = IOUtils.toString(stream);

            // Need to save off the body so that the input stream can be reset later
            // by the parent class.
            request.setAttribute(ReverseProxyServlet.PROXY_REQUEST_BODY, requestBody);

            mockBody = mockMapRegexHelper.getMockBodyForBody(requestBody);
            if (mockBody != null) {
                writeMockResponse(response, mockBody);
                return true;
            }
        }

    } catch (Exception e) {
        return false;
    }

    return false;
}

From source file:com.acc.storefront.filters.StorefrontFilter.java

protected boolean isGetMethod(final HttpServletRequest httpRequest) {
    return "GET".equalsIgnoreCase(httpRequest.getMethod());
}

From source file:org.owasp.dependencytrack.controller.token.TokenHandlerInterceptor.java

/**
 * Intercepts an incoming requests, determines if method was POST and enforces token policy.
 * @param request The HttpServletRequest to intercept
 * @param response The HttpServletResponse
 * @param handler not-used but required for interface definition
 * @return a Boolean indicating if the request should be further processed.
 * @throws Exception Required by interface
 *//*from www .j a  v a2  s  .com*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    if ("POST".equalsIgnoreCase(request.getMethod())) {
        if (!TokenManager.isTokenValid(request)) {
            response.sendError(HttpServletResponse.SC_FORBIDDEN, "Incorrect token value");
            return false;
        }
    }
    return true;
}

From source file:de.hybris.platform.chinesepspwechatpay.controllers.misc.ChineseWeChatPayResponseController.java

public String getPostRequestBody(final HttpServletRequest req) {
    if (RequestMethod.POST.name().equalsIgnoreCase(req.getMethod())) {
        final StringBuilder sb = new StringBuilder();
        try (BufferedReader br = req.getReader()) {
            final char[] charBuffer = new char[128];
            int bytesRead;
            while ((bytesRead = br.read(charBuffer)) != -1) {
                sb.append(charBuffer, 0, bytesRead);
            }//from ww w.j av  a  2 s.c o  m
        } catch (final IOException e) {
            LOG.warn("failed to read request body");
        }
        return sb.toString();
    }
    return "";
}

From source file:ar.com.zauber.commons.social.oauth.twitter.Twitter4JOAuthAccessManager.java

/** @see OAuthAccessManager#getAccessToken(HttpServletRequest) */
public final OAuthAccessToken getAccessToken(final HttpServletRequest request) {
    if (!request.getMethod().equals("GET")) {
        throw new OAuthAccessException("Authentication method not supported: " + request.getMethod());
    }//from ww w .  j a va 2 s . c o m

    final String oauthToken = request.getParameter("oauth_token");
    final String oauthVerifier = request.getParameter("oauth_verifier");

    if (oauthToken != null) {
        return getAccessToken(oauthToken, oauthVerifier);
    }

    return null;
}

From source file:dinamica.util.matcher.RegexRequestMatcher.java

/**
 * Performs the match of the request URL ({@code servletPath + pathInfo + queryString}
 * ) against the compiled pattern. If the query string is present, a question mark
 * will be prepended.//from  w  w w. ja  va 2 s  .  c om
 *
 * @param request the request to match
 * @return true if the pattern matches the URL, false otherwise.
 */
public boolean matches(HttpServletRequest request) {
    if (httpMethod != null && request.getMethod() != null && httpMethod != valueOf(request.getMethod())) {
        return false;
    }

    String url = request.getServletPath();
    String pathInfo = request.getPathInfo();
    String query = request.getQueryString();

    if (pathInfo != null || query != null) {
        StringBuilder sb = new StringBuilder(url);

        if (pathInfo != null) {
            sb.append(pathInfo);
        }

        if (query != null) {
            sb.append('?').append(query);
        }
        url = sb.toString();
    }

    if (logger.isDebugEnabled()) {
        logger.debug("Checking match of request : '" + url + "'; against '" + pattern + "'");
    }

    return pattern.matcher(url).matches();
}

From source file:org.addsimplicity.anicetus.web.TelemetryServletFilter.java

@SuppressWarnings("unchecked")
private void setRequestOnSession(TelemetryHttpSession session, HttpServletRequest request) {
    session.setMethod(request.getMethod());
    session.setProtocol(request.getProtocol());
    if (request.getContentType() != null) {
        session.setContentType(request.getContentType(), HeaderType.Request);
    }/*from ww w .  j a  v  a  2  s. c  o  m*/
    session.setRequestURL(request.getRequestURI());

    Enumeration<String> pnames = request.getParameterNames();
    while (pnames.hasMoreElements()) {
        String name = pnames.nextElement();
        session.setParameter(name, request.getParameter(name));
    }

    Enumeration<String> hnames = request.getHeaderNames();
    while (hnames.hasMoreElements()) {
        String name = hnames.nextElement();
        String value = request.getHeader(name);
        if (value != null) {
            session.setHeader(name, request.getHeader(name), HeaderType.Request);
        }
    }

    String parent = request.getHeader(s_PARENT_NAME);
    if (parent == null) {
        parent = request.getParameter(s_PARENT_NAME);
    }

    if (parent != null) {
        try {
            UUID parentId = UUID.fromString(parent);
            session.setParentId(parentId);
        } catch (IllegalArgumentException iae) {
            // TODO - Exception handler
        }

    }
}