Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  Kontraktor-Http Copyright (c) Ruediger Moeller, All rights reserved.
  
  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 3.0 of the License, or (at your option) any later version.
  
  This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
 
 See https://www.gnu.org/licenses/lgpl.txt
 */
 
 package org.nustaq.kontraktor.remoting.http;
 
 
 import java.io.File;
 import java.util.Map;

Created by ruedi on 25/05/15. singleton to manage http server instances. Currently tied to Undertow however implicitely shields kontraktor-http from getting too dependent on Undertow (which is an excellent piece of software, so no plans to migrate anytime soon)
 
 public class Http4K {
 
     protected static Http4K instance;
     public static void set(Http4K http) {
          = http;
     }
 
     public static Http4K get() {
         synchronized (Http4K.class) {
             if (  == null ) {
                 System.setProperty("org.jboss.logging.provider","slf4j");
                  = new Http4K();
             }
             return ;
         }
     }
 
     public static BldFourK BuildString hostNameint portSSLContext ctx ) {
         return get().builder(hostName,port,ctx);
     }
 
     public static BldFourK BuildString hostNameint port) {
         return get().builder(hostName,port,null);
     }
 
     // a map of port=>server
     protected Map<IntegerPair<PathHandler,Undertow>> serverMap = new HashMap<>();

    
creates or gets an undertow web server instance mapped by port. hostname must be given in case a new server instance has to be instantiated

Parameters:
port
hostName
Returns:
 
     public synchronized Pair<PathHandlerUndertowgetServer(int portString hostName) {
         return getServer(port,hostName,null);
     }
 
     public synchronized Pair<PathHandlerUndertowgetServer(int portString hostNameSSLContext context) {
         Pair<PathHandlerUndertowpair = .get(port);
         if (pair == null) {
             PathHandler pathHandler = new PathHandler();
             Undertow.Builder builder = Undertow.builder()
                                            .setIoThreads(2)
                                            .setWorkerThreads(2);
             Undertow server = customize(builder,pathHandler,port,hostName,context).build();
             server.start();
             pair = new Pair<>(pathHandler,server);
             .put(port,pair);
         }
         return pair;
     }
 
     public BldFourK builder(String hostNameint portSSLContext ctx) {
        return new BldFourK(hostName,port,ctx);
    }
    public BldFourK builder(String hostNameint port) {
        return new BldFourK(hostName,port,null);
    }
    protected Undertow.Builder customize(Undertow.Builder builderPathHandler rootPathHandlerint portString hostNameSSLContext context) {
        if ( context == null ) {
            return builder
                       .addHttpListener(porthostName)
                       .setHandler(rootPathHandler);
        } else {
            return builder
                       .addHttpsListener(port,hostName,context)
                       .setHandler(rootPathHandler);
        }
    }

    
publishes given file root

Parameters:
hostName
urlPath - prefixPath (e.g. /myapp/resource)
port
root - directory to be published
    public Http4K publishFileSystemString hostNameString urlPathint portFile root ) {
        if ( ! root.isDirectory() ) {
            throw new RuntimeException("root must be an existing direcory");
        }
        Pair<PathHandlerUndertowserver = getServer(porthostName);
        server.car().addPrefixPath(urlPathnew ResourceHandler(new FileResourceManager(root,100)));
        return this;
    }
    public Http4K publishResourcePathString hostNameString urlPathint portDynamicResourceManager manboolean compress ) {
        Pair<PathHandlerUndertowserver = getServer(porthostName);
        ResourceHandler handler = new ResourceHandler(man);
        if ( compress ) {
            HttpHandler httpHandler = new EncodingHandler.Builder().build(new HashMap<>()).wrap(handler);
            server.car().addPrefixPathurlPathhttpHandler);
        } else {
            server.car().addPrefixPathurlPathhandler);
        }
        return this;
    }

    
utility, just redirects to approriate connector Publishes an actor/service via websockets protocol with given encoding. if this should be connectable from non-java code recommended coding is 'new Coding(SerializerType.JsonNoRefPretty)' (dev), 'new Coding(SerializerType.JsonNoRef)' (production) SerializerType.FSTSer is the most effective for java to java communication.
    public IPromise<ActorServerpublishWebSocketPublisher publisher ) {
        return publisher.publish();
    }

    
utility, just redirects to approriate connector.
    public IPromise<ActorServerpublishHttpPublisher publisher ) {
        return publisher.publish();
    }
New to GrepCode? Check out our FAQ X