Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / PasswordServerActivity.java @ 038ac9a4

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
35
                OnClickListener {
36

    
37
        private Server server;
38
        private String modifiedPassword;
39

    
40
        public void onCreate(Bundle savedInstanceState) {
41
                super.onCreate(savedInstanceState);
42
                trackPageView(PAGE_PASSCODE);
43
                setContentView(R.layout.viewchangepassword);
44
                server = (Server) this.getIntent().getExtras().get("server");
45
                setupButtons();
46
        }
47

    
48
        private void setupButtons() {
49
                Button update = (Button) findViewById(R.id.password_change_button);
50
                update.setOnClickListener(this);
51
        }
52

    
53
        @Override
54
        public void onClick(View v) {
55
                String password = ((EditText) findViewById(R.id.password_edittext))
56
                                .getText().toString();
57
                String confirm = ((EditText) findViewById(R.id.password_confirm_edittext))
58
                                .getText().toString();
59
                if (password.equals(confirm)) {
60
                        trackEvent(CATEGORY_SERVER, EVENT_PASSWORD, "", -1);
61
                        modifiedPassword = password;
62
                        new PasswordServerTask().execute((Void[]) null);
63
                } else {
64
                        showToast("The password and confirmation do not match");
65
                }
66
        }
67

    
68
        private void showToast(String message) {
69
                Context context = getApplicationContext();
70
                int duration = Toast.LENGTH_SHORT;
71
                Toast toast = Toast.makeText(context, message, duration);
72
                toast.show();
73
        }
74

    
75
        private CloudServersException parseCloudServersException(
76
                        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 void startServerError(String message, HttpBundle bundle) {
107
                Intent viewIntent = new Intent(getApplicationContext(),
108
                                ServerErrorActivity.class);
109
                viewIntent.putExtra("errorMessage", message);
110
                viewIntent.putExtra("response", bundle.getResponseText());
111
                viewIntent.putExtra("request", bundle.getCurlRequest());
112
                startActivity(viewIntent);
113
        }
114

    
115
        private class PasswordServerTask extends AsyncTask<Void, Void, HttpBundle> {
116

    
117
                private CloudServersException exception;
118

    
119
                protected void onPreExecute() {
120
                        showToast("Change root password process has begun");
121
                }
122

    
123
                @Override
124
                protected HttpBundle doInBackground(Void... arg0) {
125
                        HttpBundle bundle = null;
126
                        try {
127
                                bundle = (new ServerManager()).changePassword(server,
128
                                                modifiedPassword, getApplicationContext());
129
                        } catch (CloudServersException e) {
130
                                exception = e;
131
                        }
132
                        return bundle;
133
                }
134

    
135
                @Override
136
                protected void onPostExecute(HttpBundle bundle) {
137
                        HttpResponse response = bundle.getResponse();
138
                        if (response != null) {
139
                                int statusCode = response.getStatusLine().getStatusCode();
140
                                if (statusCode == 204) {
141
                                        String mustMatch = "The server's root password has successfully been changed.";
142
                                        Toast passwordError = Toast.makeText(
143
                                                        getApplicationContext(), mustMatch,
144
                                                        Toast.LENGTH_SHORT);
145
                                        passwordError.show();
146
                                        finish();
147
                                }
148
                                if (statusCode != 204) {
149
                                        CloudServersException cse = parseCloudServersException(response);
150
                                        if ("".equals(cse.getMessage())) {
151
                                                startServerError(
152
                                                                "There was a problem changing your password.",
153
                                                                bundle);
154
                                        } else {
155
                                                startServerError(
156
                                                                "There was a problem changing your password: "
157
                                                                                + cse.getMessage() + " " + statusCode,
158
                                                                bundle);
159
                                        }
160
                                }
161
                        } else if (exception != null) {
162
                                startServerError("There was a problem changing your password: "
163
                                                + exception.getMessage(), bundle);
164

    
165
                        }
166
                }
167

    
168
        }
169
}