Back to project page android-rackspacecloud.
The source code is released under:
MIT License
If you think the Android project android-rackspacecloud listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.rackspacecloud.android; /* w ww . j a v a2 s . c o m*/ import java.io.IOException; import java.io.StringReader; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.impl.client.BasicResponseHandler; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import android.content.Context; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.rackspace.cloud.servers.api.client.CloudServersException; import com.rackspace.cloud.servers.api.client.Server; import com.rackspace.cloud.servers.api.client.ServerManager; import com.rackspace.cloud.servers.api.client.http.HttpBundle; import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; public class PasswordServerActivity extends GaActivity implements OnClickListener{ private Server server; private String modifiedPassword; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); trackPageView(PAGE_PASSCODE); setContentView(R.layout.viewchangepassword); server = (Server) this.getIntent().getExtras().get("server"); setupButtons(); } private void setupButtons() { Button update = (Button) findViewById(R.id.password_change_button); update.setOnClickListener(this); } @Override public void onClick(View v) { String password = ((EditText)findViewById(R.id.password_edittext)).getText().toString(); String confirm = ((EditText)findViewById(R.id.password_confirm_edittext)).getText().toString(); if(password.equals(confirm)){ trackEvent(CATEGORY_SERVER, EVENT_PASSWORD, "", -1); modifiedPassword = password; new PasswordServerTask().execute((Void[]) null); } else{ showToast("The password and confirmation do not match"); } } private void showToast(String message) { Context context = getApplicationContext(); int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, message, duration); toast.show(); } private CloudServersException parseCloudServersException(HttpResponse response) { CloudServersException cse = new CloudServersException(); try { BasicResponseHandler responseHandler = new BasicResponseHandler(); String body = responseHandler.handleResponse(response); CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(parser); xmlReader.parse(new InputSource(new StringReader(body))); cse = parser.getException(); } catch (ClientProtocolException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (IOException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (ParserConfigurationException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (SAXException e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } catch (FactoryConfigurationError e) { cse = new CloudServersException(); cse.setMessage(e.getLocalizedMessage()); } return cse; } private void startServerError(String message, HttpBundle bundle){ Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class); viewIntent.putExtra("errorMessage", message); viewIntent.putExtra("response", bundle.getResponseText()); viewIntent.putExtra("request", bundle.getCurlRequest()); startActivity(viewIntent); } private class PasswordServerTask extends AsyncTask<Void, Void, HttpBundle> { private CloudServersException exception; protected void onPreExecute(){ showToast("Change root password process has begun"); } @Override protected HttpBundle doInBackground(Void... arg0) { HttpBundle bundle = null; try { bundle = (new ServerManager()).changePassword(server, modifiedPassword, getApplicationContext()); } catch (CloudServersException e) { exception = e; } return bundle; } @Override protected void onPostExecute(HttpBundle bundle) { HttpResponse response = bundle.getResponse(); if (response != null) { int statusCode = response.getStatusLine().getStatusCode(); if(statusCode == 204){ String mustMatch = "The server's root password has successfully been changed."; Toast passwordError = Toast.makeText(getApplicationContext(), mustMatch, Toast.LENGTH_SHORT); passwordError.show(); finish(); } if (statusCode != 204) { CloudServersException cse = parseCloudServersException(response); if ("".equals(cse.getMessage())) { startServerError("There was a problem changing your password.", bundle); } else { startServerError("There was a problem changing your password: " + cse.getMessage() + " " + statusCode, bundle); } } } else if (exception != null) { startServerError("There was a problem changing your password: " + exception.getMessage(), bundle); } } } }