Search in sources :

Example 1 with RouterHandler

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();
}
Also used : HttpPrefixPredicate(io.openk9.reactor.netty.util.HttpPrefixPredicate) BundleActivator(org.osgi.framework.BundleActivator) HttpMethod(io.netty.handler.codec.http.HttpMethod) BundleContext(org.osgi.framework.BundleContext) BundleEvent(org.osgi.framework.BundleEvent) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) AutoCloseables(io.openk9.osgi.util.AutoCloseables) BundleTracker(org.osgi.util.tracker.BundleTracker) BaseStaticEndpoint(io.openk9.http.osgi.BaseStaticEndpoint) Bundle(org.osgi.framework.Bundle) RouterHandler(io.openk9.http.web.RouterHandler) Dictionary(java.util.Dictionary) ServiceRegistration(org.osgi.framework.ServiceRegistration) Bundle(org.osgi.framework.Bundle) BundleTrackerCustomizer(org.osgi.util.tracker.BundleTrackerCustomizer) AutoCloseables(io.openk9.osgi.util.AutoCloseables) RouterHandler(io.openk9.http.web.RouterHandler) BundleEvent(org.osgi.framework.BundleEvent) BundleContext(org.osgi.framework.BundleContext)

Example 2 with RouterHandler

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);
}
Also used : AutoCloseables(io.openk9.osgi.util.AutoCloseables) HttpPrefixPredicate(io.openk9.reactor.netty.util.HttpPrefixPredicate) ServiceTrackerCustomizer(org.osgi.util.tracker.ServiceTrackerCustomizer) PluginInfo(io.openk9.plugin.api.PluginInfo) RequiredArgsConstructor(lombok.RequiredArgsConstructor) HttpMethod(io.netty.handler.codec.http.HttpMethod) BaseStaticEndpoint(io.openk9.http.osgi.BaseStaticEndpoint) Strings(io.openk9.common.api.constant.Strings) RouterHandler(io.openk9.http.web.RouterHandler) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration) RouterHandler(io.openk9.http.web.RouterHandler) PluginInfo(io.openk9.plugin.api.PluginInfo)

Aggregations

HttpMethod (io.netty.handler.codec.http.HttpMethod)2 BaseStaticEndpoint (io.openk9.http.osgi.BaseStaticEndpoint)2 RouterHandler (io.openk9.http.web.RouterHandler)2 AutoCloseables (io.openk9.osgi.util.AutoCloseables)2 HttpPrefixPredicate (io.openk9.reactor.netty.util.HttpPrefixPredicate)2 BundleContext (org.osgi.framework.BundleContext)2 ServiceRegistration (org.osgi.framework.ServiceRegistration)2 Strings (io.openk9.common.api.constant.Strings)1 PluginInfo (io.openk9.plugin.api.PluginInfo)1 Dictionary (java.util.Dictionary)1 RequiredArgsConstructor (lombok.RequiredArgsConstructor)1 Bundle (org.osgi.framework.Bundle)1 BundleActivator (org.osgi.framework.BundleActivator)1 BundleEvent (org.osgi.framework.BundleEvent)1 ServiceReference (org.osgi.framework.ServiceReference)1 BundleTracker (org.osgi.util.tracker.BundleTracker)1 BundleTrackerCustomizer (org.osgi.util.tracker.BundleTrackerCustomizer)1 ServiceTrackerCustomizer (org.osgi.util.tracker.ServiceTrackerCustomizer)1