Example usage for javax.servlet.http HttpServletRequest getInputStream

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

Introduction

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

Prototype

public ServletInputStream getInputStream() throws IOException;

Source Link

Document

Retrieves the body of the request as binary data using a ServletInputStream .

Usage

From source file:com.berrysys.ussdgw.HttpUtils.java

/**
 * Gets the payload./*  ww  w  .j  a  v  a2s.  c o  m*/
 *
 * @param req the req
 * @return the payload
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static String getPayload(HttpServletRequest req) throws IOException {
    InputStream is = req.getInputStream();
    StringWriter writer = new StringWriter();
    try {
        IOUtils.copy(is, writer);
    } catch (IOException e1) {
        log.catching(e1);
    } finally {
        try {
            is.close();
        } catch (IOException e1) {
            log.catching(e1);
        }
    }

    final String payload = writer.toString();
    return payload;
}

From source file:ke.alphacba.cms.core.util.RequestUtils.java

public static String getJSONString(HttpServletRequest request) {
    String CHARSET = "UTF-8";
    String json = "";
    try {//from w  w w . j  a va  2 s . co m
        ServletInputStream in = request.getInputStream();
        String content = IOUtils.toString(in, CHARSET);
        json = URLDecoder.decode(content, CHARSET);
        json = json.substring(json.indexOf("=") + 1);
    } catch (IOException e) {

    }
    return json;
}

From source file:com.easarrive.aws.plugins.common.util.SNSUtil.java

public static StringBuilder getHttpRequestContent(HttpServletRequest request) {
    if (request == null) {
        return null;
    }/*  w  w w .  jav a 2s . c om*/
    StringBuilder builder = null;
    try {
        builder = getHttpRequestContent(request.getInputStream());
    } catch (IOException e2) {
        e2.printStackTrace();
    }
    return builder;
}

From source file:com.code.savemarks.utils.Utils.java

/**
 * Deserialize an object from an HttpServletRequest input stream. Does not
 * throw any exceptions; instead, exceptions are logged and null is returned.
 * /*from www . jav a 2  s. c o  m*/
 * @param req An HttpServletRequest that contains a serialized object.
 * @return An object instance, or null if an exception occurred.
 */
public static Object deserialize(HttpServletRequest req) {
    if (req.getContentLength() == 0) {
        log.severe("request content length is 0");
        return null;
    }
    try {
        byte[] bytesIn = new byte[req.getContentLength()];
        req.getInputStream().readLine(bytesIn, 0, bytesIn.length);
        return deserialize(bytesIn);
    } catch (IOException e) {
        log.log(Level.SEVERE, "Error deserializing task", e);
        return null; // don't retry task
    }
}

From source file:jeeves.server.sources.ServiceRequestFactory.java

private static Element extractXmlParameters(HttpServletRequest req) throws IOException, JDOMException {
    return Xml.loadStream(req.getInputStream());
}

From source file:co.edu.unal.arqdsoft.presentacion.JSON.java

/**
 * // w  w w. ja  va  2s  .co  m
 * @param request
 * @return JSONObject con los parametros del request
 * @throws Exception 
 */
public static JSONObject toObject(HttpServletRequest request) throws Exception {
    if (request.getParameter("accion") != null) {//Servidor independiente
        JSONObject r = new JSONObject();
        r.put("accion", request.getParameter("accion"));
        r.put("datos", request.getParameter("datos"));
        return r;
    } else {//Servidor base netbeans
        InputStream is = request.getInputStream();
        byte[] charr = new byte[is.available()];
        is.read(charr);
        return (JSONObject) JSONValue.parse(new String(charr, "UTF-8"));
    }
}

From source file:com.wavemaker.runtime.server.ServerUtils.java

public static String readInput(HttpServletRequest request) throws IOException {

    InputStream is = request.getInputStream();
    if (is == null) {
        throw new WMRuntimeException("no input stream found in request");
    }/*from ww w  . ja  v  a 2 s. c  o m*/

    String input = IOUtils.toString(is, ServerConstants.DEFAULT_ENCODING);

    is.close();

    return input;
}

From source file:com.groupon.odo.HttpUtilities.java

/**
 * Obtain collection of Parameters from request
 *
 * @param httpServletRequest// w  ww  . j  a  v a 2 s  .c  om
 * @return
 * @throws Exception
 */
public static Map<String, String[]> mapUrlEncodedParameters(HttpServletRequest httpServletRequest)
        throws Exception {

    InputStream body = httpServletRequest.getInputStream();
    java.util.Scanner s = new java.util.Scanner(body).useDelimiter("\\A");
    Map<String, String[]> mapPostParameters = new HashMap<String, String[]>();

    try {
        if (s.hasNext()) {
            String requestData = s.next();
            String[] splitRequestData = requestData.split("&");
            for (String requestPart : splitRequestData) {
                String[] parts = requestPart.split("=");
                ArrayList<String> values = new ArrayList<String>();
                if (mapPostParameters.containsKey(parts[0])) {
                    values = new ArrayList<String>(Arrays.asList(mapPostParameters.get(parts[0])));
                    mapPostParameters.remove(parts[0]);
                }

                if (parts.length > 1) {
                    values.add(parts[1]);
                }

                mapPostParameters.put(parts[0], values.toArray(new String[values.size()]));
            }
        }
    } catch (Exception e) {
        throw new Exception("Could not parse request data: " + e.getMessage());
    }

    return mapPostParameters;
}

From source file:com.provenance.cloudprovenance.policyhandler.ws.controler.PolicyRequestHandler.java

public static String getBody(HttpServletRequest request) throws IOException {

    String body = null;//  w  w w. j av  a  2  s . c  om
    StringBuilder stringBuilder = new StringBuilder();
    BufferedReader bufferedReader = null;

    try {
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            char[] charBuffer = new char[128];
            int bytesRead = -1;
            while ((bytesRead = bufferedReader.read(charBuffer)) > 0) {
                stringBuilder.append(charBuffer, 0, bytesRead);
            }
        } else {
            stringBuilder.append("");
        }
    } catch (IOException ex) {
        throw ex;
    } finally {
        if (bufferedReader != null) {
            try {
                bufferedReader.close();
            } catch (IOException ex) {
                throw ex;
            }
        }
    }

    body = stringBuilder.toString();
    return body;
}

From source file:org.apache.atlas.web.util.Servlets.java

public static String getRequestPayload(HttpServletRequest request) throws IOException {
    //request is an instance of LocalServletRequest for calls from LocalAtlasClient
    if (request instanceof LocalServletRequest) {
        return ((LocalServletRequest) request).getPayload();
    }//w w  w. j a v  a 2  s  .c om

    StringWriter writer = new StringWriter();
    IOUtils.copy(request.getInputStream(), writer);
    return writer.toString();
}