Search in sources :

Example 1 with MCRComponent

use of org.mycore.common.config.MCRComponent in project mycore by MyCoRe-Org.

the class MCRAutoDeploy method deployWebResources.

private void deployWebResources(final ServletContext servletContext, final MCRComponent comp) {
    final String webRoot = servletContext.getRealPath("/");
    if (webRoot != null) {
        try {
            final JarFile jar = new JarFile(comp.getJarFile());
            LOGGER.info("Deploy web resources to {}...", webRoot);
            Collections.list(jar.entries()).stream().filter(file -> file.getName().startsWith(RESOURCE_DIR)).forEach(file -> {
                final String fileName = file.getName().substring(RESOURCE_DIR.length());
                LOGGER.debug("...deploy {}", fileName);
                final File f = new File(webRoot + File.separator + fileName);
                if (file.isDirectory()) {
                    f.mkdir();
                } else {
                    try {
                        final InputStream is = jar.getInputStream(file);
                        final FileOutputStream fos = new FileOutputStream(f);
                        while (is.available() > 0) {
                            fos.write(is.read());
                        }
                        fos.close();
                    } catch (IOException e) {
                        LOGGER.error("Couldn't deploy file {}.", fileName, e);
                    }
                }
            });
            LOGGER.info("...done.");
            jar.close();
        } catch (final IOException e) {
            LOGGER.error("Couldn't parse JAR!", e);
        }
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) MCRRuntimeComponentDetector(org.mycore.common.config.MCRRuntimeComponentDetector) MCRComponent(org.mycore.common.config.MCRComponent) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Collectors(java.util.stream.Collectors) File(java.io.File) Document(org.jdom2.Document) List(java.util.List) Logger(org.apache.logging.log4j.Logger) JDOMException(org.jdom2.JDOMException) Optional(java.util.Optional) DispatcherType(javax.servlet.DispatcherType) ServletContext(javax.servlet.ServletContext) Namespace(org.jdom2.Namespace) MCRStartupHandler(org.mycore.common.events.MCRStartupHandler) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) EnumSet(java.util.EnumSet) InputStream(java.io.InputStream) Element(org.jdom2.Element) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 2 with MCRComponent

use of org.mycore.common.config.MCRComponent in project mycore by MyCoRe-Org.

the class MCRAutoDeploy method registerWebFragment.

private void registerWebFragment(final ServletContext servletContext, final MCRComponent comp) {
    if (!isHandledByServletContainer(servletContext, comp)) {
        try {
            final JarFile jar = new JarFile(comp.getJarFile());
            Collections.list(jar.entries()).stream().filter(file -> file.getName().equals(WEB_FRAGMENT)).findFirst().ifPresent(file -> {
                final SAXBuilder builder = new SAXBuilder();
                try {
                    final InputStream is = jar.getInputStream(file);
                    final Document doc = builder.build(is);
                    final Element root = doc.getRootElement();
                    final Namespace ns = root.getNamespace();
                    final List<Element> filters = root.getChildren("filter", ns);
                    final List<Element> fmaps = root.getChildren("filter-mapping", ns);
                    filters.forEach(filter -> {
                        final String name = filter.getChildText("filter-name", ns);
                        final String className = filter.getChildText("filter-class", ns);
                        fmaps.stream().filter(mapping -> mapping.getChildText("filter-name", ns).equals(name)).findFirst().ifPresent(mapping -> {
                            LOGGER.info("Register Filter {} ({})...", name, className);
                            Optional.ofNullable(servletContext.addFilter(name, className)).<Runnable>map(fr -> () -> {
                                final List<Element> dispatchers = mapping.getChildren("dispatcher", ns);
                                final EnumSet<DispatcherType> eDT = dispatchers.isEmpty() ? null : dispatchers.stream().map(d -> DispatcherType.valueOf(d.getTextTrim())).collect(Collectors.toCollection(() -> EnumSet.noneOf(DispatcherType.class)));
                                final List<Element> servletNames = mapping.getChildren("servlet-name", ns);
                                if (!servletNames.isEmpty()) {
                                    fr.addMappingForServletNames(eDT, false, servletNames.stream().map(sn -> {
                                        LOGGER.info("...add servlet mapping: {}", sn.getTextTrim());
                                        return sn.getTextTrim();
                                    }).toArray(String[]::new));
                                }
                                final List<Element> urlPattern = mapping.getChildren("url-pattern", ns);
                                if (!urlPattern.isEmpty()) {
                                    fr.addMappingForUrlPatterns(eDT, false, urlPattern.stream().map(url -> {
                                        LOGGER.info("...add url mapping: {}", url.getTextTrim());
                                        return url.getTextTrim();
                                    }).toArray(String[]::new));
                                }
                            }).orElse(() -> LOGGER.warn("Filter {} already registered!", name)).run();
                        });
                    });
                    final List<Element> servlets = root.getChildren("servlet", ns);
                    final List<Element> smaps = root.getChildren("servlet-mapping", ns);
                    servlets.forEach(servlet -> {
                        final String name = servlet.getChildText("servlet-name", ns);
                        final String className = servlet.getChildText("servlet-class", ns);
                        smaps.stream().filter(mapping -> mapping.getChildText("servlet-name", ns).equals(name)).findFirst().ifPresent(mapping -> {
                            LOGGER.info("Register Servlet {} ({})...", name, className);
                            Optional.ofNullable(servletContext.addServlet(name, className)).<Runnable>map(sr -> () -> mapping.getChildren("url-pattern", ns).stream().forEach(url -> {
                                LOGGER.info("...add url mapping: {}", url.getTextTrim());
                                sr.addMapping(url.getTextTrim());
                            })).orElse(() -> LOGGER.error("Servlet{} already registered!", name)).run();
                        });
                    });
                } catch (IOException | JDOMException e) {
                    LOGGER.error("Couldn't parse " + WEB_FRAGMENT, e);
                }
            });
            jar.close();
        } catch (final IOException e) {
            LOGGER.error("Couldn't parse JAR!", e);
        }
    }
}
Also used : SAXBuilder(org.jdom2.input.SAXBuilder) MCRRuntimeComponentDetector(org.mycore.common.config.MCRRuntimeComponentDetector) MCRComponent(org.mycore.common.config.MCRComponent) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Collectors(java.util.stream.Collectors) File(java.io.File) Document(org.jdom2.Document) List(java.util.List) Logger(org.apache.logging.log4j.Logger) JDOMException(org.jdom2.JDOMException) Optional(java.util.Optional) DispatcherType(javax.servlet.DispatcherType) ServletContext(javax.servlet.ServletContext) Namespace(org.jdom2.Namespace) MCRStartupHandler(org.mycore.common.events.MCRStartupHandler) Collections(java.util.Collections) LogManager(org.apache.logging.log4j.LogManager) EnumSet(java.util.EnumSet) InputStream(java.io.InputStream) Element(org.jdom2.Element) SAXBuilder(org.jdom2.input.SAXBuilder) InputStream(java.io.InputStream) Element(org.jdom2.Element) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Document(org.jdom2.Document) JDOMException(org.jdom2.JDOMException) Namespace(org.jdom2.Namespace) DispatcherType(javax.servlet.DispatcherType)

Aggregations

File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Collections (java.util.Collections)2 EnumSet (java.util.EnumSet)2 List (java.util.List)2 Optional (java.util.Optional)2 JarFile (java.util.jar.JarFile)2 Collectors (java.util.stream.Collectors)2 DispatcherType (javax.servlet.DispatcherType)2 ServletContext (javax.servlet.ServletContext)2 LogManager (org.apache.logging.log4j.LogManager)2 Logger (org.apache.logging.log4j.Logger)2 Document (org.jdom2.Document)2 Element (org.jdom2.Element)2 JDOMException (org.jdom2.JDOMException)2 Namespace (org.jdom2.Namespace)2 SAXBuilder (org.jdom2.input.SAXBuilder)2 MCRComponent (org.mycore.common.config.MCRComponent)2