root / src / com / rackspacecloud / android / AddFileActivity.java @ 006434d8
History | View | Annotate | Download (5.6 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.StringReader; |
5 |
|
6 |
import javax.xml.parsers.FactoryConfigurationError; |
7 |
import javax.xml.parsers.ParserConfigurationException; |
8 |
import javax.xml.parsers.SAXParser; |
9 |
import javax.xml.parsers.SAXParserFactory; |
10 |
|
11 |
import org.apache.http.HttpResponse; |
12 |
import org.apache.http.client.ClientProtocolException; |
13 |
import org.apache.http.impl.client.BasicResponseHandler; |
14 |
import org.xml.sax.InputSource; |
15 |
import org.xml.sax.SAXException; |
16 |
import org.xml.sax.XMLReader; |
17 |
|
18 |
import com.rackspace.cloud.files.api.client.ContainerObjectManager; |
19 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
20 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
21 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
22 |
|
23 |
import android.app.Activity; |
24 |
import android.app.AlertDialog; |
25 |
import android.content.Context; |
26 |
import android.content.DialogInterface; |
27 |
import android.content.Intent; |
28 |
import android.os.AsyncTask; |
29 |
import android.os.Bundle; |
30 |
import android.view.View; |
31 |
import android.view.View.OnClickListener; |
32 |
import android.widget.Button; |
33 |
import android.widget.EditText; |
34 |
|
35 |
public class AddFileActivity extends Activity implements OnClickListener{ |
36 |
|
37 |
private Context context; |
38 |
private EditText fileName;
|
39 |
private EditText contents;
|
40 |
private String containerName; |
41 |
private String path; |
42 |
|
43 |
/** Called when the activity is first created. */
|
44 |
@Override
|
45 |
public void onCreate(Bundle savedInstanceState) { |
46 |
super.onCreate(savedInstanceState);
|
47 |
setContentView(R.layout.addtextfile); |
48 |
context = getApplicationContext(); |
49 |
containerName = (String) this.getIntent().getExtras().get("Cname"); |
50 |
path = (String) this.getIntent().getExtras().get("curPath"); |
51 |
setUpInputs(); |
52 |
} |
53 |
|
54 |
private void setUpInputs(){ |
55 |
((Button) findViewById(R.id.new_file_button)).setOnClickListener(this); |
56 |
fileName = ((EditText)findViewById(R.id.file_name_text)); |
57 |
contents = ((EditText)findViewById(R.id.new_file_text)); |
58 |
} |
59 |
|
60 |
public void onClick(View arg0) { |
61 |
if ("".equals(fileName.getText().toString())) { |
62 |
showAlert("Required Fields Missing", " File name is required."); |
63 |
} else {
|
64 |
//showActivityIndicators();
|
65 |
new SaveFileTask().execute((Void[]) null); |
66 |
} |
67 |
} |
68 |
|
69 |
//using cloudServersException, it works for us too
|
70 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
71 |
CloudServersException cse = new CloudServersException();
|
72 |
try {
|
73 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
74 |
String body = responseHandler.handleResponse(response);
|
75 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
76 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
77 |
XMLReader xmlReader = saxParser.getXMLReader(); |
78 |
xmlReader.setContentHandler(parser); |
79 |
xmlReader.parse(new InputSource(new StringReader(body))); |
80 |
cse = parser.getException(); |
81 |
} catch (ClientProtocolException e) {
|
82 |
cse = new CloudServersException();
|
83 |
cse.setMessage(e.getLocalizedMessage()); |
84 |
} catch (IOException e) { |
85 |
cse = new CloudServersException();
|
86 |
cse.setMessage(e.getLocalizedMessage()); |
87 |
} catch (ParserConfigurationException e) { |
88 |
cse = new CloudServersException();
|
89 |
cse.setMessage(e.getLocalizedMessage()); |
90 |
} catch (SAXException e) {
|
91 |
cse = new CloudServersException();
|
92 |
cse.setMessage(e.getLocalizedMessage()); |
93 |
} catch (FactoryConfigurationError e) { |
94 |
cse = new CloudServersException();
|
95 |
cse.setMessage(e.getLocalizedMessage()); |
96 |
} |
97 |
return cse;
|
98 |
} |
99 |
|
100 |
private void showAlert(String title, String message) { |
101 |
try {
|
102 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
103 |
alert.setTitle(title); |
104 |
alert.setMessage(message); |
105 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
106 |
public void onClick(DialogInterface dialog, int which) { |
107 |
return;
|
108 |
} }); |
109 |
alert.show(); |
110 |
} catch (Exception e) { |
111 |
e.printStackTrace(); |
112 |
} |
113 |
} |
114 |
|
115 |
private void startFileError(String message, HttpBundle bundle){ |
116 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
117 |
viewIntent.putExtra("errorMessage", message);
|
118 |
viewIntent.putExtra("response", bundle.getResponseText());
|
119 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
120 |
startActivity(viewIntent); |
121 |
} |
122 |
|
123 |
private class SaveFileTask extends AsyncTask<Void, Void, HttpBundle> { |
124 |
private CloudServersException exception;
|
125 |
|
126 |
@Override
|
127 |
protected HttpBundle doInBackground(Void... arg0) { |
128 |
HttpBundle bundle = null;
|
129 |
try {
|
130 |
bundle = (new ContainerObjectManager(context)).addObject(containerName, path, fileName.getText().toString(), "text/plain", contents.getText().toString()); |
131 |
} catch (CloudServersException e) {
|
132 |
exception = e; |
133 |
} |
134 |
return bundle;
|
135 |
} |
136 |
|
137 |
@Override
|
138 |
protected void onPostExecute(HttpBundle bundle) { |
139 |
HttpResponse response = bundle.getResponse(); |
140 |
if (response != null) { |
141 |
int statusCode = response.getStatusLine().getStatusCode();
|
142 |
if (statusCode == 201) { |
143 |
setResult(Activity.RESULT_OK); |
144 |
finish(); |
145 |
} else {
|
146 |
CloudServersException cse = parseCloudServersException(response); |
147 |
if ("".equals(cse.getMessage())) { |
148 |
startFileError("There was a problem creating your file.", bundle);
|
149 |
} else {
|
150 |
startFileError("There was a problem creating your file: " + cse.getMessage() + " Check file name and try again", bundle); |
151 |
} |
152 |
} |
153 |
} else if (exception != null) { |
154 |
startFileError("There was a problem creating your file: " + exception.getMessage()+" Check file name and try again", bundle); |
155 |
} |
156 |
} |
157 |
} |
158 |
} |