Java Code Examples for javax.servlet.ServletRequest#getInputStream()

The following examples show how to use javax.servlet.ServletRequest#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestHelper.java    From bird-java with MIT License 8 votes vote down vote up
/**
 * 获取请求Body
 *
 * @param request 请求
 * @return string
 */
public static String getBodyString(ServletRequest request) {
    StringBuilder sb = new StringBuilder();

    try (
            InputStream inputStream = request.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")))
    ) {
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        LOGGER.error(e.getMessage(), e);
    }
    return sb.toString();
}
 
Example 2
Source File: ExhaustRequestFilter.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain)
    throws IOException, ServletException
{
  try {
    chain.doFilter(request, response);
  }
  finally {
    if (exhaustRequest(request, response)) {
      try (InputStream in = request.getInputStream()) {
        ByteStreams.exhaust(in);
      }
      catch (Exception e) {
        log.debug("Unable to exhaust request", e);
      }
    }
  }
}
 
Example 3
Source File: WebhookCallbackTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
    HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    if (httpServletRequest.getContentType().equals("application/json")) {
        InputStream inputStream = request.getInputStream();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] buffer = new byte[2048];
        int readCntOnce;

        while ((readCntOnce = inputStream.read(buffer)) >= 0) {
            out.write(buffer, 0, readCntOnce);
        }

        JsonArray elements = new Gson().fromJson(new String(out.toByteArray()), JsonArray.class);
        if (elements.size() == 2) {
            ((HttpServletResponse) response).setStatus(200);
            isSuccess = true;
            return;
        }

        ((HttpServletResponse) response).setStatus(500);
    }
}
 
Example 4
Source File: ServletBlockingHttpExchange.java    From quarkus-http with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getInputStream() {
    ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
    try {
        return request.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: ServletBlockingHttpExchange.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public InputStream getInputStream() {
    ServletRequest request = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getServletRequest();
    try {
        return request.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: AbstractJettyServerTestCase.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
	super.service(req, res);
	long contentLength = req.getContentLength();
	if (contentLength != -1) {
		InputStream in = req.getInputStream();
		long byteCount = 0;
		byte[] buffer = new byte[4096];
		int bytesRead;
		while ((bytesRead = in.read(buffer)) != -1) {
			byteCount += bytesRead;
		}
		assertEquals("Invalid content-length", contentLength, byteCount);
	}
}
 
Example 7
Source File: EDocLiteTestServlet.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * This overridden method saves data to the public static <code>postData</code> variable
 * 
 * @see javax.servlet.GenericServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
 */
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(req.getInputStream()));
    StringBuilder sb = new StringBuilder();
    String result = null;
    while ((result = br.readLine()) != null) {
        sb.append(result + "\n");
    }
    br.close();
    postData = sb.toString();
}
 
Example 8
Source File: GzipServletFilter.java    From syndesis with Apache License 2.0 4 votes vote down vote up
/**
 * Default constructor using wrapped input stream.
 */
GzipServletInputStream(ServletRequest request) throws IOException {
    super();
    gzipStream = new GZIPInputStream(request.getInputStream());
}
 
Example 9
Source File: RemoteRequest.java    From logbook with MIT License 4 votes vote down vote up
default ServletInputStream getInputStream(final ServletRequest request) throws IOException {
    return request.getInputStream();
}