root / src / com / rackspace / cloud / files / api / client / ContainerObjectManager.java @ 038ac9a4
History | View | Annotate | Download (9.4 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 |
import android.util.Log; |
30 |
|
31 |
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser; |
32 |
import com.rackspace.cloud.servers.api.client.Account; |
33 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
34 |
import com.rackspace.cloud.servers.api.client.EntityManager; |
35 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
36 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author Phillip Toohill
|
41 |
*
|
42 |
*/
|
43 |
public class ContainerObjectManager extends EntityManager { |
44 |
|
45 |
public String LOG = "ContainerObjectManager"; |
46 |
private Context context; |
47 |
public static final String storageToken = Account.getAccount() |
48 |
.getStorageToken(); |
49 |
|
50 |
public ContainerObjectManager(Context context) { |
51 |
this.context = context;
|
52 |
} |
53 |
|
54 |
public ArrayList<ContainerObjects> createList(boolean detail, |
55 |
String passName) throws CloudServersException { |
56 |
|
57 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
58 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), passName)
|
59 |
+ "?format=xml";
|
60 |
HttpGet get = new HttpGet(url);
|
61 |
ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>(); |
62 |
|
63 |
get.addHeader("Content-Type", "application/xml"); |
64 |
get.addHeader(CustomHttpClient.X_STORAGE_TOKEN, storageToken); |
65 |
|
66 |
try {
|
67 |
HttpResponse resp = httpclient.execute(get); |
68 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
69 |
String body = responseHandler.handleResponse(resp);
|
70 |
|
71 |
if (resp.getStatusLine().getStatusCode() == 200 |
72 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
73 |
ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
|
74 |
SAXParser saxParser = SAXParserFactory.newInstance() |
75 |
.newSAXParser(); |
76 |
XMLReader xmlReader = saxParser.getXMLReader(); |
77 |
xmlReader.setContentHandler(filesXMLParser); |
78 |
|
79 |
xmlReader.parse(new InputSource(new StringReader(body))); |
80 |
files = filesXMLParser.getViewFiles(); |
81 |
|
82 |
} else {
|
83 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
84 |
SAXParser saxParser = SAXParserFactory.newInstance() |
85 |
.newSAXParser(); |
86 |
XMLReader xmlReader = saxParser.getXMLReader(); |
87 |
xmlReader.setContentHandler(parser); |
88 |
xmlReader.parse(new InputSource(new StringReader(body))); |
89 |
CloudServersException cse = parser.getException(); |
90 |
throw cse;
|
91 |
} |
92 |
} catch (ClientProtocolException e) {
|
93 |
CloudServersException cse = new CloudServersException();
|
94 |
cse.setMessage(e.getLocalizedMessage()); |
95 |
throw cse;
|
96 |
} catch (IOException e) { |
97 |
CloudServersException cse = new CloudServersException();
|
98 |
cse.setMessage(e.getLocalizedMessage()); |
99 |
throw cse;
|
100 |
} catch (ParserConfigurationException e) { |
101 |
CloudServersException cse = new CloudServersException();
|
102 |
cse.setMessage(e.getLocalizedMessage()); |
103 |
throw cse;
|
104 |
} catch (SAXException e) {
|
105 |
CloudServersException cse = new CloudServersException();
|
106 |
cse.setMessage(e.getLocalizedMessage()); |
107 |
throw cse;
|
108 |
} catch (FactoryConfigurationError e) { |
109 |
CloudServersException cse = new CloudServersException();
|
110 |
cse.setMessage(e.getLocalizedMessage()); |
111 |
throw cse;
|
112 |
} |
113 |
return files;
|
114 |
|
115 |
} |
116 |
|
117 |
public HttpBundle deleteObject(String Container, String Object) |
118 |
throws CloudServersException {
|
119 |
HttpResponse resp = null;
|
120 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
121 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
122 |
+ "/" + Object); |
123 |
HttpDelete deleteObject = new HttpDelete(url);
|
124 |
|
125 |
deleteObject.addHeader("X-Auth-Token", Account.getAccount()
|
126 |
.getAuthToken()); |
127 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
128 |
|
129 |
HttpBundle bundle = new HttpBundle();
|
130 |
bundle.setCurlRequest(deleteObject); |
131 |
|
132 |
try {
|
133 |
resp = httpclient.execute(deleteObject); |
134 |
bundle.setHttpResponse(resp); |
135 |
} catch (ClientProtocolException e) {
|
136 |
CloudServersException cse = new CloudServersException();
|
137 |
cse.setMessage(e.getLocalizedMessage()); |
138 |
throw cse;
|
139 |
} catch (IOException e) { |
140 |
CloudServersException cse = new CloudServersException();
|
141 |
cse.setMessage(e.getLocalizedMessage()); |
142 |
throw cse;
|
143 |
} catch (FactoryConfigurationError e) { |
144 |
CloudServersException cse = new CloudServersException();
|
145 |
cse.setMessage(e.getLocalizedMessage()); |
146 |
throw cse;
|
147 |
} |
148 |
return bundle;
|
149 |
} |
150 |
|
151 |
public HttpBundle getObject(String Container, String Object) |
152 |
throws CloudServersException {
|
153 |
HttpResponse resp = null;
|
154 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
155 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
156 |
+ "/" + Object); |
157 |
HttpGet getObject = new HttpGet(url);
|
158 |
getObject |
159 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
160 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
161 |
|
162 |
HttpBundle bundle = new HttpBundle();
|
163 |
bundle.setCurlRequest(getObject); |
164 |
|
165 |
try {
|
166 |
resp = httpclient.execute(getObject); |
167 |
bundle.setHttpResponse(resp); |
168 |
} catch (ClientProtocolException e) {
|
169 |
CloudServersException cse = new CloudServersException();
|
170 |
cse.setMessage(e.getLocalizedMessage()); |
171 |
throw cse;
|
172 |
} catch (IOException e) { |
173 |
CloudServersException cse = new CloudServersException();
|
174 |
cse.setMessage(e.getLocalizedMessage()); |
175 |
throw cse;
|
176 |
} catch (FactoryConfigurationError e) { |
177 |
CloudServersException cse = new CloudServersException();
|
178 |
cse.setMessage(e.getLocalizedMessage()); |
179 |
throw cse;
|
180 |
} |
181 |
return bundle;
|
182 |
} |
183 |
|
184 |
public HttpBundle addObject(String Container, String Path, String Object, |
185 |
String type) throws CloudServersException { |
186 |
HttpResponse resp = null;
|
187 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
188 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
189 |
+ "/" + Path + Object); |
190 |
HttpPut addObject = new HttpPut(url);
|
191 |
|
192 |
addObject |
193 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
194 |
addObject.addHeader("Content-Type", type);
|
195 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
196 |
|
197 |
HttpBundle bundle = new HttpBundle();
|
198 |
bundle.setCurlRequest(addObject); |
199 |
|
200 |
try {
|
201 |
resp = httpclient.execute(addObject); |
202 |
bundle.setHttpResponse(resp); |
203 |
} catch (ClientProtocolException e) {
|
204 |
CloudServersException cse = new CloudServersException();
|
205 |
cse.setMessage(e.getLocalizedMessage()); |
206 |
throw cse;
|
207 |
} catch (IOException e) { |
208 |
CloudServersException cse = new CloudServersException();
|
209 |
cse.setMessage(e.getLocalizedMessage()); |
210 |
throw cse;
|
211 |
} catch (FactoryConfigurationError e) { |
212 |
CloudServersException cse = new CloudServersException();
|
213 |
cse.setMessage(e.getLocalizedMessage()); |
214 |
throw cse;
|
215 |
} |
216 |
return bundle;
|
217 |
} |
218 |
|
219 |
/*
|
220 |
* used for adding text files, requires an extra parameter to store the data
|
221 |
* for the file
|
222 |
*/
|
223 |
public HttpBundle addObject(String Container, String Path, String Object, |
224 |
String type, String data) throws CloudServersException { |
225 |
HttpResponse resp = null;
|
226 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
227 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), Container |
228 |
+ "/" + Path + Object); |
229 |
HttpPut addObject = new HttpPut(url);
|
230 |
|
231 |
addObject |
232 |
.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
233 |
addObject.addHeader("Content-Type", type);
|
234 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
235 |
|
236 |
StringEntity tmp = null;
|
237 |
try {
|
238 |
tmp = new StringEntity(data);
|
239 |
} catch (UnsupportedEncodingException e) { |
240 |
CloudServersException cse = new CloudServersException();
|
241 |
cse.setMessage(e.getLocalizedMessage()); |
242 |
throw cse;
|
243 |
} |
244 |
addObject.setEntity(tmp); |
245 |
|
246 |
HttpBundle bundle = new HttpBundle();
|
247 |
bundle.setCurlRequest(addObject); |
248 |
|
249 |
try {
|
250 |
resp = httpclient.execute(addObject); |
251 |
bundle.setHttpResponse(resp); |
252 |
} catch (ClientProtocolException e) {
|
253 |
CloudServersException cse = new CloudServersException();
|
254 |
cse.setMessage(e.getLocalizedMessage()); |
255 |
throw cse;
|
256 |
} catch (IOException e) { |
257 |
CloudServersException cse = new CloudServersException();
|
258 |
cse.setMessage(e.getLocalizedMessage()); |
259 |
throw cse;
|
260 |
} catch (FactoryConfigurationError e) { |
261 |
CloudServersException cse = new CloudServersException();
|
262 |
cse.setMessage(e.getLocalizedMessage()); |
263 |
throw cse;
|
264 |
} |
265 |
return bundle;
|
266 |
} |
267 |
|
268 |
private String getSafeURL(String badURL, String name) { |
269 |
URI uri = null; |
270 |
try {
|
271 |
uri = new URI("https", badURL.substring(8), "/" + name + "/", ""); |
272 |
} catch (URISyntaxException e1) { |
273 |
// TODO Auto-generated catch block
|
274 |
e1.printStackTrace(); |
275 |
} |
276 |
String url = null; |
277 |
try {
|
278 |
url = uri.toURL().toString(); |
279 |
} catch (MalformedURLException e1) { |
280 |
// TODO Auto-generated catch block
|
281 |
e1.printStackTrace(); |
282 |
} |
283 |
return url.substring(0, url.length() - 2); |
284 |
} |
285 |
|
286 |
} |