Example usage for com.amazonaws.services.s3.model AccessControlList getGrants

List of usage examples for com.amazonaws.services.s3.model AccessControlList getGrants

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model AccessControlList getGrants.

Prototype

@Deprecated
public Set<Grant> getGrants() 

Source Link

Document

Gets the set of Grant objects in this access control list (ACL).

Usage

From source file:cloudExplorer.Acl.java

License:Open Source License

void setAccess(String id, int what, String access_key, String secret_key, String endpoint, String bucket) {
    try {/*www .j a  va  2  s  .  c  o  m*/

        Collection<Grant> grantCollection = new ArrayList<Grant>();
        AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
        AmazonS3 s3Client = new AmazonS3Client(credentials,
                new ClientConfiguration().withSignerOverride("S3SignerType"));
        s3Client.setEndpoint(endpoint);
        AccessControlList bucketAcl = s3Client.getBucketAcl(bucket);
        Grant grant = null;
        if (what == 0) {

            grant = new Grant(new CanonicalGrantee(id), Permission.Read);
            grantCollection.add(grant);
        }

        if (what == 1) {
            grant = new Grant(new CanonicalGrantee(id), Permission.FullControl);
            grantCollection.add(grant);
        }

        if (what == 3) {
            bucketAcl.getGrants().clear();
        }

        bucketAcl.getGrants().addAll(grantCollection);
        s3Client.setBucketAcl(bucket, bucketAcl);

    } catch (AmazonServiceException ase) {
        NewJFrame.jTextArea1.append("\n\nError: " + ase.getErrorMessage());
    }
}

From source file:com.epam.dlab.automation.cloud.aws.AmazonHelper.java

License:Apache License

public static void printBucketGrants(String bucketName) {
    LOGGER.info("Print grants for bucket {} on Amazon: ", bucketName);
    if (ConfigPropertyValue.isRunModeLocal()) {
        LOGGER.info("  action skipped for run in local mode");
        return;//from www .  j  a  va2  s.  c  o m
    }
    AWSCredentials credentials = getCredentials();
    AmazonS3 s3 = new AmazonS3Client(credentials);

    s3.setRegion(getRegion());
    AccessControlList acl = s3.getBucketAcl(bucketName);
    for (Grant grant : acl.getGrants()) {
        LOGGER.info(grant);
    }
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

/**
 * check if the param acl has the same owner than the parameter owner and
 * have almost one of the permission set in the parameter permissions
 * @param acl/*  w ww  .ja  va2s  . c  o m*/
 * @param owner
 * @param permissions almost one
 * @return
 */
private boolean hasPermissions(AccessControlList acl, Owner owner, EnumSet<Permission> permissions) {
    boolean result = false;
    for (Grant grant : acl.getGrants()) {
        if (grant.getGrantee().getIdentifier().equals(owner.getId())
                && permissions.contains(grant.getPermission())) {
            result = true;
            break;
        }
    }
    return result;
}

From source file:io.milton.s3.AmazonS3ManagerImpl.java

License:Open Source License

@Override
public boolean isPublicEntity(String bucketName, String keyName) {
    LOG.info("Gets the AccessControlList (ACL) for the specified object " + keyName
            + " in the specified bucket " + bucketName);

    final String GROUPS_USERS = "http://acs.amazonaws.com/groups/global/AllUsers";
    try {//from   w  w w  .  jav  a 2  s . c o  m
        AccessControlList accessControlList = amazonS3Client.getObjectAcl(bucketName, keyName);
        for (Iterator<Grant> iterator = accessControlList.getGrants().iterator(); iterator.hasNext();) {
            Grant grant = iterator.next();
            if (grant.getPermission().equals(Permission.Read)
                    && grant.getGrantee().getIdentifier().equals(GROUPS_USERS)) {
                return true;
            }
        }
    } catch (AmazonServiceException ase) {
        LOG.warn(ase.getMessage(), ase);
    } catch (AmazonClientException ace) {
        LOG.warn(ace.getMessage(), ace);
    }
    return false;
}

From source file:org.apache.manifoldcf.crawler.connectors.amazons3.AmazonS3Connector.java

License:Apache License

@Override
public void processDocuments(String[] documentIdentifiers, IExistingVersions statuses, Specification spec,
        IProcessActivity activities, int jobMode, boolean usesDefaultAuthority)
        throws ManifoldCFException, ServiceInterruption {
    AmazonS3 amazons3Client = getClient();
    if (amazons3Client == null)
        throw new ManifoldCFException("Amazon client can not connect at the moment");
    String[] acls = null;/*  w w w . j a v  a2s .  com*/

    // loop documents and process
    for (String documentIdentifier : documentIdentifiers) {
        try {
            if (documentIdentifier != null && StringUtils.isNotEmpty(documentIdentifier)) {
                String versionString;
                String[] aclsToUse;

                if (documentIdentifier.split(STD_SEPARATOR_BUCKET_AND_KEY) == null
                        && documentIdentifier.length() < 1) {
                    continue;
                }

                S3Artifact s3Artifact = getS3Artifact(documentIdentifier);
                S3Object s3Obj = amazons3Client
                        .getObject(new GetObjectRequest(s3Artifact.getBucketName(), s3Artifact.getKey()));

                if (s3Obj == null) {
                    // no such document in the bucket now
                    // delete document
                    activities.deleteDocument(documentIdentifier);
                    continue;
                }

                Logging.connectors.info("Content-Type: " + s3Obj.getObjectMetadata().getContentType());
                ObjectMetadata objectMetadata = s3Obj.getObjectMetadata();
                Date lastModified = objectMetadata.getLastModified();
                StringBuilder sb = new StringBuilder();
                if (lastModified == null) {
                    // remove the content
                    activities.deleteDocument(documentIdentifier);
                    continue;
                }

                aclsToUse = new String[0];

                AccessControlList objectAcl = amazons3Client.getObjectAcl(s3Artifact.getBucketName(),
                        s3Artifact.getKey());

                Set<Grant> grants = objectAcl.getGrants();
                String[] users = getUsers(grants);
                // sort

                aclsToUse = users;
                Arrays.sort(aclsToUse);
                packList(sb, aclsToUse, '+');
                if (aclsToUse.length > 0) {
                    sb.append('+');
                    pack(sb, AmazonS3Config.defaultAuthorityDenyToken, '+');
                } else
                    sb.append('-');

                //
                sb.append(lastModified.toString());
                versionString = sb.toString();

                Logging.connectors.debug("version string : " + versionString);

                if (versionString.length() > 0
                        && !activities.checkDocumentNeedsReindexing(documentIdentifier, versionString)) {
                    Logging.connectors.info("Document need not to be reindexed : " + documentIdentifier);
                    continue;
                }

                Logging.connectors.debug("JIRA: Processing document identifier '" + documentIdentifier + "'");

                long startTime = System.currentTimeMillis();
                String errorCode = null;
                String errorDesc = null;
                Long fileSize = null;

                try {
                    String mimeType = "text/plain";// default

                    // tika works starts
                    InputStream in = null;

                    String document = null;
                    try {
                        in = s3Obj.getObjectContent();

                        parser.parse(in, handler, metadata, context);
                        mimeType = tika.detect(in);
                        document = handler.toString();
                        if (document == null)
                            continue;
                        metadata.set(Metadata.CONTENT_TYPE, mimeType);
                    } catch (Exception e) {
                        Logging.connectors.error("Error while parsing tika contents", e);
                    } finally {
                        if (in != null)
                            IOUtils.closeQuietly(in);
                    }

                    String documentURI = getDocumentURI(s3Artifact);

                    Logging.connectors.debug("document : " + documentURI);

                    // need some investigation
                    if (!activities.checkURLIndexable(documentURI)) {
                        errorCode = activities.EXCLUDED_URL;
                        errorDesc = "Excluded because of URL ('" + documentURI + "')";
                        activities.noDocument(documentIdentifier, versionString);
                        continue;
                    }
                    if (!activities.checkMimeTypeIndexable(mimeType)) {
                        errorCode = activities.EXCLUDED_MIMETYPE;
                        errorDesc = "Excluded because of mime type ('" + mimeType + "')";
                        activities.noDocument(documentIdentifier, versionString);
                        continue;
                    }
                    if (!activities.checkDateIndexable(lastModified)) {
                        errorCode = activities.EXCLUDED_DATE;
                        errorDesc = "Excluded because of date (" + lastModified + ")";
                        activities.noDocument(documentIdentifier, versionString);
                        continue;
                    }

                    // otherwise process
                    RepositoryDocument rd = new RepositoryDocument();
                    // Turn into acls and add into
                    // description
                    String[] denyAclsToUse;
                    if (aclsToUse.length > 0)
                        denyAclsToUse = new String[] { AmazonS3Config.defaultAuthorityDenyToken };
                    else
                        denyAclsToUse = new String[0];
                    rd.setSecurity(RepositoryDocument.SECURITY_TYPE_DOCUMENT, aclsToUse, denyAclsToUse);

                    rd.setMimeType(mimeType);

                    if (lastModified != null)
                        rd.setModifiedDate(lastModified);

                    // set all meta-data fields
                    addAllMetaData(rd, metadata);

                    // get document

                    try {
                        byte[] documentBytes = document.getBytes(StandardCharsets.UTF_8);
                        long fileLength = documentBytes.length;

                        if (!activities.checkLengthIndexable(fileLength)) {
                            errorCode = activities.EXCLUDED_LENGTH;
                            errorDesc = "Excluded because of document length (" + fileLength + ")";
                            activities.noDocument(documentIdentifier, versionString);
                            continue;
                        }

                        InputStream is = new ByteArrayInputStream(documentBytes);
                        try {
                            rd.setBinary(is, fileLength);
                            activities.ingestDocumentWithException(documentIdentifier, versionString,
                                    documentURI, rd);

                            errorCode = "OK";
                            fileSize = new Long(fileLength);
                        } finally {
                            if (is != null)
                                IOUtils.closeQuietly(is);
                        }
                    } catch (Exception e) {
                        Logging.connectors.error(e);
                    }
                } catch (Exception e) {
                    Logging.connectors.error(e);
                }

            }
        } catch (AmazonServiceException e) {
            Logging.connectors.error(e);
        } catch (AmazonClientException e) {
            Logging.connectors.error(e);
        }

    }

}