Search in sources :

Example 31 with ServletInputStream

use of javax.servlet.ServletInputStream in project async-http-client by AsyncHttpClient.

the class NonAsciiContentLengthTest method setUpGlobal.

@BeforeClass(alwaysRun = true)
public void setUpGlobal() throws Exception {
    server = new Server();
    ServerConnector connector = addHttpConnector(server);
    server.setHandler(new AbstractHandler() {

        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            // Can only handle bodies of up to 1024 bytes.
            int MAX_BODY_SIZE = 1024;
            byte[] b = new byte[MAX_BODY_SIZE];
            int offset = 0;
            int numBytesRead;
            try (ServletInputStream is = request.getInputStream()) {
                while ((numBytesRead = is.read(b, offset, MAX_BODY_SIZE - offset)) != -1) {
                    offset += numBytesRead;
                }
            }
            assertEquals(request.getContentLength(), offset);
            response.setStatus(200);
            response.setCharacterEncoding(request.getCharacterEncoding());
            response.setContentLength(request.getContentLength());
            try (ServletOutputStream os = response.getOutputStream()) {
                os.write(b, 0, offset);
            }
        }
    });
    server.start();
    port1 = connector.getLocalPort();
}
Also used : ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServletInputStream(javax.servlet.ServletInputStream) Server(org.eclipse.jetty.server.Server) ServletOutputStream(javax.servlet.ServletOutputStream) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) BeforeClass(org.testng.annotations.BeforeClass)

Example 32 with ServletInputStream

use of javax.servlet.ServletInputStream in project Asqatasun by Asqatasun.

the class LoginControllerTest method testDisplayLoginPage.

/**
     * Test of displayLoginPage method, of class LoginController.
     */
public void testDisplayLoginPage() {
    System.out.println("displayLoginPage");
    Model model = new ExtendedModelMap();
    setUpMockAuthenticationContext();
    LoginController instance = new LoginController();
    String expResult = TgolKeyStore.LOGIN_VIEW_NAME;
    String result = instance.displayLoginPage("", new HttpServletRequest() {

        @Override
        public String getAuthType() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Cookie[] getCookies() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public long getDateHeader(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getHeader(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Enumeration getHeaders(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Enumeration getHeaderNames() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public int getIntHeader(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getMethod() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getPathInfo() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getPathTranslated() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getContextPath() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getQueryString() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRemoteUser() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isUserInRole(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Principal getUserPrincipal() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRequestedSessionId() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRequestURI() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public StringBuffer getRequestURL() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getServletPath() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public HttpSession getSession(boolean bln) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public HttpSession getSession() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isRequestedSessionIdValid() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isRequestedSessionIdFromCookie() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isRequestedSessionIdFromURL() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isRequestedSessionIdFromUrl() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Object getAttribute(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Enumeration getAttributeNames() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getCharacterEncoding() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void setCharacterEncoding(String string) throws UnsupportedEncodingException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public int getContentLength() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getContentType() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getParameter(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Enumeration getParameterNames() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String[] getParameterValues(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Map getParameterMap() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getProtocol() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getScheme() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getServerName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public int getServerPort() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public BufferedReader getReader() throws IOException {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRemoteAddr() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRemoteHost() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void setAttribute(String string, Object o) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void removeAttribute(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Locale getLocale() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public Enumeration getLocales() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public boolean isSecure() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public RequestDispatcher getRequestDispatcher(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getRealPath(String string) {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public int getRemotePort() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getLocalName() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getLocalAddr() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public int getLocalPort() {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    }, model);
    assertEquals(expResult, result);
}
Also used : Locale(java.util.Locale) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Enumeration(java.util.Enumeration) HttpSession(javax.servlet.http.HttpSession) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) RequestDispatcher(javax.servlet.RequestDispatcher) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) Model(org.springframework.ui.Model) BufferedReader(java.io.BufferedReader) Map(java.util.Map) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Principal(java.security.Principal)

Example 33 with ServletInputStream

use of javax.servlet.ServletInputStream in project calcite-avatica by apache.

the class AvaticaProtobufHandler method handle.

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    try (final Context ctx = this.requestTimer.start()) {
        // Check if the user is OK to proceed.
        if (!isUserPermitted(serverConfig, request, response)) {
            LOG.debug("HTTP request from {} is unauthenticated and authentication is required", request.getRemoteAddr());
            return;
        }
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        if (request.getMethod().equals("POST")) {
            final byte[] requestBytes;
            // Avoid a new buffer creation for every HTTP request
            final UnsynchronizedBuffer buffer = threadLocalBuffer.get();
            try (ServletInputStream inputStream = request.getInputStream()) {
                requestBytes = AvaticaUtils.readFullyToBytes(inputStream, buffer);
            } finally {
                buffer.reset();
            }
            HandlerResponse<byte[]> handlerResponse;
            try {
                if (null != serverConfig && serverConfig.supportsImpersonation()) {
                    // If we can't extract a user, need to throw 401 in that case.
                    String remoteUser = serverConfig.getRemoteUserExtractor().extract(request);
                    // Invoke the ProtobufHandler inside as doAs for the remote user.
                    // The doAsRemoteUser call may disallow a user, need to throw 403 in that case.
                    handlerResponse = serverConfig.doAsRemoteUser(remoteUser, request.getRemoteAddr(), new Callable<HandlerResponse<byte[]>>() {

                        @Override
                        public HandlerResponse<byte[]> call() {
                            return pbHandler.apply(requestBytes);
                        }
                    });
                } else {
                    handlerResponse = pbHandler.apply(requestBytes);
                }
            } catch (RemoteUserExtractionException e) {
                LOG.debug("Failed to extract remote user from request", e);
                handlerResponse = pbHandler.unauthenticatedErrorResponse(e);
            } catch (RemoteUserDisallowedException e) {
                LOG.debug("Remote user is not authorized", e);
                handlerResponse = pbHandler.unauthorizedErrorResponse(e);
            } catch (Exception e) {
                LOG.debug("Error invoking request from {}", baseRequest.getRemoteAddr(), e);
                // Catch at the highest level of exceptions
                handlerResponse = pbHandler.convertToErrorResponse(e);
            }
            baseRequest.setHandled(true);
            response.setStatus(handlerResponse.getStatusCode());
            response.getOutputStream().write(handlerResponse.getResponse());
        }
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) UnsynchronizedBuffer(org.apache.calcite.avatica.util.UnsynchronizedBuffer) ServletInputStream(javax.servlet.ServletInputStream) Callable(java.util.concurrent.Callable) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 34 with ServletInputStream

use of javax.servlet.ServletInputStream in project lucene-solr by apache.

the class SolrRequestParserTest method testParameterIncompatibilityException1.

@Test
public void testParameterIncompatibilityException1() throws Exception {
    HttpServletRequest request = getMock("/solr/select", "application/x-www-form-urlencoded", 100);
    // we emulate Jetty that returns empty stream when parameters were parsed before:
    when(request.getInputStream()).thenReturn(new ServletInputStream() {

        @Override
        public int read() {
            return -1;
        }

        @Override
        public boolean isFinished() {
            return true;
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setReadListener(ReadListener readListener) {
        }
    });
    FormDataRequestParser formdata = new FormDataRequestParser(2048);
    try {
        formdata.parseParamsAndFillStreams(request, new ArrayList<ContentStream>());
        fail("should throw SolrException");
    } catch (SolrException solre) {
        assertTrue(solre.getMessage().startsWith("Solr requires that request parameters"));
        assertEquals(500, solre.code());
    }
    verify(request).getInputStream();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FormDataRequestParser(org.apache.solr.servlet.SolrRequestParsers.FormDataRequestParser) ContentStream(org.apache.solr.common.util.ContentStream) ServletInputStream(javax.servlet.ServletInputStream) ReadListener(javax.servlet.ReadListener) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 35 with ServletInputStream

use of javax.servlet.ServletInputStream in project undertow by undertow-io.

the class AsyncInputStreamServlet method doPost.

@Override
protected void doPost(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final AsyncContext context = req.startAsync();
    final ServletOutputStream outputStream = resp.getOutputStream();
    ServletInputStream inputStream = req.getInputStream();
    final MyListener listener = new MyListener(outputStream, inputStream, context);
    inputStream.setReadListener(listener);
    outputStream.setWriteListener(listener);
}
Also used : ServletInputStream(javax.servlet.ServletInputStream) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext)

Aggregations

ServletInputStream (javax.servlet.ServletInputStream)76 IOException (java.io.IOException)50 Test (org.junit.Test)43 HttpServletRequest (javax.servlet.http.HttpServletRequest)42 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