All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jclouds.blobstore.functions.ObjectMD5 Maven / Gradle / Ivy

/**
 *
 * Copyright (C) 2010 Cloud Conscious, LLC. 
 *
 * ====================================================================
 * 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.
 * ====================================================================
 */

package org.jclouds.blobstore.functions;

import java.io.IOException;

import javax.inject.Inject;

import org.jclouds.blobstore.domain.Blob;
import org.jclouds.crypto.Crypto;
import org.jclouds.io.Payloads;

import com.google.common.base.Function;
import com.google.common.base.Throwables;

public class ObjectMD5 implements Function {

   protected final Blob.Factory blobFactory;
   protected final Crypto crypto;

   @Inject
   ObjectMD5(Crypto crypto, Blob.Factory blobFactory) {
      this.blobFactory = blobFactory;
      this.crypto = crypto;
   }

   public byte[] apply(Object from) {
      Blob object;
      if (from instanceof Blob) {
         object = (Blob) from;
      } else {
         object = blobFactory.create(null);
         object.setPayload(Payloads.newPayload(from));
      }
      if (object.getMetadata().getContentMetadata().getContentMD5() == null)
         try {
            Payloads.calculateMD5(object, crypto.md5());
         } catch (IOException e) {
            Throwables.propagate(e);
         }
      return object.getPayload().getContentMetadata().getContentMD5();
   }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy