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