Skip to content

Commit bd60382

Browse files
committed
swift hpcloud
1 parent cad625e commit bd60382

4 files changed

Lines changed: 60 additions & 30 deletions

File tree

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.FileInputStream;
55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.util.Map;
78
import java.util.Properties;
89

910
import javax.ws.rs.client.Entity;
@@ -17,7 +18,6 @@
1718
import org.openstack.api.identity.admin.resources.TenantResource;
1819
import org.openstack.model.exceptions.OpenStackException;
1920
import org.openstack.model.storage.StorageObjectProperties;
20-
import org.openstack.model.storage.swift.SwiftStorageObjectProperties;
2121

2222
import com.google.common.base.Preconditions;
2323

@@ -45,27 +45,30 @@ public StorageObjectProperties head() {
4545
return properties;
4646
}
4747

48-
public Response post(StorageObjectProperties changeProperties) {
48+
public Response post(Properties properties) {
49+
Preconditions.checkNotNull(properties, "You have to supply properties");
4950
Invocation.Builder b = target.request(MediaType.APPLICATION_JSON);
50-
SwiftHeaderUtils.setHeadersForProperties(b, changeProperties);
51+
for(String name : properties.stringPropertyNames()) {
52+
b = b.header("x-object-meta-" + name, properties.getProperty(name));
53+
}
5154
return b.method("POST");
5255
}
5356

5457
public Response delete() {
5558
return target.request(MediaType.WILDCARD).delete();
5659
}
5760

58-
public InputStream openStream() {
59-
return target.request(MediaType.APPLICATION_OCTET_STREAM).get(InputStream.class);
61+
public <T> T get(Class<T> c) {
62+
return target.request(MediaType.WILDCARD).get(c);
6063
}
6164

6265
public Response put() throws OpenStackException {
63-
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
64-
builder = builder.header("Content-Length", "0");
65-
return builder.put(Entity.entity(new byte[1], "application/directory"));
66+
Invocation.Builder builder = target.request();
67+
builder = builder.header("Content-Length", 0);
68+
return builder.put(Entity.entity(new byte[1],"application/directory"));
6669
}
6770

68-
public Response put(File srcFile, SwiftStorageObjectProperties properties) throws OpenStackException {
71+
public Response put(File srcFile, Map<String, String> properties) throws OpenStackException {
6972
FileInputStream fis = null;
7073
try {
7174
fis = new FileInputStream(srcFile);
@@ -76,26 +79,30 @@ public Response put(File srcFile, SwiftStorageObjectProperties properties) throw
7679
IOUtils.closeQuietly(fis);
7780
}
7881
}
82+
83+
public Response put(File srcFile) throws OpenStackException {
84+
return put(srcFile, null);
85+
}
7986

80-
public Response put(InputStream objectStream, long objectStreamLength, SwiftStorageObjectProperties properties) throws OpenStackException {
81-
Preconditions.checkNotNull(properties, "You have to supply object propeties");
82-
Preconditions.checkNotNull(properties, "You have to supply object name");
87+
public Response put(InputStream objectStream, long objectStreamLength, Map<String, String> properties) throws OpenStackException {
88+
//Preconditions.checkNotNull(properties, "You have to supply object name");
8389
try {
8490
Invocation.Builder builder = target.request(MediaType.APPLICATION_JSON);
8591

8692
byte[] bytes = IOUtils.toByteArray(objectStream);
8793

8894
builder = builder.header("Content-Length", bytes.length);
8995

90-
SwiftHeaderUtils.setHeadersForProperties(builder, properties);
91-
92-
Response response = null;
93-
if (properties.getContentType() != null) {
94-
MediaType contentType = MediaType.valueOf(properties.getContentType());
95-
response = builder.put(Entity.entity(bytes, contentType), Response.class);
96+
if(properties != null) {
97+
for(String name : properties.keySet()) {
98+
builder = builder.header("x-object-meta-" + name, properties.get(name));
99+
}
100+
String contentType = properties.get("Content-Type") != null ? properties.get("Content-Type") : MediaType.APPLICATION_OCTET_STREAM;
101+
return builder.put(Entity.entity(bytes, contentType), Response.class);
96102
} else {
97-
response = builder.put(Entity.entity(bytes, MediaType.APPLICATION_OCTET_STREAM_TYPE), Response.class);
103+
return builder.put(Entity.entity(bytes, MediaType.APPLICATION_OCTET_STREAM), Response.class);
98104
}
105+
99106
/*
100107
MultivaluedMap<String, String> responseHeaders = response.getHeaders().asMap();
101108
@@ -107,10 +114,13 @@ public Response put(InputStream objectStream, long objectStreamLength, SwiftStor
107114
}
108115
return responseProperties;
109116
*/
110-
return response;
111117
} catch(IOException e) {
112118
throw new OpenStackException(e.getMessage(), e);
113119
}
114120
}
121+
122+
public Response put(InputStream objectStream, long objectStreamLength) {
123+
return put(objectStream, objectStreamLength, null);
124+
}
115125

116126
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ public static StorageObjectProperties unmarshalHeaders(ResponseHeaders response)
4343
return properties;
4444
}
4545

46+
/*
4647
public static Invocation.Builder setHeadersForProperties(Invocation.Builder builder, StorageObjectProperties changeProperties) {
4748
4849
for (Map.Entry<String, String> tag : changeProperties.getCustomProperties().entrySet()) {
49-
builder.header("x-object-meta-" + tag.getKey(), tag.getValue());
50+
5051
}
5152
return builder;
5253
}
54+
*/
5355

5456
}

src/main/java/org/openstack/client/StorageClient.java

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.openstack.client;
22

3+
import java.io.File;
34
import java.io.InputStream;
45
import java.util.List;
6+
import java.util.Map;
7+
import java.util.Properties;
58

69
import javax.ws.rs.core.Response;
710

@@ -11,7 +14,6 @@
1114
import org.openstack.model.storage.StorageObjectProperties;
1215
import org.openstack.model.storage.swift.SwiftContainer;
1316
import org.openstack.model.storage.swift.SwiftStorageObject;
14-
import org.openstack.model.storage.swift.SwiftStorageObjectProperties;
1517

1618
public class StorageClient {
1719

@@ -47,20 +49,36 @@ public List<SwiftStorageObject> listObjects(String containerName, String prefix)
4749

4850

4951

50-
public void createDirectory(String containerName, String objectName, String directoryName) {
51-
Response response = accountResource.container(containerName).object(objectName + directoryName).put();
52+
public void createDirectory(String containerName, String directoryName) {
53+
Response response = accountResource.container(containerName).object(directoryName).put();
5254
}
5355

54-
public void createObject(String containerId, String objectId, InputStream is, int size, SwiftStorageObjectProperties properties) {
56+
public void createObject(String containerId, String objectId, InputStream is, int size, Map<String, String> properties) {
5557
Response response = accountResource.container(containerId).object(objectId).put(is,size, properties);
5658
}
5759

60+
public void createObject(String containerId, String objectId, File file, Map<String,String> properties) {
61+
Response response = accountResource.container(containerId).object(objectId).put(file, properties);
62+
}
63+
64+
public void createObject(String containerId, String objectId, File file) {
65+
Response response = accountResource.container(containerId).object(objectId).put(file, null);
66+
}
67+
5868
public StorageObjectProperties showObject(String containerId, String objectId) {
5969
return accountResource.container(containerId).object(objectId).head();
6070
}
6171

62-
public InputStream getObjectInputStream(String containerId, String objectId) {
63-
return accountResource.container(containerId).object(objectId).openStream();
72+
public InputStream getObjectAsInputStream(String containerId, String objectId) {
73+
return accountResource.container(containerId).object(objectId).get(InputStream.class);
74+
}
75+
76+
public String getObjectAsString(String containerId, String objectId) {
77+
return accountResource.container(containerId).object(objectId).get(String.class);
78+
}
79+
80+
public byte[] getObjectAsByteArray(String containerId, String objectId) {
81+
return accountResource.container(containerId).object(objectId).get(byte[].class);
6482
}
6583

6684
public void deleteObject(String containerId, String objectId) {

src/test/java/org/openstack/client/storage/StorageIntegrationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ public void updateContainer() {
5353

5454
@Test(dependsOnMethods="createContainer", priority=3)
5555
public void createObject() {
56-
SwiftStorageObjectProperties properties = new SwiftStorageObjectProperties();
57-
properties.setName("test-object");
56+
//SwiftStorageObjectProperties properties = new SwiftStorageObjectProperties();
57+
//properties.setName("test-object");
5858
int size = 1024;
5959
InputStream is = new ByteArrayInputStream(new byte[size]);
60-
Response response = storage.container(container).object(object).put(is,size, properties);
60+
Response response = storage.container(container).object(object).put(is,size);
6161
}
6262

6363
@Test(dependsOnMethods={"createObject"}, priority=4)

0 commit comments

Comments
 (0)