Search in sources :

Example 11 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project blueocean-plugin by jenkinsci.

the class ApiHead method getDynamic.

/**
 * Exposes all {@link ApiRoutable}s to URL space.
 *
 * @param route current URL route handled by ApiHead
 * @return {@link ApiRoutable} object
 */
public ApiRoutable getDynamic(String route) {
    setApis();
    StaplerRequest request = Stapler.getCurrentRequest();
    String m = request.getMethod();
    if (m.equalsIgnoreCase("POST") || m.equalsIgnoreCase("PUT") || m.equalsIgnoreCase("PATCH")) {
        String header = request.getHeader("Content-Type");
        if (header == null || !header.contains("application/json")) {
            throw new ServiceException(415, "Content-Type: application/json required");
        }
    }
    ApiRoutable apiRoutable = apis.get(route);
    // JENKINS-46025 - Avoid caching REST API responses for IE
    StaplerResponse response = Stapler.getCurrentResponse();
    if (response != null && !response.containsHeader("Cache-Control")) {
        response.setHeader("Cache-Control", "no-cache, no-store, no-transform");
    }
    return apiRoutable;
}
Also used : ServiceException(io.jenkins.blueocean.commons.ServiceException) StaplerRequest(org.kohsuke.stapler.StaplerRequest) StaplerResponse(org.kohsuke.stapler.StaplerResponse)

Example 12 with StaplerResponse

use of org.kohsuke.stapler.StaplerResponse in project configuration-as-code-plugin by jenkinsci.

the class ConfigurationAsCode method doCheck.

@RequirePOST
@Restricted(NoExternalUse.class)
public void doCheck(StaplerRequest req, StaplerResponse res) throws Exception {
    if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
        res.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }
    final Map<Source, String> issues = checkWith(YamlSource.of(req));
    res.setContentType("application/json");
    final JSONArray warnings = new JSONArray();
    issues.entrySet().stream().map(e -> new JSONObject().accumulate("line", e.getKey().line).accumulate("warning", e.getValue())).forEach(warnings::add);
    warnings.write(res.getWriter());
}
Also used : Sequence(io.jenkins.plugins.casc.model.Sequence) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) URL(java.net.URL) Date(java.util.Date) URISyntaxException(java.net.URISyntaxException) Scanner(java.util.Scanner) CNode(io.jenkins.plugins.casc.model.CNode) Mapping(io.jenkins.plugins.casc.model.Mapping) Which(hudson.remoting.Which) Node(org.yaml.snakeyaml.nodes.Node) Initializer(hudson.init.Initializer) NonNull(edu.umd.cs.findbugs.annotations.NonNull) Map(java.util.Map) PathMatcher(java.nio.file.PathMatcher) Scalar(io.jenkins.plugins.casc.model.Scalar) URI(java.net.URI) GlobalConfiguration(jenkins.model.GlobalConfiguration) Path(java.nio.file.Path) Functions(hudson.Functions) Restricted(org.kohsuke.accmod.Restricted) Collection(java.util.Collection) Jenkins(jenkins.model.Jenkins) Set(java.util.Set) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Logger(java.util.logging.Logger) StandardCharsets(java.nio.charset.StandardCharsets) String.format(java.lang.String.format) Objects(java.util.Objects) IOUtils(org.apache.commons.io.IOUtils) Serializer(org.yaml.snakeyaml.serializer.Serializer) YamlUtils(io.jenkins.plugins.casc.yaml.YamlUtils) List(java.util.List) POST(org.kohsuke.stapler.verb.POST) Stream(java.util.stream.Stream) Writer(java.io.Writer) SchemaGeneration.writeJSONSchema(io.jenkins.plugins.casc.SchemaGeneration.writeJSONSchema) InitMilestone(hudson.init.InitMilestone) Source(io.jenkins.plugins.casc.model.Source) JSONObject(net.sf.json.JSONObject) Permission(hudson.security.Permission) YAMLException(org.yaml.snakeyaml.error.YAMLException) IntStream(java.util.stream.IntStream) Format(io.jenkins.plugins.casc.model.Scalar.Format) ByteArrayOutputStream(java.io.ByteArrayOutputStream) QueryParameter(org.kohsuke.stapler.QueryParameter) StaplerRequest(org.kohsuke.stapler.StaplerRequest) HashMap(java.util.HashMap) PLAIN(org.yaml.snakeyaml.DumperOptions.ScalarStyle.PLAIN) TreeSet(java.util.TreeSet) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) Inject(javax.inject.Inject) DumperOptions(org.yaml.snakeyaml.DumperOptions) ACLContext(hudson.security.ACLContext) ACL(hudson.security.ACL) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Extension(hudson.Extension) OutputStreamWriter(java.io.OutputStreamWriter) Resolver(org.yaml.snakeyaml.resolver.Resolver) DOUBLE_QUOTED(org.yaml.snakeyaml.DumperOptions.ScalarStyle.DOUBLE_QUOTED) LinkedHashSet(java.util.LinkedHashSet) Util(hudson.Util) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) FormValidation(hudson.util.FormValidation) HttpServletResponse(javax.servlet.http.HttpServletResponse) ManagementLink(hudson.model.ManagementLink) NoExternalUse(org.kohsuke.accmod.restrictions.NoExternalUse) StaplerResponse(org.kohsuke.stapler.StaplerResponse) IOException(java.io.IOException) Tag(org.yaml.snakeyaml.nodes.Tag) File(java.io.File) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) Klass(org.kohsuke.stapler.lang.Klass) Collectors.toList(java.util.stream.Collectors.toList) PluginManager(hudson.PluginManager) FileVisitOption(java.nio.file.FileVisitOption) Paths(java.nio.file.Paths) JSONArray(net.sf.json.JSONArray) Emitter(org.yaml.snakeyaml.emitter.Emitter) DefaultConfiguratorRegistry(io.jenkins.plugins.casc.impl.DefaultConfiguratorRegistry) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) ServletContext(javax.servlet.ServletContext) CheckForNull(edu.umd.cs.findbugs.annotations.CheckForNull) LITERAL(org.yaml.snakeyaml.DumperOptions.ScalarStyle.LITERAL) Collections(java.util.Collections) BLOCK(org.yaml.snakeyaml.DumperOptions.FlowStyle.BLOCK) FileSystems(java.nio.file.FileSystems) JSONObject(net.sf.json.JSONObject) JSONArray(net.sf.json.JSONArray) YamlSource(io.jenkins.plugins.casc.yaml.YamlSource) Source(io.jenkins.plugins.casc.model.Source) RequirePOST(org.kohsuke.stapler.interceptor.RequirePOST) Restricted(org.kohsuke.accmod.Restricted)

Aggregations

StaplerResponse (org.kohsuke.stapler.StaplerResponse)12 StaplerRequest (org.kohsuke.stapler.StaplerRequest)9 Test (org.junit.Test)8 File (java.io.File)7 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 ArrayList (java.util.ArrayList)2 FailingHttpStatusCodeException (com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)1 CheckForNull (edu.umd.cs.findbugs.annotations.CheckForNull)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 Extension (hudson.Extension)1 Functions (hudson.Functions)1 PluginManager (hudson.PluginManager)1 Util (hudson.Util)1 InitMilestone (hudson.init.InitMilestone)1 Initializer (hudson.init.Initializer)1 ManagementLink (hudson.model.ManagementLink)1 Which (hudson.remoting.Which)1 ACL (hudson.security.ACL)1 ACLContext (hudson.security.ACLContext)1