Java Code Examples for org.apache.catalina.util.ContextName#getBaseName()

The following examples show how to use org.apache.catalina.util.ContextName#getBaseName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HostConfig.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private boolean isDeployThisXML(File docBase, ContextName cn) {
    boolean deployThisXML = isDeployXML();
    if (Globals.IS_SECURITY_ENABLED && !deployThisXML) {
        // When running under a SecurityManager, deployXML may be overridden
        // on a per Context basis by the granting of a specific permission
        Policy currentPolicy = Policy.getPolicy();
        if (currentPolicy != null) {
            URL contextRootUrl;
            try {
                contextRootUrl = docBase.toURI().toURL();
                CodeSource cs = new CodeSource(contextRootUrl, (Certificate[]) null);
                PermissionCollection pc = currentPolicy.getPermissions(cs);
                Permission p = new DeployXmlPermission(cn.getBaseName());
                if (pc.implies(p)) {
                    deployThisXML = true;
                }
            } catch (MalformedURLException e) {
                // Should never happen
                log.warn("hostConfig.docBaseUrlInvalid", e);
            }
        }
    }

    return deployThisXML;
}
 
Example 2
Source File: Tomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Copy the specified WAR file to the Host's appBase and then call
 * {@link #addWebapp(String, String)} with the newly copied WAR. The WAR
 * will <b>NOT</b> be removed from the Host's appBase when the Tomcat
 * instance stops. Note that {@link ExpandWar} provides utility methods that
 * may be used to delete the WAR and/or expanded directory if required.
 *
 * @param contextPath   The context mapping to use, "" for root context.
 * @param source        The location from which the WAR should be copied
 *
 * @return The deployed Context
 *
 * @throws IOException If an I/O error occurs while copying the WAR file
 *                     from the specified URL to the appBase
 */
public Context addWebapp(String contextPath, URL source) throws IOException {

    ContextName cn = new ContextName(contextPath, null);

    // Make sure a conflicting web application has not already been deployed
    Host h = getHost();
    if (h.findChild(cn.getName()) != null) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictChild",
                source, contextPath, cn.getName()));
    }

    // Make sure appBase does not contain a conflicting web application
    File targetWar = new File(h.getAppBaseFile(), cn.getBaseName() + ".war");
    File targetDir = new File(h.getAppBaseFile(), cn.getBaseName());

    if (targetWar.exists()) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
                source, contextPath, targetWar.getAbsolutePath()));
    }
    if (targetDir.exists()) {
        throw new IllegalArgumentException(sm.getString("tomcat.addWebapp.conflictFile",
                source, contextPath, targetDir.getAbsolutePath()));
    }

    // Should be good to copy the WAR now
    URLConnection uConn = source.openConnection();

    try (InputStream is = uConn.getInputStream();
            OutputStream os = new FileOutputStream(targetWar)) {
        IOTools.flow(is, os);
    }

    return addWebapp(contextPath, targetWar.getAbsolutePath());
}
 
Example 3
Source File: HostConfig.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/**
 * Deploy applications for any directories or WAR files that are found
 * in our "application root" directory.
 * @param name The context name which should be deployed
 */
protected void deployApps(String name) {

    File appBase = host.getAppBaseFile();
    File configBase = host.getConfigBaseFile();
    ContextName cn = new ContextName(name, false);
    String baseName = cn.getBaseName();

    if (deploymentExists(cn.getName())) {
        return;
    }

    // Deploy XML descriptor from configBase
    File xml = new File(configBase, baseName + ".xml");
    if (xml.exists()) {
        deployDescriptor(cn, xml);
        return;
    }
    // Deploy WAR
    File war = new File(appBase, baseName + ".war");
    if (war.exists()) {
        deployWAR(cn, war);
        return;
    }
    // Deploy expanded folder
    File dir = new File(appBase, baseName);
    if (dir.exists())
        deployDirectory(cn, dir);
}
 
Example 4
Source File: HostConfig.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy applications for any directories or WAR files that are found
 * in our "application root" directory.
 */
protected void deployApps(String name) {

    File appBase = appBase();
    File configBase = configBase();
    ContextName cn = new ContextName(name, false);
    String baseName = cn.getBaseName();

    if (deploymentExists(cn.getName())) {
        return;
    }

    // Deploy XML descriptor from configBase
    File xml = new File(configBase, baseName + ".xml");
    if (xml.exists()) {
        deployDescriptor(cn, xml);
        return;
    }
    // Deploy WAR
    File war = new File(appBase, baseName + ".war");
    if (war.exists()) {
        deployWAR(cn, war);
        return;
    }
    // Deploy expanded folder
    File dir = new File(appBase, baseName);
    if (dir.exists())
        deployDirectory(cn, dir);
}
 
Example 5
Source File: HostConfig.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
/**
 * Deploy applications for any directories or WAR files that are found
 * in our "application root" directory.
 */
protected void deployApps(String name) {

    File appBase = appBase();
    File configBase = configBase();
    ContextName cn = new ContextName(name, false);
    String baseName = cn.getBaseName();

    if (deploymentExists(cn.getName())) {
        return;
    }

    // Deploy XML descriptor from configBase
    File xml = new File(configBase, baseName + ".xml");
    if (xml.exists()) {
        deployDescriptor(cn, xml);
        return;
    }
    // Deploy WAR
    File war = new File(appBase, baseName + ".war");
    if (war.exists()) {
        deployWAR(cn, war);
        return;
    }
    // Deploy expanded folder
    File dir = new File(appBase, baseName);
    if (dir.exists())
        deployDirectory(cn, dir);
}
 
Example 6
Source File: ManagerServlet.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Install an application for the specified path from the specified
 * web application archive.
 *
 * @param writer    Writer to render results to
 * @param tag       Revision tag to deploy from
 * @param cn        Name of the application to be installed
 * @param smClient  i18n messages using the locale of the client
 */
protected void deploy(PrintWriter writer, ContextName cn, String tag,
        StringManager smClient) {

    // NOTE: It is assumed that update is always true in this method.

    // Validate the requested context path
    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String baseName = cn.getBaseName();
    String name = cn.getName();
    String displayPath = cn.getDisplayName();

    // Find the local WAR file
    File localWar = new File(new File(versioned, tag), baseName + ".war");

    File deployedWar = new File(host.getAppBaseFile(), baseName + ".war");

    // Copy WAR to appBase
    try {
        if (isServiced(name)) {
            writer.println(smClient.getString("managerServlet.inService", displayPath));
        } else {
            addServiced(name);
            try {
                if (!deployedWar.delete()) {
                    writer.println(smClient.getString("managerServlet.deleteFail",
                            deployedWar));
                    return;
                }
                copy(localWar, deployedWar);
                // Perform new deployment
                check(name);
            } finally {
                removeServiced(name);
            }
        }
    } catch (Exception e) {
        log("managerServlet.check[" + displayPath + "]", e);
        writer.println(smClient.getString("managerServlet.exception",
                e.toString()));
        return;
    }

    writeDeployResult(writer, smClient, name, displayPath);
}
 
Example 7
Source File: HostConfig.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Deploy WAR files.
 * @param appBase The base path for applications
 * @param files The WARs to deploy
 */
protected void deployWARs(File appBase, String[] files) {

    if (files == null)
        return;

    ExecutorService es = host.getStartStopExecutor();
    List<Future<?>> results = new ArrayList<>();

    for (int i = 0; i < files.length; i++) {

        if (files[i].equalsIgnoreCase("META-INF"))
            continue;
        if (files[i].equalsIgnoreCase("WEB-INF"))
            continue;
        File war = new File(appBase, files[i]);
        if (files[i].toLowerCase(Locale.ENGLISH).endsWith(".war") &&
                war.isFile() && !invalidWars.contains(files[i]) ) {

            ContextName cn = new ContextName(files[i], true);

            if (isServiced(cn.getName())) {
                continue;
            }
            if (deploymentExists(cn.getName())) {
                DeployedApplication app = deployed.get(cn.getName());
                boolean unpackWAR = unpackWARs;
                if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
                    unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
                }
                if (!unpackWAR && app != null) {
                    // Need to check for a directory that should not be
                    // there
                    File dir = new File(appBase, cn.getBaseName());
                    if (dir.exists()) {
                        if (!app.loggedDirWarning) {
                            log.warn(sm.getString(
                                    "hostConfig.deployWar.hiddenDir",
                                    dir.getAbsoluteFile(),
                                    war.getAbsoluteFile()));
                            app.loggedDirWarning = true;
                        }
                    } else {
                        app.loggedDirWarning = false;
                    }
                }
                continue;
            }

            // Check for WARs with /../ /./ or similar sequences in the name
            if (!validateContextPath(appBase, cn.getBaseName())) {
                log.error(sm.getString(
                        "hostConfig.illegalWarName", files[i]));
                invalidWars.add(files[i]);
                continue;
            }

            results.add(es.submit(new DeployWar(this, cn, war)));
        }
    }

    for (Future<?> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString(
                    "hostConfig.deployWar.threaded.error"), e);
        }
    }
}
 
Example 8
Source File: StandardContextSF.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Store a Context as Separate file as configFile value from context exists.
 * filename can be relative to catalina.base.
 *
 * @see org.apache.catalina.storeconfig.IStoreFactory#store(java.io.PrintWriter,
 *      int, java.lang.Object)
 */
@Override
public void store(PrintWriter aWriter, int indent, Object aContext)
        throws Exception {

    if (aContext instanceof StandardContext) {
        StoreDescription desc = getRegistry().findDescription(
                aContext.getClass());
        if (desc.isStoreSeparate()) {
            URL configFile = ((StandardContext) aContext)
                    .getConfigFile();
            if (configFile != null) {
                if (desc.isExternalAllowed()) {
                    if (desc.isBackup())
                        storeWithBackup((StandardContext) aContext);
                    else
                        storeContextSeparate(aWriter, indent,
                                (StandardContext) aContext);
                    return;
                }
            } else if (desc.isExternalOnly()) {
                // Set a configFile so that the configuration is actually saved
                Context context = ((StandardContext) aContext);
                Host host = (Host) context.getParent();
                File configBase = host.getConfigBaseFile();
                ContextName cn = new ContextName(context.getName(), false);
                String baseName = cn.getBaseName();
                File xml = new File(configBase, baseName + ".xml");
                context.setConfigFile(xml.toURI().toURL());
                if (desc.isBackup())
                    storeWithBackup((StandardContext) aContext);
                else
                    storeContextSeparate(aWriter, indent,
                            (StandardContext) aContext);
                return;
            }
        }
    }
    super.store(aWriter, indent, aContext);

}
 
Example 9
Source File: ManagerServlet.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Install an application for the specified path from the specified
 * web application archive.
 *
 * @param writer    Writer to render results to
 * @param tag       Revision tag to deploy from
 * @param cn        Name of the application to be installed
 * @param smClient  i18n messages using the locale of the client
 */
protected void deploy(PrintWriter writer, ContextName cn, String tag,
        StringManager smClient) {

    // NOTE: It is assumed that update is always true in this method.

    // Validate the requested context path
    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String baseName = cn.getBaseName();
    String name = cn.getName();
    String displayPath = cn.getDisplayName();
    
    // Find the local WAR file
    File localWar = new File(new File(versioned, tag), baseName + ".war");

    File deployedWar = new File(deployed, baseName + ".war");

    // Copy WAR to appBase
    try {
        if (isServiced(name)) {
            writer.println(smClient.getString("managerServlet.inService", displayPath));
        } else {
            addServiced(name);
            try {
                if (!deployedWar.delete()) {
                    writer.println(smClient.getString("managerServlet.deleteFail",
                            deployedWar));
                    return;
                }
                copy(localWar, deployedWar);
                // Perform new deployment
                check(name);
            } finally {
                removeServiced(name);
            }
        }
    } catch (Exception e) {
        log("managerServlet.check[" + displayPath + "]", e);
        writer.println(smClient.getString("managerServlet.exception",
                e.toString()));
        return;
    }
    
    writeDeployResult(writer, smClient, name, displayPath);
}
 
Example 10
Source File: HostConfig.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Deploy WAR files.
 */
protected void deployWARs(File appBase, String[] files) {

    if (files == null)
        return;

    ExecutorService es = host.getStartStopExecutor();
    List<Future<?>> results = new ArrayList<Future<?>>();

    for (int i = 0; i < files.length; i++) {

        if (files[i].equalsIgnoreCase("META-INF"))
            continue;
        if (files[i].equalsIgnoreCase("WEB-INF"))
            continue;
        File war = new File(appBase, files[i]);
        if (files[i].toLowerCase(Locale.ENGLISH).endsWith(".war") &&
                war.isFile() && !invalidWars.contains(files[i]) ) {

            ContextName cn = new ContextName(files[i], true);

            if (isServiced(cn.getName())) {
                continue;
            }
            if (deploymentExists(cn.getName())) {
                DeployedApplication app = deployed.get(cn.getName());
                boolean unpackWAR = unpackWARs;
                if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
                    unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
                }
                if (!unpackWAR && app != null) {
                    // Need to check for a directory that should not be
                    // there
                    File dir = new File(appBase, cn.getBaseName());
                    if (dir.exists()) {
                        if (!app.loggedDirWarning) {
                            log.warn(sm.getString(
                                    "hostConfig.deployWar.hiddenDir",
                                    dir.getAbsoluteFile(),
                                    war.getAbsoluteFile()));
                            app.loggedDirWarning = true;
                        }
                    } else {
                        app.loggedDirWarning = false;
                    }
                }
                continue;
            }

            // Check for WARs with /../ /./ or similar sequences in the name
            if (!validateContextPath(appBase, cn.getBaseName())) {
                log.error(sm.getString(
                        "hostConfig.illegalWarName", files[i]));
                invalidWars.add(files[i]);
                continue;
            }

            results.add(es.submit(new DeployWar(this, cn, war)));
        }
    }

    for (Future<?> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString(
                    "hostConfig.deployWar.threaded.error"), e);
        }
    }
}
 
Example 11
Source File: ManagerServlet.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Install an application for the specified path from the specified
 * web application archive.
 *
 * @param writer    Writer to render results to
 * @param tag       Revision tag to deploy from
 * @param cn        Name of the application to be installed
 * @param smClient  i18n messages using the locale of the client
 */
protected void deploy(PrintWriter writer, ContextName cn, String tag,
        StringManager smClient) {

    // NOTE: It is assumed that update is always true in this method.

    // Validate the requested context path
    if (!validateContextName(cn, writer, smClient)) {
        return;
    }

    String baseName = cn.getBaseName();
    String name = cn.getName();
    String displayPath = cn.getDisplayName();
    
    // Find the local WAR file
    File localWar = new File(new File(versioned, tag), baseName + ".war");

    File deployedWar = new File(deployed, baseName + ".war");

    // Copy WAR to appBase
    try {
        if (isServiced(name)) {
            writer.println(smClient.getString("managerServlet.inService", displayPath));
        } else {
            addServiced(name);
            try {
                if (!deployedWar.delete()) {
                    writer.println(smClient.getString("managerServlet.deleteFail",
                            deployedWar));
                    return;
                }
                copy(localWar, deployedWar);
                // Perform new deployment
                check(name);
            } finally {
                removeServiced(name);
            }
        }
    } catch (Exception e) {
        log("managerServlet.check[" + displayPath + "]", e);
        writer.println(smClient.getString("managerServlet.exception",
                e.toString()));
        return;
    }
    
    writeDeployResult(writer, smClient, name, displayPath);
}
 
Example 12
Source File: HostConfig.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Deploy WAR files.
 */
protected void deployWARs(File appBase, String[] files) {

    if (files == null)
        return;

    ExecutorService es = host.getStartStopExecutor();
    List<Future<?>> results = new ArrayList<Future<?>>();

    for (int i = 0; i < files.length; i++) {

        if (files[i].equalsIgnoreCase("META-INF"))
            continue;
        if (files[i].equalsIgnoreCase("WEB-INF"))
            continue;
        File war = new File(appBase, files[i]);
        if (files[i].toLowerCase(Locale.ENGLISH).endsWith(".war") &&
                war.isFile() && !invalidWars.contains(files[i]) ) {

            ContextName cn = new ContextName(files[i], true);

            if (isServiced(cn.getName())) {
                continue;
            }
            if (deploymentExists(cn.getName())) {
                DeployedApplication app = deployed.get(cn.getName());
                boolean unpackWAR = unpackWARs;
                if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
                    unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
                }
                if (!unpackWAR && app != null) {
                    // Need to check for a directory that should not be
                    // there
                    File dir = new File(appBase, cn.getBaseName());
                    if (dir.exists()) {
                        if (!app.loggedDirWarning) {
                            log.warn(sm.getString(
                                    "hostConfig.deployWar.hiddenDir",
                                    dir.getAbsoluteFile(),
                                    war.getAbsoluteFile()));
                            app.loggedDirWarning = true;
                        }
                    } else {
                        app.loggedDirWarning = false;
                    }
                }
                continue;
            }

            // Check for WARs with /../ /./ or similar sequences in the name
            if (!validateContextPath(appBase, cn.getBaseName())) {
                log.error(sm.getString(
                        "hostConfig.illegalWarName", files[i]));
                invalidWars.add(files[i]);
                continue;
            }

            results.add(es.submit(new DeployWar(this, cn, war)));
        }
    }

    for (Future<?> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString(
                    "hostConfig.deployWar.threaded.error"), e);
        }
    }
}
 
Example 13
Source File: ContextConfig.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
protected void antiLocking() {

        if ((context instanceof StandardContext)
            && ((StandardContext) context).getAntiResourceLocking()) {

            Host host = (Host) context.getParent();
            String appBase = host.getAppBase();
            String docBase = context.getDocBase();
            if (docBase == null)
                return;
            originalDocBase = docBase;

            File docBaseFile = new File(docBase);
            if (!docBaseFile.isAbsolute()) {
                File file = new File(appBase);
                if (!file.isAbsolute()) {
                    file = new File(getBaseDir(), appBase);
                }
                docBaseFile = new File(file, docBase);
            }

            String path = context.getPath();
            if (path == null) {
                return;
            }
            ContextName cn = new ContextName(path, context.getWebappVersion());
            docBase = cn.getBaseName();

            if (originalDocBase.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                antiLockingDocBase = new File(
                        System.getProperty("java.io.tmpdir"),
                        deploymentCount++ + "-" + docBase + ".war");
            } else {
                antiLockingDocBase = new File(
                        System.getProperty("java.io.tmpdir"),
                        deploymentCount++ + "-" + docBase);
            }
            antiLockingDocBase = antiLockingDocBase.getAbsoluteFile();

            if (log.isDebugEnabled())
                log.debug("Anti locking context[" + context.getName()
                        + "] setting docBase to " +
                        antiLockingDocBase.getPath());

            // Cleanup just in case an old deployment is lying around
            ExpandWar.delete(antiLockingDocBase);
            if (ExpandWar.copy(docBaseFile, antiLockingDocBase)) {
                context.setDocBase(antiLockingDocBase.getPath());
            }
        }
    }