File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\AbstractModelObject.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.model;
25
26 import hudson.search.SearchFactory;
27 import org.kohsuke.stapler.StaplerRequest;
28 import org.kohsuke.stapler.StaplerResponse;
29 import org.kohsuke.stapler.Stapler;
30
31 import javax.servlet.ServletException;
32 import java.io.IOException;
33
34 import hudson.search.SearchableModelObject;
35 import hudson.search.Search;
36 import hudson.search.SearchIndexBuilder;
37 import hudson.search.SearchIndex;
38 import org.kohsuke.stapler.interceptor.RequirePOST;
39
40 /**
41 * {@link ModelObject} with some convenience methods.
42 *
43 * @author Kohsuke Kawaguchi
44 */
45 public abstract class AbstractModelObject implements SearchableModelObject {
46 /**
47 * Displays the error in a page.
48 */
49 protected final void sendError(Exception e, StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
50 sendError(e.getMessage(),req,rsp);
51 }
52
53 protected final void sendError(Exception e) throws ServletException, IOException {
54 sendError(e,Stapler.getCurrentRequest(),Stapler.getCurrentResponse());
55 }
56
57 protected final void sendError(String message, StaplerRequest req, StaplerResponse rsp) throws ServletException, IOException {
58 req.setAttribute("message",message);
59 rsp.forward(this,"error",req);
60 }
61
62 /**
63 * @param pre
64 * If true, the message is put in a PRE tag.
65 */
66 protected final void sendError(String message, StaplerRequest req, StaplerResponse rsp, boolean pre) throws ServletException, IOException {
67 req.setAttribute("message",message);
68 if(pre)
69 req.setAttribute("pre",true);
70 rsp.forward(this,"error",req);
71 }
72
73 protected final void sendError(String message) throws ServletException, IOException {
74 sendError(message,Stapler.getCurrentRequest(),Stapler.getCurrentResponse());
75 }
76
77 /**
78 * Convenience method to verify that the current request is a POST request.
79 *
80 * @deprecated
81 * Use {@link RequirePOST} on your method.
82 */
83 protected final void requirePOST() throws ServletException {
84 StaplerRequest req = Stapler.getCurrentRequest();
85 if (req==null) return; // invoked outside the context of servlet
86 String method = req.getMethod();
87 if(!method.equalsIgnoreCase("POST"))
88 throw new ServletException("Must be POST, Can't be "+method);
89 }
90
91 /**
92 * Default implementation that returns empty index.
93 */
94 protected SearchIndexBuilder makeSearchIndex() {
95 return new SearchIndexBuilder().addAllAnnotations(this);
96 }
97
98 public final SearchIndex getSearchIndex() {
99 return makeSearchIndex().make();
100 }
101
102 public Search getSearch() {
103 for (SearchFactory sf : SearchFactory.all()) {
104 Search s = sf.createFor(this);
105 if (s!=null)
106 return s;
107 }
108 return new Search();
109 }
110
111 /**
112 * Default implementation that returns the display name.
113 */
114 public String getSearchName() {
115 return getDisplayName();
116 }
117 }
118