Example usage for javax.servlet.http HttpServletRequest getContentLength

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

Introduction

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

Prototype

public int getContentLength();

Source Link

Document

Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known ir is greater than Integer.MAX_VALUE.

Usage

From source file:simplestorage.controllers.HashtableController.java

@RequestMapping(value = "/hashtable/{key}", method = RequestMethod.POST)
public ModelAndView put(HttpServletRequest request, HttpServletResponse response, @PathVariable String key)
        throws IOException {
    // If I can't read from input stream, there is nothing I can do about it.
    InputStream input = request.getInputStream();
    int length = request.getContentLength();
    if (length != -1) {
        byte[] value = new byte[length];
        input.read(value, 0, request.getContentLength());
        String userIP = request.getRemoteAddr();
        int userPort = request.getRemotePort();
        hashtableSvc.put(key, new String(value, "UTF-8"), String.format("%s:%s", userIP, userPort));
        return constructModelAndView(gson.toJson(Boolean.TRUE));
    } else {//from ww w  .ja va2 s  .  c o m
        throw new IOException("Content-Length unknown.");
    }
}

From source file:org.atomserver.server.servlet.BlockingFilter.java

private boolean isContentLengthNotSet(final HttpServletRequest request) {
    boolean isWrite = request.getMethod().equals("POST") || request.getMethod().equals("PUT");
    return isWrite && (settings.getMaxContentLength() >= 0) && (request.getContentLength() == -1);
}

From source file:com.google.acre.servlet.ProxyPassServlet.java

/**
 * Sets up the given {@link PostMethod} to send the same standard POST
 * data as was sent in the given {@link HttpServletRequest}
 * @param postMethodProxyRequest The {@link PostMethod} that we are
 *                                configuring to send a standard POST request
 * @param httpServletRequest The {@link HttpServletRequest} that contains
 *                            the POST data to be sent via the {@link PostMethod}
 *///from  ww w  .  j  ava2s  .c  o  m
private void handleStandardPost(HttpPost postMethodProxyRequest, HttpServletRequest hsr) throws IOException {
    HttpEntity re = new InputStreamEntity(hsr.getInputStream(), hsr.getContentLength());
    postMethodProxyRequest.setEntity(re);
}

From source file:com.nominanuda.web.http.ServletHelper.java

/**
 * @param servletRequest//from w  ww .  j a  v  a2 s  .c  om
 * @return an InputStream that contains at least one byte or null if underlying stream is null or empty
 * @throws IOException if thrown while accessing the underlying ServletInputStream
 */
public @Nullable InputStream getServletRequestBody(HttpServletRequest servletRequest) throws IOException {
    ServletInputStream sis = servletRequest.getInputStream();
    if (servletRequest.getContentLength() > 0) {
        return sis;
    } else {
        return null;
    }
}

From source file:gwtupload.server.gae.BlobstoreUploadAction.java

@Override
public void checkRequest(HttpServletRequest request) {
    logger.debug("BLOB-STORE-SERVLET: (" + request.getSession().getId() + ") procesing a request with size: "
            + request.getContentLength() + " bytes.");
}

From source file:org.opencastproject.staticfiles.endpoint.StaticFileRestServiceTest.java

@Test
public void testStoreStaticFileInputHttpServletRequest() throws FileUploadException, Exception {
    // Setup static file service.
    StaticFileService fileService = EasyMock.createMock(StaticFileService.class);
    String fileUuid = "12345";
    String fileName = "other.mov";
    EasyMock.expect(fileService.storeFile(eq(fileName), anyObject(InputStream.class))).andReturn(fileUuid);
    EasyMock.expect(fileService.getFileName(fileUuid)).andReturn(fileName);
    EasyMock.replay(fileService);//from  w  ww .j a  v  a  2 s .c o  m

    // Run the test
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.activate(getComponentContext(null, 100000000L));
    staticFileRestService.setSecurityService(getSecurityService());
    staticFileRestService.setStaticFileService(fileService);

    // Test a good store request
    Response result = staticFileRestService.postStaticFile(newMockRequest());
    assertEquals(Status.CREATED.getStatusCode(), result.getStatus());
    assertTrue(result.getMetadata().size() > 0);
    assertTrue(result.getMetadata().get("location").size() > 0);
    String location = result.getMetadata().get("location").get(0).toString();
    String uuid = location.substring(location.lastIndexOf("/") + 1);

    // assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(MOCK_FILE_CONTENT.getBytes("UTF-8")),
    // staticFileServiceImpl.getFile(uuid)));

    // Test a request with too large of an input stream
    HttpServletRequest tooLargeRequest = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(tooLargeRequest.getContentLength()).andReturn(1000000000).anyTimes();
    EasyMock.replay(tooLargeRequest);
    result = staticFileRestService.postStaticFile(tooLargeRequest);
    assertEquals(Status.BAD_REQUEST.getStatusCode(), result.getStatus());

    staticFileRestService.activate(getComponentContext("true", 100000000L));
    URI staticFileURL = staticFileRestService.getStaticFileURL(uuid);
    assertEquals("http://localhost/staticfiles/mh_default_org/" + uuid + "/other.mov",
            staticFileURL.toString());
}

From source file:com.zimbra.cs.zimlet.ProxyServlet.java

private byte[] copyPostedData(HttpServletRequest req) throws IOException {
    int size = req.getContentLength();
    if (req.getMethod().equalsIgnoreCase("GET") || size <= 0) {
        return null;
    }/*from  w  ww . j a  va 2s. co m*/
    InputStream is = req.getInputStream();
    ByteArrayOutputStream baos = null;
    try {
        if (size < 0)
            size = 0;
        baos = new ByteArrayOutputStream(size);
        byte[] buffer = new byte[8192];
        int num;
        while ((num = is.read(buffer)) != -1) {
            baos.write(buffer, 0, num);
        }
        return baos.toByteArray();
    } finally {
        ByteUtil.closeStream(baos);
    }
}

From source file:com.woonoz.proxy.servlet.HttpEntityEnclosingRequestHandler.java

private HttpEntity createHttpEntity(HttpServletRequest request) throws FileUploadException, IOException {
    if (ServletFileUpload.isMultipartContent(request)) {
        return createMultipartEntity(request);
    } else {/*from   w  ww . j a v  a2s  .c o  m*/
        return new BufferedHttpEntity(
                new InputStreamEntity(request.getInputStream(), request.getContentLength()));
    }
}

From source file:org.everit.osgi.webconsole.configuration.ConfigServlet.java

private String requestBody(final HttpServletRequest req) throws IOException {
    StringBuilder sb = new StringBuilder(req.getContentLength());
    String line;//w  w  w.  j av a 2s . c  o m
    BufferedReader reader = req.getReader();
    while ((line = reader.readLine()) != null) {
        sb.append(line).append("\n");
    }
    return sb.toString();
}

From source file:com.glaf.base.utils.upload.FileUploadBackGroundServlet.java

/**
 * ??Bean//  w w w.  ja  va 2 s .c om
 */
private FileUploadStatus initStatusBean(HttpServletRequest request) {
    FileUploadStatus satusBean = new FileUploadStatus();
    satusBean.setStatus("?");
    satusBean.setUploadTotalSize(request.getContentLength());
    satusBean.setProcessStartTime(System.currentTimeMillis());
    satusBean.setBaseDir(getUploadDir());
    return satusBean;
}