Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.kevoree.library;
  
 
Created by duke on 8/26/14.
 
 
 public class RestServer {
 
     private Integer port;
 
     @Param(defaultValue = "8090")
     public Integer getPort() {
         return ;
     }
 
     Undertow server;
 
 
 
     @KevoreeInject
 
     @Start
     public void start() {
          = Executors.newSingleThreadExecutor();
         if (..get() != null) {
              = ..get();
         }
          = Undertow.builder()
                 .addHttpListener("localhost")
                 .setHandler(new HttpHandler() {
                     @Override
                     public void handleRequest(final HttpServerExchange exchangethrows Exception {
                         if (exchange.getRequestMethod().equals(HttpString.tryFromString("POST"))) {
                             if (exchange.isInIoThread()) {
                                 exchange.dispatch(this);
                                 return;
                             }
                             String path = exchange.getRequestPath();
                             Future<Stringfuture = .submit(new ExternalMessageInjection(readBytesFromExchange(exchange), path));
                             exchange.getResponseHeaders().put(."text/plain");
                             exchange.getResponseSender().send(future.get());
                         }
                         exchange.endExchange();
                     }
                 }).build();
         .start();
     }
 
     @Stop
     public void stop() {
         if ( != null) {
             .stop();
         }
         if ( != null) {
             .shutdownNow();
         }
     }
 
     private static String readBytesFromExchange(HttpServerExchange exchangethrows IOException {
         InputStream inputStream = new UndertowInputStream(exchange);
         return getStringFromInputStream(inputStream);
     }
 
     private static String getStringFromInputStream(InputStream is) {
         BufferedReader br = null;
         StringBuilder sb = new StringBuilder();
         String line;
         try {
             br = new BufferedReader(new InputStreamReader(is));
             while ((line = br.readLine()) != null) {
                 sb.append(line);
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if (br != null) {
                 try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
New to GrepCode? Check out our FAQ X