Example usage for javax.servlet.http HttpServletRequest getReader

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

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest 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:org.opennms.netmgt.ncs.northbounder.TestServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    m_posted = IOUtils.toString(req.getReader());
    LOG.debug("POST:");
    LOG.debug(m_posted);/*w ww  . j  a va 2 s .c o  m*/
}

From source file:com.thinkberg.moxo.dav.MkColHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (request.getReader().readLine() != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;/*from  w w  w .ja  v  a2s  .  c  o  m*/
    }

    FileObject object = getResourceManager().getFileObject(request.getPathInfo());

    try {
        LockManager.getInstance().checkCondition(object, getIf(request));
    } catch (LockException e) {
        if (e.getLocks() != null) {
            response.sendError(SC_LOCKED);
        } else {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        }
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}

From source file:org.kurento.jsonrpc.internal.http.JsonRpcHttpRequestHandler.java

/**
 *
 * @param request//from ww  w  .j a v  a2  s.  c o  m
 * @return
 * @throws IOException
 */
private String getBodyAsString(final HttpServletRequest request) throws IOException {
    return CharStreams.toString(request.getReader());
}

From source file:org.onebusaway.nyc.vehicle_tracking.webapp.servlets.VehicleLocationCollectionServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    BufferedReader reader = req.getReader();
    StringBuffer out = new StringBuffer();
    char[] buf = new char[4096];
    while (true) {
        int n = reader.read(buf);
        if (n == -1) {
            break;
        }//from   w  w  w.  j av a2 s. com
        out.append(new String(buf, 0, n));
    }
    String body = out.toString();

    Siri siri = (Siri) _xstream.fromXML(body);
    _vehicleLocationService.handleVehicleLocation(siri, body);

    resp.setStatus(200);
    resp.setContentType("text/html;charset=UTF-8");
    ServletOutputStream writer = resp.getOutputStream();
    writer.print("ok");
    writer.close();
}

From source file:com.google.testing.testify.risk.frontend.server.api.impl.UploadApiImpl.java

@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    BufferedReader reader = req.getReader();
    StringBuilder input = new StringBuilder();
    while (reader.ready()) {
        input.append(req.getReader().readLine());
    }/*  w w w.  ja  v  a  2  s.  c o m*/
    LOG.info("Input received: " + input.toString());
    JSONArray json;
    try {
        json = new JSONArray(input.toString());
    } catch (JSONException e) {
        LOG.warning("Couldn't parse JSON: " + e.toString());
        error(resp, "Malformed JSON could not be parsed: " + e.toString());
        return;
    }
    LOG.info("JSON received: " + json.toString());
    JSONObject o;
    TaskOptions task;
    String email = userService.getEmail();
    for (int i = 0; i < json.length(); i++) {
        try {
            o = json.getJSONObject(i);
            task = TaskOptions.Builder.withUrl(UploadDataTask.URL).method(Method.POST)
                    .param("json", o.toString()).param("user", email);
            ServletUtils.queueWithRetries(UploadDataTask.QUEUE, task, "Processing data item upload");
        } catch (JSONException e) {
            LOG.warning("Couldn't parse item " + i + " in JSON array: " + e.toString());
            resp.getOutputStream().print("<p>Couldn't parse item " + i + "</p>\n");
        }
    }
}

From source file:io.v.positioning.GpsLocationServlet.java

/**
 * Processes a POST request to save device's GPS location
 *
 * @param req  The servlet request that includes JSONObject with properties to be saved
 * @param resp The servlet response informing a user if the request was successful or not
 *//*from  w w w.j av  a  2s  . c  o  m*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    BufferedReader br = req.getReader();
    try {
        JSONObject jo = new JSONObject(br.readLine());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity entity = new Entity("gps");
        entity.setProperty("androidId", jo.get("androidId"));
        entity.setProperty("latitude", jo.get("latitude"));
        entity.setProperty("longitude", jo.get("longitude"));
        entity.setProperty("deviceTime", jo.get("deviceTime"));
        entity.setProperty("serverTime", System.currentTimeMillis());
        datastore.put(entity);
        resp.getWriter().println("New GPS location added.");
    } catch (Exception e) {
        resp.getWriter().println("Recording failed. " + e.getMessage());
    }
}

From source file:com.thinkberg.webdav.MkColHandler.java

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader bufferedReader = request.getReader();
    String line = bufferedReader.readLine();
    if (line != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;//  w ww .  ja va2 s  .com
    }

    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}

From source file:io.v.positioning.BluetoothProximityServlet.java

/**
 * Processes a POST request to save nearby devices scanned using Bluetooth
 *
 * @param req  The servlet request that includes JSONObject with properties to be saved
 * @param resp The servlet response informing a user if the request was successful or not
 *//*from ww w  .jav  a  2  s.  c  om*/
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    BufferedReader br = req.getReader();
    try {
        JSONObject jo = new JSONObject(br.readLine());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity entity = new Entity("bluetooth");
        entity.setProperty("androidId", jo.get("androidId"));
        entity.setProperty("myMacAddress", jo.get("myMacAddress"));
        entity.setProperty("remoteName", jo.get("remoteName"));
        entity.setProperty("remoteAddress", jo.get("remoteAddress"));
        entity.setProperty("remoteRssi", jo.get("remoteRssi"));
        entity.setProperty("deviceTime", jo.get("deviceTime"));
        entity.setProperty("serverTime", System.currentTimeMillis());
        datastore.put(entity);
        resp.getWriter().println("New bluetooth device added.");
    } catch (Exception e) {
        resp.getWriter().println("Recording failed. " + e);
    }
}

From source file:org.efaps.webdav4vfs.handler.MkColHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    BufferedReader bufferedReader = request.getReader();
    String line = bufferedReader.readLine();
    if (line != null) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;//w ww  .  j  a  va  2  s  . c  o m
    }

    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }

    if (object.exists()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }

    if (!object.getParent().exists() || !FileType.FOLDER.equals(object.getParent().getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    try {
        object.createFolder();
        response.setStatus(HttpServletResponse.SC_CREATED);
    } catch (FileSystemException e) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
    }
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {// w  w  w .j  a v a2 s.  c  o  m
        // Get a reader to read the incoming data
        BufferedReader reader = req.getReader();

        // Get a writer to write the data in UTF-8
        res.setContentType("text/html; charset=UTF-8");
        //PrintWriter out = res.getWriter();
        out = new PrintWriter(new OutputStreamWriter(res.getOutputStream(), "UTF8"), true);

        char[] buf = new char[4 * 1024]; // 4Kchar buffer
        int len;
        while ((len = reader.read(buf, 0, buf.length)) != -1) {
            out.write(buf, 0, len);
        }
        out.flush();
    } catch (Exception e) {
        out.println("Problem filtering page to UTF-8");
        getServletContext().log(e, "Problem filtering page to UTF-8");
    }
    out.println("Done");
}