1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package tinlizard.web;
20
21 import tinlizard.util.Messages;
22
23 import java.io.IOException;
24 import java.util.Date;
25
26 import javax.servlet.RequestDispatcher;
27 import javax.servlet.ServletException;
28
29 import org.apache.commons.lang.StringUtils;
30
31 import org.kohsuke.stapler.StaplerRequest;
32 import org.kohsuke.stapler.StaplerResponse;
33 import org.kohsuke.stapler.export.Exported;
34 import org.kohsuke.stapler.export.ExportedBean;
35 import org.kohsuke.stapler.export.Flavor;
36
37 /***
38 * Base class for beans exposed via Stapler.
39 * @param <E> the object being exposed.
40 */
41 @ExportedBean(defaultVisibility = 1)
42 public abstract class ObjectWeb<E extends Object> {
43
44 private static final int MAX_DEPTH = 5;
45 private static final int MAX_DEPTH_ERROR = 451;
46
47 @Exported
48 public abstract String getName();
49
50 protected abstract E getSubject();
51
52 public abstract Date getCreated();
53
54 public abstract String getCreatedBy();
55
56 public abstract Date getLastModified();
57
58 public abstract String getLastModifiedBy();
59
60 public void doDelete(final StaplerRequest request, final StaplerResponse response) throws Exception {
61 throw new UnsupportedOperationException();
62 }
63
64 public final void doJson(final StaplerRequest request, final StaplerResponse response) throws Exception {
65 if (checkDepth(request, response)) {
66 response.serveExposedBean(request, this, Flavor.JSON);
67 }
68 }
69
70 public final void doXml(final StaplerRequest request, final StaplerResponse response) throws Exception {
71 if (checkDepth(request, response)) {
72 response.serveExposedBean(request, this, Flavor.XML);
73 }
74 }
75
76 public final void doPython(final StaplerRequest request, final StaplerResponse response) throws Exception {
77 if (checkDepth(request, response)) {
78 response.serveExposedBean(request, this, Flavor.PYTHON);
79 }
80 }
81
82 private boolean checkDepth(final StaplerRequest request, final StaplerResponse response) throws IOException {
83 if (StringUtils.isNumeric(request.getParameter("depth"))) {
84 int depth = Integer.parseInt(request.getParameter("depth"));
85
86 if (depth > MAX_DEPTH) {
87 response.sendError(MAX_DEPTH_ERROR, Messages.Error451());
88 }
89 }
90
91 return true;
92 }
93
94 protected void gotoMyIndex(final StaplerRequest request, final StaplerResponse response) throws IOException, ServletException {
95 RequestDispatcher view = request.getView(this, "index.jsp");
96 view.include(request, response);
97 }
98 }