Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7 kB)

1 3d6041e8 Phillip Toohill
package com.rackspace.cloud.files.api.client;
2 3d6041e8 Phillip Toohill
3 3d6041e8 Phillip Toohill
import java.io.IOException;
4 3d6041e8 Phillip Toohill
import java.io.StringReader;
5 3d6041e8 Phillip Toohill
import java.util.ArrayList;
6 3d6041e8 Phillip Toohill
7 3d6041e8 Phillip Toohill
import javax.xml.parsers.FactoryConfigurationError;
8 3d6041e8 Phillip Toohill
import javax.xml.parsers.ParserConfigurationException;
9 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParser;
10 3d6041e8 Phillip Toohill
import javax.xml.parsers.SAXParserFactory;
11 3d6041e8 Phillip Toohill
12 3d6041e8 Phillip Toohill
import org.apache.http.HttpResponse;
13 3d6041e8 Phillip Toohill
import org.apache.http.client.ClientProtocolException;
14 3d6041e8 Phillip Toohill
import org.apache.http.client.methods.HttpDelete;
15 3d6041e8 Phillip Toohill
import org.apache.http.client.methods.HttpGet;
16 d3ea294b Adam Menz
import org.apache.http.client.methods.HttpPut;
17 3d6041e8 Phillip Toohill
import org.apache.http.impl.client.BasicResponseHandler;
18 3d6041e8 Phillip Toohill
import org.apache.http.protocol.RequestExpectContinue;
19 3d6041e8 Phillip Toohill
import org.xml.sax.InputSource;
20 3d6041e8 Phillip Toohill
import org.xml.sax.SAXException;
21 3d6041e8 Phillip Toohill
import org.xml.sax.XMLReader;
22 3d6041e8 Phillip Toohill
23 b347d5e3 Chmouel Boudjnah
import android.content.Context;
24 3d6041e8 Phillip Toohill
25 3d6041e8 Phillip Toohill
import com.rackspace.cloud.files.api.client.parsers.ContainerObjectXMLparser;
26 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.Account;
27 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.CloudServersException;
28 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.EntityManager;
29 3d6041e8 Phillip Toohill
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
30 3d6041e8 Phillip Toohill
31 3d6041e8 Phillip Toohill
/** 
32 3d6041e8 Phillip Toohill
 * 
33 3d6041e8 Phillip Toohill
 * @author Phillip Toohill
34 3d6041e8 Phillip Toohill
 *
35 3d6041e8 Phillip Toohill
 */
36 3d6041e8 Phillip Toohill
public class ContainerObjectManager extends EntityManager {
37 3d6041e8 Phillip Toohill
38 3d6041e8 Phillip Toohill
        public String LOG = "ContainerObjectManager";
39 b347d5e3 Chmouel Boudjnah
        private Context context;
40 83774e79 Adam Menz
        public static final String storageToken = Account.getAccount().getStorageToken();
41 3d6041e8 Phillip Toohill
        
42 b347d5e3 Chmouel Boudjnah
        public ContainerObjectManager(Context context) {
43 b347d5e3 Chmouel Boudjnah
                this.context = context;
44 b347d5e3 Chmouel Boudjnah
        }
45 3d6041e8 Phillip Toohill
46 37a14877 Adam Menz
        public ArrayList<ContainerObjects> createList(boolean detail, String passName) throws CloudServersException {
47 3d6041e8 Phillip Toohill
                
48 b347d5e3 Chmouel Boudjnah
                CustomHttpClient httpclient = new CustomHttpClient(context);
49 37a14877 Adam Menz
                HttpGet get = new HttpGet(Account.getAccount().getStorageUrl()+"/"+passName + "?format=xml");
50 3d6041e8 Phillip Toohill
                ArrayList<ContainerObjects> files = new ArrayList<ContainerObjects>();
51 3d6041e8 Phillip Toohill
                
52 3d6041e8 Phillip Toohill
                
53 3d6041e8 Phillip Toohill
                get.addHeader("Content-Type", "application/xml");
54 3d6041e8 Phillip Toohill
                get.addHeader("X-Storage-Token", storageToken);
55 3d6041e8 Phillip Toohill
                
56 3d6041e8 Phillip Toohill
                
57 3d6041e8 Phillip Toohill
                                
58 3d6041e8 Phillip Toohill
                try {                        
59 3d6041e8 Phillip Toohill
                        HttpResponse resp = httpclient.execute(get);                    
60 3d6041e8 Phillip Toohill
                    BasicResponseHandler responseHandler = new BasicResponseHandler();
61 3d6041e8 Phillip Toohill
                    String body = responseHandler.handleResponse(resp);
62 3d6041e8 Phillip Toohill
                    
63 3d6041e8 Phillip Toohill
                    if (resp.getStatusLine().getStatusCode() == 200 || resp.getStatusLine().getStatusCode() == 203) {                            
64 3d6041e8 Phillip Toohill
                            ContainerObjectXMLparser filesXMLParser = new ContainerObjectXMLparser();
65 3d6041e8 Phillip Toohill
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
66 3d6041e8 Phillip Toohill
                            XMLReader xmlReader = saxParser.getXMLReader();
67 3d6041e8 Phillip Toohill
                            xmlReader.setContentHandler(filesXMLParser);
68 3d6041e8 Phillip Toohill
                    
69 3d6041e8 Phillip Toohill
                            xmlReader.parse(new InputSource(new StringReader(body)));
70 3d6041e8 Phillip Toohill
                            files = filesXMLParser.getViewFiles();
71 3d6041e8 Phillip Toohill
                            
72 3d6041e8 Phillip Toohill
                    } else {
73 3d6041e8 Phillip Toohill
                            CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
74 3d6041e8 Phillip Toohill
                            SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
75 3d6041e8 Phillip Toohill
                            XMLReader xmlReader = saxParser.getXMLReader();
76 3d6041e8 Phillip Toohill
                            xmlReader.setContentHandler(parser);
77 3d6041e8 Phillip Toohill
                            xmlReader.parse(new InputSource(new StringReader(body)));                            
78 3d6041e8 Phillip Toohill
                            CloudServersException cse = parser.getException();                            
79 3d6041e8 Phillip Toohill
                            throw cse;
80 3d6041e8 Phillip Toohill
                    }
81 3d6041e8 Phillip Toohill
                } catch (ClientProtocolException e) {
82 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
83 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
84 3d6041e8 Phillip Toohill
                        throw cse;
85 3d6041e8 Phillip Toohill
                } catch (IOException e) {
86 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
87 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
88 3d6041e8 Phillip Toohill
                        throw cse;
89 3d6041e8 Phillip Toohill
                } catch (ParserConfigurationException e) {
90 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
91 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
92 3d6041e8 Phillip Toohill
                        throw cse;
93 3d6041e8 Phillip Toohill
                } catch (SAXException e) {
94 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
95 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
96 3d6041e8 Phillip Toohill
                        throw cse;
97 3d6041e8 Phillip Toohill
                } catch (FactoryConfigurationError e) {
98 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
99 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
100 3d6041e8 Phillip Toohill
                        throw cse;
101 3d6041e8 Phillip Toohill
                }
102 3d6041e8 Phillip Toohill
                return files;
103 3d6041e8 Phillip Toohill
                
104 3d6041e8 Phillip Toohill
        }
105 3d6041e8 Phillip Toohill
106 3d6041e8 Phillip Toohill
        public HttpResponse deleteObject(String Container, String Object) throws CloudServersException {
107 3d6041e8 Phillip Toohill
                HttpResponse resp = null;
108 b347d5e3 Chmouel Boudjnah
                CustomHttpClient httpclient = new CustomHttpClient(context);
109 83774e79 Adam Menz
                HttpDelete deleteObject = new HttpDelete(Account.getAccount().getStorageUrl() + "/" + Container + "/" + Object);
110 3d6041e8 Phillip Toohill
                                
111 83774e79 Adam Menz
                deleteObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
112 3d6041e8 Phillip Toohill
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
113 3d6041e8 Phillip Toohill
114 3d6041e8 Phillip Toohill
                try {                        
115 3d6041e8 Phillip Toohill
                        resp = httpclient.execute(deleteObject);
116 3d6041e8 Phillip Toohill
                } catch (ClientProtocolException e) {
117 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
118 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
119 3d6041e8 Phillip Toohill
                        throw cse;
120 3d6041e8 Phillip Toohill
                } catch (IOException e) {
121 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
122 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
123 3d6041e8 Phillip Toohill
                        throw cse;
124 3d6041e8 Phillip Toohill
                } catch (FactoryConfigurationError e) {
125 3d6041e8 Phillip Toohill
                        CloudServersException cse = new CloudServersException();
126 3d6041e8 Phillip Toohill
                        cse.setMessage(e.getLocalizedMessage());
127 3d6041e8 Phillip Toohill
                        throw cse;
128 3d6041e8 Phillip Toohill
                }        
129 3d6041e8 Phillip Toohill
                return resp;
130 3d6041e8 Phillip Toohill
        }
131 3d6041e8 Phillip Toohill
        
132 d3ea294b Adam Menz
        public HttpResponse addObject(String Container, String Path, String Object, String type) throws CloudServersException {
133 d3ea294b Adam Menz
                HttpResponse resp = null;
134 d3ea294b Adam Menz
                CustomHttpClient httpclient = new CustomHttpClient(context);
135 d3ea294b Adam Menz
                HttpPut addObject = new HttpPut(Account.getAccount().getStorageUrl() + "/" + Container + "/" + Path + Object);
136 d3ea294b Adam Menz
                                
137 d3ea294b Adam Menz
                addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
138 d3ea294b Adam Menz
                addObject.addHeader("Content-Type", type);
139 d3ea294b Adam Menz
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
140 d3ea294b Adam Menz
141 d3ea294b Adam Menz
                try {                        
142 d3ea294b Adam Menz
                        resp = httpclient.execute(addObject);
143 d3ea294b Adam Menz
                } catch (ClientProtocolException e) {
144 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
145 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
146 d3ea294b Adam Menz
                        throw cse;
147 d3ea294b Adam Menz
                } catch (IOException e) {
148 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
149 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
150 d3ea294b Adam Menz
                        throw cse;
151 d3ea294b Adam Menz
                } catch (FactoryConfigurationError e) {
152 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
153 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
154 d3ea294b Adam Menz
                        throw cse;
155 d3ea294b Adam Menz
                }        
156 d3ea294b Adam Menz
                return resp;
157 d3ea294b Adam Menz
        }
158 d3ea294b Adam Menz
        
159 d3ea294b Adam Menz
        /*
160 d3ea294b Adam Menz
         * used for adding text files, requires an extra parameter to 
161 d3ea294b Adam Menz
         * store the data for the file
162 d3ea294b Adam Menz
         */
163 d3ea294b Adam Menz
        public HttpResponse addObject(String Container, String Path, String Object, String type, String data) throws CloudServersException {
164 d3ea294b Adam Menz
                HttpResponse resp = null;
165 d3ea294b Adam Menz
                CustomHttpClient httpclient = new CustomHttpClient(context);
166 d3ea294b Adam Menz
                HttpPut addObject = new HttpPut(Account.getAccount().getStorageUrl() + "/" + Container + "/" + Path + Object);
167 d3ea294b Adam Menz
                                
168 d3ea294b Adam Menz
                addObject.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
169 d3ea294b Adam Menz
                addObject.addHeader("Content-Type", type);
170 d3ea294b Adam Menz
                httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
171 d3ea294b Adam Menz
172 d3ea294b Adam Menz
                try {                        
173 d3ea294b Adam Menz
                        resp = httpclient.execute(addObject);
174 d3ea294b Adam Menz
                } catch (ClientProtocolException e) {
175 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
176 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
177 d3ea294b Adam Menz
                        throw cse;
178 d3ea294b Adam Menz
                } catch (IOException e) {
179 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
180 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
181 d3ea294b Adam Menz
                        throw cse;
182 d3ea294b Adam Menz
                } catch (FactoryConfigurationError e) {
183 d3ea294b Adam Menz
                        CloudServersException cse = new CloudServersException();
184 d3ea294b Adam Menz
                        cse.setMessage(e.getLocalizedMessage());
185 d3ea294b Adam Menz
                        throw cse;
186 d3ea294b Adam Menz
                }        
187 d3ea294b Adam Menz
                return resp;
188 d3ea294b Adam Menz
        }
189 3d6041e8 Phillip Toohill
190 3d6041e8 Phillip Toohill
}