root / src / com / rackspace / cloud / files / api / client / ContainerManager.java @ e6c34a0f
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.v("cdn manager", ttl + container + logRet);
|
149 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
150 |
|
151 |
HttpBundle bundle = new HttpBundle();
|
152 |
bundle.setCurlRequest(put); |
153 |
|
154 |
try {
|
155 |
resp = httpclient.execute(put); |
156 |
bundle.setHttpResponse(resp); |
157 |
} catch (ClientProtocolException e) {
|
158 |
CloudServersException cse = new CloudServersException();
|
159 |
cse.setMessage(e.getLocalizedMessage()); |
160 |
throw cse;
|
161 |
} catch (IOException e) { |
162 |
CloudServersException cse = new CloudServersException();
|
163 |
cse.setMessage(e.getLocalizedMessage()); |
164 |
throw cse;
|
165 |
} catch (FactoryConfigurationError e) { |
166 |
CloudServersException cse = new CloudServersException();
|
167 |
cse.setMessage(e.getLocalizedMessage()); |
168 |
throw cse;
|
169 |
} |
170 |
return bundle;
|
171 |
} |
172 |
public HttpBundle disable(String container, String cdn, String ttl, String logRet) |
173 |
throws CloudServersException {
|
174 |
HttpResponse resp = null;
|
175 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
176 |
String url = getSafeURL(Account.getAccount().getCdnManagementUrl(), container);
|
177 |
HttpPost post = new HttpPost(url);
|
178 |
|
179 |
post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
180 |
post.addHeader("X-TTL", ttl);
|
181 |
post.addHeader("X-Log-Retention", logRet);
|
182 |
post.addHeader("X-CDN-Enabled", cdn);
|
183 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
184 |
|
185 |
HttpBundle bundle = new HttpBundle();
|
186 |
bundle.setCurlRequest(post); |
187 |
|
188 |
try {
|
189 |
resp = httpclient.execute(post); |
190 |
bundle.setHttpResponse(resp); |
191 |
} catch (ClientProtocolException e) {
|
192 |
CloudServersException cse = new CloudServersException();
|
193 |
cse.setMessage(e.getLocalizedMessage()); |
194 |
throw cse;
|
195 |
} catch (IOException e) { |
196 |
CloudServersException cse = new CloudServersException();
|
197 |
cse.setMessage(e.getLocalizedMessage()); |
198 |
throw cse;
|
199 |
} catch (FactoryConfigurationError e) { |
200 |
CloudServersException cse = new CloudServersException();
|
201 |
cse.setMessage(e.getLocalizedMessage()); |
202 |
throw cse;
|
203 |
} |
204 |
return bundle;
|
205 |
} |
206 |
|
207 |
public HttpBundle delete(String string) throws CloudServersException { |
208 |
HttpResponse resp = null;
|
209 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
210 |
String url = getSafeURL(Account.getAccount().getStorageUrl(), string);
|
211 |
HttpDelete put = new HttpDelete(url);
|
212 |
|
213 |
put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
|
214 |
httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class); |
215 |
|
216 |
HttpBundle bundle = new HttpBundle();
|
217 |
bundle.setCurlRequest(put); |
218 |
|
219 |
try {
|
220 |
resp = httpclient.execute(put); |
221 |
bundle.setHttpResponse(resp); |
222 |
} catch (ClientProtocolException e) {
|
223 |
CloudServersException cse = new CloudServersException();
|
224 |
cse.setMessage(e.getLocalizedMessage()); |
225 |
throw cse;
|
226 |
} catch (IOException e) { |
227 |
CloudServersException cse = new CloudServersException();
|
228 |
cse.setMessage(e.getLocalizedMessage()); |
229 |
throw cse;
|
230 |
} catch (FactoryConfigurationError e) { |
231 |
CloudServersException cse = new CloudServersException();
|
232 |
cse.setMessage(e.getLocalizedMessage()); |
233 |
throw cse;
|
234 |
} |
235 |
return bundle;
|
236 |
} |
237 |
|
238 |
public ArrayList<Container> createList(boolean detail) |
239 |
throws CloudServersException {
|
240 |
|
241 |
CustomHttpClient httpclient = new CustomHttpClient(context);
|
242 |
HttpGet get = new HttpGet(Account.getAccount().getStorageUrl() + "?format=xml"); |
243 |
ArrayList<Container> containers = new ArrayList<Container>(); |
244 |
|
245 |
get.addHeader("X-Storage-Token", Account.getAccount().getStorageToken());
|
246 |
get.addHeader("Content-Type", "application/xml"); |
247 |
|
248 |
try {
|
249 |
HttpResponse resp = httpclient.execute(get); |
250 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
251 |
String body = responseHandler.handleResponse(resp);
|
252 |
|
253 |
if (resp.getStatusLine().getStatusCode() == 200 |
254 |
|| resp.getStatusLine().getStatusCode() == 203) {
|
255 |
ContainerXMLParser containerXMLParser = new ContainerXMLParser();
|
256 |
SAXParser saxParser = SAXParserFactory.newInstance() |
257 |
.newSAXParser(); |
258 |
XMLReader xmlReader = saxParser.getXMLReader(); |
259 |
xmlReader.setContentHandler(containerXMLParser); |
260 |
|
261 |
xmlReader.parse(new InputSource(new StringReader(body))); |
262 |
containers = containerXMLParser.getContainers(); |
263 |
} else {
|
264 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
265 |
SAXParser saxParser = SAXParserFactory.newInstance() |
266 |
.newSAXParser(); |
267 |
XMLReader xmlReader = saxParser.getXMLReader(); |
268 |
xmlReader.setContentHandler(parser); |
269 |
xmlReader.parse(new InputSource(new StringReader(body))); |
270 |
CloudServersException cse = parser.getException(); |
271 |
throw cse;
|
272 |
} |
273 |
} catch (ClientProtocolException e) {
|
274 |
CloudServersException cse = new CloudServersException();
|
275 |
cse.setMessage(e.getLocalizedMessage()); |
276 |
throw cse;
|
277 |
} catch (IOException e) { |
278 |
CloudServersException cse = new CloudServersException();
|
279 |
cse.setMessage(e.getLocalizedMessage()); |
280 |
throw cse;
|
281 |
} catch (ParserConfigurationException e) { |
282 |
CloudServersException cse = new CloudServersException();
|
283 |
cse.setMessage(e.getLocalizedMessage()); |
284 |
throw cse;
|
285 |
} catch (SAXException e) {
|
286 |
CloudServersException cse = new CloudServersException();
|
287 |
cse.setMessage(e.getLocalizedMessage()); |
288 |
throw cse;
|
289 |
} catch (FactoryConfigurationError e) { |
290 |
CloudServersException cse = new CloudServersException();
|
291 |
cse.setMessage(e.getLocalizedMessage()); |
292 |
throw cse;
|
293 |
} |
294 |
|
295 |
return containers;
|
296 |
} |
297 |
|
298 |
private String getSafeURL(String badURL, String name){ |
299 |
URI uri = null; |
300 |
try {
|
301 |
uri = new URI("https", badURL.substring(8), "/" + name.toString()+"/", ""); |
302 |
} catch (URISyntaxException e1) { |
303 |
// TODO Auto-generated catch block
|
304 |
e1.printStackTrace(); |
305 |
} |
306 |
String url = null; |
307 |
try {
|
308 |
url = uri.toURL().toString(); |
309 |
} catch (MalformedURLException e1) { |
310 |
// TODO Auto-generated catch block
|
311 |
e1.printStackTrace(); |
312 |
} |
313 |
return url.substring(0, url.length()-1); |
314 |
} |
315 |
|
316 |
} |