use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.
the class ConfigIntegrationTest method mockConfigServerHandler.
private ContextHandler mockConfigServerHandler(final int statusCode, final ApolloConfig result, final boolean failedAtFirstTime) {
ContextHandler context = new ContextHandler("/configs/*");
context.setHandler(new AbstractHandler() {
AtomicInteger counter = new AtomicInteger(0);
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
if (failedAtFirstTime && counter.incrementAndGet() == 1) {
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
baseRequest.setHandled(true);
return;
}
response.setContentType("application/json;charset=UTF-8");
response.setStatus(statusCode);
response.getWriter().println(gson.toJson(result));
baseRequest.setHandled(true);
}
});
return context;
}
use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.
the class ConfigIntegrationTest method testLongPollRefreshWithMultipleNamespacesAndMultipleNamespaceNotified.
@Test
public void testLongPollRefreshWithMultipleNamespacesAndMultipleNamespaceNotified() throws Exception {
final String someKey = "someKey";
final String someValue = "someValue";
final String anotherValue = "anotherValue";
long someNotificationId = 1;
long pollTimeoutInMS = 50;
Map<String, String> configurations = Maps.newHashMap();
configurations.put(someKey, someValue);
ApolloConfig apolloConfig = assembleApolloConfig(configurations);
ContextHandler configHandler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig);
ContextHandler pollHandler = mockPollNotificationHandler(pollTimeoutInMS, HttpServletResponse.SC_OK, Lists.newArrayList(new ApolloConfigNotification(apolloConfig.getNamespaceName(), someNotificationId), new ApolloConfigNotification(someOtherNamespace, someNotificationId)), false);
startServerWithHandlers(configHandler, pollHandler);
Config config = ConfigService.getAppConfig();
Config someOtherConfig = ConfigService.getConfig(someOtherNamespace);
assertEquals(someValue, config.getProperty(someKey, null));
assertEquals(someValue, someOtherConfig.getProperty(someKey, null));
final SettableFuture<Boolean> longPollFinished = SettableFuture.create();
final SettableFuture<Boolean> someOtherNamespacelongPollFinished = SettableFuture.create();
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
longPollFinished.set(true);
}
});
someOtherConfig.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
someOtherNamespacelongPollFinished.set(true);
}
});
apolloConfig.getConfigurations().put(someKey, anotherValue);
longPollFinished.get(5000, TimeUnit.MILLISECONDS);
someOtherNamespacelongPollFinished.get(5000, TimeUnit.MILLISECONDS);
assertEquals(anotherValue, config.getProperty(someKey, null));
assertEquals(anotherValue, someOtherConfig.getProperty(someKey, null));
}
use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.
the class ConfigIntegrationTest method testGetConfigWithNoLocalFileAndRemoteConfigError.
@Test
public void testGetConfigWithNoLocalFileAndRemoteConfigError() throws Exception {
ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null);
startServerWithHandlers(handler);
Config config = ConfigService.getAppConfig();
String someKey = "someKey";
String someDefaultValue = "defaultValue" + Math.random();
assertEquals(someDefaultValue, config.getProperty(someKey, someDefaultValue));
}
use of org.eclipse.jetty.server.handler.ContextHandler in project apollo by ctripcorp.
the class ConfigIntegrationTest method testGetConfigWithNoLocalFileAndRemoteConfigServiceRetry.
@Test
public void testGetConfigWithNoLocalFileAndRemoteConfigServiceRetry() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
ApolloConfig apolloConfig = assembleApolloConfig(ImmutableMap.of(someKey, someValue));
boolean failedAtFirstTime = true;
ContextHandler handler = mockConfigServerHandler(HttpServletResponse.SC_OK, apolloConfig, failedAtFirstTime);
startServerWithHandlers(handler);
Config config = ConfigService.getAppConfig();
assertEquals(someValue, config.getProperty(someKey, null));
}
use of org.eclipse.jetty.server.handler.ContextHandler in project BRFS by zhangnianli.
the class Test method main.
public static void main(String[] args) throws Exception {
int port = 8899;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
}
JettyDiskNodeHttpServer s = new JettyDiskNodeHttpServer(port);
DiskWriterManager nodeManager = new DiskWriterManager();
nodeManager.start();
DiskJettyHttpRequestHandler httpRequestHandler = new DiskJettyHttpRequestHandler();
httpRequestHandler.put(DiskOperation.OP_OPEN, new OpenMessageHandler(nodeManager));
httpRequestHandler.put(DiskOperation.OP_WRITE, new WriteMessageHandler(nodeManager));
httpRequestHandler.put(DiskOperation.OP_READ, new ReadMessageHandler());
httpRequestHandler.put(DiskOperation.OP_CLOSE, new CloseMessageHandler(nodeManager));
httpRequestHandler.put(DiskOperation.OP_DELETE, new DeleteMessageHandler(nodeManager));
ContextHandler handler = new ContextHandler("/disk");
handler.setHandler(httpRequestHandler);
// handler.setHandler(new AbstractHandler() {
//
// @Override
// public void handle(String target, Request baseRequest,
// HttpServletRequest request, HttpServletResponse response)
// throws IOException, ServletException {
// System.out.println("length=" + request.getContentLength());
// int contentLength = request.getContentLength();
// System.out.println("content length############" + contentLength);
// byte[] data = new byte[Math.max(contentLength, 0)];
// if(request.getContentLength() > 0) {
// InputUtils.readBytes(request.getInputStream(), data, 0, data.length);
//
// System.out.println(new String(data));
// }
// baseRequest.setHandled(true);
// }
// });
s.addContextHandler(handler);
s.start();
System.out.println("####################SERVER STARTED#####################");
}
Aggregations