44import java .io .FileInputStream ;
55import java .io .IOException ;
66import java .io .InputStream ;
7+ import java .util .Map ;
78import java .util .Properties ;
89
910import javax .ws .rs .client .Entity ;
1718import org .openstack .api .identity .admin .resources .TenantResource ;
1819import org .openstack .model .exceptions .OpenStackException ;
1920import org .openstack .model .storage .StorageObjectProperties ;
20- import org .openstack .model .storage .swift .SwiftStorageObjectProperties ;
2121
2222import 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}
0 commit comments