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:org.red5.server.net.servlet.RTMPTServlet.java

/**
 * Skip data sent by the client./*from   w ww .jav a 2  s.c  o  m*/
 *
 * @param req
 * @throws IOException
 */
protected void skipData(HttpServletRequest req) throws IOException {
    ByteBuffer data = ByteBuffer.allocate(req.getContentLength());
    ServletUtils.copy(req.getInputStream(), data.asOutputStream());
    data.flip();
    data = null;
}

From source file:de.zib.vold.userInterface.RESTController.java

MultiValueMap<String, String> getBody() throws IOException {
    ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
            .currentRequestAttributes();
    HttpServletRequest request = requestAttributes.getRequest();

    if (request.getContentLength() <= 0)
        return null;

    HttpInputMessage inputMessage = new ServletServerHttpRequest(request);

    return (MultiValueMap<String, String>) converter.read(LinkedMultiValueMap.class, inputMessage);
}

From source file:org.bedework.notifier.web.MethodBase.java

/** Parse the request body, and return the DOM representation.
 *
 * @param req        Servlet request object
 * @param resp       Servlet response object for bad status
 * @return Document  Parsed body or null for no body
 * @exception NoteException Some error occurred.
 *///  ww w  .j  av  a 2  s .  c  om
protected Document parseContent(final HttpServletRequest req, final HttpServletResponse resp)
        throws NoteException {
    int len = req.getContentLength();
    if (len == 0) {
        return null;
    }

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);

        //DocumentBuilder builder = factory.newDocumentBuilder();
        /*
              Reader rdr = getNsIntf().getReader(req);
                
              if (rdr == null) {
                // No content?
                return null;
              }
                
              return builder.parse(new InputSource(rdr));*/
        return null;
        //    } catch (SAXException e) {
        //    resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        //  throw new NoteException(HttpServletResponse.SC_BAD_REQUEST);
    } catch (Throwable t) {
        resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        throw new NoteException(t);
    }
}

From source file:org.webguitoolkit.ui.controls.form.fileupload.FileUploadServlet.java

public UploadListener(HttpServletRequest request, long debugDelay) {
    this.request = request;
    this.delay = debugDelay;
    totalToRead = request.getContentLength();
    this.startTime = System.currentTimeMillis();
}

From source file:com.salmon.security.xacml.demo.springmvc.rest.controller.MarketPlacePopulatorController.java

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)// w w w. j  a va 2s  .c  o m
public void whenPayloadIsStrange(HttpServletRequest request, HttpMessageNotReadableException ex) {

    StringBuilder sb = new StringBuilder("REQUEST PARTS: ");

    LOG.info("************************ JSON ERROR  ************************ ");
    LOG.info("ContentType " + request.getContentType());
    LOG.info("ContentLength " + request.getContentLength());

    StringBuffer jb = new StringBuffer();
    String line = null;
    try {
        BufferedReader reader = request.getReader();
        while ((line = reader.readLine()) != null)
            jb.append(line);
    } catch (Exception e) {
        /*report an error*/ }

    LOG.info("************************ JSON PARSING  ************************ ");
    LOG.info("Payload: " + jb.toString());
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonFactory factory = mapper.getFactory();
        JsonParser jp = factory.createParser(jb.toString());
        JsonNode actualObj = mapper.readTree(jp);
        LOG.info("JSON OBJECT CREATED: " + actualObj.toString());

        ObjectMapper driverMapper = new ObjectMapper();
        Driver jsonDriver = driverMapper.readValue(actualObj.toString(), Driver.class);

        LOG.info("DRIVER OBJECT CREATED: " + jsonDriver.toString());

    } catch (Exception e) {
        LOG.error("JSON Parsing Exception " + e.getMessage());
    }

}

From source file:es.tid.cep.esperanza.Rules.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from  w  w w . ja  v a 2  s .  com*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    response.setContentType("application/json;charset=UTF-8");
    try {
        ServletInputStream sis = request.getInputStream();
        byte[] b = new byte[request.getContentLength()];
        sis.read(b, 0, b.length);
        String expression = new String(b);
        logger.debug("rule text: ", expression);
        EPStatement statement = epService.getEPAdministrator().createEPL(expression);
        logger.debug("statement json: " + Utils.Statement2JSONObject(statement));
        statement.addListener(new GenericListener());
        out.println(Utils.Statement2JSONObject(statement));
    } catch (EPException epe) {
        logger.error("creating statement", epe);
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        out.printf("{\"error\":%s}\n", JSONObject.valueToString(epe.getMessage()));
    } finally {
        out.close();
    }
}

From source file:org.intalio.tempo.workflow.wds.servlets.WDSServlet.java

@Override
protected void doPut(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("doPut request={} contentType={} contentLength={}",
                new Object[] { request, request.getContentType(), request.getContentLength() });
    }/*w  w  w. jav a2  s. c  om*/
    String resourceUri = getResourceUri(request);
    String contentType = request.getContentType();
    if (contentType == null) {
        contentType = "application/octet-stream";
    }
    String participantToken = getParticipantToken(request);
    InputStream payloadStream = request.getInputStream();
    try {
        WDSService service = _wdsFactory.getWDSService();
        try {
            byte[] payload = IOUtils.toByteArray(payloadStream);
            Item item = new Item(resourceUri, contentType, payload);

            LOG.debug("Storing the item: {}", resourceUri);
            try {
                service.deleteItem(item.getURI(), participantToken);
            } catch (UnavailableItemException except) {
                // ignore
            }
            service.storeItem(item, participantToken);
            LOG.debug("Item {} stored OK.", resourceUri);
        } catch (UnavailableItemException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            LOG.warn("Item not found: '" + resourceUri + "'");
        } finally {
            service.close();
        }
    } finally {
        payloadStream.close();
    }
}

From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java

private HttpUriRequest prepareRequest(HttpServletRequest req, String target) throws IOException {
    String method = req.getMethod();
    if (method.equals(HttpGet.METHOD_NAME)) {
        return new HttpGet(target);
    } else if (method.equals(HttpPost.METHOD_NAME)) {
        HttpPost post = new HttpPost(target);
        post.setEntity(new ByteArrayEntity(readInputStream(req.getInputStream(), req.getContentLength()),
                ContentType.parse(req.getContentType())));
        return post;
    }//from  w w w .  ja v  a2 s .c  o m
    throw new UnsupportedOperationException("Only GET and POST are supported by this proxy.");
}