Example usage for java.io FilePermission FilePermission

List of usage examples for java.io FilePermission FilePermission

Introduction

In this page you can find the example usage for java.io FilePermission FilePermission.

Prototype


FilePermission(String path, int mask) 

Source Link

Document

Creates a new FilePermission object using an action mask.

Usage

From source file:org.eclipse.equinox.http.servlet.internal.multipart.MultipartSupportImpl.java

private void checkPermission(File baseStorage, ServletContext servletContext) {
    BundleContext bundleContext = (BundleContext) servletContext.getAttribute("osgi-bundlecontext"); //$NON-NLS-1$
    Bundle bundle = bundleContext.getBundle();
    AccessControlContext accessControlContext = bundle.adapt(AccessControlContext.class);
    if (accessControlContext == null)
        return;//w w  w.  j  a  va2  s.c o m
    accessControlContext.checkPermission(new FilePermission(baseStorage.getAbsolutePath(), "read,write")); //$NON-NLS-1$
}

From source file:org.jboss.as.test.integration.web.security.runas.ServletRunAsTestCase.java

@Deployment(name = DEPLOYMENT, managed = false, testable = false)
public static WebArchive deployment() throws Exception {
    WebArchive war = ShrinkWrap.create(WebArchive.class, "servlet-runas.war");
    war.addClasses(CallProtectedEjbServlet.class, RunAsAdminServlet.class, RunAsUserServlet.class, Hello.class,
            HelloBean.class);
    war.addAsWebResource(/*from ww w  .ja  va2s . c om*/
            PermissionUtils.createPermissionsXmlAsset(
                    new FilePermission(WORK_DIR.getAbsolutePath() + "/*", "read,write")),
            "META-INF/permissions.xml");
    return war;
}

From source file:org.sonatype.gshell.vfs.provider.truezip.TruezipFileSystem.java

/**
 * Creates a temporary local copy of a file and its descendants.
 *///from  ww  w  .j a v  a2  s . c o  m
protected java.io.File doReplicateFile(final FileObject fileObject, final FileSelector selector)
        throws Exception {
    final TruezipFileObject localFile = (TruezipFileObject) fileObject;
    final File file = localFile.getLocalFile();

    final SecurityManager sm = System.getSecurityManager();
    if (sm != null) {
        final FilePermission requiredPerm = new FilePermission(file.getAbsolutePath(), "read");
        sm.checkPermission(requiredPerm);
    }

    return file;
}

From source file:org.tinygroup.jspengine.compiler.JspRuntimeContext.java

/**
 * Method used to initialize SecurityManager data.
 *//*w w  w  . j  a v a 2s  .  c  o m*/
private void initSecurity() {

    // Setup the PermissionCollection for this web app context
    // based on the permissions configured for the root of the
    // web app context directory, then add a file read permission
    // for that directory.
    Policy policy = Policy.getPolicy();
    if (policy != null) {
        try {
            // Get the permissions for the web app context
            String docBase = context.getRealPath("/");
            if (docBase == null) {
                docBase = options.getScratchDir().toString();
            }
            String codeBase = docBase;
            if (!codeBase.endsWith(File.separator)) {
                codeBase = codeBase + File.separator;
            }
            File contextDir = new File(codeBase);
            URL url = contextDir.getCanonicalFile().toURL();
            codeSource = new CodeSource(url, (Certificate[]) null);
            permissionCollection = policy.getPermissions(codeSource);

            // Create a file read permission for web app context directory
            if (!docBase.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(docBase, "read"));
                docBase = docBase + File.separator;
            } else {
                permissionCollection
                        .add(new FilePermission(docBase.substring(0, docBase.length() - 1), "read"));
            }
            docBase = docBase + "-";
            permissionCollection.add(new FilePermission(docBase, "read"));

            // Create a file read permission for web app tempdir (work)
            // directory
            String workDir = options.getScratchDir().toString();
            if (!workDir.endsWith(File.separator)) {
                permissionCollection.add(new FilePermission(workDir, "read"));
                workDir = workDir + File.separator;
            }
            workDir = workDir + "-";
            permissionCollection.add(new FilePermission(workDir, "read"));

            // Allow the JSP to access
            // org.tinygroup.jspengine.runtime.HttpJspBase
            permissionCollection
                    .add(new RuntimePermission("accessClassInPackage.org.tinygroup.jspengine.runtime"));

            if (parentClassLoader instanceof URLClassLoader) {
                URL[] urls = ((URLClassLoader) parentClassLoader).getURLs();
                String jarUrl = null;
                String jndiUrl = null;
                for (int i = 0; i < urls.length; i++) {
                    if (jndiUrl == null && urls[i].toString().startsWith("jndi:")) {
                        jndiUrl = urls[i].toString() + "-";
                    }
                    if (jarUrl == null && urls[i].toString().startsWith("jar:jndi:")) {
                        jarUrl = urls[i].toString();
                        jarUrl = jarUrl.substring(0, jarUrl.length() - 2);
                        jarUrl = jarUrl.substring(0, jarUrl.lastIndexOf('/')) + "/-";
                    }
                }
                if (jarUrl != null) {
                    permissionCollection.add(new FilePermission(jarUrl, "read"));
                    permissionCollection.add(new FilePermission(jarUrl.substring(4), "read"));
                }
                if (jndiUrl != null)
                    permissionCollection.add(new FilePermission(jndiUrl, "read"));
            }
        } catch (Exception e) {
            context.log("Security Init for context failed", e);
        }
    }
}