Example usage for org.apache.http.protocol UriHttpRequestHandlerMapper UriHttpRequestHandlerMapper

List of usage examples for org.apache.http.protocol UriHttpRequestHandlerMapper UriHttpRequestHandlerMapper

Introduction

In this page you can find the example usage for org.apache.http.protocol UriHttpRequestHandlerMapper UriHttpRequestHandlerMapper.

Prototype

public UriHttpRequestHandlerMapper() 

Source Link

Usage

From source file:org.Cherry.Modules.Web.Engine.WebEngine.java

private UriHttpRequestHandlerMapper getRequestHandlerRegistry() {
    if (null == _registry) {
        _registry = new UriHttpRequestHandlerMapper();
        _registry.register("*", _requestDispatcher);
    }/*from  w  ww . ja v  a 2  s  .  c om*/

    return _registry;
}

From source file:com.gravspace.core.HttpServer.java

public static void start(String[] args) throws Exception {

    int port = 8082;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }//from  w w w  . j a va 2s  .c  om

    ActorSystem system = ActorSystem.create("Application-System");
    Properties config = new Properties();
    config.load(HttpServer.class.getResourceAsStream("/megapode.conf"));
    ActorRef master = system.actorOf(Props.create(CoordinatingActor.class, config), "Coordinator");

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl())
            .build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpHandler(system, master));

    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    SSLServerSocketFactory sf = null;
    if (port == 8443) {
        // Initialize SSL context
        ClassLoader cl = HttpServer.class.getClassLoader();
        URL url = cl.getResource("my.keystore");
        if (url == null) {
            System.out.println("Keystore not found");
            System.exit(1);
        }
        KeyStore keystore = KeyStore.getInstance("jks");
        keystore.load(url.openStream(), "secret".toCharArray());
        KeyManagerFactory kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        kmfactory.init(keystore, "secret".toCharArray());
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(keymanagers, null, null);
        sf = sslcontext.getServerSocketFactory();
    }

    RequestListenerThread t = new RequestListenerThread(port, httpService, sf);
    t.setDaemon(false);
    t.start();

    t.join();
}

From source file:it.danja.newsmonitor.utils.HttpServer.java

public void init() {

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl())
            .build();/*  w  ww.j  ava2s. c  o m*/

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpFileHandler(docRoot));

    // Set up the HTTP service
    httpService = new HttpService(httpproc, reqistry);

    if (port == 8443) {
        // Initialize SSL context
        ClassLoader cl = HttpServer.class.getClassLoader();
        URL url = cl.getResource("my.keystore");
        if (url == null) {
            log.info("HttpServer : Keystore not found");
            System.exit(1);
        }
        KeyStore keystore = null;
        try {
            keystore = KeyStore.getInstance("jks");
        } catch (KeyStoreException e) {

            log.error(e.getMessage());
        }
        try {
            keystore.load(url.openStream(), "secret".toCharArray());
        } catch (NoSuchAlgorithmException e) {

            log.error(e.getMessage());
        } catch (CertificateException e) {

            log.error(e.getMessage());
        } catch (IOException e) {

            log.error(e.getMessage());
        }
        KeyManagerFactory kmfactory = null;
        try {
            kmfactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        } catch (NoSuchAlgorithmException e) {

            log.error(e.getMessage());
        }
        try {
            kmfactory.init(keystore, "secret".toCharArray());
        } catch (UnrecoverableKeyException e) {

            log.error(e.getMessage());
        } catch (KeyStoreException e) {

            log.error(e.getMessage());
        } catch (NoSuchAlgorithmException e) {

            log.error(e.getMessage());
        }
        KeyManager[] keymanagers = kmfactory.getKeyManagers();
        SSLContext sslcontext = null;
        try {
            sslcontext = SSLContext.getInstance("TLS");
        } catch (NoSuchAlgorithmException e) {

            log.error(e.getMessage());
        }
        try {
            sslcontext.init(keymanagers, null, null);
        } catch (KeyManagementException e) {

            log.error(e.getMessage());
        }
        this.sf = sslcontext.getServerSocketFactory();
    }
}

From source file:org.jenkinsci.test.acceptance.update_center.MockUpdateCenter.java

public void ensureRunning() {
    if (original != null) {
        return;//from  w  w  w .j  a v a2  s .c om
    }
    // TODO this will likely not work on arbitrary controllers, so perhaps limit to the default WinstoneController
    Jenkins jenkins = injector.getInstance(Jenkins.class);
    List<String> sites = new UpdateCenter(jenkins).getJson("tree=sites[url]").findValuesAsText("url");
    if (sites.size() != 1) {
        // TODO ideally it would rather delegate to all of them, but that implies deprecating CachedUpdateCenterMetadataLoader.url and using whatever site(s) Jenkins itself specifies
        LOGGER.log(Level.WARNING, "found an unexpected number of update sites: {0}", sites);
        return;
    }
    UpdateCenterMetadata ucm;
    try {
        ucm = ucmd.get(jenkins);
    } catch (IOException x) {
        LOGGER.log(Level.WARNING, "cannot load data for mock update center", x);
        return;
    }
    JSONObject all;
    try {
        all = new JSONObject(ucm.originalJSON);
        all.remove("signature");
        JSONObject plugins = all.getJSONObject("plugins");
        LOGGER.info(() -> "editing JSON with " + plugins.length() + " plugins to reflect " + ucm.plugins.size()
                + " possible overrides");
        for (PluginMetadata meta : ucm.plugins.values()) {
            String name = meta.getName();
            String version = meta.getVersion();
            JSONObject plugin = plugins.optJSONObject(name);
            if (plugin == null) {
                LOGGER.log(Level.INFO, "adding plugin {0}", name);
                plugin = new JSONObject().accumulate("name", name);
                plugins.put(name, plugin);
            }
            plugin.put("url", name + ".hpi");
            updating(plugin, "version", version);
            updating(plugin, "gav", meta.gav);
            updating(plugin, "requiredCore", meta.requiredCore().toString());
            updating(plugin, "dependencies", new JSONArray(meta.getDependencies().stream().map(d -> {
                try {
                    return new JSONObject().accumulate("name", d.name).accumulate("version", d.version)
                            .accumulate("optional", d.optional);
                } catch (JSONException x) {
                    throw new AssertionError(x);
                }
            }).collect(Collectors.toList())));
            plugin.remove("sha1");
        }
    } catch (JSONException x) {
        LOGGER.log(Level.WARNING, "cannot prepare mock update center", x);
        return;
    }
    HttpProcessor proc = HttpProcessorBuilder.create().add(new ResponseServer("MockUpdateCenter"))
            .add(new ResponseContent()).add(new RequestConnControl()).build();
    UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
    String json = "updateCenter.post(\n" + all + "\n);";
    handlerMapper.register("/update-center.json",
            (HttpRequest request, HttpResponse response, HttpContext context) -> {
                response.setStatusCode(HttpStatus.SC_OK);
                response.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
            });
    handlerMapper.register("*.hpi", (HttpRequest request, HttpResponse response, HttpContext context) -> {
        String plugin = request.getRequestLine().getUri().replaceFirst("^/(.+)[.]hpi$", "$1");
        PluginMetadata meta = ucm.plugins.get(plugin);
        if (meta == null) {
            LOGGER.log(Level.WARNING, "no such plugin {0}", plugin);
            response.setStatusCode(HttpStatus.SC_NOT_FOUND);
            return;
        }
        File local = meta.resolve(injector, meta.getVersion());
        LOGGER.log(Level.INFO, "serving {0}", local);
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new FileEntity(local));
    });
    handlerMapper.register("*", (HttpRequest request, HttpResponse response, HttpContext context) -> {
        String location = original.replace("/update-center.json", request.getRequestLine().getUri());
        LOGGER.log(Level.INFO, "redirect to {0}", location);
        /* TODO for some reason DownloadService.loadJSONHTML does not seem to process the redirect, despite calling setInstanceFollowRedirects(true):
        response.setStatusCode(HttpStatus.SC_MOVED_TEMPORARILY);
        response.setHeader("Location", location);
         */
        HttpURLConnection uc = (HttpURLConnection) new URL(location).openConnection();
        uc.setInstanceFollowRedirects(true);
        // TODO consider caching these downloads locally like CachedUpdateCenterMetadataLoader does for the main update-center.json
        byte[] data = IOUtils.toByteArray(uc);
        String contentType = uc.getContentType();
        response.setStatusCode(HttpStatus.SC_OK);
        response.setEntity(new ByteArrayEntity(data, ContentType.create(contentType)));

    });
    server = ServerBootstrap.bootstrap().
    // could setLocalAddress if using a JenkinsController that requires it
            setHttpProcessor(proc).setHandlerMapper(handlerMapper).setExceptionLogger(serverExceptionHandler())
            .create();

    try {
        server.start();
    } catch (IOException x) {
        LOGGER.log(Level.WARNING, "cannot start mock update center", x);
        return;

    }
    original = sites.get(0);
    // TODO figure out how to deal with Docker-based controllers which would need to have an IP address for the host
    String override = "http://" + server.getInetAddress().getHostAddress() + ":" + server.getLocalPort()
            + "/update-center.json";
    LOGGER.log(Level.INFO, "replacing update site {0} with {1}", new Object[] { original, override });
    jenkins.runScript(
            "DownloadService.signatureCheck = false; Jenkins.instance.updateCenter.sites.replaceBy([new UpdateSite(UpdateCenter.ID_DEFAULT, '%s')])",
            override);
}

From source file:cpcc.ros.sim.osm.CameraTest.java

@BeforeMethod
public void setUp() throws Exception {
    int port = RandomUtils.nextInt(30000, 50000);

    gs = new WGS84();

    tempDir = File.createTempFile("camera", "");
    tempDir.delete();/*from  ww  w  .  j a  v a  2  s  . com*/
    FileUtils.forceMkdir(tempDir);

    config = mock(Configuration.class);
    when(config.getCameraHeight()).thenReturn(240);
    when(config.getCameraWidth()).thenReturn(320);
    when(config.getTileHeight()).thenReturn(256);
    when(config.getTileWidth()).thenReturn(256);
    when(config.getOriginPosition()).thenReturn(null);
    when(config.getCameraApertureAngle()).thenReturn(2.0);
    when(config.getTileServerUrl()).thenReturn("http://localhost:" + port + "/%1$d/%2$d/%3$d.png");
    when(config.getZoomLevel()).thenReturn(18);
    when(config.getGeodeticSystem()).thenReturn(gs);
    when(config.getTileCacheBaseDir()).thenReturn(tempDir.getAbsolutePath());

    camera = new Camera(config);

    testFileHandler = new MyHttpFileHandler();
    testFileHandler.getResponses().put("/18/41920/101290.png", new Object[] { "image/png", blackTileFile });
    testFileHandler.getResponses().put("/18/41920/101291.png", new Object[] { "image/png", blueTileFile });
    testFileHandler.getResponses().put("/18/41920/101292.png", new Object[] { "image/png", cyanTileFile });
    testFileHandler.getResponses().put("/18/41920/101293.png", new Object[] { "image/png", greenTileFile });
    testFileHandler.getResponses().put("/18/41920/101294.png", new Object[] { "image/png", purpleTileFile });

    testFileHandler.getResponses().put("/18/41921/101290.png", new Object[] { "image/png", redTileFile });
    testFileHandler.getResponses().put("/18/41921/101291.png", new Object[] { "image/png", whiteTileFile });
    testFileHandler.getResponses().put("/18/41921/101292.png", new Object[] { "image/png", blackTileFile });
    testFileHandler.getResponses().put("/18/41921/101293.png", new Object[] { "image/png", blueTileFile });
    testFileHandler.getResponses().put("/18/41921/101294.png", new Object[] { "image/png", cyanTileFile });

    testFileHandler.getResponses().put("/18/41922/101290.png", new Object[] { "image/png", greenTileFile });
    testFileHandler.getResponses().put("/18/41922/101291.png", new Object[] { "image/png", purpleTileFile });
    testFileHandler.getResponses().put("/18/41922/101292.png", new Object[] { "image/png", redTileFile });
    testFileHandler.getResponses().put("/18/41922/101293.png", new Object[] { "image/png", whiteTileFile });
    testFileHandler.getResponses().put("/18/41922/101294.png", new Object[] { "image/png", blackTileFile });

    testFileHandler.getResponses().put("/18/41923/101290.png", new Object[] { "image/png", blueTileFile });
    testFileHandler.getResponses().put("/18/41923/101291.png", new Object[] { "image/png", cyanTileFile });
    testFileHandler.getResponses().put("/18/41923/101292.png", new Object[] { "image/png", greenTileFile });
    testFileHandler.getResponses().put("/18/41923/101293.png", new Object[] { "image/png", purpleTileFile });
    testFileHandler.getResponses().put("/18/41923/101294.png", new Object[] { "image/png", redTileFile });

    testFileHandler.getResponses().put("/18/41924/101290.png", new Object[] { "image/png", whiteTileFile });
    testFileHandler.getResponses().put("/18/41924/101291.png", new Object[] { "image/png", blackTileFile });
    testFileHandler.getResponses().put("/18/41924/101292.png", new Object[] { "image/png", blueTileFile });
    testFileHandler.getResponses().put("/18/41924/101293.png", new Object[] { "image/png", cyanTileFile });
    testFileHandler.getResponses().put("/18/41924/101294.png", new Object[] { "image/png", greenTileFile });
    // whiteTileFile
    testFileHandler.getResponses().put("/18/41925/101290.png", new Object[] { "image/png", purpleTileFile });
    testFileHandler.getResponses().put("/18/41925/101291.png", new Object[] { "image/png", redTileFile });
    testFileHandler.getResponses().put("/18/41925/101292.png", new Object[] { "image/png", whiteTileFile });
    testFileHandler.getResponses().put("/18/41925/101293.png", new Object[] { "image/png", blackTileFile });
    testFileHandler.getResponses().put("/18/41925/101294.png", new Object[] { "image/png", blueTileFile });

    testFileHandler.getResponses().put("/18/41926/101290.png", new Object[] { "image/png", cyanTileFile });
    testFileHandler.getResponses().put("/18/41926/101291.png", new Object[] { "image/png", greenTileFile });
    testFileHandler.getResponses().put("/18/41926/101292.png", new Object[] { "image/png", purpleTileFile });
    testFileHandler.getResponses().put("/18/41926/101293.png", new Object[] { "image/png", redTileFile });
    testFileHandler.getResponses().put("/18/41926/101294.png", new Object[] { "image/png", whiteTileFile });

    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Test/1.1")).add(new ResponseContent()).add(new ResponseConnControl())
            .build();

    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", testFileHandler);

    HttpService httpService = new HttpService(httpproc, reqistry);

    requestListenerThread = new RequestListenerThread(port, httpService);
    requestListenerThread.setDaemon(false);
    requestListenerThread.start();
}

From source file:org.apache.http.testserver.HttpServer.java

public HttpServer() throws IOException {
    super();
    this.reqistry = new UriHttpRequestHandlerMapper();
}