Search in sources :

Example 1 with ApplicationParameter

use of org.apache.tomcat.util.descriptor.web.ApplicationParameter in project tomcat by apache.

the class StandardContext method addApplicationParameter.

/**
 * Add a new application parameter for this application.
 *
 * @param parameter The new application parameter
 */
@Override
public void addApplicationParameter(ApplicationParameter parameter) {
    synchronized (applicationParametersLock) {
        String newName = parameter.getName();
        for (ApplicationParameter p : applicationParameters) {
            if (newName.equals(p.getName()) && !p.getOverride()) {
                return;
            }
        }
        ApplicationParameter[] results = Arrays.copyOf(applicationParameters, applicationParameters.length + 1);
        results[applicationParameters.length] = parameter;
        applicationParameters = results;
    }
    fireContainerEvent("addApplicationParameter", parameter);
}
Also used : ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter)

Example 2 with ApplicationParameter

use of org.apache.tomcat.util.descriptor.web.ApplicationParameter in project tomcat by apache.

the class ContextMBean method findApplicationParameters.

/**
 * Return the set of application parameters for this application.
 * @return a string array with a representation of each parameter
 * @throws MBeanException propagated from the managed resource access
 */
public String[] findApplicationParameters() throws MBeanException {
    Context context = doGetManagedResource();
    ApplicationParameter[] params = context.findApplicationParameters();
    String[] stringParams = new String[params.length];
    for (int counter = 0; counter < params.length; counter++) {
        stringParams[counter] = params[counter].toString();
    }
    return stringParams;
}
Also used : Context(org.apache.catalina.Context) ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint)

Example 3 with ApplicationParameter

use of org.apache.tomcat.util.descriptor.web.ApplicationParameter in project tomcat by apache.

the class StandardContextSF method storeChildren.

/**
 * Store the specified context element children.
 *
 * @param aWriter Current output writer
 * @param indent Indentation level
 * @param aContext Context to store
 * @param parentDesc The element description
 * @throws Exception Configuration storing error
 */
@Override
public void storeChildren(PrintWriter aWriter, int indent, Object aContext, StoreDescription parentDesc) throws Exception {
    if (aContext instanceof StandardContext) {
        StandardContext context = (StandardContext) aContext;
        // Store nested <Listener> elements
        LifecycleListener[] listeners = context.findLifecycleListeners();
        List<LifecycleListener> listenersArray = new ArrayList<>();
        for (LifecycleListener listener : listeners) {
            if (!(listener instanceof ThreadLocalLeakPreventionListener)) {
                listenersArray.add(listener);
            }
        }
        storeElementArray(aWriter, indent, listenersArray.toArray());
        // Store nested <Valve> elements
        Valve[] valves = context.getPipeline().getValves();
        storeElementArray(aWriter, indent, valves);
        // Store nested <Loader> elements
        Loader loader = context.getLoader();
        storeElement(aWriter, indent, loader);
        // Store nested <Manager> elements
        if (context.getCluster() == null || !context.getDistributable()) {
            Manager manager = context.getManager();
            storeElement(aWriter, indent, manager);
        }
        // Store nested <Realm> element
        Realm realm = context.getRealm();
        if (realm != null) {
            Realm parentRealm = null;
            // @TODO is this case possible?
            if (context.getParent() != null) {
                parentRealm = context.getParent().getRealm();
            }
            if (realm != parentRealm) {
                storeElement(aWriter, indent, realm);
            }
        }
        // Store nested resources
        WebResourceRoot resources = context.getResources();
        storeElement(aWriter, indent, resources);
        // Store nested <WrapperListener> elements
        String[] wLifecycles = context.findWrapperLifecycles();
        getStoreAppender().printTagArray(aWriter, "WrapperListener", indent + 2, wLifecycles);
        // Store nested <WrapperLifecycle> elements
        String[] wListeners = context.findWrapperListeners();
        getStoreAppender().printTagArray(aWriter, "WrapperLifecycle", indent + 2, wListeners);
        // Store nested <Parameter> elements
        ApplicationParameter[] appParams = context.findApplicationParameters();
        storeElementArray(aWriter, indent, appParams);
        // Store nested naming resources elements (EJB,Resource,...)
        NamingResourcesImpl nresources = context.getNamingResources();
        storeElement(aWriter, indent, nresources);
        // Store nested watched resources <WatchedResource>
        String[] wresources = context.findWatchedResources();
        wresources = filterWatchedResources(context, wresources);
        getStoreAppender().printTagArray(aWriter, "WatchedResource", indent + 2, wresources);
        // Store nested <JarScanner> elements
        JarScanner jarScanner = context.getJarScanner();
        storeElement(aWriter, indent, jarScanner);
        // Store nested <CookieProcessor> elements
        CookieProcessor cookieProcessor = context.getCookieProcessor();
        storeElement(aWriter, indent, cookieProcessor);
    }
}
Also used : ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) ArrayList(java.util.ArrayList) Loader(org.apache.catalina.Loader) LifecycleListener(org.apache.catalina.LifecycleListener) Manager(org.apache.catalina.Manager) JarScanner(org.apache.tomcat.JarScanner) ThreadLocalLeakPreventionListener(org.apache.catalina.core.ThreadLocalLeakPreventionListener) CookieProcessor(org.apache.tomcat.util.http.CookieProcessor) StandardContext(org.apache.catalina.core.StandardContext) Valve(org.apache.catalina.Valve) NamingResourcesImpl(org.apache.catalina.deploy.NamingResourcesImpl) Realm(org.apache.catalina.Realm) WebResourceRoot(org.apache.catalina.WebResourceRoot)

Example 4 with ApplicationParameter

use of org.apache.tomcat.util.descriptor.web.ApplicationParameter in project tomcat by apache.

the class StandardContext method mergeParameters.

/**
 * Merge the context initialization parameters specified in the application
 * deployment descriptor with the application parameters described in the
 * server configuration, respecting the <code>override</code> property of
 * the application parameters appropriately.
 */
private void mergeParameters() {
    Map<String, String> mergedParams = new HashMap<>();
    String[] names = findParameters();
    for (String s : names) {
        mergedParams.put(s, findParameter(s));
    }
    ApplicationParameter[] params = findApplicationParameters();
    for (ApplicationParameter param : params) {
        if (param.getOverride()) {
            if (mergedParams.get(param.getName()) == null) {
                mergedParams.put(param.getName(), param.getValue());
            }
        } else {
            mergedParams.put(param.getName(), param.getValue());
        }
    }
    ServletContext sc = getServletContext();
    for (Map.Entry<String, String> entry : mergedParams.entrySet()) {
        sc.setInitParameter(entry.getKey(), entry.getValue());
    }
}
Also used : ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ServletContext(jakarta.servlet.ServletContext) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) FilterMap(org.apache.tomcat.util.descriptor.web.FilterMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap)

Example 5 with ApplicationParameter

use of org.apache.tomcat.util.descriptor.web.ApplicationParameter in project tomee by apache.

the class TomcatWebAppBuilder method deployWebApps.

// 
// OpenEJB WebAppBuilder
// 
/**
 * {@inheritDoc}
 */
@Override
public void deployWebApps(final AppInfo appInfo, final ClassLoader classLoader) throws Exception {
    try {
        for (final WebAppInfo webApp : appInfo.webApps) {
            // look for context.xml
            final File war = new File(webApp.path);
            URL contextXmlUrl = null;
            if (war.isDirectory()) {
                final File cXml = new File(war, Constants.ApplicationContextXml).getAbsoluteFile();
                if (cXml.exists()) {
                    contextXmlUrl = cXml.toURI().toURL();
                    LOGGER.info("using context file " + cXml.getAbsolutePath());
                }
            } else {
                // war
                try (final JarFile warAsJar = new JarFile(war)) {
                    final JarEntry entry = warAsJar.getJarEntry(Constants.ApplicationContextXml);
                    if (entry != null) {
                        contextXmlUrl = new URL("jar:" + war.getAbsoluteFile().toURI().toURL().toExternalForm() + "!/" + Constants.ApplicationContextXml);
                    }
                }
            }
            if (isAlreadyDeployed(appInfo, webApp)) {
                continue;
            }
            StandardContext standardContext;
            {
                final ClassLoader containerLoader = Helper.get();
                final Host host = hosts.getDefault();
                if (StandardHost.class.isInstance(host) && !StandardContext.class.getName().equals(StandardHost.class.cast(host).getContextClass())) {
                    try {
                        standardContext = StandardContext.class.cast(containerLoader.loadClass(StandardHost.class.cast(host).getContextClass()).newInstance());
                    } catch (final Throwable th) {
                        LOGGER.warning("Can't use context class specified, using default StandardContext", th);
                        standardContext = new StandardContext();
                    }
                } else {
                    standardContext = new StandardContext();
                }
                // should be optional but in maven parent is app loader and not maven loader which is the real parent
                final ClassLoader currentParent = standardContext.getParentClassLoader();
                if (currentParent == null || isParent(currentParent, containerLoader)) {
                    standardContext.setParentClassLoader(containerLoader);
                }
            }
            standardContext.setUnpackWAR(!"false".equalsIgnoreCase(appInfo.properties.getProperty("tomcat.unpackWar")));
            if (contextXmlUrl != null) {
                standardContext.setConfigFile(contextXmlUrl);
            }
            // default context path must start with / but not end with slash
            try {
                if (webApp.defaultContextPath != null && webApp.defaultContextPath.matches("^/\\w*[^/]$")) {
                    standardContext.setPath(webApp.defaultContextPath);
                }
            } catch (final Exception e) {
                // don't fail because it's a hack, just output the exception
                e.printStackTrace();
            }
            if (standardContext.getPath() != null) {
                webApp.contextRoot = standardContext.getPath();
            }
            if (webApp.contextRoot.startsWith("/") || webApp.contextRoot.startsWith(File.separator)) {
                webApp.contextRoot = webApp.contextRoot.substring(1);
            }
            if (webApp.contextRoot.startsWith(File.separator)) {
                webApp.contextRoot = webApp.contextRoot.replaceFirst(File.separator, "");
            }
            // /!\ take care, StandardContext default host = "_" and not null or localhost
            final String hostname = Contexts.getHostname(standardContext);
            if (hostname != null && !"_".equals(hostname)) {
                webApp.host = hostname;
            }
            final ApplicationParameter appParam = new ApplicationParameter();
            appParam.setName(OPENEJB_WEBAPP_MODULE_ID);
            appParam.setValue(webApp.moduleId);
            standardContext.addApplicationParameter(appParam);
            if (!isAlreadyDeployed(appInfo, webApp)) {
                if (standardContext.getPath() == null) {
                    if (webApp.contextRoot != null && webApp.contextRoot.startsWith("/")) {
                        standardContext.setPath(webApp.contextRoot);
                    } else if (isRoot(webApp.contextRoot)) {
                        standardContext.setPath("");
                    } else {
                        standardContext.setPath("/" + webApp.contextRoot);
                    }
                }
                if (standardContext.getDocBase() == null) {
                    standardContext.setDocBase(webApp.path);
                }
                String docBase = standardContext.getDocBase();
                File docBaseFile = new File(docBase);
                if (docBase != null && docBaseFile.isFile() && docBase.endsWith(".war")) {
                    DeploymentLoader.unpack(docBaseFile);
                    if (standardContext.getPath().endsWith(".war")) {
                        standardContext.setPath(removeFirstSlashAndWar("/" + standardContext.getPath()));
                        standardContext.setName(standardContext.getPath());
                        webApp.contextRoot = standardContext.getPath();
                    }
                    standardContext.setDocBase(docBase.substring(0, docBase.length() - 4));
                }
                if (isRoot(standardContext.getName())) {
                    standardContext.setName("");
                    webApp.contextRoot = "";
                }
                if (isAlreadyDeployed(appInfo, webApp)) {
                    // possible because of the previous renaming
                    continue;
                }
                // but here we have all the classloading logic
                if (classLoader != null) {
                    standardContext.setParentClassLoader(classLoader);
                    standardContext.setDelegate(true);
                }
                String host = webApp.host;
                if (host == null) {
                    host = hosts.getDefaultHost();
                    LOGGER.info("using default host: " + host);
                }
                if (classLoader != null) {
                    appInfo.autoDeploy = false;
                    deployWar(standardContext, host, appInfo);
                } else {
                    // force a normal deployment with lazy building of AppInfo
                    deployWar(standardContext, host, null);
                }
                // TODO should we copy the information in the appInfo using the jee object tree or add more to the info tree
                // this might then move to the assembler after webapp is deployed so we can read information from info tree
                // and build up all policy context from there instead of from Tomcat internal objects
                final TomcatSecurityConstaintsToJaccPermissionsTransformer transformer = new TomcatSecurityConstaintsToJaccPermissionsTransformer(standardContext);
                final PolicyContext policyContext = transformer.createResourceAndDataPermissions();
                final JaccPermissionsBuilder jaccPermissionsBuilder = new JaccPermissionsBuilder();
                jaccPermissionsBuilder.install(policyContext);
            }
        }
    } finally {
        // cleanup temp var passing
        for (final WebAppInfo webApp : appInfo.webApps) {
            appInfo.properties.remove(webApp);
        }
    }
}
Also used : ApplicationParameter(org.apache.tomcat.util.descriptor.web.ApplicationParameter) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) JaccPermissionsBuilder(org.apache.openejb.assembler.classic.JaccPermissionsBuilder) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) URL(java.net.URL) LifecycleException(org.apache.catalina.LifecycleException) NameNotFoundException(javax.naming.NameNotFoundException) IOException(java.io.IOException) NamingException(javax.naming.NamingException) OpenEJBException(org.apache.openejb.OpenEJBException) OpenEJBRuntimeException(org.apache.openejb.OpenEJBRuntimeException) WebAppInfo(org.apache.openejb.assembler.classic.WebAppInfo) PolicyContext(org.apache.openejb.assembler.classic.PolicyContext) StandardHost(org.apache.catalina.core.StandardHost) StandardContext(org.apache.catalina.core.StandardContext) File(java.io.File) JarFile(java.util.jar.JarFile) TomcatSecurityConstaintsToJaccPermissionsTransformer(org.apache.tomee.catalina.security.TomcatSecurityConstaintsToJaccPermissionsTransformer)

Aggregations

ApplicationParameter (org.apache.tomcat.util.descriptor.web.ApplicationParameter)6 StandardContext (org.apache.catalina.core.StandardContext)2 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)2 ServletContext (jakarta.servlet.ServletContext)1 File (java.io.File)1 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 JarEntry (java.util.jar.JarEntry)1 JarFile (java.util.jar.JarFile)1 NameNotFoundException (javax.naming.NameNotFoundException)1 NamingException (javax.naming.NamingException)1 Context (org.apache.catalina.Context)1 Host (org.apache.catalina.Host)1 LifecycleException (org.apache.catalina.LifecycleException)1