Search in sources :

Example 1 with UndertowExternalMountsAsset

use of org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset in project wildfly-swarm by wildfly-swarm.

the class ContextPathArchivePreparerTest method testExternalMount.

@SuppressWarnings("unchecked")
@Test
public void testExternalMount() throws Exception {
    WARArchive archive = DefaultWarDeploymentFactory.archiveFromCurrentApp();
    assertThat(archive.getContextRoot()).isNull();
    URL url = getClass().getClassLoader().getResource("mounts.yml");
    ConfigViewFactory factory = new ConfigViewFactory(new Properties());
    factory.load("test", url);
    factory.withProfile("test");
    ConfigViewImpl view = factory.get(true);
    List<String> mounts = view.resolve("swarm.context.mounts").as(List.class).getValue();
    ContextPathArchivePreparer preparer = new ContextPathArchivePreparer(archive);
    preparer.mounts = mounts;
    preparer.process();
    Node externalMount = archive.get(WARArchive.EXTERNAL_MOUNT_PATH);
    assertThat(externalMount).isNotNull();
    assertThat(externalMount.getAsset()).isInstanceOf(UndertowExternalMountsAsset.class);
    UndertowExternalMountsAsset externalMountAsset = (UndertowExternalMountsAsset) externalMount.getAsset();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(externalMountAsset.openStream()))) {
        assertThat(reader.readLine()).endsWith("external1");
        assertThat(reader.readLine()).endsWith("external2");
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) Node(org.jboss.shrinkwrap.api.Node) Properties(java.util.Properties) WARArchive(org.wildfly.swarm.undertow.WARArchive) URL(java.net.URL) ConfigViewImpl(org.wildfly.swarm.container.config.ConfigViewImpl) UndertowExternalMountsAsset(org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset) ConfigViewFactory(org.wildfly.swarm.container.config.ConfigViewFactory) BufferedReader(java.io.BufferedReader) List(java.util.List) Test(org.junit.Test)

Example 2 with UndertowExternalMountsAsset

use of org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset in project wildfly-swarm by wildfly-swarm.

the class StaticContentContainer method staticContent.

/**
 * Enable static content to be served from a given base in the classpath.
 *
 * @param base The path prefix to use for static content.
 * @return
 */
@SuppressWarnings("unchecked")
default T staticContent(String base) {
    try {
        // Add all the static content from the current app to the archive
        Archive allResources = DefaultWarDeploymentFactory.archiveFromCurrentApp();
        // Here we define static as basically anything that's not a
        // Java class file or under WEB-INF or META-INF
        mergeIgnoringDuplicates(allResources, base, Filters.exclude(".*\\.class$"));
    } catch (Exception ex) {
        log.log(Level.WARNING, "Error setting up static resources", ex);
    }
    Node node = get(EXTERNAL_MOUNT_PATH);
    UndertowExternalMountsAsset asset;
    if (node == null) {
        asset = new UndertowExternalMountsAsset();
        add(asset, EXTERNAL_MOUNT_PATH);
    } else {
        Asset tempAsset = node.getAsset();
        if (!(tempAsset instanceof UndertowExternalMountsAsset)) {
            asset = new UndertowExternalMountsAsset(tempAsset.openStream());
            add(asset, EXTERNAL_MOUNT_PATH);
        } else {
            asset = (UndertowExternalMountsAsset) node.getAsset();
        }
    }
    // Add external mounts for static content so changes are picked up
    // immediately during development
    Path webResources = Paths.get(System.getProperty("user.dir"), "src", "main", "webapp");
    if (base != null) {
        webResources = webResources.resolve(base);
    }
    if (Files.exists(webResources)) {
        asset.externalMount(webResources.toString());
    }
    webResources = Paths.get(System.getProperty("user.dir"), "src", "main", "resources");
    if (base != null) {
        webResources = webResources.resolve(base);
    }
    if (Files.exists(webResources)) {
        asset.externalMount(webResources.toString());
    }
    return (T) this;
}
Also used : ArchivePath(org.jboss.shrinkwrap.api.ArchivePath) Path(java.nio.file.Path) BasicPath(org.jboss.shrinkwrap.impl.base.path.BasicPath) Archive(org.jboss.shrinkwrap.api.Archive) UndertowExternalMountsAsset(org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset) Node(org.jboss.shrinkwrap.api.Node) Asset(org.jboss.shrinkwrap.api.asset.Asset) UndertowExternalMountsAsset(org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset)

Example 3 with UndertowExternalMountsAsset

use of org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset in project wildfly-swarm by wildfly-swarm.

the class ContextPathArchivePreparer method process.

@Override
public void process() {
    WARArchive warArchive = archive.as(WARArchive.class);
    if (warArchive.getContextRoot() == null) {
        warArchive.setContextRoot(contextPath.get());
    }
    UndertowExternalMountsAsset ut = null;
    if (mounts != null) {
        for (String mountPath : mounts) {
            Path staticPath = Paths.get(mountPath);
            if (!staticPath.isAbsolute()) {
                staticPath = Paths.get(System.getProperty("user.dir"), staticPath.toString()).normalize();
            }
            if (ut == null) {
                ut = new UndertowExternalMountsAsset();
            }
            ut.externalMount(staticPath.toString());
        }
    }
    if (ut != null) {
        warArchive.add(ut, WARArchive.EXTERNAL_MOUNT_PATH);
    }
}
Also used : Path(java.nio.file.Path) UndertowExternalMountsAsset(org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset) WARArchive(org.wildfly.swarm.undertow.WARArchive)

Aggregations

UndertowExternalMountsAsset (org.wildfly.swarm.undertow.internal.UndertowExternalMountsAsset)3 Path (java.nio.file.Path)2 Node (org.jboss.shrinkwrap.api.Node)2 WARArchive (org.wildfly.swarm.undertow.WARArchive)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 List (java.util.List)1 Properties (java.util.Properties)1 Archive (org.jboss.shrinkwrap.api.Archive)1 ArchivePath (org.jboss.shrinkwrap.api.ArchivePath)1 Asset (org.jboss.shrinkwrap.api.asset.Asset)1 BasicPath (org.jboss.shrinkwrap.impl.base.path.BasicPath)1 Test (org.junit.Test)1 ConfigViewFactory (org.wildfly.swarm.container.config.ConfigViewFactory)1 ConfigViewImpl (org.wildfly.swarm.container.config.ConfigViewImpl)1