Package com.sun.net.httpserver

Examples of com.sun.net.httpserver.HttpServer


        new JolokiaServer(cfg,false);
    }

    @Test
    public void customHttpServer() throws IOException, NoSuchFieldException, IllegalAccessException {
        HttpServer httpServer = HttpServer.create();
        JvmAgentConfig cfg = new JvmAgentConfig("");
        JolokiaServer server = new JolokiaServer(httpServer,cfg,false);
        Field field = JolokiaServer.class.getDeclaredField("httpServer");
        field.setAccessible(true);
        assertNull(field.get(server));
View Full Code Here


    @Test
    public void testRun() throws Exception {
        final int port = 34256;
        InetSocketAddress address = new InetSocketAddress(port);
        HttpServer httpServer = HttpServer.create(address, 0);
        final String path = "/notify";
        final AtomicBoolean notified = new AtomicBoolean(false);


        httpServer.createContext(path, new HttpHandler() {
            @Override
            public void handle(HttpExchange httpExchange) throws IOException {
                notified.set(true);
                httpExchange.sendResponseHeaders(200, 0);
                httpExchange.close();
            }
        });
        try {
            httpServer.start();
            TransactionlessTesting.get().run(new TestTask() {
                @Override
                public void run() throws Exception {

                    final Metadata metadata = _metadataRepository.save(MetadataRepositoryTest.newMetadata(_inc));

                    MetadataNotifier notifier = new MetadataNotifier();
                    notifier.setUrl("http://localhost:" + port + path);
                    notifier.setName("MyNotifier");
                    notifier.setEnabled(true);
                    notifier = _notifierRepository.saveAndFlush(notifier);

                    assertEquals(1, _notifierRepository.count());

                    MetadataNotification notification = new MetadataNotification().
                            setNotified(false).
                            setAction(MetadataNotificationAction.UPDATE).
                            setMetadataUuid(metadata.getUuid());
                    final MetadataNotificationId notificationId = new MetadataNotificationId().
                            setMetadataId(metadata.getId()).
                            setNotifierId(notifier.getId());
                    notification.setId(notificationId);
                    _notificationRepository.save(notification);

                    _applicationContext.getBean(MetadataNotifierTask.class).run();
                }
            });
        } finally {
            httpServer.stop(0);
        }

        assertTrue(notified.get());
    }
View Full Code Here

public class GeonetHttpRequestFactoryTest {
    @Test
    public void testReadUrl() throws Exception {
        final int port = 29483;
        InetSocketAddress address = new InetSocketAddress(port);
        HttpServer httpServer = HttpServer.create(address, 0);
        final Element expectedResponse = new Element("resource").addContent(new Element("id").setText("test"));
        HttpHandler requestHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = Xml.getString(expectedResponse).getBytes();
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String urlPath = "/1234.xml";
        httpServer.createContext(urlPath, requestHandler);
        try {
            httpServer.start();
            final XmlRequest xmlRequest = new GeonetHttpRequestFactory().createXmlRequest(new URL ("http://localhost:"+port+ urlPath));
            final Element response = xmlRequest.execute();
            assertEquals(Xml.getString(expectedResponse), Xml.getString(response));
        } finally {
            httpServer.stop(0);
        }
    }
View Full Code Here

    }
    @Test
    public void testFollowsRedirects() throws Exception {
        final int port = 29484;
        InetSocketAddress address = new InetSocketAddress(port);
        HttpServer httpServer = HttpServer.create(address, 0);

        final Element expectedResponse = new Element("resource").addContent(new Element("id").setText("test"));
        HttpHandler finalHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = Xml.getString(expectedResponse).getBytes();
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String finalUrlPath = "/final.xml";
        httpServer.createContext(finalUrlPath, finalHandler);

        HttpHandler permRedirectHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = finalUrlPath.getBytes();
                exchange.getResponseHeaders().add("location", finalUrlPath);
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_MOVED_PERM,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String permUrlPath = "/permRedirect.xml";
        httpServer.createContext(permUrlPath, permRedirectHandler);

        HttpHandler tempRedirectHandler = new HttpHandler() {

            @Override
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = finalUrlPath.getBytes();
                exchange.getResponseHeaders().add("location", finalUrlPath);
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_MOVED_TEMP,
                        response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        };
        final String tempUrlPath = "/tempRedirect.xml";
        httpServer.createContext(tempUrlPath, tempRedirectHandler);


        try {
            httpServer.start();
            XmlRequest xmlRequest = new GeonetHttpRequestFactory().createXmlRequest(new URL ("http://localhost:"+port+ permUrlPath));
            Element response = xmlRequest.execute();
            assertEquals(Xml.getString(expectedResponse), Xml.getString(response));

            xmlRequest = new GeonetHttpRequestFactory().createXmlRequest(new URL ("http://localhost:"+port+ tempUrlPath));
            response = xmlRequest.execute();
            assertEquals(Xml.getString(expectedResponse), Xml.getString(response));
        } finally {
            httpServer.stop(0);
        }
    }
View Full Code Here

            if (factory == null) {
                s_logger.error("Unable to load HTTP server factory");
                System.exit(1);
            }

            HttpServer server = factory.createHttpServerInstance(httpListenPort);
            server.createContext("/getscreen", new ConsoleProxyThumbnailHandler());
            server.createContext("/resource/", new ConsoleProxyResourceHandler());
            server.createContext("/ajax", new ConsoleProxyAjaxHandler());
            server.createContext("/ajaximg", new ConsoleProxyAjaxImageHandler());
            server.setExecutor(new ThreadExecutor()); // creates a default executor
            server.start();
        } catch (Exception e) {
            s_logger.error(e.getMessage(), e);
            System.exit(1);
        }
    }
View Full Code Here

    }

    private static void startupHttpCmdPort() {
        try {
            s_logger.info("Listening for HTTP CMDs on port " + httpCmdListenPort);
            HttpServer cmdServer = HttpServer.create(new InetSocketAddress(httpCmdListenPort), 2);
            cmdServer.createContext("/cmd", new ConsoleProxyCmdHandler());
            cmdServer.setExecutor(new ThreadExecutor()); // creates a default executor
            cmdServer.start();
        } catch (Exception e) {
            s_logger.error(e.getMessage(), e);
            System.exit(1);
        }
    }
View Full Code Here

        clientMac = dis.getMessageDigest().digest();
        dis.close();
    }

    public static void test() {
        HttpServer server = null;
        try {
            serverDigest = MessageDigest.getInstance("MD5");
            clientDigest = MessageDigest.getInstance("MD5");
            server = startHttpServer();

            int port = server.getAddress().getPort();
            out.println ("Server listening on port: " + port);
            client("http://localhost:" + port + "/chunked/");

            if (!MessageDigest.isEqual(clientMac, serverMac)) {
                throw new RuntimeException(
                 "Data received is NOT equal to the data sent");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (server != null)
                server.stop(0);
        }
    }
View Full Code Here

    /**
     * Http Server
     */
    static HttpServer startHttpServer() throws IOException {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(0), 0);
        HttpHandler httpHandler = new SimpleHandler();
        httpServer.createContext("/chunked/", httpHandler);
        httpServer.start();
        return httpServer;
    }
View Full Code Here

        //Set up the tables in the db
        Database db = new Database();
        db.initialize();

        // Begin running the server
        HttpServer mock_server = HttpServer.create(new InetSocketAddress(8000), 0);
        mock_server.createContext("/", new HTTPHandler());
        mock_server.setExecutor(null); // creates a default executor
        mock_server.start();

        // Create example server
        System.out.println(Server.get("http://www.google.com"));
        if (Server.get("http://www.google.com") == -2) {
            Server server1 = new Server("http://www.google.com");
View Full Code Here

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        InetSocketAddress addr = new InetSocketAddress(80);
        HttpServer server = HttpServer.create(addr, 0);
       
        server.createContext("/lightSound", new LightHandler());
        server.createContext("/speakSound", new SpeakHandler());
        server.setExecutor(Executors.newCachedThreadPool());
        server.start();
       
        System.out.println("Server is listening on port 80");
    }
View Full Code Here

TOP

Related Classes of com.sun.net.httpserver.HttpServer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.