Java Code Examples for org.eclipse.jetty.server.Server#getURI()

The following examples show how to use org.eclipse.jetty.server.Server#getURI() . 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: HttpDataPointProtobufReceiverConnectionTest.java    From signalfx-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testBackfill() throws Exception {
    Server server = new Server(0);
    server.setHandler(new BackfillHandler());
    server.start();
    URI uri = server.getURI();
    DataPointReceiver dpr = new HttpDataPointProtobufReceiverFactory(
            new SignalFxEndpoint(uri.getScheme(), uri.getHost(), uri.getPort()))
            .createDataPointReceiver();

    ArrayList<SignalFxProtocolBuffers.PointValue> values = new ArrayList<SignalFxProtocolBuffers.PointValue>(Arrays.asList(
            SignalFxProtocolBuffers.PointValue.newBuilder().setTimestamp(System.currentTimeMillis())
                    .setValue(SignalFxProtocolBuffers.Datum.newBuilder().setDoubleValue(123.0)).build()
    ));
    HashMap<String,String> dims = new HashMap<String,String>();
    dims.put("baz", "gorch");
    dims.put("moo", "cow");

    dpr.backfillDataPoints(AUTH_TOKEN, "foo.bar.baz", "counter", "ABC123", dims, values);
    server.stop();
}
 
Example 2
Source File: WorkerServer.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private static String getErrorMessage(Server server, int port, Exception ex) {
    if (ex instanceof BindException) {
        final URI uri = server.getURI();
        return String.format("%s http://%s:%d", ex.getMessage(), uri.getHost(), port);
    }

    return ex.getMessage();
}
 
Example 3
Source File: Runtime.java    From incubator-heron with Apache License 2.0 5 votes vote down vote up
private static String getErrorMessage(Server server, int port, Exception ex) {
  if (ex instanceof BindException) {
    final URI uri = server.getURI();
    return String.format("%s http://%s:%d", ex.getMessage(), uri.getHost(), port);
  }

  return ex.getMessage();
}
 
Example 4
Source File: IpcServletFilterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void init() throws Exception {
  server = new Server(new InetSocketAddress("localhost", 0));
  ServletHandler handler = new ServletHandler();
  handler.addServletWithMapping(OkServlet.class, "/test/foo/*");
  handler.addServletWithMapping(OkServlet.class, "/api/*");
  handler.addServletWithMapping(BadRequestServlet.class, "/bad/*");
  handler.addServletWithMapping(FailServlet.class, "/throw/*");
  handler.addServletWithMapping(CustomEndpointServlet.class, "/endpoint/*");
  handler.addServletWithMapping(OkServlet.class, "/*");
  handler.addFilterWithMapping(IpcServletFilter.class, "/*", EnumSet.of(DispatcherType.REQUEST));
  server.setHandler(handler);
  server.start();
  baseUri = server.getURI();
}
 
Example 5
Source File: GuiceServletFilterTest.java    From spectator with Apache License 2.0 5 votes vote down vote up
@BeforeAll
public static void init() throws Exception {
  server = new Server(new InetSocketAddress("localhost", 0));
  ServletContextHandler handler = new ServletContextHandler(server, "/");
  handler.addEventListener(new TestListener());
  handler.addFilter(GuiceFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
  server.setHandler(handler);
  server.start();
  baseUri = server.getURI();
}
 
Example 6
Source File: HttpDataPointProtobufReceiverConnectionTest.java    From signalfx-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpConnection() throws Exception {
    Server server = new Server(0);
    server.setHandler(new AddPointsHandler());
    server.start();
    URI uri = server.getURI();
    DataPointReceiver dpr = new HttpDataPointProtobufReceiverFactory(
            new SignalFxEndpoint(uri.getScheme(), uri.getHost(), uri.getPort()))
            .createDataPointReceiver();
    dpr.addDataPoints(AUTH_TOKEN, Collections.singletonList(
            SignalFxProtocolBuffers.DataPoint.newBuilder().setSource("source").build()));
    server.stop();
}
 
Example 7
Source File: HttpClientTestBase.java    From selenium with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldAllowUrlsWithSchemesToBeUsed() throws Exception {
  Server server = new Server(PortProber.findFreePort());
  ServletContextHandler handler = new ServletContextHandler();
  handler.setContextPath("");

  class Canned extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
      try (PrintWriter writer = resp.getWriter()) {
        writer.append("Hello, World!");
      }
    }
  }
  ServletHolder holder = new ServletHolder(new Canned());
  handler.addServlet(holder, "/*");
  server.setHandler(handler);

  server.start();
  try {
    // This is a terrible choice of URL
    HttpClient client = createFactory().createClient(new URL("http://example.com"));

    URI uri = server.getURI();
    HttpRequest request = new HttpRequest(
        GET,
        String.format("http://%s:%s/hello", uri.getHost(), uri.getPort()));

    HttpResponse response = client.execute(request);

    assertThat(string(response)).isEqualTo("Hello, World!");
  } finally {
    server.stop();
  }
}
 
Example 8
Source File: ServerLauncher.java    From bromium with MIT License 4 votes vote down vote up
public static void main(final String[] args) {
  InetSocketAddress _inetSocketAddress = new InetSocketAddress("localhost", 8080);
  final Server server = new Server(_inetSocketAddress);
  WebAppContext _webAppContext = new WebAppContext();
  final Procedure1<WebAppContext> _function = (WebAppContext it) -> {
    it.setResourceBase("WebRoot");
    it.setWelcomeFiles(new String[] { "index.html" });
    it.setContextPath("/");
    AnnotationConfiguration _annotationConfiguration = new AnnotationConfiguration();
    WebXmlConfiguration _webXmlConfiguration = new WebXmlConfiguration();
    WebInfConfiguration _webInfConfiguration = new WebInfConfiguration();
    MetaInfConfiguration _metaInfConfiguration = new MetaInfConfiguration();
    it.setConfigurations(new Configuration[] { _annotationConfiguration, _webXmlConfiguration, _webInfConfiguration, _metaInfConfiguration });
    it.setAttribute(WebInfConfiguration.CONTAINER_JAR_PATTERN, ".*/com\\.hribol\\.bromium\\.dsl\\.web/.*,.*\\.jar");
    it.setInitParameter("org.mortbay.jetty.servlet.Default.useFileMappedBuffer", "false");
  };
  WebAppContext _doubleArrow = ObjectExtensions.<WebAppContext>operator_doubleArrow(_webAppContext, _function);
  server.setHandler(_doubleArrow);
  String _name = ServerLauncher.class.getName();
  final Slf4jLog log = new Slf4jLog(_name);
  try {
    server.start();
    URI _uRI = server.getURI();
    String _plus = ("Server started " + _uRI);
    String _plus_1 = (_plus + "...");
    log.info(_plus_1);
    final Runnable _function_1 = () -> {
      try {
        log.info("Press enter to stop the server...");
        final int key = System.in.read();
        if ((key != (-1))) {
          server.stop();
        } else {
          log.warn("Console input is not available. In order to stop the server, you need to cancel process manually.");
        }
      } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
      }
    };
    new Thread(_function_1).start();
    server.join();
  } catch (final Throwable _t) {
    if (_t instanceof Exception) {
      final Exception exception = (Exception)_t;
      log.warn(exception.getMessage());
      System.exit(1);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example 9
Source File: Activator.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
public void start(BundleContext context) throws Exception {
	super.start(context);

	IWorkspace workspace = ResourcesPlugin.getWorkspace();
	String webappRoot = workspace.getRoot().getLocation().toString().concat("/webapp");
	new File(webappRoot).mkdirs();

	Activator.WEB_APP_WORKSPACE = webappRoot;

	// create the server on a free port
	Server server = new Server(0);

	// Configure the ResourceHandler. Setting the resource base indicates where the
	// files should be served out of.
	ResourceHandler resource_handler = new ResourceHandler();
	resource_handler.setDirectoriesListed(true);
	resource_handler.setResourceBase(webappRoot);

	// Add the ResourceHandler to the server.
	HandlerCollection handlers = new HandlerCollection(true);
	handlers.addHandler(resource_handler);

	server.setHandler(handlers);

	// Start server up.
	try {
		server.start();
	} catch (Exception e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	// Get the URI of the server and set it global for the
	// diagram handlers to access
	if (null != server && null != server.getURI()) {
		String localhost = server.getURI().toString();
		Activator.URL = localhost;
	}

	writeVegaFiles();

	plugin = this;
}