Statistics
| Branch: | Revision:

root / src / com / rackspace / cloud / files / api / client / ContainerObjectManager.java @ a2b9aced

History | View | Annotate | Download (14.7 kB)

1 3d6041e8 Phillip Toohill
package com.rackspace.cloud.files.api.client;
2 3d6041e8 Phillip Toohill
3 802fbd9e koutsoub
import java.io.File;
4 3d6041e8 Phillip Toohill
import java.io.IOException;
5 3d6041e8 Phillip Toohill
import java.io.StringReader;
6 006434d8 Adam Menz
import java.net.MalformedURLException;
7 006434d8 Adam Menz
import java.net.URI;
8 006434d8 Adam Menz
import java.net.URISyntaxException;
9 3d6041e8 Phillip Toohill
import java.util.ArrayList;
10 a2b9aced koutsoub
import java.util.HashMap;
11 ef6fa21a koutsoub
import java.util.List;
12 732d9c2c koutsoub
import java.util.Map;
13 732d9c2c koutsoub
import java.util.Map.Entry;
14 3d6041e8 Phillip Toohill
15 3d6041e8 Phillip Toohill
import javax.xml.parsers.FactoryConfigurationError;
16 3d6041e8 Phillip Toohill
import javax.xml.parsers.ParserConfigurationException;
17 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParser;
18 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParserFactory;
19 3d6041e8 Phillip Toohill
20 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
21 3d6041e8 Phillip Toohill
import org.apache.http.client.ClientProtocolException;
22 3d6041e8 Phillip Toohill
import org.apache.http.client.methods.HttpDelete;
23 3d6041e8 Phillip Toohill
import org.apache.http.client.methods.HttpGet;
24 732d9c2c koutsoub
import org.apache.http.client.methods.HttpPost;
25 d3ea294b Adam Menz
import org.apache.http.client.methods.HttpPut;
26 7b27f0dc koutsoub
import org.apache.http.client.methods.HttpRequestBase;
27 802fbd9e koutsoub
import org.apache.http.entity.FileEntity;
28 3d6041e8 Phillip Toohill
import org.apache.http.impl.client.BasicResponseHandler;
29 3d6041e8 Phillip Toohill
import org.apache.http.protocol.RequestExpectContinue;
30 3d6041e8 Phillip Toohill
import org.xml.sax.InputSource;
31 3d6041e8 Phillip Toohill
import org.xml.sax.SAXException;
32 3d6041e8 Phillip Toohill
import org.xml.sax.XMLReader;
33 3d6041e8 Phillip Toohill
34 b347d5e3 Chmouel Boudjnah
import android.content.Context;
35 856ad13d koutsoub
import android.util.Log;
36 3d6041e8 Phillip Toohill
37 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser;
38 d37efce5 koutsoub
import com.rackspace.cloud.files.api.client.parsers.ObjectVersionXMLParser;
39 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.Account;
40 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
41 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.EntityManager;
42 32731215 Adam Menz
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
43 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
44 3d6041e8 Phillip Toohill
45 038ac9a4 koutsoub
/**
46 3d6041e8 Phillip Toohill
 * 
47 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
48 038ac9a4 koutsoub
 * 
49 3d6041e8 Phillip Toohill
 */
50 3d6041e8 Phillip Toohill
public class ContainerObjectManager extends EntityManager {
51 7b27f0dc koutsoub
        public final static int GET=0;
52 7b27f0dc koutsoub
        public final static int PUT=1;
53 7b27f0dc koutsoub
        public final static int POST=2;
54 7b27f0dc koutsoub
        public final static int DELETE=3;
55 3d6041e8 Phillip Toohill
        public String LOG = "ContainerObjectManager";
56 b347d5e3 Chmouel Boudjnah
        private Context context;
57 038ac9a4 koutsoub
        public static final String storageToken = Account.getAccount()
58 038ac9a4 koutsoub
                        .getStorageToken();
59 038ac9a4 koutsoub
60 b347d5e3 Chmouel Boudjnah
        public ContainerObjectManager(Context context) {
61 b347d5e3 Chmouel Boudjnah
                this.context = context;
62 b347d5e3 Chmouel Boudjnah
        }
63 ef6fa21a koutsoub
        
64 da02192c koutsoub
        
65 7b27f0dc koutsoub
        private List<ContainerObjects> executeGet(String containerName, String url) throws CloudServersException{
66 da02192c koutsoub
                CustomHttpClient httpclient = new CustomHttpClient(context);
67 da02192c koutsoub
                HttpGet get = new HttpGet(url);
68 da02192c koutsoub
                ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>();
69 da02192c koutsoub
70 da02192c koutsoub
                get.addHeader("Content-Type", "application/xml");
71 da02192c koutsoub
                get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken);
72 da02192c koutsoub
73 da02192c koutsoub
                try {
74 da02192c koutsoub
                        HttpResponse resp = httpclient.execute(get);
75 da02192c koutsoub
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
76 da02192c koutsoub
                        String body = responseHandler.handleResponse(resp);
77 da02192c koutsoub
78 da02192c koutsoub
                        if (resp.getStatusLine().getStatusCode() == 200
79 da02192c koutsoub
                                        || resp.getStatusLine().getStatusCode() == 203) {
80 da02192c koutsoub
                                ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
81 da02192c koutsoub
                                SAXParser saxParser = SAXParserFactory.newInstance()
82 da02192c koutsoub
                                                .newSAXParser();
83 da02192c koutsoub
                                XMLReader xmlReader = saxParser.getXMLReader();
84 da02192c koutsoub
                                xmlReader.setContentHandler(filesXMLParser);
85 da02192c koutsoub
86 da02192c koutsoub
                                xmlReader.parse(new InputSource(new StringReader(body)));
87 da02192c koutsoub
                                files = filesXMLParser.getViewFiles();
88 da02192c koutsoub
                                for(ContainerObjects o :files)
89 7b27f0dc koutsoub
                                        o.setContainerName(containerName);
90 da02192c koutsoub
                        } else {
91 da02192c koutsoub
                                CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
92 da02192c koutsoub
                                SAXParser saxParser = SAXParserFactory.newInstance()
93 da02192c koutsoub
                                                .newSAXParser();
94 da02192c koutsoub
                                XMLReader xmlReader = saxParser.getXMLReader();
95 da02192c koutsoub
                                xmlReader.setContentHandler(parser);
96 da02192c koutsoub
                                xmlReader.parse(new InputSource(new StringReader(body)));
97 da02192c koutsoub
                                CloudServersException cse = parser.getException();
98 da02192c koutsoub
                                throw cse;
99 da02192c koutsoub
                        }
100 da02192c koutsoub
                } catch (ClientProtocolException e) {
101 da02192c koutsoub
                        CloudServersException cse = new CloudServersException();
102 da02192c koutsoub
                        cse.setMessage(e.getLocalizedMessage());
103 da02192c koutsoub
                        throw cse;
104 da02192c koutsoub
                } catch (IOException e) {
105 da02192c koutsoub
                        CloudServersException cse = new CloudServersException();
106 da02192c koutsoub
                        cse.setMessage(e.getLocalizedMessage());
107 da02192c koutsoub
                        throw cse;
108 da02192c koutsoub
                } catch (ParserConfigurationException e) {
109 da02192c koutsoub
                        CloudServersException cse = new CloudServersException();
110 da02192c koutsoub
                        cse.setMessage(e.getLocalizedMessage());
111 da02192c koutsoub
                        throw cse;
112 da02192c koutsoub
                } catch (SAXException e) {
113 da02192c koutsoub
                        CloudServersException cse = new CloudServersException();
114 da02192c koutsoub
                        cse.setMessage(e.getLocalizedMessage());
115 da02192c koutsoub
                        throw cse;
116 da02192c koutsoub
                } catch (FactoryConfigurationError e) {
117 da02192c koutsoub
                        CloudServersException cse = new CloudServersException();
118 da02192c koutsoub
                        cse.setMessage(e.getLocalizedMessage());
119 da02192c koutsoub
                        throw cse;
120 da02192c koutsoub
                }
121 da02192c koutsoub
                return files;
122 7b27f0dc koutsoub
        }
123 7b27f0dc koutsoub
                
124 7b27f0dc koutsoub
        public List<ContainerObjects> createList(String containerName,
125 7b27f0dc koutsoub
                        boolean shared, String prefix) throws CloudServersException {
126 7b27f0dc koutsoub
                Log.i(LOG,"Create List:"+containerName+ " prefix "+prefix);
127 7b27f0dc koutsoub
                while(prefix.endsWith("/"))
128 7b27f0dc koutsoub
                        prefix = prefix.substring(0,prefix.length()-1);
129 7b27f0dc koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), containerName)
130 7b27f0dc koutsoub
                                + "?format=xml&shared="+shared+"&prefix="+prefix+"&delimiter=/";
131 7b27f0dc koutsoub
                Log.i(LOG," Getting "+containerName+":"+shared+" "+prefix);
132 7b27f0dc koutsoub
                Log.i(LOG," URL:"+url);
133 7b27f0dc koutsoub
                return executeGet(containerName, url);
134 da02192c koutsoub
135 da02192c koutsoub
        }
136 da02192c koutsoub
        
137 7b27f0dc koutsoub
        public List<ContainerObjects> createOtherList(String containerName,
138 7b27f0dc koutsoub
                        String user) throws CloudServersException {
139 7b27f0dc koutsoub
                Log.i(LOG,"Create List:"+containerName);                
140 7b27f0dc koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl().replaceAll(Account.getAccount().getUsername(), user), containerName)
141 856ad13d koutsoub
                                + "?format=xml";
142 7b27f0dc koutsoub
                return executeGet(containerName, url);
143 856ad13d koutsoub
144 856ad13d koutsoub
        }
145 856ad13d koutsoub
        
146 7b27f0dc koutsoub
        public List<ContainerObjects> createList(String containerName,
147 7b27f0dc koutsoub
                        String prefix) throws CloudServersException {
148 7b27f0dc koutsoub
                while(prefix.endsWith("/"))
149 74c976a0 koutsoub
                        prefix = prefix.substring(0,prefix.length()-1);
150 7b27f0dc koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), containerName)
151 74c976a0 koutsoub
                                + "?format=xml&prefix="+prefix+"&delimiter=/";
152 74c976a0 koutsoub
                Log.i(LOG,url);
153 7b27f0dc koutsoub
                return executeGet(containerName, url);
154 856ad13d koutsoub
155 856ad13d koutsoub
        }
156 856ad13d koutsoub
        
157 d37efce5 koutsoub
        public List<ObjectVersion> getObjectVersions(String Container, String Object)
158 d37efce5 koutsoub
                        throws CloudServersException {
159 d37efce5 koutsoub
                CustomHttpClient httpclient = new CustomHttpClient(context);
160 d37efce5 koutsoub
                String url = Account.getAccount().getStorageUrl()+"/"+ Container
161 d37efce5 koutsoub
                                + "/" + Object+"?format=xml&version=list";
162 7b27f0dc koutsoub
                Log.i(LOG,url);
163 d37efce5 koutsoub
                HttpGet get = new HttpGet(url);
164 d37efce5 koutsoub
                List<ObjectVersion> versions =null;
165 d37efce5 koutsoub
                get.addHeader("Content-Type", "application/xml");
166 d37efce5 koutsoub
                get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken);
167 d37efce5 koutsoub
168 d37efce5 koutsoub
                try {
169 d37efce5 koutsoub
                        HttpResponse resp = httpclient.execute(get);
170 d37efce5 koutsoub
                        BasicResponseHandler responseHandler = new BasicResponseHandler();
171 d37efce5 koutsoub
                        String body = responseHandler.handleResponse(resp);
172 d37efce5 koutsoub
173 d37efce5 koutsoub
                        if (resp.getStatusLine().getStatusCode() == 200
174 d37efce5 koutsoub
                                        || resp.getStatusLine().getStatusCode() == 203) {
175 d37efce5 koutsoub
                                ObjectVersionXMLParser filesXMLParser = new ObjectVersionXMLParser();
176 d37efce5 koutsoub
                                SAXParser saxParser = SAXParserFactory.newInstance()
177 d37efce5 koutsoub
                                                .newSAXParser();
178 d37efce5 koutsoub
                                XMLReader xmlReader = saxParser.getXMLReader();
179 d37efce5 koutsoub
                                xmlReader.setContentHandler(filesXMLParser);
180 d37efce5 koutsoub
181 d37efce5 koutsoub
                                xmlReader.parse(new InputSource(new StringReader(body)));
182 d37efce5 koutsoub
                                versions = filesXMLParser.getContainers();
183 d37efce5 koutsoub
                                
184 d37efce5 koutsoub
185 d37efce5 koutsoub
                        } else {
186 d37efce5 koutsoub
                                CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
187 d37efce5 koutsoub
                                SAXParser saxParser = SAXParserFactory.newInstance()
188 d37efce5 koutsoub
                                                .newSAXParser();
189 d37efce5 koutsoub
                                XMLReader xmlReader = saxParser.getXMLReader();
190 d37efce5 koutsoub
                                xmlReader.setContentHandler(parser);
191 d37efce5 koutsoub
                                xmlReader.parse(new InputSource(new StringReader(body)));
192 d37efce5 koutsoub
                                CloudServersException cse = parser.getException();
193 d37efce5 koutsoub
                                throw cse;
194 d37efce5 koutsoub
                        }
195 d37efce5 koutsoub
                } catch (ClientProtocolException e) {
196 d37efce5 koutsoub
                        CloudServersException cse = new CloudServersException();
197 d37efce5 koutsoub
                        cse.setMessage(e.getLocalizedMessage());
198 d37efce5 koutsoub
                        throw cse;
199 d37efce5 koutsoub
                } catch (IOException e) {
200 d37efce5 koutsoub
                        CloudServersException cse = new CloudServersException();
201 d37efce5 koutsoub
                        cse.setMessage(e.getLocalizedMessage());
202 d37efce5 koutsoub
                        throw cse;
203 d37efce5 koutsoub
                } catch (ParserConfigurationException e) {
204 d37efce5 koutsoub
                        CloudServersException cse = new CloudServersException();
205 d37efce5 koutsoub
                        cse.setMessage(e.getLocalizedMessage());
206 d37efce5 koutsoub
                        throw cse;
207 d37efce5 koutsoub
                } catch (SAXException e) {
208 d37efce5 koutsoub
                        CloudServersException cse = new CloudServersException();
209 d37efce5 koutsoub
                        cse.setMessage(e.getLocalizedMessage());
210 d37efce5 koutsoub
                        throw cse;
211 d37efce5 koutsoub
                } catch (FactoryConfigurationError e) {
212 d37efce5 koutsoub
                        CloudServersException cse = new CloudServersException();
213 d37efce5 koutsoub
                        cse.setMessage(e.getLocalizedMessage());
214 d37efce5 koutsoub
                        throw cse;
215 d37efce5 koutsoub
                }
216 d37efce5 koutsoub
                return versions;
217 d37efce5 koutsoub
        }
218 d37efce5 koutsoub
        
219 7b27f0dc koutsoub
        public List<ContainerObjects> createListMyShared(
220 7b27f0dc koutsoub
                        String passName, List<Container> containers, String prefix) throws CloudServersException {
221 ef6fa21a koutsoub
222 ef6fa21a koutsoub
                ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>();
223 ef6fa21a koutsoub
                for(Container con :containers ){
224 856ad13d koutsoub
                        Log.i(LOG,"GEETING FILES OF COntainer:"+con.getName());
225 ef6fa21a koutsoub
                        if(con.getName().equalsIgnoreCase("trash")||con.getName().equals(Container.MYSHARED)||con.getName().equals(Container.OTHERS)){}
226 ef6fa21a koutsoub
                        else
227 ef6fa21a koutsoub
                        try{
228 7b27f0dc koutsoub
                                List<ContainerObjects> temp = createList(con.getName(), true, prefix);
229 ef6fa21a koutsoub
                                for(ContainerObjects o : temp){
230 856ad13d koutsoub
                                        Log.i(LOG,o.getCName()+" "+o.isShared());
231 7b27f0dc koutsoub
                                        if(o.isShared()||o.isSubDir()){
232 856ad13d koutsoub
                                                o.setContainerName(Container.MYSHARED);
233 ef6fa21a koutsoub
                                                files.add(o);
234 856ad13d koutsoub
                                        }
235 ef6fa21a koutsoub
                                }
236 ef6fa21a koutsoub
                        }
237 ef6fa21a koutsoub
                        catch(CloudServersException e){
238 ef6fa21a koutsoub
                                
239 ef6fa21a koutsoub
                        }
240 ef6fa21a koutsoub
                }
241 ef6fa21a koutsoub
                return files;
242 ef6fa21a koutsoub
243 ef6fa21a koutsoub
        }
244 7b27f0dc koutsoub
        private HttpBundle executeMethod(int methodName ,String url) throws CloudServersException{
245 7b27f0dc koutsoub
                return executeMethod(methodName, url, null, null);
246 7b27f0dc koutsoub
        }
247 7b27f0dc koutsoub
        
248 7b27f0dc koutsoub
        private HttpBundle executeMethod(int methodName ,String url,String contentType) throws CloudServersException{
249 7b27f0dc koutsoub
                return executeMethod(methodName, url, contentType, null);
250 7b27f0dc koutsoub
        }
251 7b27f0dc koutsoub
        
252 7b27f0dc koutsoub
        private HttpBundle executeMethod(int methodName ,String url, String contentType, Map<String,String> headers) throws CloudServersException{
253 3d6041e8 Phillip Toohill
                HttpResponse resp = null;
254 b347d5e3 Chmouel Boudjnah
                CustomHttpClient httpclient = new CustomHttpClient(context);
255 7b27f0dc koutsoub
                Log.d(LOG, url);
256 7b27f0dc koutsoub
                HttpRequestBase addObject = null;
257 7b27f0dc koutsoub
                switch (methodName) {
258 7b27f0dc koutsoub
                case GET:
259 7b27f0dc koutsoub
                        addObject = new HttpGet(url);
260 7b27f0dc koutsoub
                        break;
261 7b27f0dc koutsoub
                case PUT:
262 7b27f0dc koutsoub
                        addObject = new HttpPut(url);
263 7b27f0dc koutsoub
                        break;
264 7b27f0dc koutsoub
                case POST:
265 7b27f0dc koutsoub
                        addObject = new HttpPost(url);
266 7b27f0dc koutsoub
                        break;
267 7b27f0dc koutsoub
                case DELETE:
268 7b27f0dc koutsoub
                        addObject = new HttpDelete(url);
269 7b27f0dc koutsoub
                        break;
270 7b27f0dc koutsoub
271 7b27f0dc koutsoub
                default:
272 7b27f0dc koutsoub
                        CloudServersException cse = new CloudServersException();
273 7b27f0dc koutsoub
                        cse.setMessage("Invalid method");
274 7b27f0dc koutsoub
                        throw cse;
275 7b27f0dc koutsoub
                }
276 038ac9a4 koutsoub
277 7b27f0dc koutsoub
                addObject
278 7b27f0dc koutsoub
                                .addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
279 7b27f0dc koutsoub
                if(contentType!=null)
280 7b27f0dc koutsoub
                        addObject.addHeader("Content-Type", contentType);
281 7b27f0dc koutsoub
                //addObject.addHeader("Content-Type",t);
282 7b27f0dc koutsoub
                if(headers!=null)
283 7b27f0dc koutsoub
                        for(Entry<String,String> entry : headers.entrySet()){
284 7b27f0dc koutsoub
                                addObject.addHeader(entry.getKey(),entry.getValue());
285 7b27f0dc koutsoub
                        }
286 3d6041e8 Phillip Toohill
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
287 3d6041e8 Phillip Toohill
288 32731215 Adam Menz
                HttpBundle bundle = new HttpBundle();
289 7b27f0dc koutsoub
                bundle.setCurlRequest(addObject);
290 038ac9a4 koutsoub
291 038ac9a4 koutsoub
                try {
292 7b27f0dc koutsoub
                        resp = httpclient.execute(addObject);
293 32731215 Adam Menz
                        bundle.setHttpResponse(resp);
294 3d6041e8 Phillip Toohill
                } catch (ClientProtocolException e) {
295 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
296 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
297 3d6041e8 Phillip Toohill
                        throw cse;
298 3d6041e8 Phillip Toohill
                } catch (IOException e) {
299 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
300 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
301 3d6041e8 Phillip Toohill
                        throw cse;
302 3d6041e8 Phillip Toohill
                } catch (FactoryConfigurationError e) {
303 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
304 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
305 3d6041e8 Phillip Toohill
                        throw cse;
306 038ac9a4 koutsoub
                }
307 32731215 Adam Menz
                return bundle;
308 3d6041e8 Phillip Toohill
        }
309 038ac9a4 koutsoub
310 7b27f0dc koutsoub
        public HttpBundle deleteObject(String Container, String Object)
311 038ac9a4 koutsoub
                        throws CloudServersException {
312 7b27f0dc koutsoub
                
313 038ac9a4 koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container
314 038ac9a4 koutsoub
                                + "/" + Object);
315 7b27f0dc koutsoub
                return executeMethod(DELETE, url);
316 7b27f0dc koutsoub
        }
317 a2b9aced koutsoub
        
318 a2b9aced koutsoub
        public HttpBundle trashObject(String container, String Object)
319 a2b9aced koutsoub
                        throws CloudServersException {
320 a2b9aced koutsoub
                
321 a2b9aced koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container.TRASH
322 a2b9aced koutsoub
                                + "/" + Object);
323 a2b9aced koutsoub
                Map<String,String> headers = new HashMap<String, String>();
324 a2b9aced koutsoub
                headers.put("X-Move-From","/"+container+"/"+Object);
325 a2b9aced koutsoub
                return executeMethod(PUT, url,"text/plain; charset=UTF-8",headers);
326 a2b9aced koutsoub
        }
327 a2b9aced koutsoub
        
328 a2b9aced koutsoub
        public HttpBundle restoreObject(String container, String Object)
329 a2b9aced koutsoub
                        throws CloudServersException {
330 a2b9aced koutsoub
                
331 a2b9aced koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container.PITHOS
332 a2b9aced koutsoub
                                + "/" + Object);
333 a2b9aced koutsoub
                Map<String,String> headers = new HashMap<String, String>();
334 a2b9aced koutsoub
                headers.put("X-Move-From","/"+container+"/"+Object);
335 a2b9aced koutsoub
                return executeMethod(PUT, url,"text/plain; charset=UTF-8",headers);
336 a2b9aced koutsoub
        }
337 038ac9a4 koutsoub
338 7b27f0dc koutsoub
        public HttpBundle getObject(String Container, String Object)
339 7b27f0dc koutsoub
                        throws CloudServersException {
340 7b27f0dc koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container
341 7b27f0dc koutsoub
                                + "/" + Object);
342 7b27f0dc koutsoub
                return executeMethod(GET, url);
343 51938302 Adam Menz
        }
344 d37efce5 koutsoub
        
345 d37efce5 koutsoub
        
346 038ac9a4 koutsoub
347 038ac9a4 koutsoub
        public HttpBundle addObject(String Container, String Path, String Object,
348 038ac9a4 koutsoub
                        String type) throws CloudServersException {
349 038ac9a4 koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container
350 038ac9a4 koutsoub
                                + "/" + Path + Object);
351 7b27f0dc koutsoub
                return executeMethod(DELETE, url,type);
352 3d6041e8 Phillip Toohill
        }
353 732d9c2c koutsoub
        
354 7b27f0dc koutsoub
        
355 7b27f0dc koutsoub
        
356 732d9c2c koutsoub
        public HttpBundle updateObject(String Container, String Path, String Object,
357 732d9c2c koutsoub
                        String type, Map<String,String> headers) throws CloudServersException {
358 732d9c2c koutsoub
                String url;
359 732d9c2c koutsoub
                if(Container !=null)
360 732d9c2c koutsoub
                        url = getSafeURL(Account.getAccount().getStorageUrl(), Container
361 732d9c2c koutsoub
                                + "/" + Path + Object);
362 732d9c2c koutsoub
                else{
363 732d9c2c koutsoub
                        url = getSafeURL(Account.getAccount().getStorageUrl(),"");
364 732d9c2c koutsoub
                        if(url.endsWith("/"))
365 732d9c2c koutsoub
                                url = url.substring(0, url.length()-1);
366 732d9c2c koutsoub
                }
367 732d9c2c koutsoub
                url =url+"?update=";
368 7b27f0dc koutsoub
                Log.d(LOG, url);
369 7b27f0dc koutsoub
                return executeMethod(DELETE, url,  "        text/plain; charset=UTF-8", headers);
370 732d9c2c koutsoub
        }
371 038ac9a4 koutsoub
372 7b27f0dc koutsoub
        
373 802fbd9e koutsoub
        
374 802fbd9e koutsoub
        /*
375 7b27f0dc koutsoub
         * used for adding sdcard files, requires an extra parameter to store the data
376 802fbd9e koutsoub
         * for the file
377 802fbd9e koutsoub
         */
378 802fbd9e koutsoub
        public HttpBundle addFileObject(String Container, String Path, String Object,
379 802fbd9e koutsoub
                        String type,  String file) throws CloudServersException {
380 802fbd9e koutsoub
                HttpResponse resp = null;
381 802fbd9e koutsoub
                CustomHttpClient httpclient = new CustomHttpClient(context);
382 802fbd9e koutsoub
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container
383 802fbd9e koutsoub
                                + "/" + Path + Object);
384 802fbd9e koutsoub
                HttpPut addObject = new HttpPut(url);
385 802fbd9e koutsoub
386 802fbd9e koutsoub
                addObject
387 802fbd9e koutsoub
                                .addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
388 802fbd9e koutsoub
                addObject.addHeader("Content-Type", type);
389 802fbd9e koutsoub
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
390 802fbd9e koutsoub
                if (file.startsWith("file://")) {
391 802fbd9e koutsoub
            file = file.substring(7);
392 802fbd9e koutsoub
        }
393 802fbd9e koutsoub
                FileEntity tmp = null;
394 802fbd9e koutsoub
                
395 802fbd9e koutsoub
                        tmp = new FileEntity(new File(file),type);
396 802fbd9e koutsoub
                
397 802fbd9e koutsoub
                addObject.setEntity(tmp);
398 802fbd9e koutsoub
399 802fbd9e koutsoub
                HttpBundle bundle = new HttpBundle();
400 802fbd9e koutsoub
                bundle.setCurlRequest(addObject);
401 802fbd9e koutsoub
402 802fbd9e koutsoub
                try {
403 802fbd9e koutsoub
                        resp = httpclient.execute(addObject);
404 32731215 Adam Menz
                        bundle.setHttpResponse(resp);
405 d3ea294b Adam Menz
                } catch (ClientProtocolException e) {
406 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
407 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
408 d3ea294b Adam Menz
                        throw cse;
409 d3ea294b Adam Menz
                } catch (IOException e) {
410 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
411 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
412 d3ea294b Adam Menz
                        throw cse;
413 d3ea294b Adam Menz
                } catch (FactoryConfigurationError e) {
414 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
415 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
416 d3ea294b Adam Menz
                        throw cse;
417 038ac9a4 koutsoub
                }
418 32731215 Adam Menz
                return bundle;
419 d3ea294b Adam Menz
        }
420 038ac9a4 koutsoub
421 038ac9a4 koutsoub
        private String getSafeURL(String badURL, String name) {
422 006434d8 Adam Menz
                URI uri = null;
423 006434d8 Adam Menz
                try {
424 006434d8 Adam Menz
                        uri = new URI("https", badURL.substring(8), "/" + name + "/", "");
425 006434d8 Adam Menz
                } catch (URISyntaxException e1) {
426 006434d8 Adam Menz
                        // TODO Auto-generated catch block
427 006434d8 Adam Menz
                        e1.printStackTrace();
428 006434d8 Adam Menz
                }
429 006434d8 Adam Menz
                String url = null;
430 006434d8 Adam Menz
                try {
431 006434d8 Adam Menz
                        url = uri.toURL().toString();
432 006434d8 Adam Menz
                } catch (MalformedURLException e1) {
433 006434d8 Adam Menz
                        // TODO Auto-generated catch block
434 006434d8 Adam Menz
                        e1.printStackTrace();
435 006434d8 Adam Menz
                }
436 038ac9a4 koutsoub
                return url.substring(0, url.length() - 2);
437 006434d8 Adam Menz
        }
438 3d6041e8 Phillip Toohill
439 3d6041e8 Phillip Toohill
}