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);
}
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);
}
}
}
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();
}
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();
}
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());
}
Aggregations