Search in sources :

Example 36 with ContextHandler

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;
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) 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)

Example 37 with ContextHandler

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));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) ApolloConfigNotification(com.ctrip.framework.apollo.core.dto.ApolloConfigNotification) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 38 with ContextHandler

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));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 39 with ContextHandler

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));
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) Config(com.ctrip.framework.apollo.Config) BaseIntegrationTest(com.ctrip.framework.apollo.BaseIntegrationTest) Test(org.junit.Test)

Example 40 with ContextHandler

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#####################");
}
Also used : ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) NettyHttpContextHandler(com.bonree.brfs.disknode.server.netty.NettyHttpContextHandler) CloseMessageHandler(com.bonree.brfs.disknode.server.handler.impl.CloseMessageHandler) DiskWriterManager(com.bonree.brfs.disknode.DiskWriterManager) ReadMessageHandler(com.bonree.brfs.disknode.server.handler.impl.ReadMessageHandler) DeleteMessageHandler(com.bonree.brfs.disknode.server.handler.impl.DeleteMessageHandler) WriteMessageHandler(com.bonree.brfs.disknode.server.handler.impl.WriteMessageHandler) OpenMessageHandler(com.bonree.brfs.disknode.server.handler.impl.OpenMessageHandler)

Aggregations

ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)127 Server (org.eclipse.jetty.server.Server)47 ServerConnector (org.eclipse.jetty.server.ServerConnector)27 URI (java.net.URI)21 ResourceHandler (org.eclipse.jetty.server.handler.ResourceHandler)20 Test (org.junit.Test)20 ContextHandlerCollection (org.eclipse.jetty.server.handler.ContextHandlerCollection)19 ServletContextHandler (org.eclipse.jetty.servlet.ServletContextHandler)19 IOException (java.io.IOException)18 Handler (org.eclipse.jetty.server.Handler)14 HandlerCollection (org.eclipse.jetty.server.handler.HandlerCollection)14 SessionHandler (org.eclipse.jetty.server.session.SessionHandler)14 File (java.io.File)13 ServletException (javax.servlet.ServletException)13 DefaultHandler (org.eclipse.jetty.server.handler.DefaultHandler)13 BeforeClass (org.junit.BeforeClass)11 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)10 Config (com.ctrip.framework.apollo.Config)10 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)10 WebAppContext (org.eclipse.jetty.webapp.WebAppContext)10