/src/main/java/org/openstack/api/storage/ObjectResource.java

http://github.com/woorea/openstack-java-sdk · Java · 114 lines · 73 code · 20 blank · 21 comment · 4 complexity · 11e53e542077a843c4cb0dce9a0b17f4 MD5 · raw file

  1. package org.openstack.api.storage;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.Map;
  7. import java.util.Properties;
  8. import javax.ws.rs.client.Entity;
  9. import javax.ws.rs.client.Invocation;
  10. import javax.ws.rs.client.Target;
  11. import javax.ws.rs.core.MediaType;
  12. import javax.ws.rs.core.Response;
  13. import org.openstack.api.common.Resource;
  14. import org.openstack.api.identity.admin.resources.TenantResource;
  15. import org.openstack.model.exceptions.OpenStackException;
  16. import org.openstack.model.storage.StorageObjectProperties;
  17. import com.google.common.base.Preconditions;
  18. public class ObjectResource extends Resource {
  19. // Storage Objects
  20. // Verb URI Description
  21. // GET /account/container/object Retrieve object
  22. // PUT /account/container/object Create/Update Object
  23. // PUT /account/container/object Chunked transfer encoding
  24. // DELETE /account/container/object Delete container
  25. // HEAD /account/container/object Retrieve object metadata
  26. // POST /account/container/object Update object metadata
  27. private TenantResource t = null; //new TenantResource();
  28. public ObjectResource(Target target, Properties properties) {
  29. super(target, properties);
  30. }
  31. public StorageObjectProperties head() {
  32. Response response = target.request(MediaType.APPLICATION_JSON).head();
  33. StorageObjectProperties properties = SwiftHeaderUtils.unmarshalHeaders(response.getHeaders());
  34. return properties;
  35. }
  36. public Response post(Properties properties) {
  37. Preconditions.checkNotNull(properties, "You have to supply properties");
  38. Invocation.Builder b = target.request(MediaType.APPLICATION_JSON);
  39. for(String name : properties.stringPropertyNames()) {
  40. b = b.header("x-object-meta-" + name, properties.getProperty(name));
  41. }
  42. return b.method("POST");
  43. }
  44. public Response delete() {
  45. return target.request(MediaType.WILDCARD).delete();
  46. }
  47. public <T> T get(Class<T> c) {
  48. return target.request(MediaType.WILDCARD).get(c);
  49. }
  50. public Response put() throws OpenStackException {
  51. Invocation.Builder builder = target.request();
  52. builder = builder.header("Content-Length", 0);
  53. return builder.put(Entity.entity(new byte[1],"application/directory"));
  54. }
  55. public Response put(InputStream is, Map<String, String> headers) throws OpenStackException {
  56. Invocation.Builder builder = target.request(MediaType.WILDCARD);
  57. if(headers != null) {
  58. for(Map.Entry<String, String> entry : headers.entrySet()) {
  59. builder = builder.header("x-object-meta-"+entry.getKey(), entry.getValue());
  60. }
  61. }
  62. long start = System.currentTimeMillis();
  63. Response response = builder.put(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
  64. long end = System.currentTimeMillis();
  65. System.out.println("TIME : " + (end - start));
  66. return response;
  67. /*
  68. try {
  69. Map<String, String> swiftHeaders = new HashMap<String, String>();
  70. for(Map.Entry<String, String> entry : headers.entrySet()) {
  71. swiftHeaders.put("x-object-meta-"+entry.getKey(), entry.getValue());
  72. }
  73. swiftHeaders.put("X-Auth-Token", properties.getProperty("auth.token"));
  74. HttpUtils.upload("PUT", target.getUri().toURL(), swiftHeaders, is);
  75. return null;
  76. } catch (IOException e) {
  77. throw new OpenStackException(e.getMessage(), e);
  78. }
  79. */
  80. }
  81. public Response put(File file, Map<String, String> headers) throws OpenStackException {
  82. try {
  83. return put(new FileInputStream(file), headers);
  84. } catch (IOException e) {
  85. throw new OpenStackException(e.getMessage(), e);
  86. }
  87. }
  88. public Response put(File file) throws OpenStackException {
  89. return put(file, null);
  90. }
  91. public Response put(InputStream objectStream) {
  92. return put(objectStream, null);
  93. }
  94. }