Java Code Examples for org.mortbay.jetty.servlet.Context#setContextPath()

The following examples show how to use org.mortbay.jetty.servlet.Context#setContextPath() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestHTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Test
@TestJetty
public void testJetty() throws Exception {
  Context context = new Context();
  context.setContextPath("/");
  context.addServlet(MyServlet.class, "/bar");
  Server server = TestJettyHelper.getJettyServer();
  server.addHandler(context);
  server.start();
  URL url = new URL(TestJettyHelper.getJettyURL(), "/bar");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  assertEquals(conn.getResponseCode(), HttpURLConnection.HTTP_OK);
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  assertEquals(reader.readLine(), "foo");
  reader.close();
}
 
Example 2
Source File: TestHFSTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Test
@TestJetty
public void testJetty() throws Exception {
  Context context = new Context();
  context.setContextPath("/");
  context.addServlet(MyServlet.class, "/bar");
  Server server = TestJettyHelper.getJettyServer();
  server.addHandler(context);
  server.start();
  URL url = new URL(TestJettyHelper.getJettyURL(), "/bar");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  assertEquals(conn.getResponseCode(), HttpURLConnection.HTTP_OK);
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  assertEquals(reader.readLine(), "foo");
  reader.close();
}
 
Example 3
Source File: TestHTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
@Test
@TestJetty
public void testJetty() throws Exception {
  Context context = new Context();
  context.setContextPath("/");
  context.addServlet(MyServlet.class, "/bar");
  Server server = TestJettyHelper.getJettyServer();
  server.addHandler(context);
  server.start();
  URL url = new URL(TestJettyHelper.getJettyURL(), "/bar");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  assertEquals(conn.getResponseCode(), HttpURLConnection.HTTP_OK);
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  assertEquals(reader.readLine(), "foo");
  reader.close();
}
 
Example 4
Source File: Main.java    From hbase-indexer with Apache License 2.0 6 votes vote down vote up
private void startHttpServer() throws Exception {
    server = new Server();
    SelectChannelConnector selectChannelConnector = new SelectChannelConnector();
    selectChannelConnector.setPort(11060);
    server.setConnectors(new Connector[]{selectChannelConnector});

    PackagesResourceConfig packagesResourceConfig = new PackagesResourceConfig("com/ngdata/hbaseindexer/rest");

    ServletHolder servletHolder = new ServletHolder(new ServletContainer(packagesResourceConfig));
    servletHolder.setName("HBase-Indexer");


    Context context = new Context(server, "/", Context.NO_SESSIONS);
    context.addServlet(servletHolder, "/*");
    context.setContextPath("/");
    context.setAttribute("indexerModel", indexerModel);
    context.setAttribute("indexerSupervisor", indexerSupervisor);

    server.setHandler(context);
    server.start();
}
 
Example 5
Source File: TestHFSTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
@Test
@TestJetty
public void testJetty() throws Exception {
  Context context = new Context();
  context.setContextPath("/");
  context.addServlet(MyServlet.class, "/bar");
  Server server = TestJettyHelper.getJettyServer();
  server.addHandler(context);
  server.start();
  URL url = new URL(TestJettyHelper.getJettyURL(), "/bar");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  assertEquals(conn.getResponseCode(), HttpURLConnection.HTTP_OK);
  BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  assertEquals(reader.readLine(), "foo");
  reader.close();
}
 
Example 6
Source File: TestWebDelegationToken.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testFallbackToPseudoDelegationTokenAuthenticator()
    throws Exception {
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(UserServlet.class), "/bar");

  try {
    jetty.start();
    final URL url = new URL(getJettyURL() + "/foo/bar");

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        DelegationTokenAuthenticatedURL.Token token =
            new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl =
            new DelegationTokenAuthenticatedURL();
        HttpURLConnection conn = aUrl.openConnection(url, token);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(FOO_USER, ret.get(0));

        aUrl.getDelegationToken(url, token, FOO_USER);
        Assert.assertNotNull(token.getDelegationToken());
        Assert.assertEquals(new Text("token-kind"),
            token.getDelegationToken().getKind());
        return null;
      }
    });
  } finally {
    jetty.stop();
  }
}
 
Example 7
Source File: TestWebDelegationToken.java    From hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalDelegationTokenSecretManager() throws Exception {
  DummyDelegationTokenSecretManager secretMgr
      = new DummyDelegationTokenSecretManager();
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(AFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(PingServlet.class), "/bar");
  try {
    secretMgr.startThreads();
    context.setAttribute(DelegationTokenAuthenticationFilter.
            DELEGATION_TOKEN_SECRET_MANAGER_ATTR, secretMgr);
    jetty.start();
    URL authURL = new URL(getJettyURL() + "/foo/bar?authenticated=foo");

    DelegationTokenAuthenticatedURL.Token token =
        new DelegationTokenAuthenticatedURL.Token();
    DelegationTokenAuthenticatedURL aUrl =
        new DelegationTokenAuthenticatedURL();

    aUrl.getDelegationToken(authURL, token, FOO_USER);
    Assert.assertNotNull(token.getDelegationToken());
    Assert.assertEquals(new Text("fooKind"),
        token.getDelegationToken().getKind());

  } finally {
    jetty.stop();
    secretMgr.stopThreads();
  }
}
 
Example 8
Source File: AuthenticatorTestCase.java    From hadoop with Apache License 2.0 5 votes vote down vote up
protected void startJetty() throws Exception {
  server = new Server(0);
  context = new Context();
  context.setContextPath("/foo");
  server.setHandler(context);
  context.addFilter(new FilterHolder(TestFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(TestServlet.class), "/bar");
  host = "localhost";
  port = getLocalPort();
  server.getConnectors()[0].setHost(host);
  server.getConnectors()[0].setPort(port);
  server.start();
  System.out.println("Running embedded servlet container at: http://" + host + ":" + port);
}
 
Example 9
Source File: TestWebAppProxyServlet.java    From big-c with Apache License 2.0 5 votes vote down vote up
/**
 * Simple http server. Server should send answer with status 200
 */
@BeforeClass
public static void start() throws Exception {
  server = new Server(0);
  Context context = new Context();
  context.setContextPath("/foo");
  server.setHandler(context);
  context.addServlet(new ServletHolder(TestServlet.class), "/bar");
  server.getConnectors()[0].setHost("localhost");
  server.start();
  originalPort = server.getConnectors()[0].getLocalPort();
  LOG.info("Running embedded servlet container at: http://localhost:"
      + originalPort);
}
 
Example 10
Source File: TestWebAppProxyServlet.java    From hadoop with Apache License 2.0 5 votes vote down vote up
/**
 * Simple http server. Server should send answer with status 200
 */
@BeforeClass
public static void start() throws Exception {
  server = new Server(0);
  Context context = new Context();
  context.setContextPath("/foo");
  server.setHandler(context);
  context.addServlet(new ServletHolder(TestServlet.class), "/bar");
  server.getConnectors()[0].setHost("localhost");
  server.start();
  originalPort = server.getConnectors()[0].getLocalPort();
  LOG.info("Running embedded servlet container at: http://localhost:"
      + originalPort);
}
 
Example 11
Source File: TestWebDelegationToken.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testExternalDelegationTokenSecretManager() throws Exception {
  DummyDelegationTokenSecretManager secretMgr
      = new DummyDelegationTokenSecretManager();
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(AFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(PingServlet.class), "/bar");
  try {
    secretMgr.startThreads();
    context.setAttribute(DelegationTokenAuthenticationFilter.
            DELEGATION_TOKEN_SECRET_MANAGER_ATTR, secretMgr);
    jetty.start();
    URL authURL = new URL(getJettyURL() + "/foo/bar?authenticated=foo");

    DelegationTokenAuthenticatedURL.Token token =
        new DelegationTokenAuthenticatedURL.Token();
    DelegationTokenAuthenticatedURL aUrl =
        new DelegationTokenAuthenticatedURL();

    aUrl.getDelegationToken(authURL, token, FOO_USER);
    Assert.assertNotNull(token.getDelegationToken());
    Assert.assertEquals(new Text("fooKind"),
        token.getDelegationToken().getKind());

  } finally {
    jetty.stop();
    secretMgr.stopThreads();
  }
}
 
Example 12
Source File: TestWebDelegationToken.java    From big-c with Apache License 2.0 5 votes vote down vote up
@Test
public void testFallbackToPseudoDelegationTokenAuthenticator()
    throws Exception {
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(UserServlet.class), "/bar");

  try {
    jetty.start();
    final URL url = new URL(getJettyURL() + "/foo/bar");

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        DelegationTokenAuthenticatedURL.Token token =
            new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl =
            new DelegationTokenAuthenticatedURL();
        HttpURLConnection conn = aUrl.openConnection(url, token);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(FOO_USER, ret.get(0));

        aUrl.getDelegationToken(url, token, FOO_USER);
        Assert.assertNotNull(token.getDelegationToken());
        Assert.assertEquals(new Text("token-kind"),
            token.getDelegationToken().getKind());
        return null;
      }
    });
  } finally {
    jetty.stop();
  }
}
 
Example 13
Source File: AuthenticatorTestCase.java    From big-c with Apache License 2.0 5 votes vote down vote up
protected void startJetty() throws Exception {
  server = new Server(0);
  context = new Context();
  context.setContextPath("/foo");
  server.setHandler(context);
  context.addFilter(new FilterHolder(TestFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(TestServlet.class), "/bar");
  host = "localhost";
  port = getLocalPort();
  server.getConnectors()[0].setHost(host);
  server.getConnectors()[0].setPort(port);
  server.start();
  System.out.println("Running embedded servlet container at: http://" + host + ":" + port);
}
 
Example 14
Source File: RequestTracingFilterOldServletComponentTest.java    From wingtips with Apache License 2.0 5 votes vote down vote up
private static Handler generateServletContextHandler() throws IOException {
    ServletHandler servletHandler = new ServletHandler();

    servletHandler.addServletWithMapping(BlockingServlet.class, BLOCKING_PATH);
    servletHandler.addServletWithMapping(BlockingForwardServlet.class, BLOCKING_FORWARD_PATH);
    servletHandler.addFilterWithMapping(RequestTracingFilter.class.getName(), "/*", Handler.ALL);

    Context context = new Context(null, null, null, servletHandler, null);
    context.setContextPath("/");
    return context;
}
 
Example 15
Source File: AuthenticatorTestCase.java    From registry with Apache License 2.0 5 votes vote down vote up
protected void startJetty() throws Exception {
    server = new Server(0);
    context = new Context();
    context.setContextPath("/foo");
    server.setHandler(context);
    context.addFilter(new FilterHolder(TestFilter.class), "/*", 0);
    context.addServlet(new ServletHolder(TestServlet.class), "/bar");
    host = "localhost";
    port = getLocalPort();
    server.getConnectors()[0].setHost(host);
    server.getConnectors()[0].setPort(port);
    server.start();
    System.out.println("Running embedded servlet container at: http://" + host + ":" + port);
}
 
Example 16
Source File: TestWebDelegationToken.java    From hadoop with Apache License 2.0 4 votes vote down vote up
private void testDelegationTokenAuthenticatedURLWithNoDT(
    Class<? extends Filter> filterClass)  throws Exception {
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(filterClass), "/*", 0);
  context.addServlet(new ServletHolder(UserServlet.class), "/bar");

  try {
    jetty.start();
    final URL url = new URL(getJettyURL() + "/foo/bar");

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        DelegationTokenAuthenticatedURL.Token token =
            new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl =
            new DelegationTokenAuthenticatedURL();
        HttpURLConnection conn = aUrl.openConnection(url, token);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(FOO_USER, ret.get(0));

        try {
          aUrl.getDelegationToken(url, token, FOO_USER);
          Assert.fail();
        } catch (AuthenticationException ex) {
          Assert.assertTrue(ex.getMessage().contains(
              "delegation token operation"));
        }
        return null;
      }
    });
  } finally {
    jetty.stop();
  }
}
 
Example 17
Source File: TestWebDelegationToken.java    From big-c with Apache License 2.0 4 votes vote down vote up
private void testDelegationTokenAuthenticatedURLWithNoDT(
    Class<? extends Filter> filterClass)  throws Exception {
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(filterClass), "/*", 0);
  context.addServlet(new ServletHolder(UserServlet.class), "/bar");

  try {
    jetty.start();
    final URL url = new URL(getJettyURL() + "/foo/bar");

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        DelegationTokenAuthenticatedURL.Token token =
            new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl =
            new DelegationTokenAuthenticatedURL();
        HttpURLConnection conn = aUrl.openConnection(url, token);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals(FOO_USER, ret.get(0));

        try {
          aUrl.getDelegationToken(url, token, FOO_USER);
          Assert.fail();
        } catch (AuthenticationException ex) {
          Assert.assertTrue(ex.getMessage().contains(
              "delegation token operation"));
        }
        return null;
      }
    });
  } finally {
    jetty.stop();
  }
}
 
Example 18
Source File: TestWebDelegationToken.java    From big-c with Apache License 2.0 4 votes vote down vote up
@Test
public void testHttpUGI() throws Exception {
  final Server jetty = createJettyServer();
  Context context = new Context();
  context.setContextPath("/foo");
  jetty.setHandler(context);
  context.addFilter(new FilterHolder(PseudoDTAFilter.class), "/*", 0);
  context.addServlet(new ServletHolder(UGIServlet.class), "/bar");

  try {
    jetty.start();
    final URL url = new URL(getJettyURL() + "/foo/bar");

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser(FOO_USER);
    ugi.doAs(new PrivilegedExceptionAction<Void>() {
      @Override
      public Void run() throws Exception {
        DelegationTokenAuthenticatedURL.Token token =
            new DelegationTokenAuthenticatedURL.Token();
        DelegationTokenAuthenticatedURL aUrl =
            new DelegationTokenAuthenticatedURL();

        // user foo
        HttpURLConnection conn = aUrl.openConnection(url, token);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        List<String> ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals("remoteuser=" + FOO_USER+ ":ugi=" + FOO_USER, 
            ret.get(0));

        // user ok-user via proxyuser foo
        conn = aUrl.openConnection(url, token, OK_USER);
        Assert.assertEquals(HttpURLConnection.HTTP_OK,
            conn.getResponseCode());
        ret = IOUtils.readLines(conn.getInputStream());
        Assert.assertEquals(1, ret.size());
        Assert.assertEquals("realugi=" + FOO_USER +":remoteuser=" + OK_USER + 
                ":ugi=" + OK_USER, ret.get(0));

        return null;
      }
    });
  } finally {
    jetty.stop();
  }
}
 
Example 19
Source File: JettyMain.java    From fraud-detection-tutorial with Apache License 2.0 4 votes vote down vote up
public static void main(String args[]) throws Exception {

    if (args.length == 0) {
      System.out.println("JettyMain {port} {zookeeperQuorum}");
    }
    try {
      Configuration config = HBaseConfiguration.create();
      config.addResource("/etc/hbase/conf/hbase-site.xml");
      config.set(HConstants.ZOOKEEPER_QUORUM, args[1]);
      //config.set(HConstants.ZOOKEEPER_CLIENT_PORT, "2181");

      HBaseService hbaseService = new HBaseService(config);

      Server server = new Server(Integer.parseInt(args[0]));

      WebAppContext webapp = new WebAppContext();
      webapp.setContextPath("/fe");
      webapp.setResourceBase("./webapp");
      webapp.setWelcomeFiles(new String[]{"index.html"});

      Context servletContext = new Context();
      servletContext.setContextPath("/be");
      servletContext.addServlet(GraphTsvServlet.class, "/graph.tsv");
      servletContext.addServlet(FileGetterServlet.class, "/file/*");
      servletContext.addServlet(NodeAutoCompleteServlet.class, "/auto/*");
      servletContext.addServlet(GraphNodeJsonServlet.class, "/nodeGraph/*");
      servletContext.addServlet(NodeHomeDashBoardInfoServlet.class, "/nodeDash/*");

      server.setHandlers(new Handler[]{webapp, servletContext});

      System.out.println("-");

      GraphTsvServlet.setHBaseService(hbaseService);
      FileGetterServlet.setHBaseService(hbaseService);
      NodeAutoCompleteServlet.setHBaseService(hbaseService);
      GraphNodeJsonServlet.setHBaseService(hbaseService);
      NodeHomeDashBoardInfoServlet.setHBaseService(hbaseService);

      server.start();
      server.join();

    } finally {
      System.out.println("Step 7: Shut Down");
    }
  }
 
Example 20
Source File: JettyTest.java    From fraud-detection-tutorial with Apache License 2.0 2 votes vote down vote up
public static void main(String args[]) throws Exception {

    LocalHBaseCluster hbaseCluster = new LocalHBaseCluster();
    Configuration config = hbaseCluster.getConfiguration();
    QueueToHBaseProcess queueHBaseProcess = null;
    try {
      System.out.println("Step 1: Config");

      HBaseService hbaseService = new HBaseService(config);

      System.out.println("Step 2: Service");

      hbaseService.generateTables();

      System.out.println("Step 2.5: Tables");

      populateInitialNodes(hbaseService);

      System.out.println("Step 2.75: Rules");

      //populateRules(hbaseService);

      System.out.println("Step 3: Nodes Populated");

      EventListener listener = new InternalQueueListener();

      System.out.println("Step 4: Listener");

      long sleepBetweenIterations = 5000;
      long maxIterations = 200;
      int updateNodeListEveryNIterations = 100;
      int updateEtcFileEveryNRequest = 7;

      NodeDataSymulator sym = new NodeDataSymulator(listener, hbaseService,
              sleepBetweenIterations, maxIterations,
              updateNodeListEveryNIterations, updateEtcFileEveryNRequest);

      queueHBaseProcess = new QueueToHBaseProcess(hbaseService);

      queueHBaseProcess.start();

      sym.run(10);

      System.out.println("Step 5: Sym");


      System.out.println("Step 6: API Test");

      Server server = new Server(8082);

      WebAppContext webapp = new WebAppContext();
      webapp.setContextPath("/fe");
      webapp.setResourceBase("./webapp");
      webapp.setWelcomeFiles(new String[]{"index.html"});


      Context servletContext = new Context();
      servletContext.setContextPath("/be");
      servletContext.addServlet(GraphTsvServlet.class, "/graph.tsv");
      servletContext.addServlet(FileGetterServlet.class, "/file/*");
      servletContext.addServlet(NodeAutoCompleteServlet.class, "/auto/*");
      servletContext.addServlet(GraphNodeJsonServlet.class, "/nodeGraph/*");
      servletContext.addServlet(NodeHomeDashBoardInfoServlet.class, "/nodeDash/*");

      server.setHandlers(new Handler[]{webapp, servletContext});

      System.out.println("-");

      GraphTsvServlet.setHBaseService(hbaseService);
      FileGetterServlet.setHBaseService(hbaseService);
      NodeAutoCompleteServlet.setHBaseService(hbaseService);
      GraphNodeJsonServlet.setHBaseService(hbaseService);
      NodeHomeDashBoardInfoServlet.setHBaseService(hbaseService);

      server.start();
      server.join();

    } finally {
      queueHBaseProcess.stop();

      hbaseCluster.shutDown();


      System.out.println("Step 7: Shut Down");
    }
  }