root / src / com / rackspacecloud / android / PasswordServerActivity.java @ f58bf9f4
History | View | Annotate | Download (5 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.app.AlertDialog; |
20 |
import android.content.DialogInterface; |
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.parsers.CloudServersFaultXMLParser; |
33 |
|
34 |
public class PasswordServerActivity extends Activity implements OnClickListener{ |
35 |
|
36 |
private Server server;
|
37 |
private String modifiedPassword; |
38 |
|
39 |
public void onCreate(Bundle savedInstanceState) { |
40 |
super.onCreate(savedInstanceState);
|
41 |
setContentView(R.layout.viewchangepassword); |
42 |
server = (Server) this.getIntent().getExtras().get("server"); |
43 |
setupButtons(); |
44 |
} |
45 |
|
46 |
private void setupButtons() { |
47 |
Button update = (Button) findViewById(R.id.password_change_button); |
48 |
update.setOnClickListener(this);
|
49 |
} |
50 |
|
51 |
@Override
|
52 |
public void onClick(View v) { |
53 |
String password = ((EditText)findViewById(R.id.password_edittext)).getText().toString();
|
54 |
String confirm = ((EditText)findViewById(R.id.password_confirm_edittext)).getText().toString();
|
55 |
if(password.equals(confirm)){
|
56 |
new PasswordServerTask().execute((Void[]) null); |
57 |
} |
58 |
else{
|
59 |
String mustMatch = "The password and confirmation do not match."; |
60 |
Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT); |
61 |
passwordError.show(); |
62 |
} |
63 |
} |
64 |
|
65 |
private void showAlert(String title, String message) { |
66 |
AlertDialog alert = new AlertDialog.Builder(this).create(); |
67 |
alert.setTitle(title); |
68 |
alert.setMessage(message); |
69 |
alert.setButton("OK", new DialogInterface.OnClickListener() { |
70 |
public void onClick(DialogInterface dialog, int which) { |
71 |
return;
|
72 |
} }); |
73 |
alert.show(); |
74 |
} |
75 |
|
76 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
77 |
CloudServersException cse = new CloudServersException();
|
78 |
try {
|
79 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
80 |
String body = responseHandler.handleResponse(response);
|
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 |
cse = parser.getException(); |
87 |
} catch (ClientProtocolException e) {
|
88 |
cse = new CloudServersException();
|
89 |
cse.setMessage(e.getLocalizedMessage()); |
90 |
} catch (IOException e) { |
91 |
cse = new CloudServersException();
|
92 |
cse.setMessage(e.getLocalizedMessage()); |
93 |
} catch (ParserConfigurationException e) { |
94 |
cse = new CloudServersException();
|
95 |
cse.setMessage(e.getLocalizedMessage()); |
96 |
} catch (SAXException e) {
|
97 |
cse = new CloudServersException();
|
98 |
cse.setMessage(e.getLocalizedMessage()); |
99 |
} catch (FactoryConfigurationError e) { |
100 |
cse = new CloudServersException();
|
101 |
cse.setMessage(e.getLocalizedMessage()); |
102 |
} |
103 |
return cse;
|
104 |
} |
105 |
|
106 |
private class PasswordServerTask extends AsyncTask<Void, Void, HttpResponse> { |
107 |
|
108 |
private CloudServersException exception;
|
109 |
|
110 |
@Override
|
111 |
protected HttpResponse doInBackground(Void... arg0) { |
112 |
HttpResponse resp = null;
|
113 |
try {
|
114 |
resp = (new ServerManager()).changePassword(server, modifiedPassword);
|
115 |
} catch (CloudServersException e) {
|
116 |
exception = e; |
117 |
} |
118 |
return resp;
|
119 |
} |
120 |
|
121 |
@Override
|
122 |
protected void onPostExecute(HttpResponse response) { |
123 |
if (response != null) { |
124 |
int statusCode = response.getStatusLine().getStatusCode();
|
125 |
if(statusCode == 204){ |
126 |
String mustMatch = "The server's root password has successfully been changed."; |
127 |
Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT); |
128 |
passwordError.show(); |
129 |
finish(); |
130 |
} |
131 |
if (statusCode != 204) { |
132 |
CloudServersException cse = parseCloudServersException(response); |
133 |
if ("".equals(cse.getMessage())) { |
134 |
showAlert("Error", "There was a problem changing your password."); |
135 |
} else {
|
136 |
showAlert("Error", "There was a problem changing your password: " + cse.getMessage() + " " + statusCode); |
137 |
} |
138 |
} |
139 |
} else if (exception != null) { |
140 |
showAlert("Error", "There was a problem changing your password: " + exception.getMessage()); |
141 |
|
142 |
} |
143 |
} |
144 |
|
145 |
|
146 |
} |
147 |
} |