Example usage for com.amazonaws.util StringUtils isNullOrEmpty

List of usage examples for com.amazonaws.util StringUtils isNullOrEmpty

Introduction

In this page you can find the example usage for com.amazonaws.util StringUtils isNullOrEmpty.

Prototype

public static boolean isNullOrEmpty(String value) 

Source Link

Usage

From source file:nz.net.orcon.kanban.controllers.BoardController.java

License:Open Source License

public Map<String, CardEvent> getHistoryEvent(ObjectContentManager ocm, String boardId, String phaseId,
        String cardId, String category, String detail) {

    QueryManager qm = ocm.getQueryManager();
    org.apache.jackrabbit.ocm.query.Filter qmFilter = qm.createFilter(CardEvent.class);

    logger.info("Setting Scope: " + boardId + "," + phaseId + "," + cardId);

    String scope = String.format(URI.HISTORY_URI, boardId, phaseId, IdentifierTools.escapeNumber(cardId), "/");

    logger.info("Setting Scope: " + scope);

    qmFilter.setScope(scope);//  w  w w. jav  a  2  s . co m

    String categoryFilter = "";
    String detailFilter = "";

    if (category != null) {
        categoryFilter = "(@category='${category}')".replaceAll("\\$\\{category\\}", category);
    }

    if (detail != null) {
        detailFilter = "(jcr:contains(@detail,'${detail}'))".replaceAll("\\$\\{detail\\}", detail);
    }

    boolean categoryPresent = !StringUtils.isNullOrEmpty(categoryFilter);
    boolean detailPresent = !StringUtils.isNullOrEmpty(detailFilter);

    if (categoryPresent && detailPresent) {
        qmFilter.addJCRExpression(categoryFilter + " or " + detailFilter);
    } else if (categoryPresent && !detailPresent) {
        qmFilter.addJCRExpression(categoryFilter);
    } else if (!categoryPresent && detailPresent) {
        qmFilter.addJCRExpression(detailFilter);
    }

    logger.info("Running Query: " + qmFilter.getScope() + " - " + qmFilter.toString());

    Query query = qm.createQuery(qmFilter);
    Iterator<CardEvent> objectIterator = ocm.getObjectIterator(query);
    Map<String, CardEvent> list = new HashMap<String, CardEvent>();

    while (objectIterator.hasNext()) {
        CardEvent cardEvent = objectIterator.next();
        list.put(cardEvent.getCard(), cardEvent);
    }

    return list;
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.S3Backend.java

License:Apache License

public void init(CachingDataStore store, String homeDir, Properties prop) throws DataStoreException {

    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {/*from   w w w.j  a v a 2s. co  m*/
        startTime = new Date();
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        LOG.debug("init");
        this.store = store;
        s3ReqDecorator = new S3RequestDecorator(prop);
        s3service = Utils.openService(prop);
        if (bucket == null || "".equals(bucket.trim())) {
            bucket = prop.getProperty(S3Constants.S3_BUCKET);
        }
        String region = prop.getProperty(S3Constants.S3_REGION);
        Region s3Region = null;
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region ec2Region = Regions.getCurrentRegion();
            if (ec2Region != null) {
                s3Region = Region.fromValue(ec2Region.getName());
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION
                        + "] not configured and cannot be derived from environment");
            }
        } else {
            if (Utils.DEFAULT_AWS_BUCKET_REGION.equals(region)) {
                s3Region = Region.US_Standard;
            } else if (Region.EU_Ireland.toString().equals(region)) {
                s3Region = Region.EU_Ireland;
            } else {
                s3Region = Region.fromValue(region);
            }
        }

        if (!s3service.doesBucketExist(bucket)) {
            s3service.createBucket(bucket, s3Region);
            LOG.info("Created bucket [{}] in [{}] ", bucket, region);
        } else {
            LOG.info("Using bucket [{}] in [{}] ", bucket, region);
        }

        int writeThreads = 10;
        String writeThreadsStr = prop.getProperty(S3Constants.S3_WRITE_THREADS);
        if (writeThreadsStr != null) {
            writeThreads = Integer.parseInt(writeThreadsStr);
        }
        LOG.info("Using thread pool of [{}] threads in S3 transfer manager.", writeThreads);
        tmx = new TransferManager(s3service, (ThreadPoolExecutor) Executors.newFixedThreadPool(writeThreads,
                new NamedThreadFactory("s3-transfer-manager-worker")));

        int asyncWritePoolSize = 10;
        String maxConnsStr = prop.getProperty(S3Constants.S3_MAX_CONNS);
        if (maxConnsStr != null) {
            asyncWritePoolSize = Integer.parseInt(maxConnsStr) - writeThreads;
        }

        asyncWriteExecuter = (ThreadPoolExecutor) Executors.newFixedThreadPool(asyncWritePoolSize,
                new NamedThreadFactory("s3-write-worker"));
        String renameKeyProp = prop.getProperty(S3Constants.S3_RENAME_KEYS);
        boolean renameKeyBool = (renameKeyProp == null || "".equals(renameKeyProp)) ? false
                : Boolean.parseBoolean(renameKeyProp);
        LOG.info("Rename keys [{}]", renameKeyBool);
        if (renameKeyBool) {
            renameKeys();
        }
        LOG.debug("S3 Backend initialized in [{}] ms", +(System.currentTimeMillis() - startTime.getTime()));
    } catch (Exception e) {
        LOG.debug("  error ", e);
        Map<String, String> filteredMap = Maps.newHashMap();
        if (prop != null) {
            filteredMap = Maps.filterKeys(Maps.fromProperties(prop), new Predicate<String>() {
                @Override
                public boolean apply(String input) {
                    return !input.equals(S3Constants.ACCESS_KEY) && !input.equals(S3Constants.SECRET_KEY);
                }
            });
        }
        throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}

From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.Utils.java

License:Apache License

/**
 * Create AmazonS3Client from properties.
 * /*from  w ww  .ja v a 2  s.c  o  m*/
 * @param prop properties to configure @link {@link AmazonS3Client}
 * @return {@link AmazonS3Client}
 */
public static AmazonS3Client openService(final Properties prop) {
    String accessKey = prop.getProperty(S3Constants.ACCESS_KEY);
    String secretKey = prop.getProperty(S3Constants.SECRET_KEY);
    AmazonS3Client s3service = null;
    if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey)) {
        LOG.info("Configuring Amazon Client from environment");
        s3service = new AmazonS3Client(getClientConfiguration(prop));
    } else {
        LOG.info("Configuring Amazon Client from property file.");
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        s3service = new AmazonS3Client(credentials, getClientConfiguration(prop));
    }
    String region = prop.getProperty(S3Constants.S3_REGION);
    String endpoint = null;
    String propEndPoint = prop.getProperty(S3Constants.S3_END_POINT);
    if ((propEndPoint != null) & !"".equals(propEndPoint)) {
        endpoint = propEndPoint;
    } else {
        if (StringUtils.isNullOrEmpty(region)) {
            com.amazonaws.regions.Region s3Region = Regions.getCurrentRegion();
            if (s3Region != null) {
                region = s3Region.getName();
            } else {
                throw new AmazonClientException("parameter [" + S3Constants.S3_REGION
                        + "] not configured and cannot be derived from environment");
            }
        }
        if (DEFAULT_AWS_BUCKET_REGION.equals(region)) {
            endpoint = S3 + DOT + AWSDOTCOM;
        } else if (Region.EU_Ireland.toString().equals(region)) {
            endpoint = "s3-eu-west-1" + DOT + AWSDOTCOM;
        } else {
            endpoint = S3 + DASH + region + DOT + AWSDOTCOM;
        }
    }
    /*
     * setting endpoint to remove latency of redirection. If endpoint is
     * not set, invocation first goes us standard region, which
     * redirects it to correct location.
     */
    s3service.setEndpoint(endpoint);
    LOG.info("S3 service endpoint [{}] ", endpoint);
    return s3service;
}

From source file:org.apache.james.blob.objectstorage.crypto.CryptoConfigBuilder.java

License:Apache License

public CryptoConfig build() {
    Preconditions.checkState(!StringUtils.isNullOrEmpty(salt));
    Preconditions.checkState(password != null && password.length > 0);
    return new CryptoConfig(Hex.encode(Hex.decode(salt)), password);
}

From source file:org.rdswitchboard.harvesters.pmh.Harvester.java

License:Open Source License

/**
 * Harvester constructor/*from   w ww . jav a  2 s  . com*/
 * 
 * @param repoUrl : The Repository URL
 * @param folderBase : The address of the folder, there received data must be saved.
 * @throws JAXBException 
 * @throws IOException 
 * @throws Exception 
 */
public Harvester(final Properties properties) throws Exception {
    repoUrl = properties.getProperty("url");
    if (StringUtils.isNullOrEmpty(repoUrl))
        throw new IllegalArgumentException("The OAI:PMH Repository URL can not be empty");

    repoPrefix = properties.getProperty("name");

    if (StringUtils.isNullOrEmpty(repoPrefix))
        throw new IllegalArgumentException("The OAI:PMH Repository Prefix can not be empty");

    metadataPrefix = properties.getProperty("metadata");

    String accessKey = properties.getProperty("aws.access.key");
    String secretKey = properties.getProperty("aws.secret.key");
    if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey))
        s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());
    else
        s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));

    bucketName = properties.getProperty("s3.bucket");
    folderName = properties.getProperty("folder");

    if (StringUtils.isNullOrEmpty(bucketName) && StringUtils.isNullOrEmpty(folderName))
        throw new IllegalArgumentException(
                "Please enter either local folder name or AWS S3 Bucket name to store the harvested files");
    if (!StringUtils.isNullOrEmpty(bucketName) && !StringUtils.isNullOrEmpty(folderName))
        throw new IllegalArgumentException(
                "S3 bucket and local folder parameters can not be used at the same time. Please disable one in the configuration file.");

    try {
        File fileBlackList = new File(properties.getProperty("black.list"));
        if (fileBlackList.isFile()) {
            List<String> list = FileUtils.readLines(fileBlackList);
            blackList = new HashSet<String>();
            for (String l : list) {
                String s = l.trim();
                if (!s.isEmpty())
                    blackList.add(s);
            }
        }
    } catch (Exception e) {
        blackList = null;
    }

    try {
        File fileWhiteList = new File(properties.getProperty("white.list"));
        if (fileWhiteList.isFile()) {
            List<String> list = FileUtils.readLines(fileWhiteList);
            whiteList = new HashSet<String>();
            for (String l : list) {
                String s = l.trim();
                if (!s.isEmpty())
                    whiteList.add(s);
            }
        }
    } catch (Exception e) {
        whiteList = null;
    }

    if (null != blackList && !blackList.isEmpty() && null != whiteList && !whiteList.isEmpty())
        throw new Exception(
                "The black and the withe list parameters can not be set at the same time. Please disable one in the configuration file.");

    connectionTimeout = Integer.parseInt(properties.getProperty("conn.timeout", "0"));
    readTimeout = Integer.parseInt(properties.getProperty("read.timeout", "0"));
    maxAttempts = Integer.parseInt(properties.getProperty("max.attempts", "0"));
    attemptDelay = Integer.parseInt(properties.getProperty("attempt.delay", "0"));
    failOnError = Boolean.parseBoolean(properties.getProperty("fail.on.error", "true"));

}

From source file:org.rdswitchboard.harvesters.pmh.Harvester.java

License:Open Source License

public void downloadRecords(SetStatus set) throws HarvesterException, UnsupportedEncodingException, IOException,
        InterruptedException, XPathExpressionException, SAXException, ParserConfigurationException {

    // Generate the URL of request
    String url = null;//  ww w.ja v  a2s.com
    ;
    if (set.hasToken()) {
        try {
            url = repoUrl + String.format(URL_LIST_RECORDS_RESUMPTION_TOKEN,
                    URLEncoder.encode(set.getToken(), "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
    if (null == url) {
        if (!set.hasName())
            url = repoUrl + String.format(URL_LIST_DEFAULT_RECORDS, metadataPrefix);
        else
            url = repoUrl + String.format(URL_LIST_RECORDS, URLEncoder.encode(set.getName(), "UTF-8"),
                    metadataPrefix);
    }

    System.out.println("Downloading records: " + url);

    String xml = null;

    // Get XML document 
    URLConnection conn = new URL(url).openConnection();
    if (connectionTimeout > 0)
        conn.setConnectTimeout(connectionTimeout);
    if (readTimeout > 0)
        conn.setReadTimeout(readTimeout);
    try (InputStream is = conn.getInputStream()) {
        if (null != is)
            xml = IOUtils.toString(is, StandardCharsets.UTF_8.name());
    }

    // Check if xml has been returned and check what it had a valid root element
    if (null == xml)
        throw new HarvesterException("The XML document is empty");
    Document doc = dbf.newDocumentBuilder().parse(new InputSource(new StringReader(xml)));

    // Extract root node
    Node root = (Node) XPATH_OAI_PMH.evaluate(doc, XPathConstants.NODE);
    if (null == root)
        throw new HarvesterException("The document is not an OAI:PMH file");

    // Check for error node
    Node error = (Node) XPATH_ERROR.evaluate(root, XPathConstants.NODE);
    if (null != error && error instanceof Element) {
        String code = ((Element) error).getAttribute("code");
        String message = ((Element) error).getTextContent();

        if (ERR_NO_RECORDS_MATCH.equals(code)) {
            System.out.println("Error: The set is empty");

            set.setFiles(0);
            set.resetToken();

            return;
        } else
            throw new HarvesterException(code, message);
    }

    Node nodeToken = (Node) XPATH_RECORDS_RESUMPTION_TOKEN.evaluate(root, XPathConstants.NODE);

    if (null != nodeToken && nodeToken instanceof Element) {
        String tokenString = ((Element) nodeToken).getTextContent();
        if (null != tokenString && !tokenString.isEmpty())
            set.setToken(tokenString);
        else
            set.resetToken();

        set.setCursor(((Element) nodeToken).getAttribute("cursor"));
        set.setSize(((Element) nodeToken).getAttribute("completeListSize"));

        set.dumpToken(System.out);
    } else
        set.resetToken();

    String filePath = repoPrefix + "/" + metadataPrefix + "/" + harvestDate + "/" + set.getNameSafe() + "/"
            + set.getFiles() + ".xml";

    if (StringUtils.isNullOrEmpty(bucketName)) {

        FileUtils.writeStringToFile(new File(folderName, filePath), xml);

    } else {
        byte[] bytes = xml.getBytes(StandardCharsets.UTF_8);

        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentEncoding(StandardCharsets.UTF_8.name());
        metadata.setContentType("text/xml");
        metadata.setContentLength(bytes.length);

        InputStream inputStream = new ByteArrayInputStream(bytes);

        PutObjectRequest request = new PutObjectRequest(bucketName, filePath, inputStream, metadata);

        s3client.putObject(request);
    }

    set.incFiles();
}

From source file:org.rdswitchboard.harvesters.pmh.Harvester.java

License:Open Source License

/**
 * Alternative function to organize the harvest process. The difference with another function
 * is in data storage. The harvest2 function will store files in the raw format as they come
 * from the server./*ww w  .j  a v  a  2s.  c o  m*/
 * The harvesting method should never be mixed. The harvesting folder must be wiped out if 
 * switching to this method, or function will fail.
 * @param prefix A metadata prefix
 * @throws Exception
 */
public boolean harvest() throws Exception {
    if (StringUtils.isNullOrEmpty(metadataPrefix))
        throw new IllegalArgumentException("The OAI:PMH Metadata Prefix can not be empty");

    System.out.println("Downloading set list");

    boolean result = false;

    if (null == whiteList || whiteList.isEmpty()) {

        System.out.println(
                "There is no whitelist found. Proceeding with downloading the list of all available sets.");

        // download all sets in the repository
        Map<String, String> mapSets = listSets();

        if (null == mapSets || mapSets.isEmpty()) {
            System.out.println("Processing default set");

            result = harvestSet(new SetStatus(null, "Default"));
        } else {

            result = false;

            for (Map.Entry<String, String> entry : mapSets.entrySet()) {

                SetStatus set = new SetStatus(entry.getKey().trim(),
                        URLDecoder.decode(entry.getValue(), StandardCharsets.UTF_8.name()));

                // if black list exists and item is blacklisted, continue
                if (null != blackList && blackList.contains(set)) {
                    set.setFiles(-2);
                    saveSetStats(set); // set was ignored
                    continue;
                }

                System.out.println("Processing set: "
                        + URLDecoder.decode(entry.getValue(), StandardCharsets.UTF_8.name()));

                if (!harvestSet(set)) {
                    System.err.println(
                            "The harvesting job has been aborted due to an error. If you want harvesting to be continued, please set option 'fail.on.error' to 'false' in the configuration file");
                    result = false;
                    break;
                } else
                    result = true;
            }

        }
    } else {
        for (String item : whiteList) {
            if (!harvestSet(new SetStatus(item, item))) {
                System.err.println(
                        "The harvesting job has been aborted due to an error. If you want harvesting to be continued, please set option 'fail.on.error' to 'false' in the configuration file");
                result = false;
                break;
            } else
                result = true;
        }

    }
    if (result) {
        String filePath = repoPrefix + "/" + metadataPrefix + "/latest.txt";

        if (StringUtils.isNullOrEmpty(bucketName)) {

            FileUtils.writeStringToFile(new File(folderName, filePath), harvestDate);

        } else {

            byte[] bytes = harvestDate.getBytes(StandardCharsets.UTF_8);

            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentEncoding(StandardCharsets.UTF_8.name());
            metadata.setContentType("text/plain");
            metadata.setContentLength(bytes.length);

            InputStream inputStream = new ByteArrayInputStream(bytes);

            PutObjectRequest request = new PutObjectRequest(bucketName, filePath, inputStream, metadata);

            s3client.putObject(request);
        }
    }

    return result;
}

From source file:org.rdswitchboard.importers.browser.s3.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*from w w  w .  j a  v a2  s  .c  o m*/
        if (args.length == 0 || StringUtils.isNullOrEmpty(args[0]))
            throw new Exception("Please provide properties file");

        String propertiesFile = args[0];
        Properties properties = new Properties();
        try (InputStream in = new FileInputStream(propertiesFile)) {
            properties.load(in);
        }

        String source = properties.getProperty("data.source.id");

        if (StringUtils.isNullOrEmpty(source))
            throw new IllegalArgumentException("Source can not be empty");

        System.out.println("Source: " + source);

        String baseUrl = properties.getProperty("base.url");

        if (StringUtils.isNullOrEmpty(baseUrl))
            throw new IllegalArgumentException("Base URL can not be empty");

        System.out.println("Base URL: " + baseUrl);

        String sessionId = properties.getProperty("session.id");

        if (StringUtils.isNullOrEmpty(sessionId))
            throw new IllegalArgumentException("Session Id can not be empty");

        System.out.println("Session Id: " + sessionId);

        String accessKey = properties.getProperty("aws.access.key");
        String secretKey = properties.getProperty("aws.secret.key");

        String bucket = properties.getProperty("s3.bucket");

        if (StringUtils.isNullOrEmpty(bucket))
            throw new IllegalArgumentException("AWS S3 Bucket can not be empty");

        System.out.println("S3 Bucket: " + bucket);

        String prefix = properties.getProperty("s3.prefix");

        if (StringUtils.isNullOrEmpty(prefix))
            throw new IllegalArgumentException("AWS S3 Prefix can not be empty");

        System.out.println("S3 Prefix: " + prefix);

        String crosswalk = properties.getProperty("crosswalk");
        Templates template = null;

        if (!StringUtils.isNullOrEmpty(crosswalk)) {
            System.out.println("Crosswalk: " + crosswalk);

            template = TransformerFactory.newInstance()
                    .newTemplates(new StreamSource(new FileInputStream(crosswalk)));
        }

        ObjectMapper mapper = new ObjectMapper();

        Client client = Client.create();
        Cookie cookie = new Cookie("PHPSESSID", properties.getProperty("session"));

        AmazonS3 s3client;

        if (!StringUtils.isNullOrEmpty(accessKey) && !StringUtils.isNullOrEmpty(secretKey)) {
            System.out.println(
                    "Connecting to AWS via Access and Secret Keys. This is not safe practice, consider to use IAM Role instead.");

            AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
            s3client = new AmazonS3Client(awsCredentials);
        } else {
            System.out.println("Connecting to AWS via Instance Profile Credentials");

            s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());
        }

        //String file = "rda/rif/class:collection/54800.xml";

        ListObjectsRequest listObjectsRequest;
        ObjectListing objectListing;

        String file = prefix + "/latest.txt";
        S3Object object = s3client.getObject(new GetObjectRequest(bucket, file));

        String latest;
        try (InputStream txt = object.getObjectContent()) {
            latest = prefix + "/" + IOUtils.toString(txt, StandardCharsets.UTF_8).trim() + "/";
        }

        System.out.println("S3 Repository: " + latest);

        listObjectsRequest = new ListObjectsRequest().withBucketName(bucket).withPrefix(latest);
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {

                file = objectSummary.getKey();

                System.out.println("Processing file: " + file);

                object = s3client.getObject(new GetObjectRequest(bucket, file));
                String xml = null;

                if (null != template) {
                    Source reader = new StreamSource(object.getObjectContent());
                    StringWriter writer = new StringWriter();

                    Transformer transformer = template.newTransformer();
                    transformer.transform(reader, new StreamResult(writer));

                    xml = writer.toString();
                } else {
                    InputStream is = object.getObjectContent();

                    xml = IOUtils.toString(is, ENCODING);
                }

                URL url = new URL(baseUrl + "/registry/import/import_s3/");

                StringBuilder sb = new StringBuilder();
                addParam(sb, "id", source);
                addParam(sb, "xml", xml);

                //System.out.println(sb.toString());

                WebResource webResource = client.resource(url.toString());
                ClientResponse response = webResource
                        .header("User-Agent",
                                "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0")
                        .accept(MediaType.APPLICATION_JSON, "*/*").acceptLanguage("en-US", "en")
                        .type(MediaType.APPLICATION_FORM_URLENCODED).cookie(cookie)
                        .post(ClientResponse.class, sb.toString());

                if (response.getStatus() != 200) {
                    throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
                }

                String output = response.getEntity(String.class);

                Result result = mapper.readValue(output, Result.class);

                if (!result.getStatus().equals("OK")) {
                    System.err.println(result.getMessage());

                    break;
                } else
                    System.out.println(result.getMessage());
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.rdswitchboard.tests.crosswalk.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*from  w  ww.j a va  2  s.  c o  m*/
        String propertiesFile = PROPERTIES_FILE;

        if (args.length != 0 && !StringUtils.isNullOrEmpty(args[0]))
            propertiesFile = args[0];

        Properties properties = new Properties();
        try (InputStream in = new FileInputStream(propertiesFile)) {
            properties.load(in);
        }

        String accessKey = properties.getProperty("aws.access.key");
        String secretKey = properties.getProperty("aws.secret.key");

        String bucket = properties.getProperty("s3.bucket");

        if (StringUtils.isNullOrEmpty(bucket))
            throw new IllegalArgumentException("AWS S3 Bucket can not be empty");

        System.out.println("S3 Bucket: " + bucket);

        String key = properties.getProperty("s3.key");

        if (StringUtils.isNullOrEmpty(key))
            throw new IllegalArgumentException("AWS S3 Key can not be empty");

        System.out.println("S3 Key: " + key);

        String crosswalk = properties.getProperty("crosswalk");
        if (StringUtils.isNullOrEmpty(crosswalk))
            throw new IllegalArgumentException("Crosswalk can not be empty");

        System.out.println("Crosswalk: " + crosswalk);

        String outFileName = properties.getProperty("out", OUT_FILE_NAME);
        System.out.println("Out: " + outFileName);

        AmazonS3 s3client;
        if (!StringUtils.isNullOrEmpty(accessKey) && !StringUtils.isNullOrEmpty(secretKey)) {
            System.out.println(
                    "Connecting to AWS via Access and Secret Keys. This is not safe practice, consider to use IAM Role instead.");

            AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
            s3client = new AmazonS3Client(awsCredentials);
        } else {
            System.out.println("Connecting to AWS via Instance Profile Credentials");

            s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());
        }

        S3Object object = s3client.getObject(new GetObjectRequest(bucket, key));

        Templates template = TransformerFactory.newInstance()
                .newTemplates(new StreamSource(new FileInputStream(crosswalk)));
        StreamSource reader = new StreamSource(object.getObjectContent());
        StreamResult result = (StringUtils.isNullOrEmpty(outFileName) || outFileName.equals("stdout"))
                ? new StreamResult(System.out)
                : new StreamResult(new FileOutputStream(outFileName));

        Transformer transformer = template.newTransformer();
        transformer.transform(reader, result);

        /*
             DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
          TransformerFactory tFactory = TransformerFactory.newInstance();
          XPath xPath = XPathFactory.newInstance().newXPath();
                  
          DocumentBuilder builder = dFactory.newDocumentBuilder();
        Document document = builder.parse(object.getObjectContent());
        Transformer transformer1 = tFactory.newTemplates(
         new StreamSource(new FileInputStream(crosswalk))).newTransformer(); 
        Transformer transformer2 = tFactory.newTransformer();
                
        NodeList metadata = (NodeList)xPath.evaluate("/OAI-PMH/ListRecords/record/metadata",
              document.getDocumentElement(), XPathConstants.NODESET);
                
        for (int i = 0; i < metadata.getLength(); ++i) {
           System.out.println("Converting node: " + i);
                   
            Element e = (Element) metadata.item(i);
            Node mets = e.getElementsByTagName("mets").item(0);
            Node rifcs = document.createElement("registryObjects");
                    
           DOMSource xmlSource = new DOMSource(mets);
            DOMResult xmlResult = new DOMResult(rifcs);
                    
            transformer1.transform(xmlSource, xmlResult);
                
            e.removeChild(mets);
            e.appendChild(xmlResult.getNode());
                    
        //    e.replaceChild(rifcs, xmlResult.getNode());             
        }
                  
        StreamResult result = (StringUtils.isNullOrEmpty(outFileName) || outFileName.equals("stdout")) 
              ? new StreamResult(System.out)
              : new StreamResult(new FileOutputStream(outFileName));
                  
                
        transformer2.transform(new DOMSource(document), result);
                
                 
         */

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.rdswitchboard.utils.s3.find.App.java

License:Open Source License

public static void main(String[] args) {
    try {/*  w w w  .j  ava  2s .c  o m*/
        if (args.length != 2)
            throw new IllegalArgumentException("Bucket name and search string can not be empty");

        String buckey = args[0];
        String search = args[1];
        String prefix = null;
        int pos = buckey.indexOf('/');
        if (pos > 0) {
            prefix = buckey.substring(pos + 1);
            buckey = buckey.substring(0, pos);
        }

        AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());

        //         AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());        

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(buckey);

        if (!StringUtils.isNullOrEmpty(prefix))
            listObjectsRequest.setPrefix(prefix);

        ObjectListing objectListing;

        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                String key = objectSummary.getKey();
                System.out.println(" - " + key);

                S3Object object = s3client.getObject(new GetObjectRequest(buckey, key));
                String str = IOUtils.toString(object.getObjectContent());
                if (str.contains(search)) {
                    System.out.println("Found!");

                    FileUtils.writeStringToFile(new File("s3/" + key), str);
                }
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (Exception e) {
        e.printStackTrace();
    }
}