7070import com .google .gson .JsonObject ;
7171import com .google .gson .JsonParser ;
7272import com .rackspacecloud .client .cloudfiles .expections .*;
73- import com .rackspacecloud .client .cloudfiles .torrent .FileDownloadedTorrentException ;
7473import com .rackspacecloud .client .cloudfiles .wrapper .RequestEntityWrapper ;
7574
7675/**
@@ -2458,6 +2457,95 @@ public String storeObjectAs(String container, File obj, String contentType, Stri
24582457 }
24592458 }
24602459 }
2460+
2461+ /**
2462+ * Store a file on the server
2463+ *
2464+ * @param sharedStorageURL
2465+ * @param container The name of the container
2466+ * @param obj The File containing the file to copy over
2467+ * @param contentType The MIME type of the file
2468+ * @param name The name of the file on the server
2469+ * @return The ETAG if the save was successful, null otherwise
2470+ * @throws IOException There was an IO error doing network communication
2471+ * @throws HttpException There was an error with the http protocol
2472+ * @throws FilesException
2473+ */
2474+ public String storeSharedObjectAs (String sharedStorageURL , String container ,
2475+ File obj , String contentType , String name ) throws OverQuotaException ,
2476+ FilesException , IOException , HttpException {
2477+
2478+ if (!this .isLoggedin ()) {
2479+ throw new FilesAuthorizationException ("You must be logged in" , null , null );
2480+ }
2481+
2482+ if (!isValidContainerName (container )) {
2483+ throw new FilesInvalidNameException (name );
2484+ }
2485+
2486+ if (!isValidObjectName (name )) {
2487+ throw new FilesInvalidNameException (container );
2488+ }
2489+
2490+ if (!obj .exists ()) {
2491+ throw new FileNotFoundException (name + " does not exist" );
2492+ }
2493+
2494+ if (obj .isDirectory ()) {
2495+ throw new IOException ("The alleged file was a directory" );
2496+ }
2497+
2498+
2499+ HttpPut method = null ;
2500+ try {
2501+ method = new HttpPut (sharedStorageURL + "/"
2502+ + sanitizeForURI (container ) + "/"
2503+ + sanitizeForURI (name ));
2504+ method .getParams ().setIntParameter ("http.socket.timeout" , connectionTimeOut );
2505+ method .setHeader (FilesConstants .X_AUTH_TOKEN , authToken );
2506+ if (useETag ) {
2507+ method .setHeader (FilesConstants .E_TAG , md5Sum (obj ));
2508+ }
2509+ //method.setEntity(new RequestEntityWrapper(new FileEntity(obj, contentType), callback));
2510+ FilesResponse response = new FilesResponse (client .execute (method ));
2511+
2512+ if (response .getStatusCode () == HttpStatus .SC_UNAUTHORIZED ) {
2513+ method .abort ();
2514+ if (login ()) {
2515+ method = new HttpPut (sharedStorageURL + "/"
2516+ + sanitizeForURI (container ) + "/"
2517+ + sanitizeForURI (name ));
2518+ method .getParams ().setIntParameter ("http.socket.timeout" , connectionTimeOut );
2519+ method .setHeader (FilesConstants .X_AUTH_TOKEN , authToken );
2520+ if (useETag ) {
2521+ method .setHeader (FilesConstants .E_TAG , md5Sum (obj ));
2522+ }
2523+ //method.setEntity(new RequestEntityWrapper(new FileEntity(obj, contentType), callback));
2524+ response = new FilesResponse (client .execute (method ));
2525+ } else {
2526+ throw new FilesAuthorizationException ("Re-login failed" , response .getResponseHeaders (), response .getStatusLine ());
2527+ }
2528+ }
2529+ int statusCode = response .getStatusCode ();
2530+
2531+ switch (statusCode ) {
2532+ case HttpStatus .SC_CREATED :
2533+ return response .getResponseHeader (FilesConstants .E_TAG ).getValue ();
2534+ case HttpStatus .SC_PRECONDITION_FAILED :
2535+ throw new FilesException ("Etag missmatch" , response .getResponseHeaders (), response .getStatusLine ());
2536+ case HttpStatus .SC_LENGTH_REQUIRED :
2537+ throw new FilesException ("Length miss-match" , response .getResponseHeaders (), response .getStatusLine ());
2538+ case HttpStatus .SC_REQUEST_TOO_LONG :
2539+ throw new OverQuotaException ("Quota excedeed" , response .getResponseHeaders (), response .getStatusLine ());
2540+ default :
2541+ throw new FilesException ("Unexpected Server Response" , response .getResponseHeaders (), response .getStatusLine ());
2542+ }
2543+ } finally {
2544+ if (method != null ) {
2545+ method .abort ();
2546+ }
2547+ }
2548+ }
24612549
24622550 /**
24632551 * Copies the file to Cloud Files, keeping the original file name in Cloud
@@ -3075,17 +3163,25 @@ public byte[] getObject(String container, String objName)
30753163 }
30763164 return null ;
30773165 }
3078-
3079- public InputStream getObjectAsStreamTorrent (Long fileId , Long version , String container , String objName )
3166+
3167+ /**
3168+ * Get's the given shared object's content as a stream
3169+ *
3170+ * @param container The name of the container
3171+ * @param objName The name of the object
3172+ * @return An input stream that will give the objects content when read
3173+ * from.
3174+ * @throws IOException There was an IO error doing network communication
3175+ * @throws HttpException There was an error with the http protocol
3176+ * @throws FilesAuthorizationException
3177+ * @throws FilesNotFoundException The container does not exist
3178+ * @throws FilesInvalidNameException
3179+ */
3180+ public InputStream getSharedObjectAsStream (String sharedStorageURL , String container , String objName )
30803181 throws IOException , HttpException , FilesAuthorizationException ,
3081- FilesInvalidNameException , FilesNotFoundException , FileDownloadedTorrentException {
3082-
3083- //logger.info("INIT: getObjectAsStreamTorrent storageURL:"+ storageURL);
3084-
3182+ FilesInvalidNameException , FilesNotFoundException {
30853183 if (this .isLoggedin ()) {
3086-
30873184 if (isValidContainerName (container ) && isValidObjectName (objName )) {
3088-
30893185 if (objName .length () > FilesConstants .OBJECT_NAME_LENGTH ) {
30903186 logger .warn ("Object Name supplied was truncated to Max allowed of "
30913187 + FilesConstants .OBJECT_NAME_LENGTH
@@ -3095,92 +3191,49 @@ public InputStream getObjectAsStreamTorrent(Long fileId, Long version, String co
30953191 logger .warn ("Truncated Object Name is: " + objName );
30963192 }
30973193
3098- HttpGet method = new HttpGet (storageURL + "/"
3194+ HttpGet method = new HttpGet (sharedStorageURL + "/"
30993195 + sanitizeForURI (container ) + "/"
31003196 + sanitizeForURI (objName ));
31013197 method .getParams ().setIntParameter ("http.socket.timeout" ,
31023198 connectionTimeOut );
31033199 method .setHeader (FilesConstants .X_AUTH_TOKEN , authToken );
3104-
3105- String keyFile = String .valueOf (fileId ) + ":" + String .valueOf (version );
3106- method .setHeader (FilesConstants .X_CHECK_TORRENT_KEYFILE , keyFile );
3107- method .setHeader (FilesConstants .X_AUTHENTICATION_URL , authenticationURL );
3108-
3109- FilesResponse response = new FilesResponse (client .execute (method ));
3110-
3200+ FilesResponse response = new FilesResponse (
3201+ client .execute (method ));
31113202
31123203 if (response .getStatusCode () == HttpStatus .SC_UNAUTHORIZED ) {
3113-
3114- logger .info ("response.getStatusCode() == " + HttpStatus .SC_UNAUTHORIZED );
3115-
31163204 method .abort ();
31173205 login ();
3118- method = new HttpGet (storageURL + "/"
3206+ method = new HttpGet (sharedStorageURL + "/"
31193207 + sanitizeForURI (container ) + "/"
31203208 + sanitizeForURI (objName ));
31213209 method .getParams ().setIntParameter ("http.socket.timeout" ,
31223210 connectionTimeOut );
31233211 method .setHeader (FilesConstants .X_AUTH_TOKEN , authToken );
3124-
3125- method .setHeader (FilesConstants .X_CHECK_TORRENT_KEYFILE , keyFile );
3126- method .setHeader (FilesConstants .X_AUTHENTICATION_URL , authenticationURL );
3127-
3128-
31293212 response = new FilesResponse (client .execute (method ));
3130-
3131-
31323213 }
31333214
31343215 if (response .getStatusCode () == HttpStatus .SC_OK ) {
3135-
3136- Header header = response .getResponseHeader ("BitTorrent" );
3137-
3138- if (header != null ) {
3139-
3140- String message = "Torrent data retreived, " + "Container: " + container + " did not have object " + objName ;
3141- logger .info (message );
3142-
3143- byte [] torrentData = IOUtils .toByteArray (response .getResponseBodyAsStream ());
3144-
3145- FileDownloadedTorrentException fdownlTorException = new FileDownloadedTorrentException (torrentData , message );
3146-
3147- throw fdownlTorException ;
3148-
3149-
3150- } else {
3151-
3152- //logger.info("Object data retreived : " + objName);
3153- // DO NOT RELEASE THIS CONNECTION
3154- return response .getResponseBodyAsStream ();
3155- }
3156-
3157-
3216+ logger .info ("Object data retreived : " + objName );
3217+ // DO NOT RELEASE THIS CONNECTION
3218+ return response .getResponseBodyAsStream ();
31583219 } else if (response .getStatusCode () == HttpStatus .SC_NOT_FOUND ) {
3159-
3160- logger .info ("response.getStatusCode() == " + HttpStatus .SC_NOT_FOUND );
31613220 method .abort ();
31623221 throw new FilesNotFoundException ("Container: " + container
31633222 + " did not have object " + objName ,
31643223 response .getResponseHeaders (),
31653224 response .getStatusLine ());
3166-
31673225 }
3168-
3169-
31703226 } else {
31713227 if (!isValidObjectName (objName )) {
31723228 throw new FilesInvalidNameException (objName );
31733229 } else {
31743230 throw new FilesInvalidNameException (container );
31753231 }
31763232 }
3177-
31783233 } else {
31793234 throw new FilesAuthorizationException ("You must be logged in" ,
31803235 null , null );
31813236 }
3182-
3183-
31843237 return null ;
31853238 }
31863239
0 commit comments