use of io.openk9.http.web.RouterHandler in project openk9 by smclab.
the class HttpBundleExtender method start.
@Override
public void start(BundleContext context) throws Exception {
_bundleTracker = new BundleTracker<>(context, Bundle.ACTIVE | Bundle.STOPPING, new BundleTrackerCustomizer<AutoCloseables.AutoCloseableSafe>() {
@Override
public AutoCloseables.AutoCloseableSafe addingBundle(Bundle bundle, BundleEvent event) {
if (bundle.getState() != Bundle.ACTIVE) {
removedBundle(bundle, event, NOTHING);
return null;
}
Dictionary<String, String> headers = bundle.getHeaders(null);
String contextPath = headers.get(REACTIVE_WEB_CONTEXT_PATH);
if (contextPath == null) {
removedBundle(bundle, event, NOTHING);
return null;
}
String staticFolder = headers.get(REACTIVE_WEB_CONTEXT_PATH_FOLDER);
String staticFolderVar;
if (staticFolder == null) {
staticFolderVar = "";
} else {
staticFolderVar = staticFolder;
}
RouterHandler routerHandler = router -> router.route(HttpPrefixPredicate.of(contextPath, HttpMethod.GET), BaseStaticEndpoint.of(bundle, contextPath, staticFolderVar, ""));
BundleContext bundleContext = bundle.getBundleContext();
ServiceRegistration<RouterHandler> serviceRegistration = bundleContext.registerService(RouterHandler.class, routerHandler, null);
return AutoCloseables.mergeAutoCloseableToSafe(serviceRegistration::unregister);
}
@Override
public void modifiedBundle(Bundle bundle, BundleEvent event, AutoCloseables.AutoCloseableSafe object) {
removedBundle(bundle, event, object);
addingBundle(bundle, event);
}
@Override
public void removedBundle(Bundle bundle, BundleEvent event, AutoCloseables.AutoCloseableSafe object) {
object.close();
}
});
_bundleTracker.open();
}
use of io.openk9.http.web.RouterHandler in project openk9 by smclab.
the class PluginInfoServiceTrackerCustomizer method addingService.
@Override
public AutoCloseables.AutoCloseableSafe addingService(ServiceReference<PluginInfo> reference) {
PluginInfo pluginInfo = _bundleContext.getService(reference);
String path = "/plugins/" + pluginInfo.getPluginId() + "/static";
RouterHandler routerHandler = router -> router.route(HttpPrefixPredicate.of(path, HttpMethod.GET), BaseStaticEndpoint.of(reference.getBundle(), path, Strings.BLANK, ""));
ServiceRegistration<RouterHandler> serviceRegistration = _bundleContext.registerService(RouterHandler.class, routerHandler, null);
return AutoCloseables.mergeAutoCloseableToSafe(serviceRegistration::unregister);
}
Aggregations