package it.unibas.icar.temp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;
import org.apache.camel.ContextTestSupport;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jetty.JettyHttpComponent;
import org.apache.camel.component.jetty.JettyHttpEndpoint;
import org.apache.camel.component.mock.MockEndpoint;
public class TestHttpsRoute extends ContextTestSupport {
protected String expectedBody = "world!";
public void testEndpoint() throws Exception {
MockEndpoint mockEndpoint = resolveMandatoryEndpoint("mock:a", MockEndpoint.class);
mockEndpoint.expectedBodiesReceived(expectedBody);
invokeHttpEndpoint();
mockEndpoint.assertIsSatisfied();
List list = mockEndpoint.getReceivedExchanges();
Exchange exchange = list.get(0);
assertNotNull("exchange", exchange);
Message in = exchange.getIn();
assertNotNull("in", in);
Map headers = in.getHeaders();
log.info("Headers: " + headers);
assertTrue("Should be more than one header but was: " + headers, headers.size() > 0);
}
public void testHelloEndpoint() throws Exception {
ByteArrayOutputStream os = new ByteArrayOutputStream();
InputStream is = new URL("https://localhost:18196/hello").openStream();
int c;
while ((c = is.read()) >= 0) {
os.write(c);
}
String data = new String(os.toByteArray());
assertEquals("Hello World", data);
}
protected void invokeHttpEndpoint() throws IOException {
template.sendBodyAndHeader("jetty:https://localhost:18196/SSLTest/", expectedBody, "Content-Type", "application/xml");
}
@Override
protected RouteBuilder createRouteBuilder() throws Exception {
return new RouteBuilder() {
public void configure() throws Exception {
JettyHttpEndpoint jettyEndpoint = context.getEndpoint("jetty:https://localhost:18196/hello", JettyHttpEndpoint.class);
JettyHttpComponent componentJetty = (JettyHttpComponent) context.getComponent("jetty");
componentJetty.setSslPassword("qwerty");
componentJetty.setSslKeyPassword("qwerty");
from(jettyEndpoint).to("mock:a");
Processor proc = new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.getOut(true).setBody("Hello World");
}
};
from(jettyEndpoint).process(proc);
}
};
}
}