Search in sources :

Example 66 with DataStoreException

use of org.apache.jackrabbit.core.data.DataStoreException in project jackrabbit-oak by apache.

the class S3DataStoreFactory method asCloseable.

private static Closeable asCloseable(final CachingDataStore store, final File tempHomeDir) {
    return new Closeable() {

        @Override
        public void close() throws IOException {
            try {
                while (!store.getPendingUploads().isEmpty()) {
                    log.info("Waiting for following uploads to finish: " + store.getPendingUploads());
                    Thread.sleep(1000);
                }
                store.close();
                FileUtils.deleteDirectory(tempHomeDir);
            } catch (DataStoreException e) {
                throw new IOException(e);
            } catch (InterruptedException e) {
                throw new IOException(e);
            }
        }
    };
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) Closeable(java.io.Closeable) IOException(java.io.IOException)

Example 67 with DataStoreException

use of org.apache.jackrabbit.core.data.DataStoreException in project jackrabbit-oak by apache.

the class SafeDataStoreBlobStore method getStream.

@Override
protected InputStream getStream(String blobId) throws IOException {
    try {
        DataRecord record = getDataRecord(blobId);
        if (record == null) {
            log.warn("No blob found for id [{}]", blobId);
            return new ByteArrayInputStream(new byte[0]);
        }
        InputStream in = getDataRecord(blobId).getStream();
        if (!(in instanceof BufferedInputStream)) {
            in = new BufferedInputStream(in);
        }
        return StatsCollectingStreams.wrap(stats, blobId, in);
    } catch (DataStoreException e) {
        throw new IOException(e);
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) InMemoryDataRecord(org.apache.jackrabbit.oak.plugins.blob.datastore.InMemoryDataRecord) DataRecord(org.apache.jackrabbit.core.data.DataRecord) IOException(java.io.IOException)

Example 68 with DataStoreException

use of org.apache.jackrabbit.core.data.DataStoreException in project jackrabbit-oak by apache.

the class AbstractSharedCachingDataStore method addRecord.

@Override
public DataRecord addRecord(InputStream inputStream, BlobOptions blobOptions) throws DataStoreException {
    Stopwatch watch = Stopwatch.createStarted();
    try {
        TransientFileFactory fileFactory = TransientFileFactory.getInstance();
        File tmpFile = fileFactory.createTransientFile("upload", null, tmp);
        // Copy the stream to the temporary file and calculate the
        // stream length and the message digest of the stream
        MessageDigest digest = MessageDigest.getInstance(DIGEST);
        OutputStream output = new DigestOutputStream(new FileOutputStream(tmpFile), digest);
        long length = 0;
        try {
            length = IOUtils.copyLarge(inputStream, output);
        } finally {
            output.close();
        }
        DataIdentifier identifier = new DataIdentifier(encodeHexString(digest.digest()));
        LOG.debug("SHA-256 of [{}], length =[{}] took [{}] ms ", identifier, length, watch.elapsed(TimeUnit.MILLISECONDS));
        // otherwise add to backend
        if (blobOptions.getUpload() == SYNCHRONOUS || !cache.stage(identifier.toString(), tmpFile)) {
            backend.write(identifier, tmpFile);
            LOG.info("Added blob [{}] to backend", identifier);
            // offer to download cache
            cache.getDownloadCache().put(identifier.toString(), tmpFile);
        }
        return getRecordIfStored(identifier);
    } catch (Exception e) {
        LOG.error("Error in adding record");
        throw new DataStoreException("Error in adding record ", e);
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) DataIdentifier(org.apache.jackrabbit.core.data.DataIdentifier) TransientFileFactory(org.apache.jackrabbit.util.TransientFileFactory) DigestOutputStream(java.security.DigestOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DigestOutputStream(java.security.DigestOutputStream) FileOutputStream(java.io.FileOutputStream) Stopwatch(com.google.common.base.Stopwatch) MessageDigest(java.security.MessageDigest) File(java.io.File) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException)

Example 69 with DataStoreException

use of org.apache.jackrabbit.core.data.DataStoreException in project jackrabbit-oak by apache.

the class S3Backend method write.

/**
     * It uploads file to Amazon S3. If file size is greater than 5MB, this
     * method uses parallel concurrent connections to upload.
     */
@Override
public void write(DataIdentifier identifier, File file) throws DataStoreException {
    String key = getKeyName(identifier);
    ObjectMetadata objectMetaData = null;
    long start = System.currentTimeMillis();
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        // check if the same record already exists
        try {
            objectMetaData = s3service.getObjectMetadata(bucket, key);
        } catch (AmazonServiceException ase) {
            if (!(ase.getStatusCode() == 404 || ase.getStatusCode() == 403)) {
                throw ase;
            }
        }
        if (objectMetaData != null) {
            long l = objectMetaData.getContentLength();
            if (l != file.length()) {
                throw new DataStoreException("Collision: " + key + " new length: " + file.length() + " old length: " + l);
            }
            LOG.debug("[{}]'s exists, lastmodified = [{}]", key, objectMetaData.getLastModified().getTime());
            CopyObjectRequest copReq = new CopyObjectRequest(bucket, key, bucket, key);
            copReq.setNewObjectMetadata(objectMetaData);
            Copy copy = tmx.copy(s3ReqDecorator.decorate(copReq));
            try {
                copy.waitForCopyResult();
                LOG.debug("lastModified of [{}] updated successfully.", identifier);
            } catch (Exception e2) {
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
        if (objectMetaData == null) {
            try {
                // start multipart parallel upload using amazon sdk
                Upload up = tmx.upload(s3ReqDecorator.decorate(new PutObjectRequest(bucket, key, file)));
                // wait for upload to finish
                up.waitForUploadResult();
                LOG.debug("synchronous upload to identifier [{}] completed.", identifier);
            } catch (Exception e2) {
                throw new DataStoreException("Could not upload " + key, e2);
            }
        }
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
    LOG.debug("write of [{}], length=[{}], in [{}]ms", identifier, file.length(), (System.currentTimeMillis() - start));
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) CopyObjectRequest(com.amazonaws.services.s3.model.CopyObjectRequest) Copy(com.amazonaws.services.s3.transfer.Copy) AmazonServiceException(com.amazonaws.AmazonServiceException) Upload(com.amazonaws.services.s3.transfer.Upload) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) AmazonServiceException(com.amazonaws.AmazonServiceException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) AmazonClientException(com.amazonaws.AmazonClientException) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 70 with DataStoreException

use of org.apache.jackrabbit.core.data.DataStoreException in project jackrabbit-oak by apache.

the class S3Backend method read.

@Override
public InputStream read(DataIdentifier identifier) throws DataStoreException {
    long start = System.currentTimeMillis();
    String key = getKeyName(identifier);
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
        S3Object object = s3service.getObject(bucket, key);
        InputStream in = object.getObjectContent();
        LOG.debug("[{}] read took [{}]ms", identifier, (System.currentTimeMillis() - start));
        return in;
    } catch (AmazonServiceException e) {
        throw new DataStoreException("Object not found: " + key, e);
    } finally {
        if (contextClassLoader != null) {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }
    }
}
Also used : DataStoreException(org.apache.jackrabbit.core.data.DataStoreException) InputStream(java.io.InputStream) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object)

Aggregations

DataStoreException (org.apache.jackrabbit.core.data.DataStoreException)85 IOException (java.io.IOException)35 AmazonServiceException (com.amazonaws.AmazonServiceException)28 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)18 DataIdentifier (org.apache.jackrabbit.core.data.DataIdentifier)15 File (java.io.File)14 AmazonClientException (com.amazonaws.AmazonClientException)12 StorageException (com.microsoft.azure.storage.StorageException)11 InputStream (java.io.InputStream)9 URISyntaxException (java.net.URISyntaxException)9 RepositoryException (javax.jcr.RepositoryException)9 CopyObjectRequest (com.amazonaws.services.s3.model.CopyObjectRequest)7 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)7 Copy (com.amazonaws.services.s3.transfer.Copy)7 Upload (com.amazonaws.services.s3.transfer.Upload)7 FileObject (org.apache.commons.vfs2.FileObject)7 FileSystemException (org.apache.commons.vfs2.FileSystemException)7 BufferedInputStream (java.io.BufferedInputStream)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 S3Object (com.amazonaws.services.s3.model.S3Object)5