Java Code Examples for javax.servlet.ServletRequest#getContentLength()
The following examples show how to use
javax.servlet.ServletRequest#getContentLength() .
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: HttpUtils.java From utils with Apache License 2.0 | 6 votes |
public static String getString(ServletRequest request) throws Exception { char[] readerBuffer = new char[request.getContentLength()]; BufferedReader bufferedReader = request.getReader(); int portion = bufferedReader.read(readerBuffer); int amount = portion; while (amount < readerBuffer.length) { portion = bufferedReader.read(readerBuffer, amount, readerBuffer.length - amount); amount = amount + portion; } StringBuilder stringBuffer = new StringBuilder((int) (readerBuffer.length * 1.5)); for (int index = 0; index < readerBuffer.length; index++) { char c = readerBuffer[index]; stringBuffer.append(c); } String xml = stringBuffer.toString(); return xml; }
Example 2
Source File: JsonTool.java From velocity-tools with Apache License 2.0 | 6 votes |
/** * Configuration. Parses request body if appropriate. * @param parser configuration values */ protected void configure(ValueParser parser) { super.configure(parser); if (root() == null) { ServletRequest request = (ServletRequest)parser.get(ViewContext.REQUEST); if (request.getContentLength() > 0 && isJsonContentType(request.getContentType())) { try { initJSON(request.getReader()); } catch (Exception e) { getLog().error("could not parse JSON object", e); } } } }
Example 3
Source File: XmlTool.java From velocity-tools with Apache License 2.0 | 6 votes |
/** * Configuration. Parses request body if appropriate. * @param values configuration values */ protected void configure(ValueParser values) { super.configure(values); if (isEmpty()) { ServletRequest request = (ServletRequest)values.get(ViewContext.REQUEST); if (request.getContentLength() > 0) { String mimeType = request.getContentType(); if (XmlUtils.isXmlMimeType(mimeType)) { try { setRoot(XmlUtils.parse(request.getReader())); } catch (Exception e) { getLog().error("could not parse given XML string", e); } } } } }
Example 4
Source File: AbstractJettyServerTestCase.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 5
Source File: ContentLengthFilter.java From nifi with Apache License 2.0 | 5 votes |
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; String httpMethod = httpRequest.getMethod(); // Check the HTTP method because the spec says clients don't have to send a content-length header for methods // that don't use it. So even though an attacker may provide a large body in a GET request, the body should go // unread and a size filter is unneeded at best. See RFC 2616 section 14.13, and RFC 1945 section 10.4. boolean willExamine = maxContentLength > 0 && (httpMethod.equalsIgnoreCase("POST") || httpMethod.equalsIgnoreCase("PUT")); if (!willExamine) { logger.debug("No length check of request with method {} and maximum {}", httpMethod, formatSize(maxContentLength)); chain.doFilter(request, response); return; } HttpServletResponse httpResponse = (HttpServletResponse) response; int contentLength = request.getContentLength(); if (contentLength > maxContentLength) { // Request with a client-specified length greater than our max is rejected: httpResponse.setContentType("text/plain"); httpResponse.getOutputStream().write(("Payload Too Large - limit is " + formatSize(maxContentLength)).getBytes()); httpResponse.setStatus(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE); logger.warn("Content length check rejected request with content-length {} greater than maximum {}", formatSize(contentLength), formatSize(maxContentLength)); } else { // If or when the request is read, this limits the read to our max: logger.debug("Content length check allowed request with content-length {} less than maximum {}", formatSize(contentLength), formatSize(maxContentLength)); chain.doFilter(new LimitedContentLengthRequest(httpRequest, maxContentLength), response); } }