Example usage for javax.servlet ServletRequest getReader

List of usage examples for javax.servlet ServletRequest getReader

Introduction

In this page you can find the example usage for javax.servlet ServletRequest getReader.

Prototype

public BufferedReader getReader() throws IOException;

Source Link

Document

Retrieves the body of the request as character data using a BufferedReader.

Usage

From source file:com.wss.log.LogController.java

@RequestMapping(method = RequestMethod.POST, value = "post_log") // headers = {"Content-type=application/json", "Accept:application/json"}
public @ResponseBody String insertProduct(ServletRequest request) throws IOException {
    BufferedReader reader = request.getReader();
    StringBuilder sb = new StringBuilder();
    String line;/*from   w ww .j a  v a  2s.  c  o  m*/
    while ((line = reader.readLine()) != null) {
        sb.append(line);
        sb.append("\n");
    }
    String log = sb.toString();

    try {
        sendMessage(log);
    } catch (IOException ex) {
        logger.debug(log, ex);
        return ex.getMessage();
    }
    return SUCCESS;
}

From source file:eu.rethink.lhcb.broker.servlet.WellKnownServlet.java

private void handleRequest(HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {

    // add header for cross domain stuff
    resp.addHeader("Access-Control-Allow-Origin", "*");
    String host = req.getHeader("X-Forwarded-Host");
    if (host == null)
        host = req.getHeader("Host");

    // setting external host here helps BrokerWebSocketListener to return info about HTTP interface
    // Broker might not know how it is accessible. This is a workaround for it
    LHCBBroker.externalHost = host;//from ww w  .j a v a  2s .c o m
    final AsyncContext asyncContext = req.startAsync();
    asyncContext.start(() -> {
        ServletRequest aReq = asyncContext.getRequest();
        String payload = null;
        try {
            payload = IOUtils.toString(aReq.getReader());
        } catch (IOException e) {
            e.printStackTrace();
        }

        String finalPayload = payload;

        Map<String, String[]> params = aReq.getParameterMap();
        LOG.debug("payload: {}\r\nparams: {}", payload, params);

        RequestCallback cb = new RequestCallback() {

            @Override
            public void response(Message msg) {
                resp.setStatus(HttpServletResponse.SC_OK);
                try {
                    asyncContext.getResponse().getWriter().write(msg.toString());
                    asyncContext.getResponse().getWriter().flush();
                    asyncContext.complete();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error(Exception e) {
                String error = "Request failed.\r\npayload: " + finalPayload + "\r\nparams: " + params;
                LOG.error(error + "\r\nreason: " + e.getLocalizedMessage(),
                        e instanceof InvalidMessageException ? null : e);
                if (e instanceof InvalidMessageException) {
                    resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
                } else {
                    resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);

                }
                completeAsyncContext(asyncContext, error + "\r\nreason: " + e.getLocalizedMessage());
            }
        };

        try {
            Message msg = null;

            if (payload.length() > 0) {
                msg = Message.fromString(payload);
            } else {
                msg = Message.fromParams(params);
            }

            requestHandler.handleRequest(msg, cb);
        } catch (InvalidMessageException e) {
            cb.error(e);
        }
    });
}

From source file:net.paoding.rose.web.paramresolver.ServletRequestDataBinder.java

/**
 * ?/*from ww w.ja  v a 2 s . c om*/
 * 
 * Extension point that subclasses can use to add extra bind values
 * for a request. Invoked before {@link #doBind(MutablePropertyValues)}. The
 * default implementation is empty.
 * 
 * @param mpvs
 *            the property values that will be used for data binding
 * @param request
 *            the current request
 */
protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request) {
    String type = request.getParameter("_rest");
    if ((StringUtils.isNotBlank(type)) && (type.equalsIgnoreCase("rest"))) {
        StringBuffer data = new StringBuffer();
        String line = null;
        try {
            BufferedReader reader = request.getReader();
            while ((line = reader.readLine()) != null) {
                data.append(line);
            }
            String jsonStr = data.toString();
            if (StringUtils.isNotBlank(jsonStr)) {
                JSONObject json = JSONObject.fromObject(jsonStr);
                @SuppressWarnings("unchecked")
                Iterator<String> it = json.keys();
                while (it.hasNext()) {
                    String key = it.next();
                    mpvs.addPropertyValue(key, json.get(key));
                }
            }
        } catch (Exception e) {

        }
    }
}

From source file:org.opencms.search.solr.spellchecking.CmsSolrSpellchecker.java

/**
 * Returns the body of the request. This method is used to read posted JSON data.
 *
 * @param request The request.//from  ww w  . ja v a2  s.com
 *
 * @return String representation of the request's body.
 *
 * @throws IOException in case reading the request fails
 */
private String getRequestBody(ServletRequest request) throws IOException {

    final StringBuilder sb = new StringBuilder();

    String line = request.getReader().readLine();
    while (null != line) {
        sb.append(line);
        line = request.getReader().readLine();
    }

    return sb.toString();
}