-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Expand file tree
/
Copy pathRenderOnDemandClosure.java
More file actions
161 lines (148 loc) · 6.87 KB
/
RenderOnDemandClosure.java
File metadata and controls
161 lines (148 loc) · 6.87 KB
1
2
3
4
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* The MIT License
*
* Copyright (c) 2011, CloudBees, Inc.
*
* 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.widgets;
import hudson.Extension;
import hudson.Util;
import hudson.model.Descriptor;
import hudson.security.csrf.CrumbExclusion;
import hudson.util.PackedMap;
import jakarta.servlet.FilterChain;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.jelly.JellyContext;
import org.apache.commons.jelly.JellyTagException;
import org.apache.commons.jelly.Script;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.framework.adjunct.AdjunctsInPage;
import org.kohsuke.stapler.jelly.DefaultScriptInvoker;
import org.xml.sax.SAXException;
/**
* Captured Jelly {@link Script} that can be rendered later on demand from JavaScript.
*
* @author Kohsuke Kawaguchi
*/
public class RenderOnDemandClosure {
/**
* Captures the recursive taglib call stack.
*/
private final Script[] bodyStack;
private final Map<String, Object> variables;
private final String currentDescriptorByNameUrl;
private final String[] adjuncts;
public RenderOnDemandClosure(JellyContext context, String attributesToCapture) {
List<Script> bodyStack = new ArrayList<>();
for (JellyContext c = context; c != null; c = c.getParent()) {
Script script = (Script) c.getVariables().get("org.apache.commons.jelly.body");
if (script != null) bodyStack.add(script);
}
this.bodyStack = bodyStack.toArray(new Script[0]);
assert !bodyStack.isEmpty(); // there must be at least one, which is the direct child of <l:renderOnDemand>
Map<String, Object> variables = new HashMap<>();
String _attributesToCapture = Util.fixEmpty(attributesToCapture);
if (_attributesToCapture != null) {
for (String v : _attributesToCapture.split(",")) {
variables.put(v.intern(), context.getVariable(v));
}
}
// capture the current base of context for descriptors
currentDescriptorByNameUrl = Descriptor.getCurrentDescriptorByNameUrl();
this.variables = PackedMap.of(variables);
Set<String> _adjuncts = AdjunctsInPage.get().getIncluded();
this.adjuncts = new String[_adjuncts.size()];
int i = 0;
for (String adjunct : _adjuncts) {
this.adjuncts[i++] = adjunct.intern();
}
LOGGER.fine(() -> "captured " + variables);
}
/**
* Renders the captured fragment.
*/
@JavaScriptMethod
public HttpResponse render() {
return new HttpResponse() {
@Override
public void generateResponse(StaplerRequest2 req, StaplerResponse2 rsp, Object node) throws IOException, ServletException {
LOGGER.fine(() -> "rendering " + req.getPathInfo());
req.getWebApp().getDispatchValidator().allowDispatch(req, rsp);
try {
new DefaultScriptInvoker() {
@Override
protected JellyContext createContext(StaplerRequest2 req, StaplerResponse2 rsp, Script script, Object it) {
JellyContext context = super.createContext(req, rsp, script, it);
for (int i = bodyStack.length - 1; i > 0; i--) { // exclude bodyStack[0]
context = new JellyContext(context);
context.setVariable("org.apache.commons.jelly.body", bodyStack[i]);
}
try {
AdjunctsInPage.get().assumeIncluded(adjuncts);
} catch (IOException | SAXException e) {
LOGGER.log(Level.WARNING, "Failed to resurrect adjunct context", e);
}
return context;
}
@Override
protected void exportVariables(StaplerRequest2 req, StaplerResponse2 rsp, Script script, Object it, JellyContext context) {
super.exportVariables(req, rsp, script, it, context);
context.setVariables(variables);
req.setAttribute("currentDescriptorByNameUrl", currentDescriptorByNameUrl);
}
}.invokeScript(req, rsp, bodyStack[0], null);
} catch (JellyTagException e) {
LOGGER.log(Level.WARNING, "Failed to evaluate the template closure", e);
throw new IOException("Failed to evaluate the template closure", e);
}
}
};
}
/**
* In JS, navigator.sendBeacon() is used to send the unload request, and always sends POST.
*
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon">MDN: sendBeacon</a>
*/
@Extension
public static class ReleaseBoundObjectsCrumbExclusion extends CrumbExclusion {
@Override
public boolean process(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
if (request.getPathInfo().startsWith("/$stapler/bound/release/")) {
chain.doFilter(request, response);
return true;
}
return false;
}
}
private static final Logger LOGGER = Logger.getLogger(RenderOnDemandClosure.class.getName());
}