Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (9.5 kB)

1
package com.rackspace.cloud.files.api.client;
2

    
3
import java.io.IOException;
4
import java.io.StringReader;
5
import java.io.UnsupportedEncodingException;
6
import java.net.MalformedURLException;
7
import java.net.URI;
8
import java.net.URISyntaxException;
9
import java.util.ArrayList;
10

    
11
import javax.xml.parsers.FactoryConfigurationError;
12
import javax.xml.parsers.ParserConfigurationException;
13
import javax.xml.parsers.SAXParser;
14
import javax.xml.parsers.SAXParserFactory;
15

    
16
import org.apache.http.HttpResponse;
17
import org.apache.http.client.ClientProtocolException;
18
import org.apache.http.client.methods.HttpDelete;
19
import org.apache.http.client.methods.HttpGet;
20
import org.apache.http.client.methods.HttpPut;
21
import org.apache.http.entity.StringEntity;
22
import org.apache.http.impl.client.BasicResponseHandler;
23
import org.apache.http.protocol.RequestExpectContinue;
24
import org.xml.sax.InputSource;
25
import org.xml.sax.SAXException;
26
import org.xml.sax.XMLReader;
27

    
28
import android.content.Context;
29

    
30
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser;
31
import com.rackspace.cloud.servers.api.client.Account;
32
import com.rackspace.cloud.servers.api.client.CloudServersException;
33
import com.rackspace.cloud.servers.api.client.EntityManager;
34
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
35
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
36

    
37
/** 
38
 * 
39
 * @author Phillip Toohill
40
 *
41
 */
42
public class ContainerObjectManager extends EntityManager {
43

    
44
        public String LOG = "ContainerObjectManager";
45
        private Context context;
46
        public static final String storageToken = Account.getAccount().getStorageToken();
47
        
48
        public ContainerObjectManager(Context context) {
49
                this.context = context;
50
        }
51

    
52
        public ArrayList<ContainerObjects> createList(boolean detail, String passName) throws CloudServersException {
53
                
54
                CustomHttpClient httpclient = new CustomHttpClient(context);
55
                String url = getSafeURL(Account.getAccount().getStorageUrl(), passName) + "?format=xml";
56
                HttpGet get = new HttpGet(url);
57
                ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>();
58
                
59
                
60
                get.addHeader("Content-Type", "application/xml");
61
                get.addHeader("X-Storage-Token", storageToken);
62
                
63
                
64
                                
65
                try {                        
66
                        HttpResponse resp = httpclient.execute(get);                    
67
                    BasicResponseHandler responseHandler = new BasicResponseHandler();
68
                    String body = responseHandler.handleResponse(resp);
69
                    
70
                    if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) {                            
71
                            ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
72
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
73
                            XMLReader xmlReader = saxParser.getXMLReader();
74
                            xmlReader.setContentHandler(filesXMLParser);
75
                    
76
                            xmlReader.parse(new InputSource(new StringReader(body)));
77
                            files = filesXMLParser.getViewFiles();
78
                            
79
                    } else {
80
                            CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
81
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
82
                            XMLReader xmlReader = saxParser.getXMLReader();
83
                            xmlReader.setContentHandler(parser);
84
                            xmlReader.parse(new InputSource(new StringReader(body)));                            
85
                            CloudServersException cse = parser.getException();                            
86
                            throw cse;
87
                    }
88
                } catch (ClientProtocolException e) {
89
                        CloudServersException cse = new CloudServersException();
90
                        cse.setMessage(e.getLocalizedMessage());
91
                        throw cse;
92
                } catch (IOException e) {
93
                        CloudServersException cse = new CloudServersException();
94
                        cse.setMessage(e.getLocalizedMessage());
95
                        throw cse;
96
                } catch (ParserConfigurationException e) {
97
                        CloudServersException cse = new CloudServersException();
98
                        cse.setMessage(e.getLocalizedMessage());
99
                        throw cse;
100
                } catch (SAXException e) {
101
                        CloudServersException cse = new CloudServersException();
102
                        cse.setMessage(e.getLocalizedMessage());
103
                        throw cse;
104
                } catch (FactoryConfigurationError e) {
105
                        CloudServersException cse = new CloudServersException();
106
                        cse.setMessage(e.getLocalizedMessage());
107
                        throw cse;
108
                }
109
                return files;
110
                
111
        }
112

    
113
        public HttpBundle deleteObject(String Container, String Object) throws CloudServersException {
114
                HttpResponse resp = null;
115
                CustomHttpClient httpclient = new CustomHttpClient(context);
116
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Object);
117
                HttpDelete deleteObject = new HttpDelete(url);
118
                                
119
                deleteObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
120
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
121

    
122
                HttpBundle bundle = new HttpBundle();
123
                bundle.setCurlRequest(deleteObject);
124
                
125
                try {                        
126
                        resp = httpclient.execute(deleteObject);
127
                        bundle.setHttpResponse(resp);
128
                } catch (ClientProtocolException e) {
129
                        CloudServersException cse = new CloudServersException();
130
                        cse.setMessage(e.getLocalizedMessage());
131
                        throw cse;
132
                } catch (IOException e) {
133
                        CloudServersException cse = new CloudServersException();
134
                        cse.setMessage(e.getLocalizedMessage());
135
                        throw cse;
136
                } catch (FactoryConfigurationError e) {
137
                        CloudServersException cse = new CloudServersException();
138
                        cse.setMessage(e.getLocalizedMessage());
139
                        throw cse;
140
                }        
141
                return bundle;
142
        }
143
        
144
        public HttpBundle getObject(String Container, String Object) throws CloudServersException {
145
                HttpResponse resp = null;
146
                CustomHttpClient httpclient = new CustomHttpClient(context);
147
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Object);
148
                HttpGet getObject = new HttpGet(url);
149
                getObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
150
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
151

    
152
                HttpBundle bundle = new HttpBundle();
153
                bundle.setCurlRequest(getObject);
154
                
155
                try {                        
156
                        resp = httpclient.execute(getObject);
157
                        bundle.setHttpResponse(resp);
158
                } catch (ClientProtocolException e) {
159
                        CloudServersException cse = new CloudServersException();
160
                        cse.setMessage(e.getLocalizedMessage());
161
                        throw cse;
162
                } catch (IOException e) {
163
                        CloudServersException cse = new CloudServersException();
164
                        cse.setMessage(e.getLocalizedMessage());
165
                        throw cse;
166
                } catch (FactoryConfigurationError e) {
167
                        CloudServersException cse = new CloudServersException();
168
                        cse.setMessage(e.getLocalizedMessage());
169
                        throw cse;
170
                }        
171
                return bundle;
172
        }
173
        
174
        public HttpBundle addObject(String Container, String Path, String Object, String type) throws CloudServersException {
175
                HttpResponse resp = null;
176
                CustomHttpClient httpclient = new CustomHttpClient(context);
177
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Path + Object);
178
                HttpPut addObject = new HttpPut(url);
179
                
180
                addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
181
                addObject.addHeader("Content-Type", type);
182
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
183
                
184
                HttpBundle bundle = new HttpBundle();
185
                bundle.setCurlRequest(addObject);
186

    
187
                try {                        
188
                        resp = httpclient.execute(addObject);
189
                        bundle.setHttpResponse(resp);
190
                } catch (ClientProtocolException e) {
191
                        CloudServersException cse = new CloudServersException();
192
                        cse.setMessage(e.getLocalizedMessage());
193
                        throw cse;
194
                } catch (IOException e) {
195
                        CloudServersException cse = new CloudServersException();
196
                        cse.setMessage(e.getLocalizedMessage());
197
                        throw cse;
198
                } catch (FactoryConfigurationError e) {
199
                        CloudServersException cse = new CloudServersException();
200
                        cse.setMessage(e.getLocalizedMessage());
201
                        throw cse;
202
                }        
203
                return bundle;
204
        }
205
        
206
        /*
207
         * used for adding text files, requires an extra parameter to 
208
         * store the data for the file
209
         */
210
        public HttpBundle addObject(String Container, String Path, String Object, String type, String data) throws CloudServersException {
211
                HttpResponse resp = null;
212
                CustomHttpClient httpclient = new CustomHttpClient(context);
213
                String url = getSafeURL(Account.getAccount().getStorageUrl(), Container + "/" + Path + Object);
214
                HttpPut addObject = new HttpPut(url);
215
                                
216
                addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
217
                addObject.addHeader("Content-Type", type);
218
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
219

    
220
                StringEntity tmp = null;
221
                try {
222
                        tmp = new StringEntity(data);
223
                } catch (UnsupportedEncodingException e) {
224
                        CloudServersException cse = new CloudServersException();
225
                        cse.setMessage(e.getLocalizedMessage());
226
                        throw cse;
227
                }
228
                addObject.setEntity(tmp);
229
                
230
                HttpBundle bundle = new HttpBundle();
231
                bundle.setCurlRequest(addObject);
232
                
233
                try {                        
234
                        resp = httpclient.execute(addObject);
235
                        bundle.setHttpResponse(resp);
236
                } catch (ClientProtocolException e) {
237
                        CloudServersException cse = new CloudServersException();
238
                        cse.setMessage(e.getLocalizedMessage());
239
                        throw cse;
240
                } catch (IOException e) {
241
                        CloudServersException cse = new CloudServersException();
242
                        cse.setMessage(e.getLocalizedMessage());
243
                        throw cse;
244
                } catch (FactoryConfigurationError e) {
245
                        CloudServersException cse = new CloudServersException();
246
                        cse.setMessage(e.getLocalizedMessage());
247
                        throw cse;
248
                }        
249
                return bundle;
250
        }
251
        
252
        private String getSafeURL(String badURL, String name){
253
                URI uri = null;
254
                try {
255
                        uri = new URI("https", badURL.substring(8), "/" + name + "/", "");
256
                } catch (URISyntaxException e1) {
257
                        // TODO Auto-generated catch block
258
                        e1.printStackTrace();
259
                }
260
                String url = null;
261
                try {
262
                        url = uri.toURL().toString();
263
                } catch (MalformedURLException e1) {
264
                        // TODO Auto-generated catch block
265
                        e1.printStackTrace();
266
                }
267
                return url.substring(0, url.length()-2);
268
        }
269

    
270
}