org.nuxeo.liveconnect.importer.aws.S3Importer.java Source code

Java tutorial

Introduction

Here is the source code for org.nuxeo.liveconnect.importer.aws.S3Importer.java

Source

/*
 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Contributors:
 *     Michael Vachette
 */

package org.nuxeo.liveconnect.importer.aws;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.nuxeo.ecm.core.api.Blob;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.DocumentModelList;
import org.nuxeo.ecm.core.blob.BlobManager;
import org.nuxeo.ecm.liveconnect.core.LiveConnectBlobProvider;
import org.nuxeo.ecm.liveconnect.core.LiveConnectFileInfo;
import org.nuxeo.runtime.api.Framework;
import org.nuxeo.runtime.transaction.TransactionHelper;

import java.io.IOException;
import java.io.Serializable;

public class S3Importer {

    private static final Log log = LogFactory.getLog(S3Importer.class);

    String provider;

    public S3Importer(String provider) {
        this.provider = provider;
    }

    public void importBucket(DocumentModel rootFolder) {
        LiveconnectS3Blobprovider blobprovider = (LiveconnectS3Blobprovider) Framework.getService(BlobManager.class)
                .getBlobProvider(provider);

        AmazonS3 s3 = blobprovider.getClient();
        String bucketName = blobprovider.getBucketName();

        final ListObjectsRequest req = new ListObjectsRequest().withBucketName(bucketName);
        ObjectListing result;
        int docsCount = 0;
        do {
            result = s3.listObjects(req);
            for (S3ObjectSummary objectSummary : result.getObjectSummaries()) {
                String name = S3LiveConnectFile.getFilename(objectSummary.getKey());
                if (name == null)
                    continue;

                DocumentModel fileDoc = getOrCreateFileDocument(rootFolder, objectSummary.getKey(), name);

                //import object
                LiveConnectFileInfo info = new LiveConnectFileInfo(
                        rootFolder.getCoreSession().getPrincipal().getName(), objectSummary.getKey(),
                        objectSummary.getETag());

                LiveConnectBlobProvider blobProvider = (LiveConnectBlobProvider) Framework
                        .getService(BlobManager.class).getBlobProvider(provider);
                try {
                    Blob blob = blobProvider.toBlob(info);
                    fileDoc.setPropertyValue("file:content", (Serializable) blob);
                    fileDoc.getCoreSession().saveDocument(fileDoc);
                } catch (IOException e) {
                    log.warn("Couldn't get Blob with ID " + info.getFileId(), e);
                }
                docsCount++;
                if (docsCount % 10 == 0) {
                    rootFolder.getCoreSession().save();
                    if (TransactionHelper.isTransactionActive()) {
                        TransactionHelper.commitOrRollbackTransaction();
                        TransactionHelper.startTransaction();
                    }
                }
            }
            req.setMarker(result.getNextMarker());
        } while (result.isTruncated());

        rootFolder.getCoreSession().save();
        if (TransactionHelper.isTransactionActive()) {
            TransactionHelper.commitOrRollbackTransaction();
            TransactionHelper.startTransaction();
        }

    }

    protected DocumentModel getOrCreateFileDocument(DocumentModel folder, String key, String name) {
        CoreSession session = folder.getCoreSession();
        String query = String.format("Select * From File Where dc:source = '%s'", key);
        DocumentModelList list = session.query(query);
        if (list.size() > 0)
            return list.get(0);

        DocumentModel file = session.createDocumentModel(folder.getPathAsString(), name, "File");
        file.setPropertyValue("dc:title", name);
        file.setPropertyValue("dc:source", key);
        return session.createDocument(file);
    }

}