root / src / com / rackspacecloud / android / EditLoadBalancerActivity.java @ 4f9d1a69
History | View | Annotate | Download (9 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 com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
19 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
20 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
21 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
22 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
23 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
24 |
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser; |
25 |
|
26 |
import android.app.Activity; |
27 |
import android.app.ProgressDialog; |
28 |
import android.content.Context; |
29 |
import android.content.Intent; |
30 |
import android.os.AsyncTask; |
31 |
import android.os.Bundle; |
32 |
import android.view.View; |
33 |
import android.view.View.OnClickListener; |
34 |
import android.view.WindowManager; |
35 |
import android.view.ViewGroup.LayoutParams; |
36 |
import android.widget.AdapterView; |
37 |
import android.widget.ArrayAdapter; |
38 |
import android.widget.Button; |
39 |
import android.widget.EditText; |
40 |
import android.widget.ProgressBar; |
41 |
import android.widget.Spinner; |
42 |
import android.widget.AdapterView.OnItemSelectedListener; |
43 |
|
44 |
public class EditLoadBalancerActivity extends Activity implements OnItemSelectedListener { |
45 |
|
46 |
private LoadBalancer loadBalancer;
|
47 |
private Protocol[] protocols; |
48 |
private Algorithm[] algorithms; |
49 |
private Context context; |
50 |
private Spinner protocolSpinner;
|
51 |
private Spinner algorithmSpinner;
|
52 |
private Protocol selectedProtocol;
|
53 |
private Algorithm selectedAlgorithm;
|
54 |
private EditText name;
|
55 |
private EditText port;
|
56 |
ProgressDialog pDialog; |
57 |
|
58 |
|
59 |
@Override
|
60 |
public void onCreate(Bundle savedInstanceState) { |
61 |
super.onCreate(savedInstanceState);
|
62 |
loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer"); |
63 |
setContentView(R.layout.updateloadbalancer); |
64 |
restoreState(savedInstanceState); |
65 |
} |
66 |
|
67 |
@Override
|
68 |
protected void onSaveInstanceState(Bundle outState) { |
69 |
super.onSaveInstanceState(outState);
|
70 |
outState.putSerializable("loadBalancer", loadBalancer);
|
71 |
} |
72 |
|
73 |
private void restoreState(Bundle state) { |
74 |
context = getApplicationContext(); |
75 |
if (state != null && state.containsKey("loadBalancer")) { |
76 |
loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
|
77 |
} |
78 |
setupInputs(); |
79 |
} |
80 |
|
81 |
private void setupInputs(){ |
82 |
name = (EditText) findViewById(R.id.edit_lb_name_text); |
83 |
name.setText(loadBalancer.getName()); |
84 |
|
85 |
port = (EditText) findViewById(R.id.edit_port_text); |
86 |
port.setText(loadBalancer.getPort()); |
87 |
|
88 |
loadProtocolSpinner(); |
89 |
loadAlgorithmSpinner(); |
90 |
|
91 |
Button submit = (Button)findViewById(R.id.update_lb_button); |
92 |
submit.setOnClickListener(new OnClickListener() {
|
93 |
|
94 |
@Override
|
95 |
public void onClick(View v) { |
96 |
new UpdateLoadBalancerTask().execute();
|
97 |
} |
98 |
}); |
99 |
} |
100 |
|
101 |
private void loadProtocolSpinner() { |
102 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
103 |
protocolSpinner.setOnItemSelectedListener(this);
|
104 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
105 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
106 |
|
107 |
/*
|
108 |
* set the spinner to the current value
|
109 |
* so user doesnt have to remember
|
110 |
*/
|
111 |
int defaultPosition = 0; |
112 |
|
113 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
114 |
protocols[i] = Protocol.getProtocols().get(i); |
115 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
116 |
if(protocolNames[i].equals(loadBalancer.getProtocol())){
|
117 |
defaultPosition = i; |
118 |
} |
119 |
} |
120 |
|
121 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
122 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
123 |
protocolSpinner.setAdapter(protocolAdapter); |
124 |
protocolSpinner.setSelection(defaultPosition); |
125 |
} |
126 |
|
127 |
private void loadAlgorithmSpinner() { |
128 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner); |
129 |
algorithmSpinner.setOnItemSelectedListener(this);
|
130 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()]; |
131 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
132 |
|
133 |
/*
|
134 |
* set the spinner to the current value
|
135 |
* so user doesnt have to remember
|
136 |
*/
|
137 |
int defaultPosition = 0; |
138 |
|
139 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){ |
140 |
algorithms[i] = Algorithm.getAlgorithms().get(i); |
141 |
algorithmNames[i] = Algorithm.getAlgorithms().get(i).getName(); |
142 |
if(algorithmNames[i].equals(loadBalancer.getAlgorithm())){
|
143 |
defaultPosition = i; |
144 |
} |
145 |
} |
146 |
|
147 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames); |
148 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
149 |
algorithmSpinner.setAdapter(algorithmAdapter); |
150 |
algorithmSpinner.setSelection(defaultPosition); |
151 |
} |
152 |
|
153 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
154 |
if (parent == protocolSpinner) {
|
155 |
selectedProtocol = protocols[position]; |
156 |
} |
157 |
else if (parent == algorithmSpinner){ |
158 |
selectedAlgorithm = algorithms[position]; |
159 |
} |
160 |
} |
161 |
|
162 |
public void onNothingSelected(AdapterView<?> parent) { |
163 |
|
164 |
} |
165 |
|
166 |
private void startLoadBalancerError(String message, HttpBundle bundle){ |
167 |
Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
|
168 |
viewIntent.putExtra("errorMessage", message);
|
169 |
viewIntent.putExtra("response", bundle.getResponseText());
|
170 |
viewIntent.putExtra("request", bundle.getCurlRequest());
|
171 |
startActivity(viewIntent); |
172 |
} |
173 |
|
174 |
//using cloudServersException, it works for us too
|
175 |
private CloudServersException parseCloudServersException(HttpResponse response) {
|
176 |
CloudServersException cse = new CloudServersException();
|
177 |
try {
|
178 |
BasicResponseHandler responseHandler = new BasicResponseHandler();
|
179 |
String body = responseHandler.handleResponse(response);
|
180 |
CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
|
181 |
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); |
182 |
XMLReader xmlReader = saxParser.getXMLReader(); |
183 |
xmlReader.setContentHandler(parser); |
184 |
xmlReader.parse(new InputSource(new StringReader(body))); |
185 |
cse = parser.getException(); |
186 |
} catch (ClientProtocolException e) {
|
187 |
cse = new CloudServersException();
|
188 |
cse.setMessage(e.getLocalizedMessage()); |
189 |
} catch (IOException e) { |
190 |
cse = new CloudServersException();
|
191 |
cse.setMessage(e.getLocalizedMessage()); |
192 |
} catch (ParserConfigurationException e) { |
193 |
cse = new CloudServersException();
|
194 |
cse.setMessage(e.getLocalizedMessage()); |
195 |
} catch (SAXException e) {
|
196 |
cse = new CloudServersException();
|
197 |
cse.setMessage(e.getLocalizedMessage()); |
198 |
} catch (FactoryConfigurationError e) { |
199 |
cse = new CloudServersException();
|
200 |
cse.setMessage(e.getLocalizedMessage()); |
201 |
} |
202 |
return cse;
|
203 |
} |
204 |
|
205 |
protected void showDialog() { |
206 |
pDialog = new ProgressDialog(this, R.style.NewDialog); |
207 |
// // Set blur to background
|
208 |
WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes(); |
209 |
lp.dimAmount = 0.0f;
|
210 |
pDialog.getWindow().setAttributes(lp); |
211 |
pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); |
212 |
pDialog.show(); |
213 |
pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); |
214 |
} |
215 |
|
216 |
private class UpdateLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
217 |
|
218 |
private CloudServersException exception;
|
219 |
|
220 |
@Override
|
221 |
protected void onPreExecute(){ |
222 |
//showToast("Resize process has begun, please confirm your resize after process finishes.");
|
223 |
showDialog(); |
224 |
} |
225 |
|
226 |
@Override
|
227 |
protected HttpBundle doInBackground(Void... arg0) { |
228 |
HttpBundle bundle = null;
|
229 |
try {
|
230 |
bundle = (new LoadBalancerManager(context)).update(loadBalancer, name.getText().toString(), selectedAlgorithm.getName(),
|
231 |
selectedProtocol.getName(), port.getText().toString()); |
232 |
} catch (CloudServersException e) {
|
233 |
exception = e; |
234 |
} |
235 |
return bundle;
|
236 |
} |
237 |
|
238 |
@Override
|
239 |
protected void onPostExecute(HttpBundle bundle) { |
240 |
pDialog.hide(); |
241 |
HttpResponse response = bundle.getResponse(); |
242 |
if (response != null) { |
243 |
int statusCode = response.getStatusLine().getStatusCode();
|
244 |
if(statusCode == 202){ |
245 |
setResult(RESULT_OK); |
246 |
finish(); |
247 |
} |
248 |
else{
|
249 |
CloudServersException cse = parseCloudServersException(response); |
250 |
if ("".equals(cse.getMessage())) { |
251 |
startLoadBalancerError("There was a problem updating your load balancer.", bundle);
|
252 |
} else {
|
253 |
startLoadBalancerError("There was a problem updating your load balancer: " + cse.getMessage(), bundle);
|
254 |
} |
255 |
} |
256 |
} else if (exception != null) { |
257 |
startLoadBalancerError("There was a problem updating your load balancer: " + exception.getMessage(), bundle);
|
258 |
|
259 |
} |
260 |
|
261 |
} |
262 |
} |
263 |
} |