Search in sources :

Example 76 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class FileStore method directory.

// -------------------------------------------------------- Private Methods
/**
 * Return a File object representing the pathname to our
 * session persistence directory, if any.  The directory will be
 * created if it does not already exist.
 */
private File directory() throws IOException {
    if (this.directory == null) {
        return null;
    }
    if (this.directoryFile != null) {
        // NOTE:  Race condition is harmless, so do not synchronize
        return this.directoryFile;
    }
    File file = new File(this.directory);
    if (!file.isAbsolute()) {
        Context context = manager.getContext();
        ServletContext servletContext = context.getServletContext();
        File work = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
        file = new File(work, this.directory);
    }
    if (!file.exists() || !file.isDirectory()) {
        if (!file.delete() && file.exists()) {
            throw new IOException(sm.getString("fileStore.deleteFailed", file));
        }
        if (!file.mkdirs() && !file.isDirectory()) {
            throw new IOException(sm.getString("fileStore.createFailed", file));
        }
    }
    this.directoryFile = file;
    return file;
}
Also used : Context(org.apache.catalina.Context) ServletContext(jakarta.servlet.ServletContext) ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) File(java.io.File)

Example 77 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class SSIServletExternalResolver method getServletContextAndPathFromVirtualPath.

protected ServletContextAndPath getServletContextAndPathFromVirtualPath(String virtualPath) throws IOException {
    if (!virtualPath.startsWith("/") && !virtualPath.startsWith("\\")) {
        return new ServletContextAndPath(context, getAbsolutePath(virtualPath));
    }
    String normalized = RequestUtil.normalize(virtualPath);
    if (isVirtualWebappRelative) {
        return new ServletContextAndPath(context, normalized);
    }
    ServletContext normContext = context.getContext(normalized);
    if (normContext == null) {
        throw new IOException(sm.getString("ssiServletExternalResolver.noContext", normalized));
    }
    // '/file1.shtml' vs '/appName1/file1.shtml'
    if (!isRootContext(normContext)) {
        String noContext = getPathWithoutContext(normContext.getContextPath(), normalized);
        return new ServletContextAndPath(normContext, noContext);
    }
    return new ServletContextAndPath(normContext, normalized);
}
Also used : ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException)

Example 78 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class SSIServletExternalResolver method getURLConnection.

protected URLConnection getURLConnection(String originalPath, boolean virtual) throws IOException {
    ServletContextAndPath csAndP = getServletContextAndPath(originalPath, virtual);
    ServletContext context = csAndP.getServletContext();
    String path = csAndP.getPath();
    URL url = context.getResource(path);
    if (url == null) {
        throw new IOException(sm.getString("ssiServletExternalResolver.noResource", path));
    }
    URLConnection urlConnection = url.openConnection();
    return urlConnection;
}
Also used : ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 79 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class WebappLoader method setPermissions.

/**
 * Configure associated class loader permissions.
 */
private void setPermissions() {
    if (!Globals.IS_SECURITY_ENABLED) {
        return;
    }
    if (context == null) {
        return;
    }
    // Tell the class loader the root of the context
    ServletContext servletContext = context.getServletContext();
    // Assigning permissions for the work directory
    File workDir = (File) servletContext.getAttribute(ServletContext.TEMPDIR);
    if (workDir != null) {
        try {
            String workDirPath = workDir.getCanonicalPath();
            classLoader.addPermission(new FilePermission(workDirPath, "read,write"));
            classLoader.addPermission(new FilePermission(workDirPath + File.separator + "-", "read,write,delete"));
        } catch (IOException e) {
        // Ignore
        }
    }
    for (URL url : context.getResources().getBaseUrls()) {
        classLoader.addPermission(url);
    }
}
Also used : ServletContext(jakarta.servlet.ServletContext) IOException(java.io.IOException) File(java.io.File) FilePermission(java.io.FilePermission) URL(java.net.URL)

Example 80 with ServletContext

use of jakarta.servlet.ServletContext in project tomcat by apache.

the class WebappLoader method setClassPath.

/**
 * Set the appropriate context attribute for our class path.  This
 * is required only because Jasper depends on it.
 */
private void setClassPath() {
    // Validate our current state information
    if (context == null) {
        return;
    }
    ServletContext servletContext = context.getServletContext();
    if (servletContext == null) {
        return;
    }
    StringBuilder classpath = new StringBuilder();
    // Assemble the class path information from our class loader chain
    ClassLoader loader = getClassLoader();
    if (delegate && loader != null) {
        // Skip the webapp loader for now as delegation is enabled
        loader = loader.getParent();
    }
    while (loader != null) {
        if (!buildClassPath(classpath, loader)) {
            break;
        }
        loader = loader.getParent();
    }
    if (delegate) {
        // Delegation was enabled, go back and add the webapp paths
        loader = getClassLoader();
        if (loader != null) {
            buildClassPath(classpath, loader);
        }
    }
    this.classpath = classpath.toString();
    // Store the assembled class path as a servlet context attribute
    servletContext.setAttribute(Globals.CLASS_PATH_ATTR, this.classpath);
}
Also used : ServletContext(jakarta.servlet.ServletContext) URLClassLoader(java.net.URLClassLoader)

Aggregations

ServletContext (jakarta.servlet.ServletContext)116 Test (org.junit.jupiter.api.Test)45 ServletConfig (jakarta.servlet.ServletConfig)34 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)17 FilterRegistration (jakarta.servlet.FilterRegistration)15 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 ServletException (jakarta.servlet.ServletException)12 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)9 Filter (jakarta.servlet.Filter)8 WebApplicationContext (org.springframework.web.context.WebApplicationContext)8 Context (org.apache.catalina.Context)7 AtmosphereFramework (org.atmosphere.cpr.AtmosphereFramework)6 Test (org.junit.Test)6