1 |
package dk.thoerup.curcuitbreaker.web; |
package dk.thoerup.circuitbreaker.web; |
2 |
|
|
3 |
import java.io.IOException; |
import java.io.IOException; |
4 |
|
|
5 |
import javax.servlet.ServletException; |
import javax.servlet.ServletException; |
6 |
import javax.servlet.http.*; |
import javax.servlet.http.HttpServletRequest; |
7 |
|
import javax.servlet.http.HttpServletResponse; |
8 |
|
|
9 |
/** |
/** |
10 |
* This is a web interface for viewing and controlling the CircuitBreakers currently registered in CircuitBreakerManager |
* This is a web interface for viewing and controlling the CircuitBreakers currently registered in CircuitBreakerManager |
11 |
* <p> |
* <p> |
12 |
* To use this servlet in you .war module just create a simple servlet where doGet just delegates to this class' doGet method |
* To use this servlet in you .war module just create a simple servlet that extends this class (instead of HttpServlet) |
13 |
* |
* The new custom servlet doesn't need to implement any of the standard servlet methods - its all done in this implementation. |
14 |
|
* |
15 |
|
* <p> |
16 |
* Example: |
* Example: |
17 |
* <pre> |
* <pre> |
18 |
* public class CircuitBreakerServlet extends HttpServlet { |
* public class CircuitBreakerServlet extends CircuitBreakerServletBase { |
19 |
* private static final long serialVersionUID = 1L; |
* private static final long serialVersionUID = 1L; |
|
* |
|
|
* CircuitBreakerServletHelper helper = new CircuitBreakerServletHelper(); |
|
|
* protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
|
|
* helper.doGet(req, resp); |
|
|
* } |
|
20 |
* } |
* } |
21 |
* </pre> |
* </pre> |
22 |
|
* |
23 |
|
* If you add a servlet init pameter to your deployment descriptor with the name 'readonly' and a integer value > 0, you can disable the |
24 |
|
* control buttons: |
25 |
|
* <p> |
26 |
|
* <init-param><param-name>readonly</param-name><param-value>1</param-value></init-param> |
27 |
|
* <!-- <init-param><param-name>readonly</param-name><param-value>1</param-value></init-param> --> |
28 |
|
* |
29 |
|
* You could also configure it dynamically from a ContextListener: |
30 |
|
* ServletRegistration.Dynamic dynconf = sce.getServletContext().addServlet("circuitbreaker", dk.thoerup.circuitbreaker.web.CircuitBreakerServletBase.class ); |
31 |
|
* dynconf.addMapping("/CircuitBreakerServlet"); |
32 |
|
* dynconf.setInitParameter("readonly", "1"); |
33 |
|
* |
34 |
*/ |
*/ |
35 |
|
|
36 |
public class CircuitBreakerServletHelper{ |
public class CircuitBreakerServletBase extends javax.servlet.http.HttpServlet { |
37 |
|
|
38 |
|
private static final long serialVersionUID = 1L; |
39 |
|
|
40 |
|
private boolean readOnly = false; |
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@Override |
45 |
|
public void init() throws ServletException { |
46 |
|
super.init(); |
47 |
|
|
48 |
|
String readOnlyStr = getInitParameter("readonly"); |
49 |
|
if (readOnlyStr != null && readOnlyStr.trim().length() >0) { |
50 |
|
int readOnlyInt = Integer.parseInt(readOnlyStr.trim()); |
51 |
|
readOnly = (readOnlyInt > 0); |
52 |
|
} |
53 |
|
} |
54 |
|
|
55 |
|
private Command getCommand(String command) throws ServletException { |
56 |
|
if (command == null || command.equals("list")) |
57 |
|
return new ListCircuitBreakers(); |
58 |
|
|
59 |
|
if (command.equals("view")) |
60 |
|
return new ViewCircuitBreaker(readOnly); |
61 |
|
if (command.equals("action")) |
62 |
|
return new ActionCommand(readOnly); |
63 |
|
if (command.equals("log")) |
64 |
|
return new LogViewCommand(); |
65 |
|
|
66 |
|
throw new ServletException("No such command:" + command); |
67 |
|
} |
68 |
|
|
69 |
|
@Override |
70 |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
71 |
ListCircuitBreakers cmd = new ListCircuitBreakers(); |
Command cmd = getCommand( req.getParameter("command")); |
72 |
|
|
73 |
String response = cmd.doCommand(req); |
String response = cmd.execute(req,resp); |
74 |
|
|
75 |
resp.setDateHeader("Expires", 0); |
resp.setDateHeader("Expires", 0); |
76 |
resp.setHeader("Pragma", "no-cache"); |
resp.setHeader("Pragma", "no-cache"); |
77 |
resp.setHeader("Cache-Control", "no-cache"); |
resp.setHeader("Cache-Control", "no-cache"); |
78 |
|
resp.setContentType("text/html"); |
79 |
|
|
80 |
resp.getWriter().print(response); |
resp.getWriter().print(response); |
81 |
} |
} |
82 |
|
|
83 |
|
@Override |
84 |
|
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
85 |
|
doGet(req, resp); |
86 |
|
} |
87 |
|
|
88 |
} |
} |