View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
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      //XXX make max depth configurable.
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  }