Package org.jboss.com.sun.net.httpserver

Examples of org.jboss.com.sun.net.httpserver.HttpServer


public class Test2 extends Test {

    public static void main (String[] args) throws Exception {
        Handler handler = new Handler();
        InetSocketAddress addr = new InetSocketAddress (0);
        HttpServer server = HttpServer.create (addr, 0);
        HttpContext ctx = server.createContext ("/test", handler);
        BasicAuthenticator a = new BasicAuthenticator ("foobar@test.realm") {
            public boolean checkCredentials (String username, String pw) {
                return "fred".equals(username) && pw.charAt(0) == 'x';
            }
        };

        ctx.setAuthenticator (a);
        ExecutorService executor = Executors.newCachedThreadPool();
        server.setExecutor (executor);
        server.start ();
        java.net.Authenticator.setDefault (new MyAuthenticator());

        URL url = new URL ("http://localhost:"+server.getAddress().getPort()+"/test/foo.html");
        System.out.print ("Test2: " );
        HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read()!= -1) {
            c ++;
        }
        server.stop(2);
        executor.shutdown();
        if (error ) {
            throw new RuntimeException ("test failed error");
        }
        if (c != 0) {
View Full Code Here


        "Cache-Control: no-cache"+CRLF+CRLF;

    public static void main (String[] args) throws Exception {
        Handler handler = new Handler();
        InetSocketAddress addr = new InetSocketAddress (0);
        HttpServer server = HttpServer.create (addr, 0);
        HttpContext ctx = server.createContext ("/test", handler);

        server.start ();

        Socket s = new Socket ("localhost", server.getAddress().getPort());

        try {
            OutputStream os = s.getOutputStream();
            os.write (cmd.getBytes());
            Thread.sleep (3000);
            s.close();
        } catch (IOException e) { }
        server.stop(2);
        if (requests != 2) {
            throw new RuntimeException ("did not receive the 2 requests");
        }
        System.out.println ("OK");
    }
View Full Code Here

    static int count = 1;
    public static void main (String[] args) throws Exception {
        System.out.print ("Test5: ");
        Handler handler = new Handler();
        InetSocketAddress addr = new InetSocketAddress (0);
        HttpServer server = HttpServer.create (addr, 0);
        int port = server.getAddress().getPort();
        HttpContext c2 = server.createContext ("/test", handler);
        c2.getAttributes().put ("name", "This is the http handler");

        ExecutorService exec = Executors.newCachedThreadPool();
        server.setExecutor (exec);
        try {
            server.start ();
            doClient(port);
            System.out.println ("OK");
        } finally {
            delay ();
            if (server != null)
                server.stop(2);
            if (exec != null)
                exec.shutdown();
        }
    }
View Full Code Here

    final static ByteBuffer requestBuf = ByteBuffer.allocate(64).put(request.getBytes());

    public static void main (String[] args) throws Exception {
        Handler handler = new Handler();
        InetSocketAddress addr = new InetSocketAddress (0);
        HttpServer server = HttpServer.create (addr, 0);
        HttpContext ctx = server.createContext ("/test", handler);

        ExecutorService executor = Executors.newCachedThreadPool();
        server.setExecutor (executor);
        server.start ();

        InetSocketAddress destaddr = new InetSocketAddress (
                "127.0.0.1", server.getAddress().getPort()
        );
        System.out.println ("destaddr " + destaddr);

        Selector selector = Selector.open ();
        int requests = 0;
        int responses = 0;
        while (true) {
            int selres = selector.select (1);
            Set<SelectionKey> selkeys = selector.selectedKeys();
            for (SelectionKey key : selkeys) {
                if (key.isReadable()) {
                    SocketChannel chan = (SocketChannel)key.channel();
                    ByteBuffer buf = (ByteBuffer)key.attachment();
                    try {
                        int x = chan.read(buf);
                        if (x == -1 || responseComplete(buf)) {
                            key.attach(null);
                            chan.close();
                            responses++;
                        }
                    } catch (IOException e) {}
                }
            }
            if (requests < NUM) {
                SocketChannel schan = SocketChannel.open(destaddr);
                requestBuf.rewind();
                int c = 0;
                while (requestBuf.remaining() > 0) {
                    c += schan.write(requestBuf);
                }
                schan.configureBlocking(false);
                schan.register(selector, SelectionKey.OP_READ, ByteBuffer.allocate(100));
                requests++;
            }
            if (responses == NUM) {
                System.out.println ("Finished clients");
                break;
            }
        }
        server.stop (1);
        selector.close();
        executor.shutdown ();

    }
View Full Code Here

    static int count = 1;
    public static void main (String[] args) throws Exception {
        System.out.print ("Test3: ");
        Handler handler = new Handler();
        InetSocketAddress addr = new InetSocketAddress (0);
        HttpServer server = HttpServer.create (addr, 0);
        int port = server.getAddress().getPort();
        HttpContext c2 = server.createContext ("/test", handler);
        c2.getAttributes().put ("name", "This is the http handler");

        ExecutorService exec = Executors.newCachedThreadPool();
        server.setExecutor (exec);
        try {
            server.start ();
            doClient(port);
            System.out.println ("OK");
        } finally {
            delay();
            if (server != null)
                server.stop(2);
            if (exec != null)
                exec.shutdown();
        }
    }
View Full Code Here

        server();
    }

    static void server() throws Exception {
        InetSocketAddress inetAddress = new InetSocketAddress(0);
        HttpServer server = HttpServer.create(inetAddress, 5);
        try {
            server.setExecutor(Executors.newFixedThreadPool(5));
            HttpContext chunkedContext = server.createContext("/chunked");
            chunkedContext.setHandler(new HttpHandler() {

                public void handle(HttpExchange msg) {
                    try {
                        try {
                            if (msg.getRequestMethod().equals("HEAD")) {
                                msg.getRequestBody().close();
                                msg.getResponseHeaders().add("Transfer-encoding", "chunked");
                                msg.sendResponseHeaders(200, -1);
                            }
                        } catch(IOException ioe) {
                            ioe.printStackTrace();
                        }
                    } finally {
                        msg.close();
                    }
                }
            });
            HttpContext clContext = server.createContext("/content");
            clContext.setHandler(new HttpHandler() {

                public void handle(HttpExchange msg) {
                    try {
                        try {
                            if (msg.getRequestMethod().equals("HEAD")) {
                                msg.getRequestBody().close();
                                msg.getResponseHeaders().add("Content-length", "1024");
                                msg.sendResponseHeaders(200, -1);
                            }
                        } catch(IOException ioe) {
                            ioe.printStackTrace();
                        }
                    } finally {
                        msg.close();
                    }
                }
            });
            server.start();
            String urlStr = "http://localhost:" + server.getAddress().getPort() + "/";
            System.out.println("Server is at " + urlStr);

            // Run the chunked client
            for(int i=0; i < 10; i++) {
                runClient(urlStr + "chunked/");
            }
            // Run the content length client
            for(int i=0; i < 10; i++) {
                runClient(urlStr + "content/");
            }
        } finally {
            // Stop the server
            ((ExecutorService)server.getExecutor()).shutdown();
            server.stop(0);
        }
    }
View Full Code Here

import org.jboss.com.sun.net.httpserver.HttpServer;

public class B6424196 {

    public static void main(String[] args) throws IOException {
        HttpServer server = HttpServer.create();
    }
View Full Code Here

public class SelCacheTest extends Test {

    static SSLContext ctx;

    public static void main (String[] args) throws Exception {
        HttpServer s1 = null;
        HttpsServer s2 = null;
        ExecutorService executor=null;
        try {
            String root = System.getProperty ("test.src")+ "/docs";
            System.out.print ("Test1: ");
            InetSocketAddress addr = new InetSocketAddress (0);
            s1 = HttpServer.create (addr, 0);
            if (s1 instanceof HttpsServer) {
                throw new RuntimeException ("should not be httpsserver");
            }
            s2 = HttpsServer.create (addr, 0);
            HttpHandler h = new FileServerHandler (root);
            HttpContext c1 = s1.createContext ("/test1", h);
            HttpContext c2 = s2.createContext ("/test1", h);
            executor = Executors.newCachedThreadPool();
            s1.setExecutor (executor);
            s2.setExecutor (executor);
            ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
            s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
            s1.start();
            s2.start();

            int port = s1.getAddress().getPort();
            int httpsport = s2.getAddress().getPort();
            test (true, "http", root+"/test1", port, "smallfile.txt", 23);
            test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
            test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
            test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
            test (false, "http", root+"/test1", port, "smallfile.txt", 23);
            test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
            test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
            test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
            System.out.println ("OK");
        } finally {
            delay();
            s1.stop(2);
            s2.stop(2);
            executor.shutdown ();
        }
    }
View Full Code Here

import org.jboss.com.sun.net.httpserver.HttpServer;

public class TestLogging extends Test {

    public static void main (String[] args) throws Exception {
        HttpServer s1 = null;
        ExecutorService executor=null;

        try {
            System.out.print ("Test9: ");
            String root = System.getProperty ("test.src")+ "/docs";
            InetSocketAddress addr = new InetSocketAddress (0);
            Logger logger = Logger.getLogger ("com.sun.net.httpserver");
            logger.setLevel (Level.ALL);
            Handler h1 = new ConsoleHandler ();
            h1.setLevel (Level.ALL);
            logger.addHandler (h1);
            s1 = HttpServer.create (addr, 0);
            logger.info (root);
            HttpHandler h = new FileServerHandler (root);
            HttpContext c1 = s1.createContext ("/test1", h);
            executor = Executors.newCachedThreadPool();
            s1.setExecutor (executor);
            s1.start();

            int p1 = s1.getAddress().getPort();

            URL url = new URL ("http://127.0.0.1:"+p1+"/test1/smallfile.txt");
            HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
            InputStream is = urlc.getInputStream();
            while (is.read() != -1) ;
            is.close();

            url = new URL ("http://127.0.0.1:"+p1+"/test1/doesntexist.txt");
            urlc = (HttpURLConnection)url.openConnection();
            try {
                is = urlc.getInputStream();
                while (is.read() != -1) ;
                is.close();
            } catch (IOException e) {
                System.out.println ("caught expected exception");
            }

            Socket s = new Socket ("127.0.0.1", p1);
            OutputStream os = s.getOutputStream();
            //os.write ("GET xxx HTTP/1.1\r\n".getBytes());
            os.write ("HELLO WORLD\r\n".getBytes());
            is = s.getInputStream();
            while (is.read() != -1) ;
            os.close(); is.close(); s.close();
            System.out.println ("OK");
        } finally {
            delay();
            if (s1 != null)
                s1.stop(2);
            if (executor != null)
                executor.shutdown();
        }
    }
View Full Code Here


    private static HttpServer createHttpServer(ExecutorService execs)
        throws Exception {
        InetSocketAddress inetAddress = new InetSocketAddress(0);
        HttpServer testServer = HttpServer.create(inetAddress, 5);
        testServer.setExecutor(execs);
        HttpContext context = testServer.createContext("/test");
        context.setHandler(new HttpHandler() {
            public void handle(HttpExchange msg) {
                try {
                    synchronized(lock) {
                        ++s_received;
View Full Code Here

TOP

Related Classes of org.jboss.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.