Example usage for javax.servlet.http HttpServletRequest getInputStream

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

Introduction

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

Prototype

public ServletInputStream getInputStream() throws IOException;

Source Link

Document

Retrieves the body of the request as binary data using a ServletInputStream .

Usage

From source file:com.intuit.autumn.web.InputStreamHttpServletRequestWrapper.java

/**
 * Constructs a request object wrapping the given request.
 *
 * @param request inbound request//from w ww.j  a va  2  s  . co m
 * @throws IllegalArgumentException if the request is null
 */

public InputStreamHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
    super(request);

    body = toByteArray(request.getInputStream());
}

From source file:grails.converters.JSON.java

/**
 * Parses the given request's InputStream and returns ether a JSONObject or a JSONArry
 *
 * @param request the JSON Request//from   w  w w. ja  v a 2  s  .  c om
 * @return ether a JSONObject or a JSONArray - depending on the given JSON
 * @throws ConverterException when the JSON content is not valid
 */
public static Object parse(HttpServletRequest request) throws ConverterException {
    Object json = request.getAttribute(CACHED_JSON);
    if (json != null) {
        return json;
    }

    String encoding = request.getCharacterEncoding();
    if (encoding == null) {
        encoding = Converter.DEFAULT_REQUEST_ENCODING;
    }
    try {
        PushbackInputStream pushbackInputStream = null;
        int firstByte = -1;
        try {
            pushbackInputStream = new PushbackInputStream(request.getInputStream());
            firstByte = pushbackInputStream.read();
        } catch (IOException ioe) {
        }

        if (firstByte == -1) {
            return new JSONObject();
        }

        pushbackInputStream.unread(firstByte);
        json = parse(pushbackInputStream, encoding);
        request.setAttribute(CACHED_JSON, json);
        return json;
    } catch (IOException e) {
        throw new ConverterException("Error parsing JSON", e);
    }
}

From source file:com.angstoverseer.web.servlet.MailServlet.java

@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    mailService.handleIncomingEmail(request.getInputStream());
}

From source file:com.xqdev.jam.MLJAM.java

private static String getBody(HttpServletRequest req) {
    try {//  ww w. ja va 2 s  .c o  m
        // Try reading the post body using characters.
        // This might throw an exception if something on the
        // server side already called getInputStream().
        // In that case we'll pull as bytes.
        Reader reader = null;
        try {
            reader = new BufferedReader(req.getReader());
        } catch (IOException e) {
            reader = new BufferedReader(new InputStreamReader(req.getInputStream(), "UTF-8"));
        }

        StringBuffer sbuf = new StringBuffer();
        char[] cbuf = new char[4096];
        int count = 0;
        while ((count = reader.read(cbuf)) != -1) {
            sbuf.append(cbuf, 0, count);
        }
        return sbuf.toString();
    } catch (IOException e2) {
        throw new ServerProblemException("IOException in reading POST body: " + e2.getMessage());
    }
}

From source file:io.kamax.mxisd.util.GsonParser.java

public <T> T parse(HttpServletRequest req, Class<T> type) throws IOException {
    return gson.fromJson(parse(req.getInputStream()), type);
}

From source file:org.impalaframework.extension.mvc.annotation.resolver.RequestBodyArgumentResolver.java

protected Object getValue(NativeWebRequest webRequest, String encoding) {
    Object nativeRequest = webRequest.getNativeRequest();
    if (nativeRequest instanceof HttpServletRequest) {
        HttpServletRequest req = (HttpServletRequest) nativeRequest;
        try {//www.  j  a v  a2 s.  com
            ServletInputStream inputStream = req.getInputStream();
            String body = FileCopyUtils.copyToString(new InputStreamReader(inputStream, encoding));
            return body;
        } catch (IOException e) {
            //FIXME log
            e.printStackTrace();
            return null;
        }
    }
    return null;
}

From source file:com.thoughtworks.go.agent.testhelpers.FakeArtifactPublisherServlet.java

protected void doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String str = IOUtils.toString(request.getInputStream());
    consoleOutput.append(str);/*  www  .  j  av  a 2  s  .  c o m*/
}

From source file:sample.mvc.ExpliotDemoController.java

@RequestMapping(value = "/csrf/messages/", method = RequestMethod.POST)
public void exploit(HttpServletRequest request, HttpServletResponse response) throws Exception {
    Message messageToSave = messageParser.parse(request.getInputStream());

    messageRepository.save(messageToSave);

    response.sendRedirect(request.getContextPath());
}

From source file:com.vigglet.servlet.PostJsonServlet.java

@Override
protected void processRequest(HttpServletRequest req, HttpServletResponse resp, User user) throws Exception {

    StringWriter writer = new StringWriter();
    IOUtils.copy(req.getInputStream(), writer, "UTF-8");
    String theString = writer.toString();

    T model = JsonUtil.read(theString, getModelClass());
    if (model != null) {
        model = setCompany(user, model);
        JsonUtil.write(resp.getOutputStream(), update(req, resp, user, model));
    } else {/*from  www .j  a v  a  2 s  . com*/
        Logger.getLogger(PostJsonServlet.class.getName()).log(Level.WARNING, "Could not read json!", theString);
    }
}

From source file:LogService.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String data = this.inputStreamToString(request.getInputStream());
    log.info("Event received : " + data);
}