Package com.kolich.havalo.entities.types

Examples of com.kolich.havalo.entities.types.HashedFileObject


    final String key, final boolean failIfNotFound) {
    try {
      return new ReentrantReadWriteEntityLock<HashedFileObject>(repo) {
        @Override
        public HashedFileObject transaction() throws Exception {
          HashedFileObject hfo = repo.getObject(key);
          if(hfo == null && !failIfNotFound) {
            hfo = new HashedFileObject(key);
            // Add the object to repository.
            repo.addObject(key, hfo);
          } else if(hfo == null && failIfNotFound) {
            // The object with the given key did not exist in
            // this repository.
View Full Code Here


    try {
      return new ReentrantReadWriteEntityLock<HashedFileObject>(repo) {
        @Override
        public HashedFileObject transaction() throws Exception {
          // Get the object we're going to delete.
          final HashedFileObject hfo = repo.getObject(key);
          // Make sure the object exists -- if there was no object
          // with the given key in the repo then it does not exist.
          if(hfo == null) {
            throw new ObjectNotFoundException("Object " +
              "not found (id=" + repo.getRepoId() + ", key=" +
                  key + ")");
          }
          // Now attempt to grab an exclusive write lock on the
          // file object do delete.  And, attempt to follow through
          // on the physical delete from the platters.
          return new ReentrantReadWriteEntityLock<HashedFileObject>(hfo) {
            @Override
            public HashedFileObject transaction() throws Exception {
              final String eTag = hfo.getFirstHeader(ETAG);
              // If we have an incoming If-Match, we need to
              // compare that against the current HFO before we
              // attempt to delete.  If the If-Match ETag does not
              // match, fail.
              if(ifMatch != null && eTag != null) {
                // OK, we have an incoming If-Match ETag, use it.
                if(!ifMatch.equals(eTag)) {
                  throw new ObjectConflictException("Failed " +
                    "to delete HFO; incoming If-Match " +
                    "ETag does not match (hfo=" +
                    hfo.getName() + ", etag=" +
                    eTag + ", if-match=" + ifMatch + ")");
                }
              }
              // OK, we either didn't have an incoming If-Match
              // to check, or we did and it passed -- grab a
View Full Code Here

                           final AsyncContext context) throws Exception {
        final Repository repo = getRepository(userKp.getKey());
        new ReentrantReadWriteEntityLock<Void>(repo) {
            @Override
            public Void transaction() throws Exception {
                final HashedFileObject hfo = getHashedFileObject(repo,
                    // The URL-decoded key of the object to delete.
                    key,
                    // Fail if not found.
                    true);
                new ReentrantReadWriteEntityLock<HashedFileObject>(hfo) {
View Full Code Here

                          final AsyncContext context) throws Exception {
        final Repository repo = getRepository(userKp.getKey());
        new ReentrantReadWriteEntityLock<Void>(repo) {
            @Override
            public Void transaction() throws Exception {
                final HashedFileObject hfo = getHashedFileObject(repo,
                    // The URL-decoded key of the object to delete.
                    key,
                    // Fail if not found.
                    true);
                new ReentrantReadWriteEntityLock<HashedFileObject>(hfo) {
View Full Code Here

                    throw new ObjectTooLargeException("The '" +
                        CONTENT_LENGTH + "' of the incoming request " +
                        "is too large. Max upload size allowed is " +
                        uploadMaxSize_ + "-bytes.");
                }
                final HashedFileObject hfo = getHashedFileObject(repo, key);
                return new ReentrantReadWriteEntityLock<HashedFileObject>(hfo) {
                    @Override
                    public HashedFileObject transaction() throws Exception {
                        final String eTag = hfo.getFirstHeader(ETAG);
                        // If we have an incoming If-Match, we need to compare
                        // that against the current HFO before we attempt to
                        // update.  If the If-Match ETag does not match, fail.
                        if(ifMatch != null && eTag != null) {
                            // OK, we have an incoming If-Match ETag, use it.
                            // NOTE: HFO's will _always_ have an ETag attached
                            // to their meta-data.  ETag's are always computed
                            // for HFO's upload. But new HFO's (one's the repo
                            // have never seen before) may not yet have an ETag.
                            if(!ifMatch.equals(eTag)) {
                                throw new ObjectConflictException("Failed " +
                                    "to update HFO; incoming If-Match ETag " +
                                    "does not match (hfo=" + hfo.getName() +
                                    ", etag=" + eTag + ", if-match=" +
                                    ifMatch + ")");
                            }
                        }
                        final DiskObject object = getCanonicalObject(
                            repo, hfo,
                            // Create the File on disk if it does not
                            // already exist. Yay!
                            true);
                        // The file itself (should exist now).
                        final File objFile = object.getFile();
                        final File tempObjFile = object.getTempFile();
                        try(final InputStream is = request.getInputStream();
                            final OutputStream os = new FileOutputStream(tempObjFile);) {
                            // Compute the ETag (an MD5 hash of the file) while
                            // copying the file into place.  The bytes of the
                            // input stream and piped into an MD5 digest _and_
                            // to the output stream -- ideally computing the
                            // hash and copying the file at the same time.
                            // Set the resulting ETag header (meta data).
                            hfo.setETag(getSHA1HashAndCopy(is, os,
                                // Only copy as much as the incoming
                                // Content-Length header sez is going
                                // to be sent.  Anything more than this
                                // is caught gracefully and dropped.
                                contentLength));
                            // Move the uploaded file into place (moves
                            // the file from the temp location to the
                            // real destination inside of the repository
                            // on disk).
                            move(tempObjFile, objFile);
                            // Set the Last-Modified header (meta data).
                            hfo.setLastModified(objFile.lastModified());
                            // Set the Content-Length header (meta data).
                            hfo.setContentLength(objFile.length());
                            // Set the Content-Type header (meta data).
                            if(contentType != null) {
                                hfo.setContentType(contentType);
                            }
                        } catch (KolichChecksum.KolichChecksumException e) {
                            // Quietly delete the object on disk when
                            // it has exceeded the max upload size allowed
                            // by this Havalo instance.
                            throw new ObjectTooLargeException("The " +
                                "size of the incoming object is too " +
                                "large. Max upload size is " +
                                uploadMaxSize_ + "-bytes.", e);
                        } finally {
                            // Delete the file from the temp upload
                            // location.  Note, this file may not exist
                            // if the upload was successful and the object
                            // was moved into place.. which is OK here.
                            deleteQuietly(tempObjFile);
                        }
                        // Append an ETag header to the response for the
                        // PUT'ed object.
                        response.setHeader(ETAG, hfo.getFirstHeader(ETAG));
                        return hfo;
                    }
                    @Override
                    public void success(final HashedFileObject e) throws Exception {
                        // On success only, ask the repo manager to
View Full Code Here

TOP

Related Classes of com.kolich.havalo.entities.types.HashedFileObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.