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:org.zalando.logbook.servlet.example.ExampleController.java

@RequestMapping(value = "/stream", produces = MediaType.TEXT_PLAIN_VALUE)
public void stream(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    ByteStreams.copy(request.getInputStream(), response.getOutputStream());
}

From source file:com.github.terma.m.server.NodeServlet.java

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response)
        throws ServletException, IOException {
    final List<Event> newEvents = GSON.fromJson(IOUtils.toString(request.getInputStream()), EVENTS_TYPE);
    EventsHolder.get().add(newEvents);//from  w  w w  .  j a  v a  2  s.c om
    NodeManager.INSTANCE.responseFromNode(request.getRemoteHost());
}

From source file:com.scistor.tab.auth.controller.HomeRestController.java

@RequestMapping(value = "/license", method = RequestMethod.POST, consumes = "text/plain")
public void postLicense(HttpServletRequest request) throws IOException {
    int size = request.getContentLength();
    if (size > KeyValue.MAX_SIZE) {

    }// w w w  .  j a v a  2  s  . c  o  m
    byte[] data = IOUtils.toByteArray(request.getInputStream(), size);
    License license = new LicenseValidator().decryptAndVerifyLicense(request.getInputStream());
    keyValueRepository.save(new KeyValue("license", data));
}

From source file:com.webpagebytes.cms.utility.HttpServletToolbox.java

public String getBodyText(HttpServletRequest request) throws IOException {
    StringWriter writer = new StringWriter();
    InputStream is = request.getInputStream();
    IOUtils.copy(is, writer, "UTF-8");
    return writer.toString();
}

From source file:io.gumga.presentation.api.voice.VoiceReceiverAPI.java

@RequestMapping(value = "/voice", method = RequestMethod.POST)
public Map recebeSom(HttpServletRequest httpRequest) throws IOException {
    String som = convertStreamToString(httpRequest.getInputStream());
    System.out.println("----->" + som);
    Map<String, Object> problemas = new HashMap<>();
    try {//from w  w  w . ja v  a2  s  . c om
        som = som.replaceFirst("data:audio/wav;base64,", "");
        byte[] decode = Base64.getDecoder().decode(som.substring(0, 512));
        int sampleRate = unsignedToBytes(decode[27]) * 256 * 256 * 256 + unsignedToBytes(decode[26]) * 256 * 256
                + unsignedToBytes(decode[25]) * 256 + unsignedToBytes(decode[24]) * 1;

        RestTemplate restTemplate = new GumgaJsonRestTemplate();
        Map<String, Object> config = new HashMap<>();

        config.put("encoding", "LINEAR16");
        config.put("sampleRate", "" + sampleRate);
        config.put("languageCode", "pt-BR");
        config.put("maxAlternatives", "1");
        config.put("profanityFilter", "false");
        Map<String, Object> context = new HashMap<>();
        context.put("phrases", CONTEXT);
        config.put("speechContext", context); //EXPLORAR DEPOIS COM FRASES PARA "AJUDAR" o reconhecedor
        Map<String, Object> audio = new HashMap<>();
        audio.put("content", som);
        Map<String, Object> request = new HashMap<>();
        request.put("config", config);
        request.put("audio", audio);
        Map resposta = restTemplate.postForObject(
                "https://speech.googleapis.com/v1beta1/speech:syncrecognize?key=AIzaSyC7E4dZ4EneRmSzVMs2qhyJYGoTK49FCYM",
                request, Map.class);
        List<Object> analiseSintatica = analiseSintatica(resposta);
        resposta.put("objects", analiseSintatica);
        return resposta;
    } catch (Exception ex) {
        problemas.put("exception", ex);
    }
    return problemas;
}

From source file:com.nominanuda.hyperapi.HttpClientHyperApiFactoryTest.java

@Test
public void testE() throws Exception {
    startServer(12000, new AbstractHandler() {
        public void handle(String uri, Request arg1, HttpServletRequest req, HttpServletResponse resp)
                throws IOException, ServletException {
            IOHelper io = new IOHelper();
            io.pipe(req.getInputStream(), resp.getOutputStream(), false, false);
        }/*from   ww  w  .j  av  a2 s .co  m*/
    });
    HttpCoreHelper httpCoreHelper = new HttpCoreHelper();
    HttpClient client = httpCoreHelper.createClient(10, 100000000, 100000000);
    HttpClientHyperApiFactory f = new HttpClientHyperApiFactory();
    f.setHttpClient(client);
    f.setUriPrefix("http://localhost:12000");
    TestHyperApi api = f.getInstance("", TestHyperApi.class);
    DataObject foo = new DataObjectImpl();
    foo.put("foo", "FOO");
    DataObject result = api.putFoo("BAR", "BAZ", foo);
    Assert.assertEquals("FOO", result.get("foo"));
}

From source file:com.vigglet.oei.standardinspection.CopyStandardinspectionToServiceServlet.java

@Override
protected void preProcessRequest(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    StringWriter writer = new StringWriter();
    IOUtils.copy(req.getInputStream(), writer, "UTF-8");
    String theString = writer.toString();
    User user = getUser(req);/*from  w  ww  .j a  v a2s.  c om*/

    Standardinspection model = JsonUtil.read(theString, Standardinspection.class);
    if (model != null) {
        model.setCompany(user.getCompany());
        model = PostStandardinspectionServlet.update(model);

        Service service = new Service();
        service.setCompany(model.getCompany());
        service.setDate(new Date().getTime());
        service.setDescription(model.getDescription());
        service.setNote(model.getName());
        service = ServiceUtil.getInstance().insertOrUpdate(service);

        int i = 0;
        for (Standardinspectionrow standardinspectionrow : model.getRows()) {
            Servicerow servicerow = new Servicerow();
            servicerow.setMaterial(standardinspectionrow.getMaterial());
            servicerow.setName(standardinspectionrow.getComment());
            servicerow.setQuantity(standardinspectionrow.getQuantity());
            servicerow.setType(standardinspectionrow.getType());
            servicerow.setListorder(i++);
            servicerow.setService(service.getId());
            ServicerowUtil.getInstance().insertOrUpdate(servicerow);
        }

        JsonUtil.write(resp.getOutputStream(), ServiceUtil.getInstance().findById(service.getId()));
    } else {
        Logger.getLogger(PostJsonServlet.class.getName()).log(Level.WARNING, "Could not read json!", theString);
    }
}

From source file:me.buom.shiro.util.SimpleHmacBuilder.java

protected byte[] toByteArray(HttpServletRequest request) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {// w  w w.  j  a  v  a2 s  . c om
        InputStream inputStream = request.getInputStream();
        if (inputStream != null) {
            byte[] buff = new byte[2048];
            int count = -1;

            while ((count = inputStream.read(buff)) > 0) {
                byteArrayOutputStream.write(buff, 0, count);
            }
            inputStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }

    return byteArrayOutputStream.toByteArray();
}

From source file:io.apiman.test.common.mock.EchoServlet.java

/**
 * Create an echo response from the inbound information in the http server
 * request.//from  w w  w .  j  av  a2s  .  c  om
 * @param request the request
 * @param withBody if request is with body
 * @return a new echo response
 */
public static EchoResponse response(HttpServletRequest request, boolean withBody) {
    EchoResponse response = new EchoResponse();
    response.setMethod(request.getMethod());
    if (request.getQueryString() != null) {
        String[] normalisedQueryString = request.getQueryString().split("&");
        Arrays.sort(normalisedQueryString);
        response.setResource(
                request.getRequestURI() + "?" + SimpleStringUtils.join("&", normalisedQueryString));
    } else {
        response.setResource(request.getRequestURI());
    }
    response.setUri(request.getRequestURI());
    Enumeration<String> headerNames = request.getHeaderNames();
    while (headerNames.hasMoreElements()) {
        String name = headerNames.nextElement();
        String value = request.getHeader(name);
        response.getHeaders().put(name, value);
    }
    if (withBody) {
        long totalBytes = 0;
        InputStream is = null;
        try {
            is = request.getInputStream();
            MessageDigest sha1 = MessageDigest.getInstance("SHA1");
            byte[] data = new byte[1024];
            int read;
            while ((read = is.read(data)) != -1) {
                sha1.update(data, 0, read);
                totalBytes += read;
            }
            ;

            byte[] hashBytes = sha1.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < hashBytes.length; i++) {
                sb.append(Integer.toString((hashBytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            String fileHash = sb.toString();

            response.setBodyLength(totalBytes);
            response.setBodySha1(fileHash);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return response;
}

From source file:ee.ria.xroad.proxy.testsuite.testcases.ServerProxyHttpError.java

@Override
public AbstractHandler getServerProxyHandler() {
    return new AbstractHandler() {
        @Override/* www .ja  va  2  s. c  o  m*/
        public void handle(String target, Request baseRequest, HttpServletRequest request,
                HttpServletResponse response) throws IOException {
            // Read all of the request.
            IOUtils.readLines(request.getInputStream());

            response.sendError(HttpServletResponse.SC_BAD_GATEWAY);
            baseRequest.setHandled(true);
        }
    };
}