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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2012-, CloudBees, Inc.
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.util;
25
26 import org.kohsuke.stapler.HttpResponses.HttpResponseException;
27 import org.kohsuke.stapler.StaplerRequest;
28 import org.kohsuke.stapler.StaplerResponse;
29
30 import javax.servlet.ServletException;
31 import java.io.IOException;
32
33 /**
34 * Server-side code related to the &lt;f:apply> button.
35 *
36 * @author Kohsuke Kawaguchi
37 * @since 1.453
38 */
39 public class FormApply {
40 /**
41 * Generates the response for the form submission in such a way that it handles the "apply" button
42 * correctly.
43 *
44 * @param destination
45 * The page that the user will be taken to upon a successful submission (in case this is not via the "apply" button.)
46 */
47 public static HttpResponseException success(final String destination) {
48 return new HttpResponseException() {
49 public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
50 if (isApply(req)) {
51 // if the submission is via 'apply', show a response in the notification bar
52 applyResponse("notificationBar.show('"+Messages.HttpResponses_Saved()+"',notificationBar.OK)")
53 .generateResponse(req,rsp,node);
54 } else {
55 rsp.sendRedirect(destination);
56 }
57 }
58 };
59 }
60
61 /**
62 * Is this submission from the "apply" button?
63 */
64 public static boolean isApply(StaplerRequest req) {
65 return Boolean.parseBoolean(req.getParameter("core:apply"));
66 }
67
68 /**
69 * Generates the response for the asynchronous background form submission (AKA the Apply button.)
70 * <p>
71 * When the response HTML includes a JavaScript function in a pre-determined name, that function gets executed.
72 * This method generates such a response from JavaScript text.
73 */
74 public static HttpResponseException applyResponse(final String script) {
75 return new HttpResponseException() {
76 public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node) throws IOException, ServletException {
77 rsp.setContentType("text/html;charset=UTF-8");
78 rsp.getWriter().println("<html><body><script>" +
79 "window.applyCompletionHandler = function (w) {" +
80 " with(w) {" +
81 script +
82 " }" +
83 "};" +
84 "</script></body></html>");
85 }
86 };
87 }
88 }
89