root / src / com / rackspacecloud / android / PasswordServerActivity.java @ 639ae8d6
History | View | Annotate | Download (5.3 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 android.app.Activity; |
19 |
import android.content.Context; |
20 |
import android.content.Intent; |
21 |
import android.os.AsyncTask; |
22 |
import android.os.Bundle; |
23 |
import android.view.View; |
24 |
import android.view.View.OnClickListener; |
25 |
import android.widget.Button; |
26 |
import android.widget.EditText; |
27 |
import android.widget.Toast; |
28 |
|
29 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
30 |
import com.rackspace.cloud.servers.api.client.Server; |
31 |
import com.rackspace.cloud.servers.api.client.ServerManager; |
32 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
33 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
34 |
|
35 |
public class PasswordServerActivity extends Activity implements OnClickListener{ |
36 |
|
37 |
private Server server;
|
38 |
private String modifiedPassword; |
39 |
|
40 |
public void onCreate(Bundle savedInstanceState) { |
41 |
super.onCreate(savedInstanceState);
|
42 |
setContentView(R.layout.viewchangepassword); |
43 |
server = (Server) this.getIntent().getExtras().get("server"); |
44 |
setupButtons(); |
45 |
} |
46 |
|
47 |
private void setupButtons() { |
48 |
Button update = (Button) findViewById(R.id.password_change_button); |
49 |
update.setOnClickListener(this);
|
50 |
} |
51 |
|
52 |
@Override
|
53 |
public void onClick(View v) { |
54 |
String password = ((EditText)findViewById(R.id.password_edittext)).getText().toString();
|
55 |
String confirm = ((EditText)findViewById(R.id.password_confirm_edittext)).getText().toString();
|
56 |
if(password.equals(confirm)){
|
57 |
modifiedPassword = password; |
58 |
new PasswordServerTask().execute((Void[]) null); |
59 |
} |
60 |
else{
|
61 |
showToast("The password and confirmation do not match");
|
62 |
} |
63 |
} |
64 |
|
65 |
private void showToast(String message) { |
66 |
Context context = getApplicationContext();
|
67 |
int duration = Toast.LENGTH_SHORT;
|
68 |
Toast toast = Toast.makeText(context, message, duration); |
69 |
toast.show(); |
70 |
} |
71 |
|
72 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
73 |
CloudServersException cse = new CloudServersException();
|
74 |
try {
|
75 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
76 |
String body = responseHandler.handleResponse(response);
|
77 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
78 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
79 |
XMLReader xmlReader = saxParser.getXMLReader(); |
80 |
xmlReader.setContentHandler(parser); |
81 |
xmlReader.parse(new InputSource(new StringReader(body))); |
82 |
cse = parser.getException(); |
83 |
} catch (ClientProtocolException e) {
|
84 |
cse = new CloudServersException();
|
85 |
cse.setMessage(e.getLocalizedMessage()); |
86 |
} catch (IOException e) { |
87 |
cse = new CloudServersException();
|
88 |
cse.setMessage(e.getLocalizedMessage()); |
89 |
} catch (ParserConfigurationException e) { |
90 |
cse = new CloudServersException();
|
91 |
cse.setMessage(e.getLocalizedMessage()); |
92 |
} catch (SAXException e) {
|
93 |
cse = new CloudServersException();
|
94 |
cse.setMessage(e.getLocalizedMessage()); |
95 |
} catch (FactoryConfigurationError e) { |
96 |
cse = new CloudServersException();
|
97 |
cse.setMessage(e.getLocalizedMessage()); |
98 |
} |
99 |
return cse;
|
100 |
} |
101 |
|
102 |
private void startServerError(String message, HttpBundle bundle){ |
103 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
104 |
viewIntent.putExtra("errorMessage", message);
|
105 |
viewIntent.putExtra("response", bundle.getResponseText());
|
106 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
107 |
startActivity(viewIntent); |
108 |
} |
109 |
|
110 |
private class PasswordServerTask extends AsyncTask<Void, Void, HttpBundle> { |
111 |
|
112 |
private CloudServersException exception;
|
113 |
|
114 |
protected void onPreExecute(){ |
115 |
showToast("Change root password process has begun");
|
116 |
} |
117 |
|
118 |
@Override
|
119 |
protected HttpBundle doInBackground(Void... arg0) { |
120 |
HttpBundle bundle = null;
|
121 |
try {
|
122 |
bundle = (new ServerManager()).changePassword(server, modifiedPassword, getApplicationContext());
|
123 |
} catch (CloudServersException e) {
|
124 |
exception = e; |
125 |
} |
126 |
return bundle;
|
127 |
} |
128 |
|
129 |
@Override
|
130 |
protected void onPostExecute(HttpBundle bundle) { |
131 |
HttpResponse response = bundle.getResponse(); |
132 |
if (response != null) { |
133 |
int statusCode = response.getStatusLine().getStatusCode();
|
134 |
if(statusCode == 204){ |
135 |
String mustMatch = "The server's root password has successfully been changed."; |
136 |
Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT); |
137 |
passwordError.show(); |
138 |
finish(); |
139 |
} |
140 |
if (statusCode != 204) { |
141 |
CloudServersException cse = parseCloudServersException(response); |
142 |
if ("".equals(cse.getMessage())) { |
143 |
startServerError("There was a problem changing your password.", bundle);
|
144 |
} else {
|
145 |
startServerError("There was a problem changing your password: " + cse.getMessage() + " " + statusCode, bundle); |
146 |
} |
147 |
} |
148 |
} else if (exception != null) { |
149 |
startServerError("There was a problem changing your password: " + exception.getMessage(), bundle);
|
150 |
|
151 |
} |
152 |
} |
153 |
|
154 |
|
155 |
} |
156 |
} |