Skip to content

Commit

Permalink
Issue 353: refactored payload/blob so that contentMetadata is a type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Cole committed Sep 19, 2010
1 parent 546630b commit 3639af0
Show file tree
Hide file tree
Showing 151 changed files with 1,735 additions and 3,189 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@
import javax.inject.Singleton;

import org.jclouds.atmosonline.saas.domain.MutableContentMetadata;
import org.jclouds.atmosonline.saas.domain.internal.DelegatingMutableContentMetadata;
import org.jclouds.blobstore.domain.BlobMetadata;
import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
import org.jclouds.http.HttpUtils;

import com.google.common.base.Function;

Expand All @@ -32,13 +35,9 @@
@Singleton
public class BlobToContentMetadata implements Function<BlobMetadata, MutableContentMetadata> {
public MutableContentMetadata apply(BlobMetadata base) {
MutableContentMetadata to = new MutableContentMetadata();
to.setContentType(base.getContentType());
to.setContentMD5(base.getContentMD5());
to.setName(base.getName());
if (base.getSize() != null)
to.setContentLength(base.getSize());
return to;
MutableBlobMetadataImpl to = new MutableBlobMetadataImpl();
HttpUtils.copy(base.getContentMetadata(), to.getContentMetadata());
return new DelegatingMutableContentMetadata(base.getName(), to.getContentMetadata());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
@Singleton
public class BlobToSystemMetadata implements Function<BlobMetadata, SystemMetadata> {
public SystemMetadata apply(BlobMetadata base) {
return new SystemMetadata(base.getContentMD5(), null, base.getLastModified(), null, null,
null, 1, null, base.getName(), null, (base.getSize() != null) ? base.getSize() : 0,
FileType.REGULAR, "root");
return new SystemMetadata(base.getContentMetadata().getContentMD5(), null, base.getLastModified(), null, null,
null, 1, null, base.getName(), null, (base.getContentMetadata().getContentLength() != null) ? base
.getContentMetadata().getContentLength() : 0, FileType.REGULAR, "root");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jclouds.blobstore.domain.internal.PageSetImpl;
import org.jclouds.blobstore.domain.internal.StorageMetadataImpl;
import org.jclouds.domain.Location;
import org.jclouds.io.payloads.BaseMutableContentMetadata;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
Expand Down Expand Up @@ -59,11 +60,11 @@ public PageSet<? extends StorageMetadata> apply(BoundedSet<? extends DirectoryEn
public StorageMetadata apply(DirectoryEntry from) {
StorageType type = from.getType() == FileType.DIRECTORY ? StorageType.FOLDER : StorageType.BLOB;
if (type == StorageType.FOLDER)
return new StorageMetadataImpl(type, from.getObjectID(), from.getObjectName(), defaultLocation.get(),
null, null, null, null, Maps.<String, String> newHashMap());
return new StorageMetadataImpl(type, from.getObjectID(), from.getObjectName(), defaultLocation
.get(), null, null, null, Maps.<String, String> newHashMap());
else
return new BlobMetadataImpl(from.getObjectID(), from.getObjectName(), defaultLocation.get(), null,
null, null, null, Maps.<String, String> newHashMap(), null, null, null, null, null);
return new BlobMetadataImpl(from.getObjectID(), from.getObjectName(), defaultLocation.get(),
null, null, null, Maps.<String, String> newHashMap(), new BaseMutableContentMetadata());
}

}), from.getToken());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jclouds.atmosonline.saas.domain.AtmosObject;
import org.jclouds.blobstore.domain.Blob;
import org.jclouds.blobstore.domain.Blob.Factory;
import org.jclouds.http.HttpUtils;

import com.google.common.base.Function;

Expand All @@ -49,9 +50,7 @@ public Blob apply(AtmosObject from) {
return null;
Blob blob = blobFactory.create(object2BlobMd.apply(from));
blob.setPayload(checkNotNull(from.getPayload(), "payload: " + from));
if (from.getContentMetadata().getContentLength() != null)
blob.getMetadata().setSize(from.getContentMetadata().getContentLength());
blob.getMetadata().setContentMD5(from.getSystemMetadata().getContentMD5());
HttpUtils.copy(from.getContentMetadata(), blob.getPayload().getContentMetadata());
blob.setAllHeaders(from.getAllHeaders());
return blob;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.jclouds.blobstore.domain.MutableBlobMetadata;
import org.jclouds.blobstore.domain.StorageType;
import org.jclouds.blobstore.domain.internal.MutableBlobMetadataImpl;
import org.jclouds.http.HttpUtils;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
Expand All @@ -58,11 +59,8 @@ public MutableBlobMetadata apply(AtmosObject from) {
MutableBlobMetadata to = new MutableBlobMetadataImpl();
to.setId(from.getSystemMetadata().getObjectID());
to.setLastModified(from.getSystemMetadata().getLastUserDataModification());
to.setContentMD5(from.getSystemMetadata().getContentMD5());
if (from.getContentMetadata().getContentType() != null)
to.setContentType(from.getContentMetadata().getContentType());
HttpUtils.copy(from.getContentMetadata(), to.getContentMetadata());
to.setName(objectName.apply(from));
to.setSize(from.getSystemMetadata().getSize());
if (from.getSystemMetadata().getType() == FileType.DIRECTORY) {
to.setType(StorageType.FOLDER);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,83 +19,20 @@

package org.jclouds.atmosonline.saas.domain;

import javax.ws.rs.core.MediaType;
import org.jclouds.atmosonline.saas.domain.internal.DelegatingMutableContentMetadata;

import com.google.inject.ImplementedBy;

/**
* metadata of the object
*
* @author Adrian Cole
*/
public class MutableContentMetadata {

private String name;
private Long contentLength;
private String contentType = MediaType.APPLICATION_OCTET_STREAM;
private byte[] contentMD5;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

/**
* Returns the total size of the downloaded object, or the chunk that's available.
* <p/>
* Chunking is only used when org.jclouds.http.GetOptions is called with options like tail,
* range, or startAt.
*
* @return the length in bytes that can be be obtained from {@link #getInput()}
* @see org.jclouds.http.HttpHeaders#CONTENT_LENGTH
* @see GetObjectOptions
*/
public Long getContentLength() {
return contentLength;
}

/**
* @see #getContentLength
*/
public void setContentLength(Long contentLength) {
this.contentLength = contentLength;
}

/**
*
* A standard MIME type describing the format of the contents. If none is provided, the default
* is binary/octet-stream.
*
* @see <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17"/>
*/
public String getContentType() {
return contentType;
}

/**
* @see #getContentType
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
@ImplementedBy(DelegatingMutableContentMetadata.class)
public interface MutableContentMetadata extends org.jclouds.io.MutableContentMetadata {

/**
* The 128-bit MD5 digest of the message (without the headers) according to RFC 1864. This header
* can be used as a message integrity check to verify that the data is the same data that was
* originally sent. Although it is optional, we recommend using the Content-MD5 mechanism as an
* end-to-end integrity check.
*
*/
public byte[] getContentMD5() {
return contentMD5;
}
public String getName();

/**
* @see #getContentMD5
*/
public void setContentMD5(byte[] contentMD5) {
this.contentMD5 = contentMD5;
}
public void setName(String name);

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.jclouds.atmosonline.saas.domain.UserMetadata;
import org.jclouds.http.internal.PayloadEnclosingImpl;
import org.jclouds.io.Payload;
import org.jclouds.io.PayloadEnclosing;
import org.jclouds.io.payloads.DelegatingPayload;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;
Expand All @@ -40,8 +38,7 @@
*
* @author Adrian Cole
*/
public class AtmosObjectImpl extends PayloadEnclosingImpl implements AtmosObject,
Comparable<AtmosObject> {
public class AtmosObjectImpl extends PayloadEnclosingImpl implements AtmosObject, Comparable<AtmosObject> {
private final UserMetadata userMetadata;
private final SystemMetadata systemMetadata;

Expand All @@ -53,15 +50,13 @@ public UserMetadata getUserMetadata() {
return userMetadata;
}

private final MutableContentMetadata _contentMetadata;
private final SetPayloadPropertiesMutableContentMetadata contentMetadata;
private MutableContentMetadata contentMetadata;
private Multimap<String, String> allHeaders = LinkedHashMultimap.create();

public AtmosObjectImpl(MutableContentMetadata contentMetadata, SystemMetadata systemMetadata,
UserMetadata userMetadata) {
super();
this.contentMetadata = linkMetadataToThis(contentMetadata);
this._contentMetadata = this.contentMetadata.getDelegate();
this.contentMetadata = contentMetadata;
this.systemMetadata = systemMetadata;
this.userMetadata = userMetadata;
}
Expand Down Expand Up @@ -102,15 +97,16 @@ public void setAllHeaders(Multimap<String, String> allHeaders) {
public int compareTo(AtmosObject o) {
if (getContentMetadata().getName() == null)
return -1;
return (this == o) ? 0 : getContentMetadata().getName().compareTo(
o.getContentMetadata().getName());
return (this == o) ? 0 : getContentMetadata().getName().compareTo(o.getContentMetadata().getName());
}

@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((_contentMetadata == null) ? 0 : _contentMetadata.hashCode());
result = prime * result + ((contentMetadata == null) ? 0 : contentMetadata.hashCode());
result = prime * result + ((systemMetadata == null) ? 0 : systemMetadata.hashCode());
result = prime * result + ((userMetadata == null) ? 0 : userMetadata.hashCode());
return result;
}

Expand All @@ -123,85 +119,32 @@ public boolean equals(Object obj) {
if (getClass() != obj.getClass())
return false;
AtmosObjectImpl other = (AtmosObjectImpl) obj;
if (_contentMetadata == null) {
if (other._contentMetadata != null)
if (contentMetadata == null) {
if (other.contentMetadata != null)
return false;
} else if (!_contentMetadata.equals(other._contentMetadata))
} else if (!contentMetadata.equals(other.contentMetadata))
return false;
if (systemMetadata == null) {
if (other.systemMetadata != null)
return false;
} else if (!systemMetadata.equals(other.systemMetadata))
return false;
if (userMetadata == null) {
if (other.userMetadata != null)
return false;
} else if (!userMetadata.equals(other.userMetadata))
return false;
return true;
}

@Override
public String toString() {
return "[contentMetadata=" + _contentMetadata + "]";
return "[contentMetadata=" + contentMetadata + "]";
}

@Override
public void setPayload(Payload data) {
linkPayloadToMetadata(data);
}

/**
* link the new payload to the contentMetadata object so that when content-related
* contentMetadata is updated on the payload, it is also copied the contentMetadata object.
*/
void linkPayloadToMetadata(Payload data) {
if (data instanceof DelegatingPayload)
super.setPayload(new SetMetadataPropertiesPayload(DelegatingPayload.class.cast(data)
.getDelegate(), _contentMetadata));
else
super.setPayload(new SetMetadataPropertiesPayload(data, _contentMetadata));
}

static class SetMetadataPropertiesPayload extends DelegatingPayload {

private transient final MutableContentMetadata contentMetadata;

public SetMetadataPropertiesPayload(Payload delegate, MutableContentMetadata contentMetadata) {
super(delegate);
this.contentMetadata = contentMetadata;
setContentType(contentMetadata.getContentType());
}

@Override
public void setContentType(String md5) {
super.setContentType(md5);
contentMetadata.setContentType(md5);
}

}

/**
* link the contentMetadata object to this so that when content-related contentMetadata is
* updated, it is also copied the currentpayload object.
*/
SetPayloadPropertiesMutableContentMetadata linkMetadataToThis(
MutableContentMetadata contentMetadata) {
return contentMetadata instanceof DelegatingMutableContentMetadata ? new SetPayloadPropertiesMutableContentMetadata(
DelegatingMutableContentMetadata.class.cast(contentMetadata).getDelegate(), this)
: new SetPayloadPropertiesMutableContentMetadata(contentMetadata, this);
}

static class SetPayloadPropertiesMutableContentMetadata extends DelegatingMutableContentMetadata {
/** The serialVersionUID */
private static final long serialVersionUID = -5072270546219814521L;
private transient final PayloadEnclosing object;

public SetPayloadPropertiesMutableContentMetadata(MutableContentMetadata delegate,
PayloadEnclosing object) {
super(delegate);
this.object = object;
}

@Override
public void setContentType(String type) {
super.setContentType(type);
if (canSetPayload())
object.getPayload().setContentType(type);
}

private boolean canSetPayload() {
return object != null && object.getPayload() != null;
}
this.payload = data;
this.contentMetadata = new DelegatingMutableContentMetadata(contentMetadata.getName(), payload.getContentMetadata());
}
}
Loading

0 comments on commit 3639af0

Please sign in to comment.