public synchronized void removeHandler(String address) { server.removeContext(PathUtils.getContextPath(address) + PathUtils.getPath(address)); handlerCount--; }
/** * Get the context path section of an address * * @param addr * @return */ public static String getContextPath(String addr) { return getContextPathInternal(getPathFromString(addr)); }
/** * Get the final path section of an address (for servlet * container, this is typically a url-pattern for an endpoint) * * @param addr * @return */ public static String getPath(String addr) { return getPathInternal(getPathFromString(addr)); }
@Override public String getContextPath() { return PathUtils.getContextPath(delegate.getRequestURI()); }
@Override public HttpContext getHttpContext() { if (contextDelegate == null) { contextDelegate = new HttpContextDelegate(delegate.getHttpContext(), PathUtils.getPath(delegate.getRequestURI())); } return contextDelegate; }
private static String getContextPathInternal(String rawpath) { String path = removeTrailingSlash(rawpath); if (path == null || path.length() == 0) { return "/"; } int idx = path.lastIndexOf("/"); return idx > 0 ? path.substring(0, idx) : path; }
public static String getContextPath(URI addr) { return getContextPathInternal(addr.getPath()); }
public static String getPath(URI addr) { return getPathInternal(addr.getPath()); }
private static String getPathInternal(String rawpath) { String path = removeTrailingSlash(rawpath); if (path == null || path.length() == 0) { return path; } int idx = path.lastIndexOf("/"); return idx > 0 ? path.substring(path.lastIndexOf("/")) : ""; }
public synchronized void addHandler(String address, HttpHandler handler) { if (server == null) //start the server on first call { InetSocketAddress isa = host != null ? new InetSocketAddress(host, port) : new InetSocketAddress(port); try { server = HttpServer.create(isa, BACKLOG); server.setExecutor(Executors.newCachedThreadPool()); server.start(); } catch (IOException e) { throw new RuntimeException(e); } } server.createContext(PathUtils.getContextPath(address) + PathUtils.getPath(address), handler); handlerCount++; }