1 |
package dk.thoerup.curcuitbreaker.web; |
2 |
|
3 |
import java.io.IOException; |
4 |
import java.io.UnsupportedEncodingException; |
5 |
import java.net.URLEncoder; |
6 |
|
7 |
import javax.servlet.http.HttpServletRequest; |
8 |
import javax.servlet.http.HttpServletResponse; |
9 |
|
10 |
import dk.thoerup.curcuitbreaker.AccountingCircuitBreaker; |
11 |
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
12 |
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
13 |
|
14 |
public class ActionCommand implements Command { |
15 |
|
16 |
private boolean readOnly; |
17 |
|
18 |
public ActionCommand(boolean readOnly) { |
19 |
this.readOnly = readOnly; |
20 |
} |
21 |
|
22 |
public String execute(HttpServletRequest req, HttpServletResponse resp) { |
23 |
String action = req.getParameter("action"); |
24 |
String breaker = req.getParameter("breaker"); |
25 |
|
26 |
CircuitBreaker cb = CircuitBreakerManager.getManager().getCircuitBreaker(breaker); |
27 |
|
28 |
if (readOnly == false) { |
29 |
if (action.equalsIgnoreCase("reset")) |
30 |
cb.reset(); |
31 |
|
32 |
if (action.equalsIgnoreCase("tripbreaker")) |
33 |
cb.tripBreaker(); |
34 |
|
35 |
if (action.equalsIgnoreCase("resetcounters")) { |
36 |
AccountingCircuitBreaker acb = (AccountingCircuitBreaker) cb; |
37 |
acb.resetCounters(); |
38 |
} |
39 |
} |
40 |
|
41 |
try { |
42 |
String uri = req.getRequestURI() + "?command=view&breaker=" + URLEncoder.encode(breaker, "UTF-8"); |
43 |
|
44 |
resp.sendRedirect( uri ); |
45 |
} catch(IOException e) { |
46 |
// what should be done here ? |
47 |
} |
48 |
return ""; |
49 |
} |
50 |
|
51 |
} |