diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a0f63b3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +sl-objectstorage/.idea/* +*/.idea/* +bin/* diff --git a/sl-objectstorage/lib/commons-io-2.4.jar b/sl-objectstorage/lib/commons-io-2.4.jar new file mode 100644 index 0000000..90035a4 Binary files /dev/null and b/sl-objectstorage/lib/commons-io-2.4.jar differ diff --git a/sl-objectstorage/src/com/softlayer/objectstorage/Client.java b/sl-objectstorage/src/com/softlayer/objectstorage/Client.java index cf6ae31..9b745d9 100644 --- a/sl-objectstorage/src/com/softlayer/objectstorage/Client.java +++ b/sl-objectstorage/src/com/softlayer/objectstorage/Client.java @@ -45,7 +45,7 @@ * other dealings in this Software without prior written authorization from * SoftLayer Technologies, Inc. * - * Portions Copyright © 2008-9 Rackspace US, Inc. + * Portions Copyright � 2008-9 Rackspace US, Inc. * * * base client for making calls to objectstorage API @@ -431,9 +431,6 @@ protected boolean isValidName(String name) { if (length == 0 || length > 256) { return false; } - if (name.indexOf('/') != -1) { - return false; - } return true; } diff --git a/sl-objectstorage/src/com/softlayer/objectstorage/ObjectFile.java b/sl-objectstorage/src/com/softlayer/objectstorage/ObjectFile.java index 0196471..fe5fb92 100644 --- a/sl-objectstorage/src/com/softlayer/objectstorage/ObjectFile.java +++ b/sl-objectstorage/src/com/softlayer/objectstorage/ObjectFile.java @@ -1,10 +1,6 @@ package com.softlayer.objectstorage; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; +import java.io.*; import java.util.Hashtable; import java.util.Iterator; import java.util.Map; @@ -46,7 +42,7 @@ * other dealings in this Software without prior written authorization from * SoftLayer Technologies, Inc. * - * Portions Copyright © 2008-9 Rackspace US, Inc. + * Portions Copyright � 2008-9 Rackspace US, Inc. * * This represents a file object on the objectstorage server * @@ -182,6 +178,44 @@ public String uploadFile(String localFileLocation, Map tags) } + /** + * upload this file from a local file copy to the objectstorage server + * + * @param inputStream + * stream of input data from file + * @param tags + * Map of tags to attach to this file + * @return etag value of this upload + * @throws EncoderException + * @throws IOException + */ + public String uploadFile(InputStream inputStream, Map tags) + throws EncoderException, IOException { + if (super.isValidName(this.name)) { + Hashtable params = super.createAuthParams(); + Iterator> it = tags.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pairs = (Map.Entry) it + .next(); + params.put(Client.X_OBJECT_META + pairs.getKey(), + pairs.getValue()); + it.remove(); + } + + String uName = super.saferUrlEncode(this.containerName); + String fName = super.saferUrlEncode(this.name); + Representation representation = new InputRepresentation(inputStream, MediaType.ALL); + ClientResource client = super.put(params, representation, + super.storageurl + "/" + uName + "/" + fName); + this.headers = client.getResponseAttributes(); + Form head = (Form) this.headers.get("org.restlet.http.headers"); + return head.getFirstValue("Etag"); + } else { + throw new EncoderException("invalid file name"); + } + + } + /** * removes this file form the objectstorage server * diff --git a/sl-objectstorage/src/com/softlayer/objectstorage/test/UnitTest.java b/sl-objectstorage/src/com/softlayer/objectstorage/test/UnitTest.java index a85a679..74e1e4b 100644 --- a/sl-objectstorage/src/com/softlayer/objectstorage/test/UnitTest.java +++ b/sl-objectstorage/src/com/softlayer/objectstorage/test/UnitTest.java @@ -1,5 +1,6 @@ package com.softlayer.objectstorage.test; +import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.List; @@ -9,6 +10,7 @@ import junit.framework.TestCase; import org.apache.commons.codec.EncoderException; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; import org.restlet.data.Form; @@ -111,6 +113,25 @@ public void testObjectFileCreate() throws IOException, EncoderException { assertTrue(false); } + @Test + public void testObjectFileCreateFromStream() throws IOException, EncoderException { + ObjectFile ofile = new ObjectFile("restletupload.txt", + "RestletContainer", baseUrl, user, password, true); + Map tags = new HashMap(); + tags.put("testtag", "testvalue"); + ofile.uploadFile(FileUtils.openInputStream(new File("restletupload.txt")), tags); + Container container = new Container("RestletContainer", baseUrl, user, + password, true); + List files = container.listObjectFiles(); + for (ObjectFile file : files) { + if (file.getName().equalsIgnoreCase("restletupload.txt")) { + + return; + } + } + assertTrue(false); + } + @Test public void testGetMetaTag() throws IOException, EncoderException { ObjectFile ofile = new ObjectFile("restletupload.txt",