Revision 4f9d1a69 src/com/rackspacecloud/android/ViewLoadBalancerActivity.java

b/src/com/rackspacecloud/android/ViewLoadBalancerActivity.java
3 3
 */
4 4
package com.rackspacecloud.android;
5 5

  
6
import java.io.IOException;
7
import java.io.StringReader;
8

  
9
import javax.xml.parsers.FactoryConfigurationError;
10
import javax.xml.parsers.ParserConfigurationException;
11
import javax.xml.parsers.SAXParser;
12
import javax.xml.parsers.SAXParserFactory;
13

  
14
import org.apache.http.HttpResponse;
15
import org.apache.http.client.ClientProtocolException;
16
import org.apache.http.impl.client.BasicResponseHandler;
17
import org.xml.sax.InputSource;
18
import org.xml.sax.SAXException;
19
import org.xml.sax.XMLReader;
20

  
6 21
import android.app.Activity;
7 22
import android.app.AlertDialog;
23
import android.app.Dialog;
8 24
import android.app.ProgressDialog;
9 25
import android.content.Context;
10 26
import android.content.DialogInterface;
27
import android.content.Intent;
11 28
import android.os.AsyncTask;
12 29
import android.os.Bundle;
13 30
import android.util.Log;
31
import android.view.Menu;
32
import android.view.MenuInflater;
33
import android.view.MenuItem;
14 34
import android.view.View;
15 35
import android.view.View.OnClickListener;
16 36
import android.view.WindowManager;
......
22 42
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
23 43
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
24 44
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException;
45
import com.rackspace.cloud.servers.api.client.CloudServersException;
46
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
47
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
25 48

  
26 49
public class ViewLoadBalancerActivity extends Activity {
50

  
51
	private static final int EDIT_LOAD_BALANCER_CODE = 184;
52
	private static final int EDIT_NODES_CODE = 185;
27 53
	
28 54
	private LoadBalancer loadBalancer;
29 55
	private LoadBalancer returnLoadBalancer;
30 56
	private Context context;
31 57
	ProgressDialog pDialog;
32
	
33
    @Override
34
    public void onCreate(Bundle savedInstanceState) {
35
        super.onCreate(savedInstanceState);
36
        loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
37
    	Log.i("VIEWLOADBALANCERS: ", loadBalancer.getAlgorithm() +","+loadBalancer.getProtocol()+","+loadBalancer.getStatus());
38

  
39
        setContentView(R.layout.view_loadbalancer);
40
        restoreState(savedInstanceState);
41
    }
42
    
58

  
59
	@Override
60
	public void onCreate(Bundle savedInstanceState) {
61
		super.onCreate(savedInstanceState);
62
		loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
63
		Log.i("VIEWLOADBALANCERS: ", loadBalancer.getAlgorithm() +","+loadBalancer.getProtocol()+","+loadBalancer.getStatus());
64

  
65
		setContentView(R.layout.view_loadbalancer);
66
		restoreState(savedInstanceState);
67
	}
68

  
43 69
	@Override
44 70
	protected void onSaveInstanceState(Bundle outState) {
45 71
		super.onSaveInstanceState(outState);
46 72
		outState.putSerializable("loadBalancer", loadBalancer);
47 73
	}
48 74

  
49
    private void restoreState(Bundle state) {
50
    	context = getApplicationContext();
51
    	if (state != null && state.containsKey("loadBalancer")) {
52
    		loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
53
    	}
54
    	new LoadLoadBalancerTask().execute((Void[]) null);   
55
    	setUpButtons();
56
    }
57
    
58
    private void setUpButtons(){
59
    	Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
60
    	editLoadBalancer.setOnClickListener(new OnClickListener() {
61
			
75
	private void restoreState(Bundle state) {
76
		context = getApplicationContext();
77
		if (state != null && state.containsKey("loadBalancer")) {
78
			loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
79
		}
80
		new LoadLoadBalancerTask().execute((Void[]) null);   
81
		setUpButtons();
82
	}
83

  
84
	private void setUpButtons(){
85
		Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
86
		editLoadBalancer.setOnClickListener(new OnClickListener() {
87

  
62 88
			@Override
63 89
			public void onClick(View v) {
64
				//Intent editLoadBalancerIntent = new Intent(context, EditLoadBalancerActivity.class);
65
				//editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
66
				//startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE);
90
				Intent editLoadBalancerIntent = new Intent(context, EditLoadBalancerActivity.class);
91
				editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
92
				startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE);
67 93
			}
68
			
94

  
69 95
		});
70
    }
71
    
72
    private void loadLoadBalancerData() {
73
    	TextView name = (TextView) findViewById(R.id.view_name);
74
    	name.setText(returnLoadBalancer.getName());
75
    	
76
    	TextView id = (TextView) findViewById(R.id.view_lb_id);
77
    	id.setText(returnLoadBalancer.getId());
78
    	
79
    	TextView protocol = (TextView) findViewById(R.id.view_protocol);
80
    	protocol.setText(returnLoadBalancer.getProtocol());
81
    	
82
    	TextView port = (TextView) findViewById(R.id.view_port);
83
    	port.setText(returnLoadBalancer.getPort());
84
    	
85
    	TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
86
    	algorithm.setText(returnLoadBalancer.getAlgorithm());
87
    	
88
    	TextView status = (TextView) findViewById(R.id.view_status);
89
        status.setText(returnLoadBalancer.getStatus());
90
    	
91
    	TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
92
    	connectionLogging.setText(returnLoadBalancer.getIsConnectionLoggingEnabled());
93
    	
94
    	loadVirutalIpData();
95
    }
96
    
97
    private void loadVirutalIpData() {
98
    	TextView vipId = (TextView) findViewById(R.id.view_vip_id);
99
    	vipId.setText(returnLoadBalancer.getVirtualIps().get(0).getId());
100
    	
101
    	TextView address = (TextView) findViewById(R.id.view_vip_address);
102
    	address.setText(returnLoadBalancer.getVirtualIps().get(0).getAddress());
103
    	
104
    	TextView ipVersion = (TextView) findViewById(R.id.view_ipversion);
105
    	ipVersion.setText(returnLoadBalancer.getVirtualIps().get(0).getIpVersion());
106
    	
107
    	TextView type = (TextView) findViewById(R.id.view_vip_type);
108
    	type.setText(returnLoadBalancer.getVirtualIps().get(0).getType());
109
    	
110
    	loadNodeData();
111
    }
96

  
97
		Button deleteLoadBalancer = (Button)findViewById(R.id.delete_loadbalancer_button);
98
		deleteLoadBalancer.setOnClickListener(new OnClickListener() {
99

  
100
			@Override
101
			public void onClick(View v) {
102
				showDialog(R.id.view_server_delete_button);
103
			}
104

  
105
		});
106
		
107
		Button editNodes = (Button)findViewById(R.id.edit_nodes_button);
108
		editNodes.setOnClickListener(new OnClickListener() {
109

  
110
			@Override
111
			public void onClick(View v) {
112
				Intent editLoadBalancerIntent = new Intent(context, EditNodesActivity.class);
113
				Log.d("info", "the nodes are null before?" + Boolean.toString(returnLoadBalancer.getNodes() == null));
114
				editLoadBalancerIntent.putExtra("nodes", returnLoadBalancer.getNodes());
115
				editLoadBalancerIntent.putExtra("loadBalancer", returnLoadBalancer);
116
				startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE);
117
			}
118
		});
119

  
120

  
121
	}
122

  
123
	@Override
124
	protected Dialog onCreateDialog(int id) {
125
		switch (id) {
126
		case R.id.view_server_delete_button:
127
			return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
128
			.setIcon(R.drawable.alert_dialog_icon)
129
			.setTitle("Delete Load Balancer")
130
			.setMessage("Are you sure you want to delete the load balancer?")
131
			.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
132
				public void onClick(DialogInterface dialog, int whichButton) {
133
					// User clicked OK so do some stuff
134
					new DeleteLoadBalancerTask().execute((Void[]) null);
135
				}
136
			})
137
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
138
				public void onClick(DialogInterface dialog, int whichButton) {
139
					// User clicked Cancel so do some stuff
140
				}
141
			})
142
			.create();
143
		}
144
		return null;
145
	}
146

  
147
	private void loadLoadBalancerData() {
148
		TextView name = (TextView) findViewById(R.id.view_name);
149
		name.setText(returnLoadBalancer.getName());
150

  
151
		TextView id = (TextView) findViewById(R.id.view_lb_id);
152
		id.setText(returnLoadBalancer.getId());
153

  
154
		TextView protocol = (TextView) findViewById(R.id.view_protocol);
155
		protocol.setText(returnLoadBalancer.getProtocol());
156

  
157
		TextView port = (TextView) findViewById(R.id.view_port);
158
		port.setText(returnLoadBalancer.getPort());
159

  
160
		TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
161
		algorithm.setText(returnLoadBalancer.getAlgorithm());
162

  
163
		TextView status = (TextView) findViewById(R.id.view_status);
164
		status.setText(returnLoadBalancer.getStatus());
165

  
166
		TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
167
		connectionLogging.setText(returnLoadBalancer.getIsConnectionLoggingEnabled());
168

  
169
		loadVirutalIpData();
170
	}
171

  
172
	private void loadVirutalIpData() {
173
		TextView vipId = (TextView) findViewById(R.id.view_vip_id);
174
		vipId.setText(returnLoadBalancer.getVirtualIps().get(0).getId());
175

  
176
		TextView address = (TextView) findViewById(R.id.view_vip_address);
177
		address.setText(returnLoadBalancer.getVirtualIps().get(0).getAddress());
178

  
179
		TextView ipVersion = (TextView) findViewById(R.id.view_ipversion);
180
		ipVersion.setText(returnLoadBalancer.getVirtualIps().get(0).getIpVersion());
181

  
182
		TextView type = (TextView) findViewById(R.id.view_vip_type);
183
		type.setText(returnLoadBalancer.getVirtualIps().get(0).getType());
184

  
185
		loadNodeData();
186
	}
187

  
188
	private void loadNodeData() {
189
		TextView nodeID = (TextView) findViewById(R.id.view_node_id);
190
		nodeID.setText(returnLoadBalancer.getNodes().get(0).getId());
191

  
192
		TextView address = (TextView) findViewById(R.id.view_node_address);
193
		address.setText(returnLoadBalancer.getNodes().get(0).getAddress());
194

  
195
		TextView nodePort = (TextView) findViewById(R.id.view_node_port);
196
		nodePort.setText(returnLoadBalancer.getNodes().get(0).getPort());
197

  
198
		TextView condition = (TextView) findViewById(R.id.view_node_condition);
199
		condition.setText(returnLoadBalancer.getNodes().get(0).getCondition());
200

  
201
		TextView nodeStatus = (TextView) findViewById(R.id.view_node_status);
202
		nodeStatus.setText(returnLoadBalancer.getNodes().get(0).getStatus());
203

  
204
	}
205
	
206
	//setup menu for when menu button is pressed
207
	public boolean onCreateOptionsMenu(Menu menu) {
208
		super.onCreateOptionsMenu(menu);
209
		MenuInflater inflater = getMenuInflater();
210
		inflater.inflate(R.menu.view_loadbalancer_menu, menu);
211
		return true;
212
	} 
112 213
    
113
    private void loadNodeData() {
114
    	TextView nodeID = (TextView) findViewById(R.id.view_node_id);
115
    	nodeID.setText(returnLoadBalancer.getNodes().get(0).getId());
116
    	
117
    	TextView address = (TextView) findViewById(R.id.view_node_address);
118
    	address.setText(returnLoadBalancer.getNodes().get(0).getAddress());
119
    	
120
    	TextView nodePort = (TextView) findViewById(R.id.view_node_port);
121
    	nodePort.setText(returnLoadBalancer.getNodes().get(0).getPort());
122
    	
123
    	TextView condition = (TextView) findViewById(R.id.view_node_condition);
124
    	condition.setText(returnLoadBalancer.getNodes().get(0).getCondition());
125

  
126
    	TextView nodeStatus = (TextView) findViewById(R.id.view_node_status);
127
    	nodeStatus.setText(returnLoadBalancer.getNodes().get(0).getStatus());
128

  
129
    }
214
    @Override 
215
    public boolean onOptionsItemSelected(MenuItem item) {
216
    	switch (item.getItemId()) {
217
    	case R.id.refresh_loadbalancer:
218
    		new LoadLoadBalancerTask().execute((Void[]) null);   
219
    		return true;
220
    	}	
221
    	return false;
222
    } 
130 223
    
131
    // HTTP request tasks
224
    @Override
225
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
226
		super.onActivityResult(requestCode, resultCode, data);
227

  
228
		if (resultCode == RESULT_OK) {
229
    		new LoadLoadBalancerTask().execute((Void[]) null);   
230
		}
231
	}
232
	
233
	private void startLoadBalancerError(String message, HttpBundle bundle){
234
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
235
		viewIntent.putExtra("errorMessage", message);
236
		viewIntent.putExtra("response", bundle.getResponseText());
237
		viewIntent.putExtra("request", bundle.getCurlRequest());
238
		startActivity(viewIntent);
239
	}
240
	
241
	//using cloudServersException, it works for us too
242
	private CloudServersException parseCloudServersException(HttpResponse response) {
243
		CloudServersException cse = new CloudServersException();
244
		try {
245
		    BasicResponseHandler responseHandler = new BasicResponseHandler();
246
		    String body = responseHandler.handleResponse(response);
247
	    	CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
248
	    	SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
249
	    	XMLReader xmlReader = saxParser.getXMLReader();
250
	    	xmlReader.setContentHandler(parser);
251
	    	xmlReader.parse(new InputSource(new StringReader(body)));		    	
252
	    	cse = parser.getException();		    	
253
		} catch (ClientProtocolException e) {
254
			cse = new CloudServersException();
255
			cse.setMessage(e.getLocalizedMessage());
256
		} catch (IOException e) {
257
			cse = new CloudServersException();
258
			cse.setMessage(e.getLocalizedMessage());
259
		} catch (ParserConfigurationException e) {
260
			cse = new CloudServersException();
261
			cse.setMessage(e.getLocalizedMessage());
262
		} catch (SAXException e) {
263
			cse = new CloudServersException();
264
			cse.setMessage(e.getLocalizedMessage());
265
		} catch (FactoryConfigurationError e) {
266
			cse = new CloudServersException();
267
			cse.setMessage(e.getLocalizedMessage());
268
		}
269
		return cse;
270
	}
271

  
272
	// HTTP request tasks
132 273
	private class PollServerTask extends AsyncTask<Void, Void, LoadBalancer> {
133
    	
274

  
134 275
		@Override
135 276
		protected LoadBalancer doInBackground(Void... arg0) {
136 277
			try {
......
142 283
			}
143 284
			return returnLoadBalancer;
144 285
		}
145
    	
286

  
146 287
		@Override
147 288
		protected void onPostExecute(LoadBalancer result) {
148 289
			returnLoadBalancer = result;
149 290
			loadLoadBalancerData();
150 291
		}
151
    }
152
	
292
	}
293

  
153 294
	private class LoadLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
154 295
		private LoadBalancersException exception;
155 296

  
156 297
		protected void onPreExecute() {
157
	        Log.d("rscloudactivity", " pre execute async");
158
	        showDialog();
159
	    }
160
		
298
			showDialog();
299
		}
300

  
161 301
		@Override
162 302
		protected LoadBalancer doInBackground(Void... arg0) {
303
			LoadBalancer result = null;
163 304
			try {
164
				returnLoadBalancer = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
305
				result = (new LoadBalancerManager(context)).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
165 306
			} catch (LoadBalancersException e) {
166 307
				exception = e;
167 308
			}
168 309
			pDialog.dismiss();
169
			return returnLoadBalancer;
310
			return result;
170 311
		}
171 312

  
172 313
		@Override
......
179 320
			returnLoadBalancer = result;
180 321
			loadLoadBalancerData();
181 322
		}
323
	} 
324

  
325
	public class DeleteLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> {
326

  
327
		private CloudServersException exception;
328

  
329
		@Override
330
		protected void onPreExecute(){
331
			showDialog();
332
		}
333
		
334
		@Override
335
		protected HttpBundle doInBackground(Void... arg0) {
336
			HttpBundle bundle = null;
337
			try {
338
				bundle = (new LoadBalancerManager(context)).delete(loadBalancer);
339
			} catch (CloudServersException e) {
340
				exception = e;
341
			}
342
			return bundle;
343
		}
344

  
345
		@Override
346
		protected void onPostExecute(HttpBundle bundle) {
347
			pDialog.dismiss();
348
			HttpResponse response = bundle.getResponse();
349
			if (response != null) {
350
				int statusCode = response.getStatusLine().getStatusCode();
351
				if (statusCode == 202) {
352
					//showToast("Delete successful");
353
					setResult(Activity.RESULT_OK);
354
					finish();
355
				} else {
356
					CloudServersException cse = parseCloudServersException(response);
357
					if ("".equals(cse.getMessage())) {
358
						startLoadBalancerError("There was a problem deleting your load balancer.", bundle);
359
					} else {
360
						startLoadBalancerError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
361
					}
362
				}
363
			} else if (exception != null) {
364
				startLoadBalancerError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);				
365
			}			
366
		}
182 367
	}
183
	
368

  
184 369
	private void showAlert(String title, String message) {
185 370
		// Can't create handler inside thread that has not called
186 371
		// Looper.prepare()
......
199 384
			e.printStackTrace();
200 385
		}
201 386
	}
202
	
387

  
203 388
	protected void showDialog() {
204 389
		pDialog = new ProgressDialog(this, R.style.NewDialog);
205 390
		// // Set blur to background

Also available in: Unified diff