Search in sources :

Example 36 with ServletInputStream

use of javax.servlet.ServletInputStream in project AngularBeans by bessemHmidi.

the class SockJsServletRequest method onDataAvailable.

@Override
public void onDataAvailable() throws IOException {
    ServletInputStream inputStream = request.getInputStream();
    do {
        while (!inputStream.isReady()) {
        //
        }
        byte[] buffer = new byte[1024 * 4];
        int length = inputStream.read(buffer);
        if (length > 0) {
            if (onDataHandler != null) {
                try {
                    onDataHandler.handle(Arrays.copyOf(buffer, length));
                } catch (SockJsException e) {
                    throw new IOException(e);
                }
            }
        }
    } while (inputStream.isReady());
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) SockJsException(org.projectodd.sockjs.SockJsException) IOException(java.io.IOException)

Example 37 with ServletInputStream

use of javax.servlet.ServletInputStream in project opennms by OpenNMS.

the class RTCPostServlet method doPost.

/**
 * {@inheritDoc}
 */
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // the path info will be the category name we need
    String pathInfo = request.getPathInfo();
    // info
    if (pathInfo == null) {
        LOG.error("Request with no path info");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No Category name given in path");
        return;
    }
    // remove the preceding slash if present
    if (pathInfo.startsWith("/")) {
        pathInfo = pathInfo.substring(1, pathInfo.length());
    }
    // since these category names can contain spaces, etc,
    // we have to URL encode them in the URL
    String categoryName = Util.decode(pathInfo);
    org.opennms.netmgt.xml.rtc.Category category = null;
    try (ServletInputStream inStream = request.getInputStream();
        InputStreamReader isr = new InputStreamReader(inStream)) {
        org.opennms.netmgt.xml.rtc.EuiLevel level = JaxbUtils.unmarshal(org.opennms.netmgt.xml.rtc.EuiLevel.class, isr);
        // for now we only deal with the first category, they're only sent
        // one
        // at a time anyway
        category = level.getCategory().get(0);
    }
    // for the categoryname in the path info
    if (!categoryName.equals(category.getCatlabel())) {
        LOG.error("Request did not supply information for category specified in path info");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No category info found for " + categoryName);
        return;
    }
    // update the category information in the CategoryModel
    this.model.updateCategory(category);
    // return a success message
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    out.println("Category data parsed successfully.");
    out.close();
    LOG.info("Successfully received information for {}", categoryName);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStreamReader(java.io.InputStreamReader) PrintWriter(java.io.PrintWriter)

Example 38 with ServletInputStream

use of javax.servlet.ServletInputStream in project Lucee by lucee.

the class HTTPServletRequestWrap method getInputStream.

@Override
public ServletInputStream getInputStream() throws IOException {
    // if(ba rr!=null) throw new IllegalStateException();
    if (barr == null) {
        if (!firstRead) {
            PageContext pc = ThreadLocalPageContext.get();
            if (pc != null) {
                return pc.formScope().getInputStream();
            }
            // throw new IllegalStateException();
            return new ServletInputStreamDummy(new byte[] {});
        }
        firstRead = false;
        if (isToBig(getContentLength())) {
            return req.getInputStream();
        }
        InputStream is = null;
        try {
            barr = IOUtil.toBytes(is = req.getInputStream());
        // Resource res = ResourcesImpl.getFileResourceProvider().getResource("/Users/mic/Temp/multipart.txt");
        // IOUtil.copy(new ByteArrayInputStream(barr), res, true);
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            barr = null;
            return new ServletInputStreamDummy(new byte[] {});
        } finally {
            IOUtil.closeEL(is);
        }
    }
    return new ServletInputStreamDummy(barr);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) InputStream(java.io.InputStream) PageContext(lucee.runtime.PageContext) ThreadLocalPageContext(lucee.runtime.engine.ThreadLocalPageContext)

Example 39 with ServletInputStream

use of javax.servlet.ServletInputStream in project tomee by apache.

the class ServerServlet method service.

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!activated) {
        response.getWriter().write("");
        return;
    }
    ServletInputStream in = request.getInputStream();
    ServletOutputStream out = response.getOutputStream();
    try {
        RequestInfos.initRequestInfo(request);
        ejbServer.service(in, out);
    } catch (ServiceException e) {
        throw new ServletException("ServerService error: " + ejbServer.getClass().getName() + " -- " + e.getMessage(), e);
    } finally {
        RequestInfos.clearRequestInfo();
    }
}
Also used : ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) ServiceException(org.apache.openejb.server.ServiceException) ServletOutputStream(javax.servlet.ServletOutputStream)

Example 40 with ServletInputStream

use of javax.servlet.ServletInputStream in project jetty.project by eclipse.

the class ClientConnectionCloseTest method test_ClientConnectionClose_ServerPartialResponse_ClientIdleTimeout.

@Test
public void test_ClientConnectionClose_ServerPartialResponse_ClientIdleTimeout() throws Exception {
    long idleTimeout = 1000;
    start(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            baseRequest.setHandled(true);
            ServletInputStream input = request.getInputStream();
            while (true) {
                int read = input.read();
                if (read < 0)
                    break;
            }
            response.getOutputStream().print("Hello");
            response.flushBuffer();
            try {
                Thread.sleep(2 * idleTimeout);
            } catch (InterruptedException x) {
                throw new InterruptedIOException();
            }
        }
    });
    String host = "localhost";
    int port = connector.getLocalPort();
    HttpDestinationOverHTTP destination = (HttpDestinationOverHTTP) client.getDestination(scheme, host, port);
    DuplexConnectionPool connectionPool = (DuplexConnectionPool) destination.getConnectionPool();
    DeferredContentProvider content = new DeferredContentProvider(ByteBuffer.allocate(8));
    CountDownLatch resultLatch = new CountDownLatch(1);
    client.newRequest(host, port).scheme(scheme).header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString()).content(content).idleTimeout(idleTimeout, TimeUnit.MILLISECONDS).onRequestSuccess(request -> {
        HttpConnectionOverHTTP connection = (HttpConnectionOverHTTP) connectionPool.getActiveConnections().iterator().next();
        Assert.assertFalse(connection.getEndPoint().isOutputShutdown());
    }).send(result -> {
        if (result.isFailed())
            resultLatch.countDown();
    });
    content.offer(ByteBuffer.allocate(8));
    content.close();
    Assert.assertTrue(resultLatch.await(2 * idleTimeout, TimeUnit.MILLISECONDS));
    Assert.assertEquals(0, connectionPool.getConnectionCount());
}
Also used : Request(org.eclipse.jetty.server.Request) HttpHeaderValue(org.eclipse.jetty.http.HttpHeaderValue) ServletException(javax.servlet.ServletException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) ServletInputStream(javax.servlet.ServletInputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) Test(org.junit.Test) StringContentProvider(org.eclipse.jetty.client.util.StringContentProvider) InterruptedIOException(java.io.InterruptedIOException) ByteBuffer(java.nio.ByteBuffer) TimeUnit(java.util.concurrent.TimeUnit) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) CountDownLatch(java.util.concurrent.CountDownLatch) HttpHeader(org.eclipse.jetty.http.HttpHeader) HttpServletRequest(javax.servlet.http.HttpServletRequest) ContentResponse(org.eclipse.jetty.client.api.ContentResponse) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpStatus(org.eclipse.jetty.http.HttpStatus) Assert(org.junit.Assert) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) InterruptedIOException(java.io.InterruptedIOException) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) CountDownLatch(java.util.concurrent.CountDownLatch) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpConnectionOverHTTP(org.eclipse.jetty.client.http.HttpConnectionOverHTTP) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) DeferredContentProvider(org.eclipse.jetty.client.util.DeferredContentProvider) HttpDestinationOverHTTP(org.eclipse.jetty.client.http.HttpDestinationOverHTTP) Test(org.junit.Test)

Aggregations

ServletInputStream (javax.servlet.ServletInputStream)79 IOException (java.io.IOException)51 HttpServletRequest (javax.servlet.http.HttpServletRequest)43 Test (org.junit.Test)43 ServletException (javax.servlet.ServletException)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)26 HttpServlet (javax.servlet.http.HttpServlet)20 DeferredContentProvider (org.eclipse.jetty.client.util.DeferredContentProvider)20 InterruptedIOException (java.io.InterruptedIOException)18 AsyncContext (javax.servlet.AsyncContext)17 ReadListener (javax.servlet.ReadListener)17 ServletOutputStream (javax.servlet.ServletOutputStream)16 AbstractHandler (org.eclipse.jetty.server.handler.AbstractHandler)16 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)15 Request (org.eclipse.jetty.server.Request)14 Response (org.eclipse.jetty.client.api.Response)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintWriter (java.io.PrintWriter)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8