Search in sources :

Example 1 with ResourceChangeListener

use of io.undertow.server.handlers.resource.ResourceChangeListener in project keycloak by keycloak.

the class SecureServerDefinition method createHttpManagementConfigContextService.

private static Service<Void> createHttpManagementConfigContextService(final String factoryName, final InjectedValue<ExtensibleHttpManagement> httpConfigContext) {
    final String contextName = "/keycloak/adapter/" + factoryName + "/";
    return new Service<Void>() {

        public void start(StartContext startContext) throws StartException {
            ExtensibleHttpManagement extensibleHttpManagement = (ExtensibleHttpManagement) httpConfigContext.getValue();
            extensibleHttpManagement.addStaticContext(contextName, new ResourceManager() {

                public Resource getResource(final String path) throws IOException {
                    KeycloakAdapterConfigService adapterConfigService = KeycloakAdapterConfigService.getInstance();
                    final String config = adapterConfigService.getJSON(factoryName);
                    if (config == null) {
                        return null;
                    }
                    return new Resource() {

                        public String getPath() {
                            return null;
                        }

                        public Date getLastModified() {
                            return null;
                        }

                        public String getLastModifiedString() {
                            return null;
                        }

                        public ETag getETag() {
                            return null;
                        }

                        public String getName() {
                            return null;
                        }

                        public boolean isDirectory() {
                            return false;
                        }

                        public List<Resource> list() {
                            return Collections.emptyList();
                        }

                        public String getContentType(MimeMappings mimeMappings) {
                            return "application/json";
                        }

                        public void serve(Sender sender, HttpServerExchange exchange, IoCallback completionCallback) {
                            sender.send(config);
                        }

                        public Long getContentLength() {
                            return Long.valueOf((long) config.length());
                        }

                        public String getCacheKey() {
                            return null;
                        }

                        public File getFile() {
                            return null;
                        }

                        public Path getFilePath() {
                            return null;
                        }

                        public File getResourceManagerRoot() {
                            return null;
                        }

                        public Path getResourceManagerRootPath() {
                            return null;
                        }

                        public URL getUrl() {
                            return null;
                        }
                    };
                }

                public boolean isResourceChangeListenerSupported() {
                    return false;
                }

                public void registerResourceChangeListener(ResourceChangeListener listener) {
                }

                public void removeResourceChangeListener(ResourceChangeListener listener) {
                }

                public void close() throws IOException {
                }
            });
        }

        public void stop(StopContext stopContext) {
            ((ExtensibleHttpManagement) httpConfigContext.getValue()).removeContext(contextName);
        }

        public Void getValue() throws IllegalStateException, IllegalArgumentException {
            return null;
        }
    };
}
Also used : Path(java.nio.file.Path) StopContext(org.jboss.msc.service.StopContext) Resource(io.undertow.server.handlers.resource.Resource) Service(org.jboss.msc.service.Service) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) IOException(java.io.IOException) MimeMappings(io.undertow.util.MimeMappings) Date(java.util.Date) URL(java.net.URL) Sender(io.undertow.io.Sender) HttpServerExchange(io.undertow.server.HttpServerExchange) StartContext(org.jboss.msc.service.StartContext) ETag(io.undertow.util.ETag) IoCallback(io.undertow.io.IoCallback) ResourceChangeListener(io.undertow.server.handlers.resource.ResourceChangeListener) List(java.util.List) ExtensibleHttpManagement(org.jboss.as.server.mgmt.domain.ExtensibleHttpManagement) File(java.io.File)

Example 2 with ResourceChangeListener

use of io.undertow.server.handlers.resource.ResourceChangeListener in project keycloak by keycloak.

the class UndertowDeployerHelper method getResourceManager.

private ResourceManager getResourceManager(final String appServerRoot, final WebArchive archive) throws IOException {
    return new ResourceManager() {

        @Override
        public Resource getResource(String path) throws IOException {
            if (path == null || path.isEmpty()) {
                return null;
            }
            Node node = archive.get(path);
            if (node == null) {
                log.warnf("Application '%s' did not found resource on path %s", archive.getName(), path);
                return null;
            } else {
                URL contextUrl = new URL(appServerRoot);
                URL myResourceUrl = new URL(contextUrl.getProtocol(), contextUrl.getHost(), contextUrl.getPort(), path, new URLStreamHandler() {

                    @Override
                    protected URLConnection openConnection(URL u) throws IOException {
                        return new URLConnection(u) {

                            @Override
                            public void connect() throws IOException {
                            }

                            @Override
                            public InputStream getInputStream() throws IOException {
                                return node.getAsset().openStream();
                            }
                        };
                    }
                });
                return new URLResource(myResourceUrl, myResourceUrl.openConnection(), path);
            }
        }

        @Override
        public boolean isResourceChangeListenerSupported() {
            return false;
        }

        @Override
        public void registerResourceChangeListener(ResourceChangeListener listener) {
            throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
        }

        @Override
        public void removeResourceChangeListener(ResourceChangeListener listener) {
            throw UndertowMessages.MESSAGES.resourceChangeListenerNotSupported();
        }

        @Override
        public void close() throws IOException {
        // TODO: Should close open streams?
        }
    };
}
Also used : URLStreamHandler(java.net.URLStreamHandler) URLResource(io.undertow.server.handlers.resource.URLResource) InputStream(java.io.InputStream) Node(org.jboss.shrinkwrap.api.Node) ResourceChangeListener(io.undertow.server.handlers.resource.ResourceChangeListener) ResourceManager(io.undertow.server.handlers.resource.ResourceManager) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Aggregations

ResourceChangeListener (io.undertow.server.handlers.resource.ResourceChangeListener)2 ResourceManager (io.undertow.server.handlers.resource.ResourceManager)2 IOException (java.io.IOException)2 URL (java.net.URL)2 IoCallback (io.undertow.io.IoCallback)1 Sender (io.undertow.io.Sender)1 HttpServerExchange (io.undertow.server.HttpServerExchange)1 Resource (io.undertow.server.handlers.resource.Resource)1 URLResource (io.undertow.server.handlers.resource.URLResource)1 ETag (io.undertow.util.ETag)1 MimeMappings (io.undertow.util.MimeMappings)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URLConnection (java.net.URLConnection)1 URLStreamHandler (java.net.URLStreamHandler)1 Path (java.nio.file.Path)1 Date (java.util.Date)1 List (java.util.List)1 ExtensibleHttpManagement (org.jboss.as.server.mgmt.domain.ExtensibleHttpManagement)1 Service (org.jboss.msc.service.Service)1