Java Code Examples for org.eclipse.jetty.util.resource.Resource#newClassPathResource()

The following examples show how to use org.eclipse.jetty.util.resource.Resource#newClassPathResource() . 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: EventServer.java    From Cognizant-Intelligent-Test-Scripter with Apache License 2.0 6 votes vote down vote up
private SslConnectionFactory getSSLConnectionFactory() {
    Resource keyStoreResource = null;
    try {
        keyStoreResource = Resource.newClassPathResource("localhost");
        System.out.println(keyStoreResource);
    } catch (Exception ex) {
        Logger.getLogger(EventServer.class.getName()).log(Level.SEVERE, null, ex);
    }

    SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setKeyStoreResource(keyStoreResource);
    String secret = readresource();
    sslContextFactory.setKeyStorePassword(Encrypt.getInstance().decrypt(secret));
    sslContextFactory.setKeyManagerPassword(Encrypt.getInstance().decrypt(secret));
    return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString());
}
 
Example 2
Source File: ApplicationGroupTest.java    From rest-utils with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticResourceIsolation() throws Exception {
  TestApp app1 = new TestApp("/app1");
  TestApp app2 = new TestApp("/app2") {
    @Override
    public void setupResources(final Configurable<?> config, final TestRestConfig appConfig) {
      config.register(RestResource.class);
      config.property(ServletProperties.FILTER_STATIC_CONTENT_REGEX, "/(index\\.html|)");
    }

    @Override
    protected ResourceCollection getStaticResources() {
      return new ResourceCollection(Resource.newClassPathResource("static"));
    }
  };

  server.registerApplication(app1);
  server.registerApplication(app2);
  server.start();

  assertThat(makeGetRequest("/app1/index.html"), is(Code.NOT_FOUND));
  assertThat(makeGetRequest("/app2/index.html"), is(Code.OK));
}
 
Example 3
Source File: JettySingleApplicationOnlyClassesTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Include JAX-RS application from org.apache.applications.classes package
            Resource.newClassPathResource("/org/apache/demo/applications/classes")
        }, PORT);
}
 
Example 4
Source File: JettyEmptyPathApplicationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Include JAX-RS application from org.apache.applications.empty package
            Resource.newClassPathResource("/org/apache/demo/applications/emptypath"),
            // Include Jackson @Providers into classpath scanning
            Resource.newResource(JacksonJsonProvider.class.getProtectionDomain().getCodeSource().getLocation())
        }, PORT);
}
 
Example 5
Source File: JettyNoApplicationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Include Jackson @Providers into classpath scanning
            Resource.newResource(JacksonJsonProvider.class.getProtectionDomain().getCodeSource().getLocation())
        }, PORT);
}
 
Example 6
Source File: JettySingleApplicationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Limit the classpath scanning to org.apache.demo.complete package
            Resource.newClassPathResource("/org/apache/demo/applications/complete")
        }, PORT);
}
 
Example 7
Source File: JettyEmptyApplicationTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Include JAX-RS application from org.apache.applications.empty package
            Resource.newClassPathResource("/org/apache/demo/applications/empty"),
            // Include Jackson @Providers into classpath scanning
            Resource.newResource(JacksonJsonProvider.class.getProtectionDomain().getCodeSource().getLocation())
        }, PORT);
}
 
Example 8
Source File: JettySingleApplicationOnlySingletonsTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public EmbeddedJettyServer() {
    super("/",
        new Resource[] {
            // Limit the classpath scanning to org.apache.demo.resources package
            Resource.newClassPathResource("/org/apache/demo/resources"),
            // Include JAX-RS application from org.apache.applications.singletons package
            Resource.newClassPathResource("/org/apache/demo/applications/singletons")
        }, PORT);
}
 
Example 9
Source File: SSLTestConfig.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * Create an SSLTestConfig based on a few caller specified options.  As needed, 
 * keystore/truststore information will be pulled from a hardcoded resource files provided 
 * by the solr test-framework based on the value of <code>checkPeerName</code>:
 * <ul>
 * <li><code>true</code> - A keystore resource file will be used that specifies 
 *     a CN of <code>localhost</code> and a SAN IP of <code>127.0.0.1</code>, to 
 *     ensure that all connections should be valid regardless of what machine runs the tests.</li> 
 * <li><code>false</code> - A keystore resource file will be used that specifies 
 *     a bogus hostname in the CN and reserved IP as the SAN, since no (valid) tests using this 
 *     SSLTestConfig should care what CN/SAN are.</li> 
 * </ul>
 *
 * @param useSSL - whether SSL should be required.
 * @param clientAuth - whether client authentication should be required.
 * @param checkPeerName - whether the client should validate the 'peer name' of the SSL Certificate (and which testing Cert should be used)
 * @see HttpClientUtil#SYS_PROP_CHECK_PEER_NAME
 */
public SSLTestConfig(boolean useSSL, boolean clientAuth, boolean checkPeerName) {
  this.useSsl = useSSL;
  this.clientAuth = clientAuth;
  this.checkPeerName = checkPeerName;

  if (useSsl) {
    assumeSslIsSafeToTest();
  }
  
  final String resourceName = checkPeerName
    ? TEST_KEYSTORE_LOCALHOST_RESOURCE : TEST_KEYSTORE_BOGUSHOST_RESOURCE;
  trustStore = keyStore = Resource.newClassPathResource(resourceName);
  if (null == keyStore || ! keyStore.exists() ) {
    throw new IllegalStateException("Unable to locate keystore resource file in classpath: "
                                    + resourceName);
  }
}
 
Example 10
Source File: Start.java    From etcd-viewer with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    int timeout = (int) Duration.ONE_HOUR.getMilliseconds();

    System.setProperty(WICKET_CFG, CfgType.development.toString());

    Server server = new Server();

    ServerConnector connector = new ServerConnector(server);

    // Set some timeout options to make debugging easier.
    connector.setIdleTimeout(timeout);
    connector.setSoLingerTime(-1);
    connector.setPort(8080);
    server.addConnector(connector);

    Resource keystore = Resource.newClassPathResource("/keystore");
    if (keystore != null && keystore.exists()) {
        // if a keystore for a SSL certificate is available, start a SSL
        // connector on port 8443.
        // By default, the quickstart comes with a Apache Wicket Quickstart
        // Certificate that expires about half way september 2021. Do not
        // use this certificate anywhere important as the passwords are
        // available in the source.

        SslContextFactory sslContextFactory = new SslContextFactory();
        sslContextFactory.setKeyStoreResource(keystore);
        sslContextFactory.setKeyStorePassword("wicket");
        sslContextFactory.setTrustStoreResource(keystore);
        sslContextFactory.setKeyManagerPassword("wicket");

        // HTTP Configuration
        HttpConfiguration httpConfig = new HttpConfiguration();
        httpConfig.setSecureScheme("https");
        httpConfig.setSecurePort(8443);
        httpConfig.setOutputBufferSize(32768);
        httpConfig.setRequestHeaderSize(8192);
        httpConfig.setResponseHeaderSize(8192);
        httpConfig.setSendServerVersion(true);
        httpConfig.setSendDateHeader(false);

        // SSL HTTP Configuration
        HttpConfiguration httpsConfig = new HttpConfiguration(httpConfig);
        httpsConfig.addCustomizer(new SecureRequestCustomizer());

        // SSL Connector
        ServerConnector sslConnector = new ServerConnector(server,
                new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()),
                new HttpConnectionFactory(httpsConfig));
        sslConnector.setPort(8443);

        server.addConnector(sslConnector);

        System.out.println("SSL access to the quickstart has been enabled on port 8443");
        System.out.println("You can access the application using SSL on https://localhost:8443");
        System.out.println();
    }

    WebAppContext bb = new WebAppContext();
    bb.setServer(server);
    bb.setContextPath("/");
    bb.setWar("src/main/webapp");

    // START JMX SERVER
    // MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
    // MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
    // server.getContainer().addEventListener(mBeanContainer);
    // mBeanContainer.start();

    server.setHandler(bb);

    try {
        System.out.println(">>> STARTING EMBEDDED JETTY SERVER, PRESS ANY KEY TO STOP");
        server.start();
        System.in.read();
        System.out.println(">>> STOPPING EMBEDDED JETTY SERVER");
        server.stop();
        server.join();
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
 
Example 11
Source File: Start.java    From wicket-orientdb with Apache License 2.0 4 votes vote down vote up
/**
 * Main function, starts the jetty server.
 *
 * @param args
 */
public static void main(String[] args)
{
	System.setProperty("wicket.configuration", "development");

	Server server = new Server();

	HttpConfiguration http_config = new HttpConfiguration();
	http_config.setSecureScheme("https");
	http_config.setSecurePort(8443);
	http_config.setOutputBufferSize(32768);

	ServerConnector http = new ServerConnector(server, new HttpConnectionFactory(http_config));
	http.setPort(8080);
	http.setIdleTimeout(1000 * 60 * 60);

	server.addConnector(http);

	Resource keystore = Resource.newClassPathResource("/keystore");
	if (keystore != null && keystore.exists())
	{
		// if a keystore for a SSL certificate is available, start a SSL
		// connector on port 8443.
		// By default, the quickstart comes with a Apache Wicket Quickstart
		// Certificate that expires about half way september 2021. Do not
		// use this certificate anywhere important as the passwords are
		// available in the source.

		SslContextFactory sslContextFactory = new SslContextFactory();
		sslContextFactory.setKeyStoreResource(keystore);
		sslContextFactory.setKeyStorePassword("wicket");
		sslContextFactory.setKeyManagerPassword("wicket");

		HttpConfiguration https_config = new HttpConfiguration(http_config);
		https_config.addCustomizer(new SecureRequestCustomizer());

		ServerConnector https = new ServerConnector(server, new SslConnectionFactory(
			sslContextFactory, "http/1.1"), new HttpConnectionFactory(https_config));
		https.setPort(8443);
		https.setIdleTimeout(500000);

		server.addConnector(https);
		System.out.println("SSL access to the examples has been enabled on port 8443");
		System.out
			.println("You can access the application using SSL on https://localhost:8443");
		System.out.println();
	}

	WebAppContext bb = new WebAppContext();
	bb.setServer(server);
	bb.setContextPath("/");
	bb.setWar("src/main/webapp");

	// uncomment next line if you want to test with JSESSIONID encoded in the urls
	// ((AbstractSessionManager)
	// bb.getSessionHandler().getSessionManager()).setUsingCookies(false);

	server.setHandler(bb);

	MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
	MBeanContainer mBeanContainer = new MBeanContainer(mBeanServer);
	server.addEventListener(mBeanContainer);
	server.addBean(mBeanContainer);

	try
	{
		server.start();
		server.join();
	}
	catch (Exception e)
	{
		e.printStackTrace();
		System.exit(100);
	}
}
 
Example 12
Source File: HelloWorldApplication.java    From rest-utils with Apache License 2.0 4 votes vote down vote up
@Override
protected ResourceCollection getStaticResources() {
  return new ResourceCollection(Resource.newClassPathResource("static"));
}
 
Example 13
Source File: StaticResourcesTest.java    From rest-utils with Apache License 2.0 4 votes vote down vote up
@Override
protected ResourceCollection getStaticResources() {
  return new ResourceCollection(Resource.newClassPathResource("static"));
}