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:info.magnolia.cms.util.RequestFormUtil.java

public static Map getParameters(HttpServletRequest request) {
    MultipartForm form = Resource.getPostedForm(request);
    if (form == null) {
        // if get use UTF8 decoding
        if (request.getMethod() == "GET") {
            return getURLParametersDecoded(request, "UTF8");
        }//from w  w  w  . ja v  a 2 s. co  m
        return request.getParameterMap();
    }
    return form.getParameters();
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

/**
 * Helper method for passing post-requests
 *///from ww  w .  j  a v a 2s.c o m
@SuppressWarnings({ "JavaDoc" })
private static HttpUriRequest createNewRequest(HttpServletRequest request, String newUrl) throws IOException {
    String method = request.getMethod();
    if (method.equals("POST")) {
        HttpPost httppost = new HttpPost(newUrl);
        if (ServletFileUpload.isMultipartContent(request)) {
            MultipartEntity entity = getMultipartEntity(request);
            httppost.setEntity(entity);
            addCustomHeaders(request, httppost, "Content-Type");
        } else {
            StringEntity entity = getEntity(request);
            httppost.setEntity(entity);
            addCustomHeaders(request, httppost);
        }
        return httppost;
    } else if (method.equals("PUT")) {
        StringEntity entity = getEntity(request);
        HttpPut httpPut = new HttpPut(newUrl);
        httpPut.setEntity(entity);
        addCustomHeaders(request, httpPut);
        return httpPut;
    } else if (method.equals("DELETE")) {
        StringEntity entity = getEntity(request);
        HttpDeleteWithBody httpDelete = new HttpDeleteWithBody(newUrl);
        httpDelete.setEntity(entity);
        addCustomHeaders(request, httpDelete);
        return httpDelete;
    } else {
        HttpGet httpGet = new HttpGet(newUrl);
        addCustomGetHeaders(request, httpGet);
        return httpGet;
    }
}

From source file:fr.paris.lutece.util.http.SecurityUtil.java

/**
 * Write request variables into the dump stringbuffer
 * @param sb The dump stringbuffer//from w  w  w  .  j  ava 2 s. com
 * @param request The HTTP request
 */
private static void dumpVariables(StringBuffer sb, HttpServletRequest request) {
    dumpVariable(sb, "AUTH_TYPE", request.getAuthType());
    dumpVariable(sb, "REQUEST_METHOD", request.getMethod());
    dumpVariable(sb, "PATH_INFO", request.getPathInfo());
    dumpVariable(sb, "PATH_TRANSLATED", request.getPathTranslated());
    dumpVariable(sb, "QUERY_STRING", request.getQueryString());
    dumpVariable(sb, "REQUEST_URI", request.getRequestURI());
    dumpVariable(sb, "SCRIPT_NAME", request.getServletPath());
    dumpVariable(sb, "LOCAL_ADDR", request.getLocalAddr());
    dumpVariable(sb, "SERVER_PROTOCOL", request.getProtocol());
    dumpVariable(sb, "REMOTE_ADDR", request.getRemoteAddr());
    dumpVariable(sb, "REMOTE_HOST", request.getRemoteHost());
    dumpVariable(sb, "HTTPS", request.getScheme());
    dumpVariable(sb, "SERVER_NAME", request.getServerName());
    dumpVariable(sb, "SERVER_PORT", String.valueOf(request.getServerPort()));
}

From source file:lc.kra.servlet.FileManagerServlet.java

@SuppressWarnings("unused")
private static void checkForPost(HttpServletRequest request) throws ServletException {
    if (!"POST".equals(request.getMethod()))
        throw new ServletException("method must be POST");
}

From source file:info.magnolia.cms.util.RequestFormUtil.java

/**
 * returns the value found in the form or the request
 * @param request/*  w w  w.j ava 2 s  .co  m*/
 * @param form
 * @param name
 * @return
 */
public static String getParameter(HttpServletRequest request, MultipartForm from, String name) {
    String param = null;
    if (from != null) {
        param = from.getParameter(name);
    }
    if (param == null) {
        if (request.getMethod().equals("GET")) {
            param = getURLParameterDecoded(request, name, "UTF8");
        } else {
            param = request.getParameter(name);
        }
    }
    return param;

}

From source file:com.gst.infrastructure.security.data.PlatformRequestLog.java

public static PlatformRequestLog from(final StopWatch task, final HttpServletRequest request)
        throws IOException {
    final String requestUrl = request.getRequestURL().toString();

    final Map<String, String[]> parameters = new HashMap<>(request.getParameterMap());
    parameters.remove("password");
    parameters.remove("_");

    return new PlatformRequestLog(task.getStartTime(), task.getTime(), request.getMethod(), requestUrl,
            parameters);//w w  w.  jav  a  2 s . c om
}

From source file:de.adorsys.oauth.authdispatcher.FixedServletUtils.java

/**
 * Creates a new HTTP request from the specified HTTP servlet request.
 *
 * @param sr              The servlet request. Must not be
 *                        {@code null}.//  w  ww.j av a  2s .  c  om
 * @param maxEntityLength The maximum entity length to accept, -1 for
 *                        no limit.
 *
 * @return The HTTP request.
 *
 * @throws IllegalArgumentException The the servlet request method is
 *                                  not GET, POST, PUT or DELETE or the
 *                                  content type header value couldn't
 *                                  be parsed.
 * @throws IOException              For a POST or PUT body that
 *                                  couldn't be read due to an I/O
 *                                  exception.
 */
public static HTTPRequest createHTTPRequest(final HttpServletRequest sr, final long maxEntityLength)
        throws IOException {

    HTTPRequest.Method method = HTTPRequest.Method.valueOf(sr.getMethod().toUpperCase());

    String urlString = reconstructRequestURLString(sr);

    URL url;

    try {
        url = new URL(urlString);

    } catch (MalformedURLException e) {

        throw new IllegalArgumentException("Invalid request URL: " + e.getMessage() + ": " + urlString, e);
    }

    HTTPRequest request = new HTTPRequest(method, url);

    try {
        request.setContentType(sr.getContentType());

    } catch (ParseException e) {

        throw new IllegalArgumentException("Invalid Content-Type header value: " + e.getMessage(), e);
    }

    Enumeration<String> headerNames = sr.getHeaderNames();

    while (headerNames.hasMoreElements()) {
        final String headerName = headerNames.nextElement();
        request.setHeader(headerName, sr.getHeader(headerName));
    }

    if (method.equals(HTTPRequest.Method.GET) || method.equals(HTTPRequest.Method.DELETE)) {

        request.setQuery(sr.getQueryString());

    } else if (method.equals(HTTPRequest.Method.POST) || method.equals(HTTPRequest.Method.PUT)) {

        if (maxEntityLength > 0 && sr.getContentLength() > maxEntityLength) {
            throw new IOException("Request entity body is too large, limit is " + maxEntityLength + " chars");
        }

        Map<String, String[]> parameterMap = sr.getParameterMap();
        StringBuilder builder = new StringBuilder();

        if (!parameterMap.isEmpty()) {
            for (Entry<String, String[]> entry : parameterMap.entrySet()) {
                String key = entry.getKey();
                String[] value = entry.getValue();
                if (value.length > 0 && value[0] != null) {
                    String encoded = URLEncoder.encode(value[0], "UTF-8");
                    builder = builder.append(key).append('=').append(encoded).append('&');
                }
            }
            String queryString = StringUtils.substringBeforeLast(builder.toString(), "&");
            request.setQuery(queryString);

        } else {
            // read body
            StringBuilder body = new StringBuilder(256);

            BufferedReader reader = sr.getReader();

            char[] cbuf = new char[256];

            int readChars;

            while ((readChars = reader.read(cbuf)) != -1) {

                body.append(cbuf, 0, readChars);

                if (maxEntityLength > 0 && body.length() > maxEntityLength) {
                    throw new IOException(
                            "Request entity body is too large, limit is " + maxEntityLength + " chars");
                }
            }

            reader.close();

            request.setQuery(body.toString());
        }
    }

    return request;
}

From source file:cltestgrid.Upload2.java

private static final boolean isMultipartContent(HttpServletRequest request) {
    if (!"post".equals(request.getMethod().toLowerCase())) {
        return false;
    }/*w w w .  java  2s  .  c  om*/
    String contentType = request.getContentType();
    return contentType != null && contentType.toLowerCase().startsWith("multipart");
}

From source file:de.adorsys.oauth.server.FixedServletUtils.java

/**
 * Creates a new HTTP request from the specified HTTP servlet request.
 *
 * @param servletRequest              The servlet request. Must not be
 *                        {@code null}./* w  w  w  . j av a 2 s  .c  om*/
 * @param maxEntityLength The maximum entity length to accept, -1 for
 *                        no limit.
 *
 * @return The HTTP request.
 *
 * @throws IllegalArgumentException The the servlet request method is
 *                                  not GET, POST, PUT or DELETE or the
 *                                  content type header value couldn't
 *                                  be parsed.
 * @throws IOException              For a POST or PUT body that
 *                                  couldn't be read due to an I/O
 *                                  exception.
 */
public static HTTPRequest createHTTPRequest(final HttpServletRequest servletRequest, final long maxEntityLength)
        throws IOException {

    HTTPRequest.Method method = HTTPRequest.Method.valueOf(servletRequest.getMethod().toUpperCase());

    String urlString = reconstructRequestURLString(servletRequest);

    URL url;

    try {
        url = new URL(urlString);

    } catch (MalformedURLException e) {

        throw new IllegalArgumentException("Invalid request URL: " + e.getMessage() + ": " + urlString, e);
    }

    HTTPRequest request = new HTTPRequest(method, url);

    try {
        request.setContentType(servletRequest.getContentType());

    } catch (ParseException e) {

        throw new IllegalArgumentException("Invalid Content-Type header value: " + e.getMessage(), e);
    }

    Enumeration<String> headerNames = servletRequest.getHeaderNames();

    while (headerNames.hasMoreElements()) {
        final String headerName = headerNames.nextElement();
        request.setHeader(headerName, servletRequest.getHeader(headerName));
    }

    if (method.equals(HTTPRequest.Method.GET) || method.equals(HTTPRequest.Method.DELETE)) {

        request.setQuery(servletRequest.getQueryString());

    } else if (method.equals(HTTPRequest.Method.POST) || method.equals(HTTPRequest.Method.PUT)) {

        if (maxEntityLength > 0 && servletRequest.getContentLength() > maxEntityLength) {
            throw new IOException("Request entity body is too large, limit is " + maxEntityLength + " chars");
        }

        Map<String, String[]> parameterMap = servletRequest.getParameterMap();
        StringBuilder builder = new StringBuilder();

        if (!parameterMap.isEmpty()) {
            for (Entry<String, String[]> entry : parameterMap.entrySet()) {
                String key = entry.getKey();
                String[] value = entry.getValue();
                if (value.length > 0 && value[0] != null) {
                    String encoded = URLEncoder.encode(value[0], "UTF-8");
                    builder = builder.append(key).append('=').append(encoded).append('&');
                }
            }
            String queryString = StringUtils.substringBeforeLast(builder.toString(), "&");
            request.setQuery(queryString);

        } else {
            // read body
            StringBuilder body = new StringBuilder(256);

            BufferedReader reader = servletRequest.getReader();
            reader.reset();
            char[] cbuf = new char[256];

            int readChars;

            while ((readChars = reader.read(cbuf)) != -1) {

                body.append(cbuf, 0, readChars);

                if (maxEntityLength > 0 && body.length() > maxEntityLength) {
                    throw new IOException(
                            "Request entity body is too large, limit is " + maxEntityLength + " chars");
                }
            }

            reader.reset();
            // reader.close();

            request.setQuery(body.toString());
        }
    }

    return request;
}

From source file:edu.usu.sdl.openstorefront.security.SecurityUtil.java

/**
 * Constructs an Admin Audit Log Message
 *
 * @param request/* www  . ja v  a2  s . c o  m*/
 * @return message
 */
public static String adminAuditLogMessage(HttpServletRequest request) {
    StringBuilder message = new StringBuilder();

    message.append("User: ").append(getCurrentUserName()).append(" (").append(getClientIp(request)).append(") ")
            .append(" Called Admin API: ");
    if (request != null) {
        message.append(request.getMethod()).append(" ");
        if (StringUtils.isNotBlank(request.getQueryString())) {
            message.append(request.getRequestURL()).append("?").append(request.getQueryString());
        } else {
            message.append(request.getRequestURL());
        }
    }

    return message.toString();
}