Search in sources :

Example 1 with HttpContext

use of com.sun.net.httpserver.HttpContext in project jetty.project by eclipse.

the class TestSPIServer method main.

public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port = 8080;
    HttpServer server = new JettyHttpServerProvider().createHttpServer(new InetSocketAddress(host, port), 10);
    server.start();
    final HttpContext httpContext = server.createContext("/", new HttpHandler() {

        public void handle(HttpExchange exchange) throws IOException {
            Headers responseHeaders = exchange.getResponseHeaders();
            responseHeaders.set("Content-Type", "text/plain");
            exchange.sendResponseHeaders(200, 0);
            OutputStream responseBody = exchange.getResponseBody();
            Headers requestHeaders = exchange.getRequestHeaders();
            Set<String> keySet = requestHeaders.keySet();
            Iterator<String> iter = keySet.iterator();
            while (iter.hasNext()) {
                String key = iter.next();
                List values = requestHeaders.get(key);
                String s = key + " = " + values.toString() + "\n";
                responseBody.write(s.getBytes());
            }
            responseBody.close();
        }
    });
    httpContext.setAuthenticator(new BasicAuthenticator("Test") {

        @Override
        public boolean checkCredentials(String username, String password) {
            if ("username".equals(username) && password.equals("password"))
                return true;
            return false;
        }
    });
    Thread.sleep(10000000);
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) Set(java.util.Set) InetSocketAddress(java.net.InetSocketAddress) Headers(com.sun.net.httpserver.Headers) OutputStream(java.io.OutputStream) HttpContext(com.sun.net.httpserver.HttpContext) HttpExchange(com.sun.net.httpserver.HttpExchange) IOException(java.io.IOException) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) HttpServer(com.sun.net.httpserver.HttpServer) Iterator(java.util.Iterator) List(java.util.List)

Example 2 with HttpContext

use of com.sun.net.httpserver.HttpContext in project jdk8u_jdk by JetBrains.

the class TestWsImport method main.

public static void main(String[] args) throws IOException {
    String javaHome = System.getProperty("java.home");
    if (javaHome.endsWith("jre")) {
        javaHome = new File(javaHome).getParent();
    }
    String wsimport = javaHome + File.separator + "bin" + File.separator + "wsimport";
    if (System.getProperty("os.name").startsWith("Windows")) {
        wsimport = wsimport.concat(".exe");
    }
    Endpoint endpoint = Endpoint.create(new TestService());
    HttpServer httpServer = null;
    try {
        // Manually create HttpServer here using ephemeral address for port
        // so as to not end up with attempt to bind to an in-use port
        httpServer = HttpServer.create(new InetSocketAddress(0), 0);
        HttpContext httpContext = httpServer.createContext("/hello");
        int port = httpServer.getAddress().getPort();
        System.out.println("port = " + port);
        httpServer.start();
        endpoint.publish(httpContext);
        String address = "http://localhost:" + port + "/hello";
        Service service = Service.create(new URL(address + "?wsdl"), new QName("http://test/jaxws/sample/", "TestService"));
        String[] wsargs = { wsimport, "-p", "wstest", "-J-Djavax.xml.accessExternalSchema=all", "-J-Dcom.sun.tools.internal.ws.Invoker.noSystemProxies=true", address + "?wsdl", "-clientjar", "wsjar.jar" };
        ProcessBuilder pb = new ProcessBuilder(wsargs);
        pb.redirectErrorStream(true);
        Process p = pb.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String s = r.readLine();
        while (s != null) {
            System.out.println(s.trim());
            s = r.readLine();
        }
        p.waitFor();
        p.destroy();
        try (JarFile jarFile = new JarFile("wsjar.jar")) {
            for (Enumeration em = jarFile.entries(); em.hasMoreElements(); ) {
                String fileName = em.nextElement().toString();
                if (fileName.contains("\\")) {
                    throw new RuntimeException("\"\\\" character detected in jar file: " + fileName);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e.getMessage());
    } finally {
        endpoint.stop();
        if (httpServer != null) {
            httpServer.stop(0);
        }
        Path p = Paths.get("wsjar.jar");
        Files.deleteIfExists(p);
        p = Paths.get("wstest");
        if (Files.exists(p)) {
            try {
                Files.walkFileTree(p, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        Files.delete(file);
                        return CONTINUE;
                    }

                    @Override
                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                        if (exc == null) {
                            Files.delete(dir);
                            return CONTINUE;
                        } else {
                            throw exc;
                        }
                    }
                });
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) URL(java.net.URL) Endpoint(javax.xml.ws.Endpoint) HttpServer(com.sun.net.httpserver.HttpServer) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Path(java.nio.file.Path) Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) QName(javax.xml.namespace.QName) HttpContext(com.sun.net.httpserver.HttpContext) Service(javax.xml.ws.Service) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Endpoint(javax.xml.ws.Endpoint) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 3 with HttpContext

use of com.sun.net.httpserver.HttpContext in project jdk8u_jdk by JetBrains.

the class BasicLongCredentials method main.

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

            public boolean checkCredentials(String username, String pw) {
                return USERNAME.equals(username) && PASSWORD.equals(pw);
            }
        };
        ctx.setAuthenticator(a);
        server.start();
        Authenticator.setDefault(new MyAuthenticator());
        URL url = new URL("http://localhost:" + server.getAddress().getPort() + "/test/");
        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
        InputStream is = urlc.getInputStream();
        int c = 0;
        while (is.read() != -1) {
            c++;
        }
        if (c != 0) {
            throw new RuntimeException("Test failed c = " + c);
        }
        if (error) {
            throw new RuntimeException("Test failed: error");
        }
        System.out.println("OK");
    } finally {
        server.stop(0);
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) BasicAuthenticator(com.sun.net.httpserver.BasicAuthenticator) InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext) HttpHandler(com.sun.net.httpserver.HttpHandler) URL(java.net.URL)

Example 4 with HttpContext

use of com.sun.net.httpserver.HttpContext in project jdk8u_jdk by JetBrains.

the class SecurityPolicy method httpd.

/**
     * Creates and starts an HTTP or proxy server that requires
     * Negotiate authentication.
     * @param scheme "Negotiate" or "Kerberos"
     * @param principal the krb5 service principal the server runs with
     * @return the server
     */
public static HttpServer httpd(String scheme, boolean proxy, String principal, String ktab) throws Exception {
    MyHttpHandler h = new MyHttpHandler();
    HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    HttpContext hc = server.createContext("/", h);
    hc.setAuthenticator(new MyServerAuthenticator(proxy, scheme, principal, ktab));
    server.start();
    return server;
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) HttpContext(com.sun.net.httpserver.HttpContext)

Example 5 with HttpContext

use of com.sun.net.httpserver.HttpContext in project spring-framework by spring-projects.

the class SimpleHttpServerFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() throws IOException {
    InetSocketAddress address = (this.hostname != null ? new InetSocketAddress(this.hostname, this.port) : new InetSocketAddress(this.port));
    this.server = HttpServer.create(address, this.backlog);
    if (this.executor != null) {
        this.server.setExecutor(this.executor);
    }
    if (this.contexts != null) {
        for (String key : this.contexts.keySet()) {
            HttpContext httpContext = this.server.createContext(key, this.contexts.get(key));
            if (this.filters != null) {
                httpContext.getFilters().addAll(this.filters);
            }
            if (this.authenticator != null) {
                httpContext.setAuthenticator(this.authenticator);
            }
        }
    }
    if (this.logger.isInfoEnabled()) {
        this.logger.info("Starting HttpServer at address " + address);
    }
    this.server.start();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) HttpContext(com.sun.net.httpserver.HttpContext)

Aggregations

HttpContext (com.sun.net.httpserver.HttpContext)13 InetSocketAddress (java.net.InetSocketAddress)10 BasicAuthenticator (com.sun.net.httpserver.BasicAuthenticator)7 HttpServer (com.sun.net.httpserver.HttpServer)7 HttpHandler (com.sun.net.httpserver.HttpHandler)5 IOException (java.io.IOException)4 HttpExchange (com.sun.net.httpserver.HttpExchange)2 URL (java.net.URL)2 ExecutorService (java.util.concurrent.ExecutorService)2 FailureException (com.github.noraui.exception.FailureException)1 TechnicalException (com.github.noraui.exception.TechnicalException)1 Headers (com.sun.net.httpserver.Headers)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 FileVisitResult (java.nio.file.FileVisitResult)1 Path (java.nio.file.Path)1