FormFillFailure.java 7.3 KB
Newer Older
1 2 3
/*
 * The MIT License
 *
4
 * Copyright (c) 2004-2017, Sun Microsystems, Inc., CloudBees, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.util;

import hudson.Functions;
import hudson.Util;
import java.io.IOException;
import java.util.Locale;
import javax.annotation.Nonnull;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

/**
40
 * Represents a failure in a form field doFillXYZ method.
41 42
 *
 * <p>
43
 * Use one of the factory methods to create an instance, then throw it from your {@code doFillXyz}
44 45
 * method.
 *
46
 * @since 2.50
47 48 49 50 51 52 53 54 55 56 57 58 59
 */
public abstract class FormFillFailure extends IOException implements HttpResponse {

    /**
     * Sends out a string error message that indicates an error.
     *
     * @param message Human readable message to be sent.
     */
    public static FormFillFailure error(@Nonnull String message) {
        return errorWithMarkup(Util.escape(message));
    }

    public static FormFillFailure warning(@Nonnull String message) {
60
        return warningWithMarkup(Util.escape(message));
61 62 63 64 65 66 67
    }

    /**
     * Sends out a string error message that indicates an error,
     * by formatting it with {@link String#format(String, Object[])}
     */
    public static FormFillFailure error(String format, Object... args) {
68
        return error(String.format(format, args));
69 70 71
    }

    public static FormFillFailure warning(String format, Object... args) {
72
        return warning(String.format(format, args));
73 74 75 76 77 78 79
    }

    /**
     * Sends out a string error message, with optional "show details" link that expands to the full stack trace.
     *
     * <p>
     * Use this with caution, so that anonymous users do not gain too much insights into the state of the system,
80
     * as error stack trace often reveals a lot of information. Consider if an error needs to be exposed
81 82 83 84 85 86 87 88 89 90 91
     * to everyone or just those who have higher access to job/hudson/etc.
     */
    public static FormFillFailure error(Throwable e, String message) {
        return _error(FormValidation.Kind.ERROR, e, message);
    }

    public static FormFillFailure warning(Throwable e, String message) {
        return _error(FormValidation.Kind.WARNING, e, message);
    }

    private static FormFillFailure _error(FormValidation.Kind kind, Throwable e, String message) {
92 93 94 95 96 97 98 99 100 101
        if (e == null) {
            return _errorWithMarkup(Util.escape(message), kind);
        }

        return _errorWithMarkup(Util.escape(message) +
                " <a href='#' class='showDetails'>"
                + Messages.FormValidation_Error_Details()
                + "</a><pre style='display:none'>"
                + Util.escape(Functions.printThrowable(e)) +
                "</pre>", kind
102 103 104 105
        );
    }

    public static FormFillFailure error(Throwable e, String format, Object... args) {
106
        return error(e, String.format(format, args));
107 108 109
    }

    public static FormFillFailure warning(Throwable e, String format, Object... args) {
110
        return warning(e, String.format(format, args));
111 112 113 114 115 116 117 118 119
    }

    /**
     * Sends out an HTML fragment that indicates an error.
     *
     * <p>
     * This method must be used with care to avoid cross-site scripting
     * attack.
     *
120 121
     * @param message Human readable message to be sent. {@code error(null)}
     *                can be used as {@code ok()}.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
     */
    public static FormFillFailure errorWithMarkup(String message) {
        return _errorWithMarkup(message, FormValidation.Kind.ERROR);
    }

    public static FormFillFailure warningWithMarkup(String message) {
        return _errorWithMarkup(message, FormValidation.Kind.WARNING);
    }

    private static FormFillFailure _errorWithMarkup(@Nonnull final String message, final FormValidation.Kind kind) {
        return new FormFillFailure(kind, message) {
            public String renderHtml() {
                StaplerRequest req = Stapler.getCurrentRequest();
                if (req == null) { // being called from some other context
                    return message;
                }
                // 1x16 spacer needed for IE since it doesn't support min-height
139 140 141
                return "<div class=" + getKind().name().toLowerCase(Locale.ENGLISH) + "><img src='" +
                        req.getContextPath() + Jenkins.RESOURCE_PATH + "/images/none.gif' height=16 width=1>" +
                        message + "</div>";
142
            }
143 144 145

            @Override
            public String toString() {
146 147 148 149 150 151 152 153 154 155 156 157 158
                return kind + ": " + message;
            }
        };
    }

    /**
     * Sends out an arbitrary HTML fragment as the output.
     */
    public static FormFillFailure respond(FormValidation.Kind kind, final String html) {
        return new FormFillFailure(kind) {
            public String renderHtml() {
                return html;
            }
159 160 161

            @Override
            public String toString() {
162 163 164 165 166 167 168 169 170 171
                return getKind() + ": " + html;
            }
        };
    }

    private final FormValidation.Kind kind;
    private boolean selectionCleared;

    /**
     * Instances should be created via one of the factory methods above.
172
     *
173 174 175 176 177 178 179 180 181 182 183
     * @param kind the kind
     */
    private FormFillFailure(FormValidation.Kind kind) {
        this.kind = kind;
    }

    private FormFillFailure(FormValidation.Kind kind, String message) {
        super(message);
        this.kind = kind;
    }

184 185
    public void generateResponse(StaplerRequest req, StaplerResponse rsp, Object node)
            throws IOException, ServletException {
186 187 188 189 190 191 192 193 194 195 196 197 198 199
        rsp.setContentType("text/html;charset=UTF-8");
        rsp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        rsp.setHeader("X-Jenkins-Select-Error", selectionCleared ? "clear" : "retain");
        rsp.getWriter().print(renderHtml());
    }

    public FormValidation.Kind getKind() {
        return kind;
    }

    public boolean isSelectionCleared() {
        return selectionCleared;
    }

200 201 202 203 204
    /**
     * Flags this failure as requiring that the select options should be cleared out.
     *
     * @return {@code this} for method chaining.
     */
205 206 207 208 209 210 211 212
    public FormFillFailure withSelectionCleared() {
        this.selectionCleared = true;
        return this;
    }

    public abstract String renderHtml();

}