root / src / com / rackspace / cloud / files / api / client / ContainerManager.java @ 5018a7f8
History | View | Annotate | Download (11.2 kB)
1 |
package com.rackspace.cloud.files.api.client; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
import java.net.MalformedURLException; |
6 |
import java.net.URI; |
7 |
import java.net.URISyntaxException; |
8 |
import java.util.ArrayList; |
9 |
|
10 |
import javax.xml.parsers.FactoryConfigurationError; |
11 |
import javax.xml.parsers.ParserConfigurationException; |
12 |
import javax.xml.parsers.SAXParser; |
13 |
import javax.xml.parsers.SAXParserFactory; |
14 |
|
15 |
import org.apache.http.HttpResponse; |
16 |
import org.apache.http.client.ClientProtocolException; |
17 |
import org.apache.http.client.methods.HttpDelete; |
18 |
import org.apache.http.client.methods.HttpGet; |
19 |
import org.apache.http.client.methods.HttpPost; |
20 |
import org.apache.http.client.methods.HttpPut; |
21 |
import org.apache.http.impl.client.BasicResponseHandler; |
22 |
import org.apache.http.protocol.RequestExpectContinue; |
23 |
import org.xml.sax.InputSource; |
24 |
import org.xml.sax.SAXException; |
25 |
import org.xml.sax.XMLReader; |
26 |
|
27 |
import android.content.Context; |
28 |
import android.text.Editable; |
29 |
import android.util.Log; |
30 |
|
31 |
import com.rackspace.cloud.files.api.client.parsers.ContainerXMLParser; |
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 |
* @author Phillip Toohill
|
40 |
*
|
41 |
*/
|
42 |
public class ContainerManager extends EntityManager { |
43 |
private Context context; |
44 |
|
45 |
public ContainerManager(Context context) { |
46 |
this.context = context;
|
47 |
} |
48 |
|
49 |
public HttpBundle create(Editable editable) throws CloudServersException { |
50 |
HttpResponse resp = null;
|
51 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
52 |
|
53 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), editable.toString());
|
54 |
HttpPut put = new HttpPut(url);
|
55 |
|
56 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
57 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
58 |
|
59 |
HttpBundle bundle = new HttpBundle();
|
60 |
bundle.setCurlRequest(put); |
61 |
|
62 |
try {
|
63 |
resp = httpclient.execute(put); |
64 |
bundle.setHttpResponse(resp); |
65 |
} catch (ClientProtocolException e) {
|
66 |
CloudServersException cse = new CloudServersException();
|
67 |
cse.setMessage(e.getLocalizedMessage()); |
68 |
throw cse;
|
69 |
} catch (IOException e) { |
70 |
CloudServersException cse = new CloudServersException();
|
71 |
cse.setMessage(e.getLocalizedMessage()); |
72 |
throw cse;
|
73 |
} catch (FactoryConfigurationError e) { |
74 |
CloudServersException cse = new CloudServersException();
|
75 |
cse.setMessage(e.getLocalizedMessage()); |
76 |
throw cse;
|
77 |
} |
78 |
return bundle;
|
79 |
} |
80 |
|
81 |
public ArrayList<Container> createCDNList(boolean detail) throws CloudServersException { |
82 |
|
83 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
84 |
HttpGet get = new HttpGet(Account.getAccount().getCdnManagementUrl()+"?format=xml"); |
85 |
ArrayList<Container> cdnContainers = new ArrayList<Container>(); |
86 |
|
87 |
get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
88 |
|
89 |
try {
|
90 |
HttpResponse resp = httpclient.execute(get); |
91 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
92 |
String body = responseHandler.handleResponse(resp);
|
93 |
|
94 |
if (resp.getStatusLine().getStatusCode() == 200) { |
95 |
ContainerXMLParser cdnContainerXMLParser = new ContainerXMLParser();
|
96 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
97 |
XMLReader xmlReader = saxParser.getXMLReader(); |
98 |
xmlReader.setContentHandler(cdnContainerXMLParser); |
99 |
|
100 |
xmlReader.parse(new InputSource(new StringReader(body))); |
101 |
cdnContainers = cdnContainerXMLParser.getContainers(); |
102 |
} else {
|
103 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
104 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
105 |
XMLReader xmlReader = saxParser.getXMLReader(); |
106 |
xmlReader.setContentHandler(parser); |
107 |
xmlReader.parse(new InputSource(new StringReader(body))); |
108 |
CloudServersException cse = parser.getException(); |
109 |
throw cse;
|
110 |
} |
111 |
} catch (ClientProtocolException e) {
|
112 |
CloudServersException cse = new CloudServersException();
|
113 |
cse.setMessage(e.getLocalizedMessage()); |
114 |
throw cse;
|
115 |
} catch (IOException e) { |
116 |
CloudServersException cse = new CloudServersException();
|
117 |
cse.setMessage(e.getLocalizedMessage()); |
118 |
throw cse;
|
119 |
} catch (ParserConfigurationException e) { |
120 |
CloudServersException cse = new CloudServersException();
|
121 |
cse.setMessage(e.getLocalizedMessage()); |
122 |
throw cse;
|
123 |
} catch (SAXException e) {
|
124 |
CloudServersException cse = new CloudServersException();
|
125 |
cse.setMessage(e.getLocalizedMessage()); |
126 |
throw cse;
|
127 |
} catch (FactoryConfigurationError e) { |
128 |
CloudServersException cse = new CloudServersException();
|
129 |
cse.setMessage(e.getLocalizedMessage()); |
130 |
throw cse;
|
131 |
} |
132 |
|
133 |
|
134 |
return cdnContainers;
|
135 |
} |
136 |
|
137 |
|
138 |
public HttpBundle enable(String container, String ttl, String logRet) |
139 |
throws CloudServersException {
|
140 |
HttpResponse resp = null;
|
141 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
142 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(), container);
|
143 |
HttpPut put = new HttpPut(url);
|
144 |
|
145 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
146 |
put.addHeader("X-TTL", ttl);
|
147 |
put.addHeader("X-Log-Retention", logRet);
|
148 |
Log.d("info cdn", url);
|
149 |
Log.v("info cdn", ttl + container + logRet);
|
150 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
151 |
|
152 |
HttpBundle bundle = new HttpBundle();
|
153 |
bundle.setCurlRequest(put); |
154 |
|
155 |
try {
|
156 |
resp = httpclient.execute(put); |
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 |
public HttpBundle disable(String container, String cdn, String ttl, String logRet) |
174 |
throws CloudServersException {
|
175 |
HttpResponse resp = null;
|
176 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
177 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(), container);
|
178 |
HttpPost post = new HttpPost(url);
|
179 |
|
180 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
181 |
post.addHeader("X-TTL", ttl);
|
182 |
post.addHeader("X-Log-Retention", logRet);
|
183 |
post.addHeader("X-CDN-Enabled", cdn);
|
184 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
185 |
|
186 |
HttpBundle bundle = new HttpBundle();
|
187 |
bundle.setCurlRequest(post); |
188 |
|
189 |
try {
|
190 |
resp = httpclient.execute(post); |
191 |
bundle.setHttpResponse(resp); |
192 |
} catch (ClientProtocolException e) {
|
193 |
CloudServersException cse = new CloudServersException();
|
194 |
cse.setMessage(e.getLocalizedMessage()); |
195 |
throw cse;
|
196 |
} catch (IOException e) { |
197 |
CloudServersException cse = new CloudServersException();
|
198 |
cse.setMessage(e.getLocalizedMessage()); |
199 |
throw cse;
|
200 |
} catch (FactoryConfigurationError e) { |
201 |
CloudServersException cse = new CloudServersException();
|
202 |
cse.setMessage(e.getLocalizedMessage()); |
203 |
throw cse;
|
204 |
} |
205 |
return bundle;
|
206 |
} |
207 |
|
208 |
public HttpBundle delete(String string) throws CloudServersException { |
209 |
HttpResponse resp = null;
|
210 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
211 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), string);
|
212 |
HttpDelete put = new HttpDelete(url);
|
213 |
|
214 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
215 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
216 |
|
217 |
HttpBundle bundle = new HttpBundle();
|
218 |
bundle.setCurlRequest(put); |
219 |
|
220 |
try {
|
221 |
resp = httpclient.execute(put); |
222 |
bundle.setHttpResponse(resp); |
223 |
} catch (ClientProtocolException e) {
|
224 |
CloudServersException cse = new CloudServersException();
|
225 |
cse.setMessage(e.getLocalizedMessage()); |
226 |
throw cse;
|
227 |
} catch (IOException e) { |
228 |
CloudServersException cse = new CloudServersException();
|
229 |
cse.setMessage(e.getLocalizedMessage()); |
230 |
throw cse;
|
231 |
} catch (FactoryConfigurationError e) { |
232 |
CloudServersException cse = new CloudServersException();
|
233 |
cse.setMessage(e.getLocalizedMessage()); |
234 |
throw cse;
|
235 |
} |
236 |
return bundle;
|
237 |
} |
238 |
|
239 |
public ArrayList<Container> createList(boolean detail) |
240 |
throws CloudServersException {
|
241 |
|
242 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
243 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl() + "?format=xml"); |
244 |
ArrayList<Container> containers = new ArrayList<Container>(); |
245 |
|
246 |
get.addHeader("X-Storage-Token", Account.getAccount().getStorageToken());
|
247 |
get.addHeader("Content-Type", "application/xml"); |
248 |
|
249 |
try {
|
250 |
HttpResponse resp = httpclient.execute(get); |
251 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
252 |
String body = responseHandler.handleResponse(resp);
|
253 |
|
254 |
if (resp.getStatusLine().getStatusCode() == 200 |
255 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
256 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser();
|
257 |
SAXParser saxParser = SAXParserFactory.newInstance() |
258 |
.newSAXParser(); |
259 |
XMLReader xmlReader = saxParser.getXMLReader(); |
260 |
xmlReader.setContentHandler(containerXMLParser); |
261 |
|
262 |
xmlReader.parse(new InputSource(new StringReader(body))); |
263 |
containers = containerXMLParser.getContainers(); |
264 |
} else {
|
265 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
266 |
SAXParser saxParser = SAXParserFactory.newInstance() |
267 |
.newSAXParser(); |
268 |
XMLReader xmlReader = saxParser.getXMLReader(); |
269 |
xmlReader.setContentHandler(parser); |
270 |
xmlReader.parse(new InputSource(new StringReader(body))); |
271 |
CloudServersException cse = parser.getException(); |
272 |
throw cse;
|
273 |
} |
274 |
} catch (ClientProtocolException e) {
|
275 |
CloudServersException cse = new CloudServersException();
|
276 |
cse.setMessage(e.getLocalizedMessage()); |
277 |
throw cse;
|
278 |
} catch (IOException e) { |
279 |
CloudServersException cse = new CloudServersException();
|
280 |
cse.setMessage(e.getLocalizedMessage()); |
281 |
throw cse;
|
282 |
} catch (ParserConfigurationException e) { |
283 |
CloudServersException cse = new CloudServersException();
|
284 |
cse.setMessage(e.getLocalizedMessage()); |
285 |
throw cse;
|
286 |
} catch (SAXException e) {
|
287 |
CloudServersException cse = new CloudServersException();
|
288 |
cse.setMessage(e.getLocalizedMessage()); |
289 |
throw cse;
|
290 |
} catch (FactoryConfigurationError e) { |
291 |
CloudServersException cse = new CloudServersException();
|
292 |
cse.setMessage(e.getLocalizedMessage()); |
293 |
throw cse;
|
294 |
} |
295 |
|
296 |
return containers;
|
297 |
} |
298 |
|
299 |
private String getSafeURL(String badURL, String name){ |
300 |
URI uri = null; |
301 |
try {
|
302 |
uri = new URI("https", badURL.substring(8), "/" + name.toString()+"/", ""); |
303 |
} catch (URISyntaxException e1) { |
304 |
// TODO Auto-generated catch block
|
305 |
e1.printStackTrace(); |
306 |
} |
307 |
String url = null; |
308 |
try {
|
309 |
url = uri.toURL().toString(); |
310 |
} catch (MalformedURLException e1) { |
311 |
// TODO Auto-generated catch block
|
312 |
e1.printStackTrace(); |
313 |
} |
314 |
return url.substring(0, url.length()-1); |
315 |
} |
316 |
|
317 |
} |