Search in sources :

Example 91 with Handler

use of org.eclipse.jetty.server.Handler in project calcite-avatica by apache.

the class HandlerFactoryTest method testJson.

@Test
public void testJson() {
    Handler handler = factory.getHandler(service, Serialization.JSON);
    assertTrue("Expected an implementation of the AvaticaHandler, " + "but got " + handler.getClass(), handler instanceof AvaticaJsonHandler);
}
Also used : Handler(org.eclipse.jetty.server.Handler) Test(org.junit.Test)

Example 92 with Handler

use of org.eclipse.jetty.server.Handler in project spring-boot by spring-projects.

the class JettyWebServer method start.

@Override
public void start() throws WebServerException {
    synchronized (this.monitor) {
        if (this.started) {
            return;
        }
        this.server.setConnectors(this.connectors);
        if (!this.autoStart) {
            return;
        }
        try {
            this.server.start();
            for (Handler handler : this.server.getHandlers()) {
                handleDeferredInitialize(handler);
            }
            Connector[] connectors = this.server.getConnectors();
            for (Connector connector : connectors) {
                try {
                    connector.start();
                } catch (IOException ex) {
                    if (connector instanceof NetworkConnector) {
                        PortInUseException.throwIfPortBindingException(ex, () -> ((NetworkConnector) connector).getPort());
                    }
                    throw ex;
                }
            }
            this.started = true;
            logger.info("Jetty started on port(s) " + getActualPortsDescription() + " with context path '" + getContextPath() + "'");
        } catch (WebServerException ex) {
            stopSilently();
            throw ex;
        } catch (Exception ex) {
            stopSilently();
            throw new WebServerException("Unable to start embedded Jetty server", ex);
        }
    }
}
Also used : NetworkConnector(org.eclipse.jetty.server.NetworkConnector) Connector(org.eclipse.jetty.server.Connector) WebServerException(org.springframework.boot.web.server.WebServerException) Handler(org.eclipse.jetty.server.Handler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) NetworkConnector(org.eclipse.jetty.server.NetworkConnector) IOException(java.io.IOException) WebServerException(org.springframework.boot.web.server.WebServerException) IOException(java.io.IOException) PortInUseException(org.springframework.boot.web.server.PortInUseException)

Example 93 with Handler

use of org.eclipse.jetty.server.Handler in project apex-malhar by apache.

the class HttpJsonChunksInputOperatorTest method testHttpInputModule.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testHttpInputModule() throws Exception {
    final List<String> receivedMessages = new ArrayList<String>();
    Handler handler = new AbstractHandler() {

        int responseCount = 0;

        @Override
        public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IOUtils.copy(request.getInputStream(), bos);
            receivedMessages.add(new String(bos.toByteArray()));
            response.setContentType("application/json");
            response.setStatus(HttpServletResponse.SC_OK);
            response.setHeader("Transfer-Encoding", "chunked");
            try {
                JSONObject json = new JSONObject();
                json.put("responseId", "response" + ++responseCount);
                byte[] bytes = json.toString().getBytes();
                response.getOutputStream().println(bytes.length);
                response.getOutputStream().write(bytes);
                response.getOutputStream().println();
                response.getOutputStream().println(0);
                response.getOutputStream().flush();
            } catch (JSONException e) {
                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error generating response: " + e.toString());
            }
            ((Request) request).setHandled(true);
        }
    };
    Server server = new Server(0);
    server.setHandler(handler);
    server.start();
    String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext";
    final AbstractHttpInputOperator operator = new HttpJsonChunksInputOperator();
    CollectorTestSink sink = new CollectorTestSink();
    operator.outputPort.setSink(sink);
    operator.setUrl(new URI(url));
    operator.setup(null);
    operator.activate(null);
    int timeoutMillis = 3000;
    while (sink.collectedTuples.isEmpty() && timeoutMillis > 0) {
        operator.emitTuples();
        timeoutMillis -= 20;
        Thread.sleep(20);
    }
    Assert.assertTrue("tuple emitted", sink.collectedTuples.size() > 0);
    Map<String, String> tuple = (Map<String, String>) sink.collectedTuples.get(0);
    Assert.assertEquals("", tuple.get("responseId"), "response1");
    operator.deactivate();
    operator.teardown();
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) ArrayList(java.util.ArrayList) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) JSONException(org.codehaus.jettison.json.JSONException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.codehaus.jettison.json.JSONObject) Map(java.util.Map) CollectorTestSink(org.apache.apex.malhar.lib.testbench.CollectorTestSink) Test(org.junit.Test)

Example 94 with Handler

use of org.eclipse.jetty.server.Handler in project apex-malhar by apache.

the class HttpPostOutputOperatorTest method testHttpOutputNode.

@Test
public void testHttpOutputNode() throws Exception {
    final List<String> receivedMessages = new ArrayList<String>();
    Handler handler = new AbstractHandler() {

        @Override
        @Consumes({ MediaType.APPLICATION_JSON })
        public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IOUtils.copy(request.getInputStream(), bos);
            receivedMessages.add(new String(bos.toByteArray()));
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println("<h1>Thanks</h1>");
            ((Request) request).setHandled(true);
            receivedMessage = true;
        }
    };
    Server server = new Server(0);
    server.setHandler(handler);
    server.start();
    String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext";
    HttpPostOutputOperator<Object> node = new HttpPostOutputOperator<Object>();
    node.setUrl(url);
    node.setup(null);
    Map<String, String> data = new HashMap<String, String>();
    data.put("somekey", "somevalue");
    node.input.process(data);
    // Wait till the message is received or a maximum timeout elapses
    int timeoutMillis = 10000;
    while (!receivedMessage && timeoutMillis > 0) {
        timeoutMillis -= 20;
        Thread.sleep(20);
    }
    Assert.assertEquals("number requests", 1, receivedMessages.size());
    JSONObject json = new JSONObject(data);
    Assert.assertTrue("request body " + receivedMessages.get(0), receivedMessages.get(0).contains(json.toString()));
    receivedMessages.clear();
    String stringData = "stringData";
    node.input.process(stringData);
    Assert.assertEquals("number requests", 1, receivedMessages.size());
    Assert.assertEquals("request body " + receivedMessages.get(0), stringData, receivedMessages.get(0));
    node.teardown();
    server.stop();
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject) Test(org.junit.Test)

Example 95 with Handler

use of org.eclipse.jetty.server.Handler in project apex-malhar by apache.

the class HttpMultiValuedMapGetOperatorTest method testOperator.

@Test
public void testOperator() throws Exception {
    final List<Map<String, String[]>> receivedRequests = new ArrayList<Map<String, String[]>>();
    Handler handler = new AbstractHandler() {

        @Override
        public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            receivedRequests.add(request.getParameterMap());
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println(request.getParameterNames().nextElement());
            ((Request) request).setHandled(true);
        }
    };
    Server server = new Server(0);
    server.setHandler(handler);
    server.start();
    String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/context";
    TestHttpGetMultiValuedMapOperator operator = new TestHttpGetMultiValuedMapOperator();
    operator.setUrl(url);
    operator.setup(null);
    CollectorTestSink<String> sink = TestUtils.setSink(operator.output, new CollectorTestSink<String>());
    TestPojo pojo = new TestPojo();
    pojo.setName1(KEY1);
    pojo.setValue11(VAL1);
    pojo.setValue12(VAL2);
    pojo.setName2(KEY2);
    pojo.setValue21(VAL1);
    pojo.setValue22(VAL2);
    pojo.setValue23(VAL3);
    operator.input.process(pojo);
    long startTms = System.currentTimeMillis();
    long waitTime = 10000L;
    while (receivedRequests.size() < 3 && System.currentTimeMillis() - startTms < waitTime) {
        Thread.sleep(250);
    }
    Assert.assertEquals("request count", 1, receivedRequests.size());
    Assert.assertEquals("parameter key count", 2, receivedRequests.get(0).size());
    Assert.assertEquals("parameter value count", 2, receivedRequests.get(0).get(KEY1).length);
    Assert.assertEquals("parameter value count", 3, receivedRequests.get(0).get(KEY2).length);
    Assert.assertEquals("parameter value", VAL1, receivedRequests.get(0).get(KEY1)[0]);
    Assert.assertEquals("parameter value", VAL2, receivedRequests.get(0).get(KEY1)[1]);
    Assert.assertEquals("parameter value", VAL1, receivedRequests.get(0).get(KEY2)[0]);
    Assert.assertEquals("parameter value", VAL2, receivedRequests.get(0).get(KEY2)[1]);
    Assert.assertEquals("parameter value", VAL3, receivedRequests.get(0).get(KEY2)[2]);
    Assert.assertNull("parameter value", receivedRequests.get(0).get("randomkey"));
    Assert.assertEquals("emitted size", 1, sink.collectedTuples.size());
    MultivaluedMapImpl map = new MultivaluedMapImpl();
    map.add(pojo.getName1(), pojo.getValue11());
    map.add(pojo.getName1(), pojo.getValue12());
    map.add(pojo.getName2(), pojo.getValue21());
    map.add(pojo.getName2(), pojo.getValue22());
    map.add(pojo.getName2(), pojo.getValue23());
    Map.Entry<String, List<String>> entry = map.entrySet().iterator().next();
    Assert.assertEquals("emitted tuples", entry.getKey(), sink.collectedTuples.get(0).trim());
}
Also used : Server(org.eclipse.jetty.server.Server) ArrayList(java.util.ArrayList) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) Handler(org.eclipse.jetty.server.Handler) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletResponse(javax.servlet.http.HttpServletResponse) MultivaluedMapImpl(com.sun.jersey.core.util.MultivaluedMapImpl) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArrayList(java.util.ArrayList) List(java.util.List) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

Handler (org.eclipse.jetty.server.Handler)119 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)37 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)37 Server (org.eclipse.jetty.server.Server)35 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)26 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)25 RequestLogHandler (org.eclipse.jetty.server.handler.RequestLogHandler)23 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)22 ArrayList (java.util.ArrayList)21 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)18 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)18 IOException (java.io.IOException)15 Test (org.junit.Test)15 HttpServletResponse (javax.servlet.http.HttpServletResponse)14 ServerConnector (org.eclipse.jetty.server.ServerConnector)13 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 Request (org.eclipse.jetty.server.Request)12 URI (java.net.URI)11 SecurityHandler (org.eclipse.jetty.security.SecurityHandler)11