Package com.sun.net.httpserver

Examples of com.sun.net.httpserver.HttpHandler


  public static void init() throws IOException {
    // create a new server listening at port 8080
    server = HttpServer.create(new InetSocketAddress(uri.getPort()), 0);

    // create a handler wrapping the JAX-RS application
    HttpHandler handler = RuntimeDelegate.getInstance().createEndpoint(new ApplicationConfig12(), HttpHandler.class);

    // map JAX-RS handler to the server root
    server.createContext(uri.getPath(), handler);

    // start the server
View Full Code Here


        }));

    /*
     * register our own handler, that receives subgraph messages
     */
    Endpoint.registerHandler("/message/", new HttpHandler() {
      /**
       * Read out a query parameter
       *
       * @param responseParts
       *            the queries as array
       * @param parameter
       *            the parameter to search for
       * @return the content part after query
       * @throws UnsupportedEncodingException
       *             error
       */
      protected String getParameter(final String[] responseParts,
          final String parameter) throws UnsupportedEncodingException {
        for (final String item : responseParts) {
          if (item.startsWith(parameter)) {
            return URLDecoder.decode(
                item.substring(parameter.length()), "UTF-8");
          }
        }
        return null;
      }

      @Override
      public void handle(final HttpExchange t) throws IOException {
        /*
         * get the sender's ip-address (hostname)
         */
        String host = t.getRemoteAddress().getHostString();
        if (!host.startsWith("http://")) {
          host = "http://" + host;
        }
        log.debug(String.format("Receiving subgraph-request from: %s",
            host));
        /*
         * get the queries
         */
        final String response = Endpoint.getResponse(t);
        final String[] responseParts = response.split("[&]");
        if (responseParts.length > 0) {
          /*
           * the port the request came from (has to be given as query
           * parameter, because when using windows, the sender's port
           * is a random one, and not the one, that can be used for
           * answer! but the host is right, so now we know the
           * endpoint-url to answer!
           */
          final String port = this.getParameter(responseParts, "from=");
          host += ":" + port;
          /*
           * now get the query-part (subgraph message)
           */
          final String queryParameter = this.getParameter(responseParts,
              "query=");

          if (queryParameter != null) {
            /*
             * forward this message to the subgraph executer ...
             */
            EndpointNetwork.this.onMessage(queryParameter, host);
            /*
             * and return result
             */
            t.getResponseHeaders()
                .add("Content-type", "text/plain");
            Endpoint.sendString(t, "OK");
          } else { /*
               * if no parameter given
               */
            t.getResponseHeaders()
                .add("Content-type", "text/plain");
            final String answer = "Bad Request: query parameter missing";
            Endpoint.sendString(t, answer);
            return;
          }
        }
      };
    });
    /*
     * This is the handler that receives streams with XML results
     */
    Endpoint.registerHandler("/answer/", new HttpHandler() {

      @Override
      public void handle(final HttpExchange t) throws IOException {
        /*
         * get the sender's ip-address (hostname)
View Full Code Here

        HttpServer server = null;
        try {
            server = HttpServer.create(new InetSocketAddress(port), 0);

            HttpHandler handler = new HttpHandler() {
                public void handle(HttpExchange e) throws IOException {
                    InputStream in = e.getRequestBody();
                    ByteArrayOutputStream _out = new ByteArrayOutputStream();
                    byte[] buf = new byte[2048];
                    int read = 0;
View Full Code Here

                    new InetSocketAddress("localhost", port), 0);
        }

        server.setExecutor(Executors.newCachedThreadPool());

        server.createContext("/", new HttpHandler() {
            public void handle(HttpExchange he) throws IOException {
                ActionContext context = new ActionContextImpl(he);
                String path = he.getRequestURI().getPath();
                // 拡張子なしをデフォルトでActionとして扱う。
                if (path.indexOf('.') == -1) {
View Full Code Here

    @Test
    public void restGatewayReferenceTimeout() throws Exception {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8090), 10);
        httpServer.setExecutor(null); // creates a default executor
        httpServer.start();
        HttpContext httpContext = httpServer.createContext("/forever", new HttpHandler() {
            public void handle(HttpExchange exchange) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException ie) {
                        //Ignore
View Full Code Here

    public HttpServer createHttpServer() throws IOException {
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8069), 0);

        // create and register our handler
        httpServer.createContext("/bucketname/standard-key.txt",new HttpHandler() {
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = "loaded=true".getBytes("UTF-8");
                    // RFC 2616 says HTTP headers are case-insensitive - but the
                // Amazon S3 client will crash if ETag has a different
                // capitalisation. And this HttpServer normalises the names
                // of headers using "ETag"->"Etag" if you use put, add or
                // set. But not if you use 'putAll' so that's what I use.
                Map<String, List<String>> responseHeaders = new HashMap();
                responseHeaders.put("ETag", Collections.singletonList("\"TEST-ETAG\""));
                responseHeaders.put("Content-Type", Collections.singletonList("text/plain"));
                exchange.getResponseHeaders().putAll(responseHeaders);
                exchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, response.length);
                exchange.getResponseBody().write(response);
                exchange.close();
            }
        });

        httpServer.createContext("/bucketname/404.txt",new HttpHandler() {
            public void handle(HttpExchange exchange) throws IOException {
                byte[] response = ("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                        "<Error>\n" +
                        "  <Code>NoSuchKey</Code>\n" +
                        "  <Message>The resource you requested does not exist</Message>\n" +
View Full Code Here

                     + "   <arg0>Hello</arg0>"
                     + "</test:sayHello>").getDocumentElement();
        HttpServer httpServer = HttpServer.create(new InetSocketAddress(8090), 10);
        httpServer.setExecutor(null); // creates a default executor
        httpServer.start();
        HttpContext httpContext = httpServer.createContext("/forever", new HttpHandler() {
            public void handle(HttpExchange exchange) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException ie) {
                        //Ignore
View Full Code Here

        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();
View Full Code Here

        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();
View Full Code Here

    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,
View Full Code Here

TOP

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

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.