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