org.mortbay.jetty.Server Java Examples
The following examples show how to use
org.mortbay.jetty.Server.
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: GitkitExample.java From identity-toolkit-java with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Server server = new Server(4567); ServletHandler servletHandler = new ServletHandler(); SessionHandler sessionHandler = new SessionHandler(); sessionHandler.setHandler(servletHandler); server.setHandler(sessionHandler); servletHandler.addServletWithMapping(LoginServlet.class, "/login"); servletHandler.addServletWithMapping(WidgetServlet.class, "/gitkit"); servletHandler.addServletWithMapping(LoginServlet.class, "/"); server.start(); server.join(); }
Example #2
Source File: TestProtocolHttpClient.java From anthelion with Apache License 2.0 | 6 votes |
protected void setUp() throws Exception { ContextHandler context = new ContextHandler(); context.setContextPath("/"); context.setResourceBase(RES_DIR); ServletHandler sh = new ServletHandler(); sh.addServlet("org.apache.jasper.servlet.JspServlet", "*.jsp"); context.addHandler(sh); context.addHandler(new SessionHandler()); server = new Server(); server.addHandler(context); conf = new Configuration(); conf.addResource("nutch-default.xml"); conf.addResource("nutch-site-test.xml"); http = new Http(); http.setConf(conf); }
Example #3
Source File: JettyLauncher.java From nextreports-server with Apache License 2.0 | 6 votes |
public static void main(String[] args) throws Exception { Server server = new Server(); SelectChannelConnector connector = new SelectChannelConnector(); connector.setPort(PORT); server.addConnector(connector); server.setStopAtShutdown(true); // the orders of handlers is very important! ContextHandler contextHandler = new ContextHandler(); contextHandler.setContextPath("/reports"); contextHandler.setResourceBase("./reports/"); contextHandler.addHandler(new ResourceHandler()); server.addHandler(contextHandler); server.addHandler(new WebAppContext("webapp", "/nextreports-server")); long t = System.currentTimeMillis(); server.start(); t = System.currentTimeMillis() - t; String version = server.getClass().getPackage().getImplementationVersion(); System.out.println("Started Jetty Server " + version + " on port " + PORT + " in " + t / 1000 + "s"); server.join(); }
Example #4
Source File: TestProtocolHttpClient.java From nutch-htmlunit with Apache License 2.0 | 6 votes |
protected void setUp() throws Exception { ContextHandler context = new ContextHandler(); context.setContextPath("/"); context.setResourceBase(RES_DIR); ServletHandler sh = new ServletHandler(); sh.addServlet("org.apache.jasper.servlet.JspServlet", "*.jsp"); context.addHandler(sh); context.addHandler(new SessionHandler()); server = new Server(); server.addHandler(context); conf = new Configuration(); conf.addResource("nutch-default.xml"); conf.addResource("nutch-site-test.xml"); http = new Http(); http.setConf(conf); }
Example #5
Source File: CustomLocalServerReceiver.java From hop with Apache License 2.0 | 6 votes |
public String getRedirectUri() throws IOException { if ( this.port == -1 ) { this.port = getUnusedPort(); } this.server = new Server( this.port ); Connector[] arr$ = this.server.getConnectors(); int len$ = arr$.length; for ( int i$ = 0; i$ < len$; ++i$ ) { Connector c = arr$[i$]; c.setHost( this.host ); } this.server.addHandler( new CustomLocalServerReceiver.CallbackHandler() ); try { this.server.start(); } catch ( Exception var5 ) { Throwables.propagateIfPossible( var5 ); throw new IOException( var5 ); } return "http://" + this.host + ":" + this.port + "/Callback/success.html"; }
Example #6
Source File: HTTPMetricsServer.java From mt-flume with Apache License 2.0 | 6 votes |
@Override public void start() { jettyServer = new Server(port); //We can use Contexts etc if we have many urls to handle. For one url, //specifying a handler directly is the most efficient. jettyServer.setHandler(new HTTPMetricsHandler()); try { jettyServer.start(); while (!jettyServer.isStarted()) { Thread.sleep(500); } } catch (Exception ex) { LOG.error("Error starting Jetty. JSON Metrics may not be available.", ex); } }
Example #7
Source File: TestJettyHelper.java From hadoop with Apache License 2.0 | 6 votes |
private Server createJettyServer() { try { InetAddress localhost = InetAddress.getByName("localhost"); String host = "localhost"; ServerSocket ss = new ServerSocket(0, 50, localhost); int port = ss.getLocalPort(); ss.close(); Server server = new Server(0); if (!ssl) { server.getConnectors()[0].setHost(host); server.getConnectors()[0].setPort(port); } else { SslSocketConnector c = new SslSocketConnectorSecure(); c.setHost(host); c.setPort(port); c.setNeedClientAuth(false); c.setKeystore(keyStore); c.setKeystoreType(keyStoreType); c.setKeyPassword(keyStorePassword); server.setConnectors(new Connector[] {c}); } return server; } catch (Exception ex) { throw new RuntimeException("Could not stop embedded servlet container, " + ex.getMessage(), ex); } }
Example #8
Source File: TestHTestCase.java From hadoop with Apache License 2.0 | 6 votes |
@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 #9
Source File: TestHFSTestCase.java From hadoop with Apache License 2.0 | 6 votes |
@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 #10
Source File: EmbeddedServer.java From xdocreport.samples with GNU Lesser General Public License v3.0 | 6 votes |
public static void main( String[] args ) throws Exception { Server server = new Server( 8080 ); WebAppContext webappcontext = new WebAppContext( "src/main/webapp", "/xdocreport-webapp" ); ContextHandlerCollection servlet_contexts = new ContextHandlerCollection(); webappcontext.setClassLoader( Thread.currentThread().getContextClassLoader() ); HandlerCollection handlers = new HandlerCollection(); handlers.setHandlers( new Handler[] { servlet_contexts, webappcontext, new DefaultHandler() } ); server.setHandler( handlers ); // JSP Servlet + Context Context jsp_ctx = new Context( servlet_contexts, "/jsp", Context.SESSIONS ); jsp_ctx.addServlet( new ServletHolder( new org.apache.jasper.servlet.JspServlet() ), "*.jsp" ); server.start(); server.join(); }
Example #11
Source File: CustomLocalServerReceiver.java From pentaho-kettle with Apache License 2.0 | 6 votes |
public String getRedirectUri() throws IOException { if ( this.port == -1 ) { this.port = getUnusedPort(); } this.server = new Server( this.port ); Connector[] arr$ = this.server.getConnectors(); int len$ = arr$.length; for ( int i$ = 0; i$ < len$; ++i$ ) { Connector c = arr$[i$]; c.setHost( this.host ); } this.server.addHandler( new CustomLocalServerReceiver.CallbackHandler() ); try { this.server.start(); } catch ( Exception var5 ) { Throwables.propagateIfPossible( var5 ); throw new IOException( var5 ); } return "http://" + this.host + ":" + this.port + "/Callback/success.html"; }
Example #12
Source File: HttpServerImpl.java From reef with Apache License 2.0 | 6 votes |
private Server tryPort(final int portNumber) throws Exception { Server srv = new Server(); final Connector connector = new SocketConnector(); connector.setHost(this.hostAddress); connector.setPort(portNumber); srv.addConnector(connector); try { srv.start(); LOG.log(Level.INFO, "Jetty Server started with port: {0}", portNumber); } catch (final BindException ex) { srv = null; LOG.log(Level.FINEST, "Cannot use host: {0},port: {1}. Will try another", new Object[] {this.hostAddress, portNumber}); } return srv; }
Example #13
Source File: TestJettyHelper.java From big-c with Apache License 2.0 | 6 votes |
private Server createJettyServer() { try { InetAddress localhost = InetAddress.getByName("localhost"); String host = "localhost"; ServerSocket ss = new ServerSocket(0, 50, localhost); int port = ss.getLocalPort(); ss.close(); Server server = new Server(0); if (!ssl) { server.getConnectors()[0].setHost(host); server.getConnectors()[0].setPort(port); } else { SslSocketConnector c = new SslSocketConnectorSecure(); c.setHost(host); c.setPort(port); c.setNeedClientAuth(false); c.setKeystore(keyStore); c.setKeystoreType(keyStoreType); c.setKeyPassword(keyStorePassword); server.setConnectors(new Connector[] {c}); } return server; } catch (Exception ex) { throw new RuntimeException("Could not stop embedded servlet container, " + ex.getMessage(), ex); } }
Example #14
Source File: CacheReplicationTestCase.java From reladomo with Apache License 2.0 | 6 votes |
protected void setupPspMithraService() { server = new Server(this.getApplicationPort1()); Context context = new Context (server,"/",Context.SESSIONS); ServletHolder holder = context.addServlet(PspServlet.class, "/PspServlet"); holder.setInitParameter("serviceInterface.MasterCacheService", "com.gs.fw.common.mithra.cache.offheap.MasterCacheService"); holder.setInitParameter("serviceClass.MasterCacheService", "com.gs.fw.common.mithra.cache.offheap.MasterCacheServiceImpl"); holder.setInitOrder(10); // System.out.println(holder.getServlet().getClass().getName()); try { server.start(); } catch (Exception e) { throw new RuntimeException("could not start server", e); } finally { } }
Example #15
Source File: TestHTestCase.java From big-c with Apache License 2.0 | 6 votes |
@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 #16
Source File: PspTestCase.java From reladomo with Apache License 2.0 | 6 votes |
protected void setupServerWithHandler( Handler handler) throws Exception { this.port = (int) (Math.random() * 10000.0 + 10000.0); this.pspUrl = "http://localhost:" + this.port + "/PspServlet"; this.server = new Server(this.port); Context context = new Context(server, "/", Context.SESSIONS); if (handler != null) { context.addHandler(handler); } ServletHolder holder = context.addServlet(PspServlet.class, "/PspServlet"); holder.setInitParameter("serviceInterface.Echo", "com.gs.fw.common.mithra.test.tinyproxy.Echo"); holder.setInitParameter("serviceClass.Echo", "com.gs.fw.common.mithra.test.tinyproxy.EchoImpl"); holder.setInitOrder(10); this.server.start(); this.servlet = (PspServlet) holder.getServlet(); }
Example #17
Source File: RemoteMithraServerTestCase.java From reladomo with Apache License 2.0 | 6 votes |
protected void setupPspMithraService() { server = new Server(this.getApplicationPort1()); Context context = new Context (server,"/",Context.SESSIONS); ServletHolder holder = context.addServlet(PspServlet.class, "/PspServlet"); holder.setInitParameter("serviceInterface.RemoteMithraService", "com.gs.fw.common.mithra.remote.RemoteMithraService"); holder.setInitParameter("serviceClass.RemoteMithraService", "com.gs.fw.common.mithra.remote.RemoteMithraServiceImpl"); holder.setInitOrder(10); try { server.start(); } catch (Exception e) { throw new RuntimeException("could not start server", e); } finally { } }
Example #18
Source File: ConsumerAndProviderTest.java From openid4java with Apache License 2.0 | 5 votes |
public ConsumerAndProviderTest(final String testName) throws Exception { super(testName); int servletPort = Integer.parseInt(System.getProperty("SERVLET_PORT", "8989")); _server = new Server(servletPort); Context context = new Context(_server, "/", Context.SESSIONS); _baseUrl = "http://localhost:" + servletPort; // + // context.getContextPath(); SampleConsumer consumer = new SampleConsumer(_baseUrl + "/loginCallback"); context.addServlet(new ServletHolder(new LoginServlet(consumer)), "/login"); context.addServlet(new ServletHolder(new LoginCallbackServlet(consumer)), "/loginCallback"); context.addServlet(new ServletHolder(new UserInfoServlet()), "/user"); SampleServer server = new SampleServer(_baseUrl + "/provider") { protected List userInteraction(ParameterList request) throws ServerException { List back = new ArrayList(); back.add("userSelectedClaimedId"); // userSelectedClaimedId back.add(Boolean.TRUE); // authenticatedAndApproved back.add("[email protected]"); // email return back; } }; context.addServlet(new ServletHolder(new ProviderServlet(server)), "/provider"); }
Example #19
Source File: Main.java From blog-codes with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws Exception { Server server = new Server(PORT); // Static file handler Context fileContext = new Context(server, "/mxgraph", Context.SESSIONS); ResourceHandler fileHandler = new ResourceHandler(); fileHandler.setResourceBase("."); fileContext.setHandler(fileHandler); // Servlets Context context = new Context(server, "/", Context.SESSIONS); context.addServlet(new ServletHolder(new Roundtrip()), "/Roundtrip"); context.addServlet(new ServletHolder(new ServerView()), "/ServerView"); context.addServlet(new ServletHolder(new ExportServlet()), "/Export"); context.addServlet(new ServletHolder(new EchoServlet()), "/Echo"); context.addServlet(new ServletHolder(new Deploy()), "/Deploy"); context.addServlet(new ServletHolder(new Link()), "/Link"); context.addServlet(new ServletHolder(new EmbedImage()), "/EmbedImage"); context.addServlet(new ServletHolder(new Backend()), "/Backend"); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { new RedirectHandler(), fileContext, context, new DefaultHandler() }); server.setHandler(handlers); System.out.println("Go to http://localhost:" + PORT + "/"); server.start(); server.join(); }
Example #20
Source File: BaseJettyServer.java From recipes-rss with Apache License 2.0 | 5 votes |
public BaseJettyServer() { System.setProperty(DynamicPropertyFactory.ENABLE_JMX, "true"); this.karyonServer = new KaryonServer(); this.injector = karyonServer.initialize(); this.jettyServer = new Server(); }
Example #21
Source File: AbstractJettyRunTask.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
@TaskAction protected void start() { ClassLoader originalClassloader = Server.class.getClassLoader(); List<File> additionalClasspath = new ArrayList<File>(); for (File additionalRuntimeJar : getAdditionalRuntimeJars()) { additionalClasspath.add(additionalRuntimeJar); } URLClassLoader jettyClassloader = new URLClassLoader(new DefaultClassPath(additionalClasspath).getAsURLArray(), originalClassloader); try { Thread.currentThread().setContextClassLoader(jettyClassloader); startJetty(); } finally { Thread.currentThread().setContextClassLoader(originalClassloader); } }
Example #22
Source File: Monitor.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public Monitor(int port, String key, Server server) throws IOException { this.server = server; if (port <= 0) { throw new IllegalStateException("Bad stop port"); } if (key == null) { throw new IllegalStateException("Bad stop key"); } this.key = key; setDaemon(true); setName("StopJettyPluginMonitor"); serverSocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1")); serverSocket.setReuseAddress(true); }
Example #23
Source File: TestWebDelegationToken.java From big-c with Apache License 2.0 | 5 votes |
@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 #24
Source File: TestWebDelegationToken.java From big-c with Apache License 2.0 | 5 votes |
protected Server createJettyServer() { try { InetAddress localhost = InetAddress.getLocalHost(); ServerSocket ss = new ServerSocket(0, 50, localhost); int port = ss.getLocalPort(); ss.close(); jetty = new Server(0); jetty.getConnectors()[0].setHost("localhost"); jetty.getConnectors()[0].setPort(port); return jetty; } catch (Exception ex) { throw new RuntimeException("Could not setup Jetty: " + ex.getMessage(), ex); } }
Example #25
Source File: ServerMojo.java From opoopress with Apache License 2.0 | 5 votes |
@Override protected void executeInternal(SiteConfigImpl config, SiteImpl site) throws MojoExecutionException, MojoFailureException { Server server = createJettyServer(site); try { server.start(); onServerStart(site, server); server.join(); } catch (Exception e) { throw new MojoExecutionException("Start server failed: " + e.getMessage(), e); } }
Example #26
Source File: TestJettyHelper.java From big-c with Apache License 2.0 | 5 votes |
/** * Returns the authority (hostname & port) used by the JettyServer. * * @return an <code>InetSocketAddress</code> with the corresponding authority. */ public static InetSocketAddress getAuthority() { Server server = getJettyServer(); try { InetAddress add = InetAddress.getByName(server.getConnectors()[0].getHost()); int port = server.getConnectors()[0].getPort(); return new InetSocketAddress(add, port); } catch (UnknownHostException ex) { throw new RuntimeException(ex); } }
Example #27
Source File: CrawlDBTestUtil.java From anthelion with Apache License 2.0 | 5 votes |
/** * Creates a new JettyServer with one static root context * * @param port port to listen to * @param staticContent folder where static content lives * @throws UnknownHostException */ public static Server getServer(int port, String staticContent) throws UnknownHostException{ Server webServer = new org.mortbay.jetty.Server(); SocketConnector listener = new SocketConnector(); listener.setPort(port); listener.setHost("127.0.0.1"); webServer.addConnector(listener); ContextHandler staticContext = new ContextHandler(); staticContext.setContextPath("/"); staticContext.setResourceBase(staticContent); staticContext.addHandler(new ResourceHandler()); webServer.addHandler(staticContext); return webServer; }
Example #28
Source File: JettyContainer.java From dubbox with Apache License 2.0 | 5 votes |
public void start() { String serverPort = ConfigUtils.getProperty(JETTY_PORT); int port; if (serverPort == null || serverPort.length() == 0) { port = DEFAULT_JETTY_PORT; } else { port = Integer.parseInt(serverPort); } connector = new SelectChannelConnector(); connector.setPort(port); ServletHandler handler = new ServletHandler(); String resources = ConfigUtils.getProperty(JETTY_DIRECTORY); if (resources != null && resources.length() > 0) { FilterHolder resourceHolder = handler.addFilterWithMapping(ResourceFilter.class, "/*", Handler.DEFAULT); resourceHolder.setInitParameter("resources", resources); } ServletHolder pageHolder = handler.addServletWithMapping(PageServlet.class, "/*"); pageHolder.setInitParameter("pages", ConfigUtils.getProperty(JETTY_PAGES)); pageHolder.setInitOrder(2); Server server = new Server(); server.addConnector(connector); server.addHandler(handler); try { server.start(); } catch (Exception e) { throw new IllegalStateException("Failed to start jetty server on " + NetUtils.getLocalHost() + ":" + port + ", cause: " + e.getMessage(), e); } }
Example #29
Source File: TestWebAppProxyServlet.java From big-c with Apache License 2.0 | 5 votes |
/** * 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 #30
Source File: HttpServerImpl.java From reef with Apache License 2.0 | 5 votes |
/** * Constructor of HttpServer that wraps Jetty Server. * * @param jettyHandler * @throws Exception */ @Inject HttpServerImpl(@Parameter(RemoteConfiguration.HostAddress.class) final String hostAddress, final JettyHandler jettyHandler, final LocalAddressProvider addressProvider, final TcpPortProvider tcpPortProvider, final LoggingScopeFactory loggingScopeFactory) throws Exception { this.loggingScopeFactory = loggingScopeFactory; this.jettyHandler = jettyHandler; this.hostAddress = UNKNOWN_HOST_NAME.equals(hostAddress) ? addressProvider.getLocalAddress() : hostAddress; try (LoggingScope ls = this.loggingScopeFactory.httpServer()) { Server srv = null; int availablePort = -1; for (final int p : tcpPortProvider) { srv = tryPort(p); if (srv != null) { availablePort = p; break; } } if (srv != null) { this.server = srv; this.port = availablePort; this.server.setHandler(jettyHandler); LOG.log(Level.INFO, "Jetty Server started with port: {0}", availablePort); } else { throw new RuntimeException("Could not find available port for http"); } } }