All Downloads are FREE. Search and download functionalities are using the official Maven repository.

hudson.util.FormFieldValidator Maven / Gradle / Ivy

package hudson.util;

import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;

import javax.servlet.ServletException;
import java.io.IOException;
import java.io.File;

import hudson.model.Hudson;
import hudson.Util;

/**
 * @author Kohsuke Kawaguchi
 */
public abstract class FormFieldValidator {
    protected final StaplerRequest request;
    protected final StaplerResponse response;
    private final boolean isAdminOnly;

    protected FormFieldValidator(StaplerRequest request, StaplerResponse response, boolean adminOnly) {
        this.request = request;
        this.response = response;
        isAdminOnly = adminOnly;
    }

    /**
     * Runs the validation code.
     */
    public final void process() throws IOException, ServletException {
        if(isAdminOnly && !Hudson.adminCheck(request,response))
            return; // failed check

        check();
    }

    protected abstract void check() throws IOException, ServletException;

    /**
     * Gets the parameter as a file.
     */
    protected final File getFileParameter(String paramName) {
        return new File(Util.fixNull(request.getParameter(paramName)));
    }

    /**
     * Sends out an HTML fragment that indicates a success.
     */
    public void ok() throws IOException, ServletException {
        response.setContentType("text/html");
        response.getWriter().print("
"); } /** * Sends out an HTML fragment that indicates an error. */ public void error(String message) throws IOException, ServletException { response.setContentType("text/html"); response.getWriter().print("
"+message+"
"); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy