Show last authors
1 {{groovy}}
2 import org.codehaus.groovy.control.MultipleCompilationErrorsException
3
4 if (request.xpage == "plain" && xwiki.hasAccessLevel("programming", doc.fullName)) {
5
6 def scriptText = request.getParameter("script") ?: "'The received script was null.'"
7 def output = new StringWriter()
8 def binding = new Binding([
9 out: new PrintWriter(output),
10 xwiki: xwiki,
11 context: context,
12 request: request,
13 util: util,
14 services: services
15 ])
16 def stacktrace = new StringWriter()
17 def errWriter = new PrintWriter(stacktrace)
18 def result = ""
19
20 try {
21 result = new GroovyShell(binding).evaluate(scriptText)
22 } catch (MultipleCompilationErrorsException e) {
23 stacktrace.append(e.message - 'startup failed, Script1.groovy: ')
24 } catch (Throwable t) {
25
26 sanitizeStacktrace(t)
27 def cause = t
28 while (cause = cause?.cause) {
29 sanitizeStacktrace(cause)
30 }
31 t.printStackTrace(errWriter)
32 }
33
34 response.contentType = "application/json"
35 def json = [
36 "executionResult" : result,
37 "outputText" : output.toString(),
38 "stacktraceText" : stacktrace.toString()
39 ]
40 def builder = new groovy.json.JsonBuilder(json)
41 println """{{{ ${builder.toString()} }}}"""
42
43 }
44
45 else if (!xwiki.hasAccessLevel("programming", doc.fullName)) {
46 response.contentType = "application/json"
47 response.setStatus(403)
48 println new groovy.json.JsonBuilder([error: "You don't have programming access"]).toString()
49 }
50 else {
51 println "This is a groovy executor"
52 }
53
54 def sanitizeStacktrace(t) {
55 def filtered = [
56 'com.google.', 'org.mortbay.',
57 'java.', 'javax.', 'sun.',
58 'groovy.', 'org.codehaus.groovy.',
59 'com.xpn.xwiki.render', 'org.apache.velocity',
60 'org.xwiki.velocity', 'com.xpn.xwiki',
61 'org.apache.struts', 'Script1'
62 ]
63 def trace = t.getStackTrace()
64 def newTrace = []
65 trace.each { stackTraceElement ->
66 if (filtered.every { !stackTraceElement.className.startsWith(it) }) {
67 newTrace << stackTraceElement
68 }
69 }
70 def clean = newTrace.toArray(newTrace as StackTraceElement[])
71 t.stackTrace = clean
72 }
73 {{/groovy}}

Get Connected