blob: 6f0905b68086f101384c7d0f6bc5ed3a89c8b777 [file] [log] [blame]
package org.jclouds.snia.cdmi.v1.options;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;
import org.jclouds.http.options.BaseHttpRequestOptions;
import com.google.common.base.Charsets;
import java.nio.charset.Charset;
import com.google.gson.JsonObject;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
/**
* CreateDataObjectOptions options supported in the REST API for the CREATE CDMI
* Data Object operation. <h2>
*
* @author Kenneth Nagin
*/
public class CreateDataObjectOptions extends BaseHttpRequestOptions {
private JsonObject jsonObjectBody = new JsonObject();
public CreateDataObjectOptions() {
jsonObjectBody.addProperty("value", new String());
}
/**
* Create CDMI data object with metadata
*
* @param metadata
* @return this
*/
public CreateDataObjectOptions metadata(Map<String, String> metadata) {
JsonObject jsonObjectMetadata = new JsonObject();
if (metadata != null) {
for (Entry<String, String> entry : metadata.entrySet()) {
jsonObjectMetadata
.addProperty(entry.getKey(), entry.getValue());
}
}
jsonObjectBody.add("metadata", jsonObjectMetadata);
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with mimetype
*
* @param mimetype
* @return
*/
public CreateDataObjectOptions mimetype(String mimetype) {
jsonObjectBody.addProperty("mimetype", mimetype);
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with value equal to empty string
*
* @return
*/
public CreateDataObjectOptions value() {
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with String value
*
* @param value
* String value
* @return
*/
public CreateDataObjectOptions value(String value) {
jsonObjectBody.addProperty("value", (value == null) ? new String()
: value);
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with byte array value
*
* @param value
* byte array value byte array is converted to a String value
* @return
*/
public CreateDataObjectOptions value(byte[] value) throws IOException {
jsonObjectBody.addProperty("value",
(value == null) ? new String() : new DataInputStream(
new ByteArrayInputStream(value)).readUTF());
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with file value
*
* @param value
* File File is converted to a String value with charset UTF_8
* @return
*/
public CreateDataObjectOptions value(File value) throws IOException {
jsonObjectBody.addProperty("value", (value == null) ? new String()
: Files.toString(value, Charsets.UTF_8));
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with file value
*
* @param value
* File
* @param charset
* character set of file File is converted to a String value
* @return
*/
public CreateDataObjectOptions value(File value, Charset charset)
throws IOException {
jsonObjectBody.addProperty("value", (value == null) ? new String()
: Files.toString(value, charset));
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with InputStream value
*
* @param value
* InputSteam InputSteam is converted to a String value with
* charset UTF_8
* @return
*/
public CreateDataObjectOptions value(InputStream value) throws IOException {
jsonObjectBody
.addProperty(
"value",
(value == null) ? new String() : CharStreams
.toString(new InputStreamReader(value,
Charsets.UTF_8)));
this.payload = jsonObjectBody.toString();
return this;
}
/**
* Create CDMI data object with InputStream value
*
* @param value
* InputSteam
* @param charset
* character set of input stream InputSteam is converted to a
* String value with charset UTF_8
* @return
*/
public CreateDataObjectOptions value(InputStream value, Charset charset)
throws IOException {
jsonObjectBody.addProperty("value", (value == null) ? new String()
: CharStreams.toString(new InputStreamReader(value, charset)));
this.payload = jsonObjectBody.toString();
return this;
}
public static class Builder {
public static CreateDataObjectOptions metadata(
Map<String, String> metadata) {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.metadata(metadata);
}
public static CreateDataObjectOptions mimetype(String mimetype) {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.mimetype(mimetype);
}
public static CreateDataObjectOptions value() {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value();
}
public static CreateDataObjectOptions value(String value) {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value);
}
public static CreateDataObjectOptions value(byte[] value)
throws IOException {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value);
}
public static CreateDataObjectOptions value(File value)
throws IOException {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value);
}
public static CreateDataObjectOptions value(File value, Charset charset)
throws IOException {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value, charset);
}
public static CreateDataObjectOptions value(InputStream value)
throws IOException {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value);
}
public static CreateDataObjectOptions value(InputStream value,
Charset charset) throws IOException {
CreateDataObjectOptions options = new CreateDataObjectOptions();
return (CreateDataObjectOptions) options.value(value, charset);
}
}
}