-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathMailMessageContentRenderer.groovy
455 lines (415 loc) · 19.5 KB
/
MailMessageContentRenderer.groovy
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright 2010-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package grails.plugins.mail
import grails.core.GrailsApplication
import grails.plugins.GrailsPluginManager
import grails.web.mapping.LinkGenerator
import grails.web.pages.GroovyPagesUriService
import groovy.text.Template
import groovy.transform.CompileStatic
import org.grails.gsp.GroovyPageTemplate
import org.grails.gsp.GroovyPagesTemplateEngine
import org.grails.web.servlet.WrappedResponseHolder
import org.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.context.ApplicationContext
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.servlet.DispatcherServlet
import org.springframework.web.servlet.i18n.FixedLocaleResolver
import org.springframework.web.servlet.support.RequestContextUtils
import jakarta.servlet.http.Cookie
import jakarta.servlet.http.HttpServletRequest
import jakarta.servlet.http.HttpServletResponse
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy
import java.util.concurrent.ConcurrentHashMap
/**
* Renders a GSP into the content of a mail message.
*/
@CompileStatic
class MailMessageContentRenderer {
GroovyPagesTemplateEngine groovyPagesTemplateEngine
GroovyPagesUriService groovyPagesUriService
GrailsApplication grailsApplication
GrailsPluginManager pluginManager
MailMessageContentRenderer() {}
/**
* @param groovyPagesTemplateEngine The GSP template engine to use
* @param groovyPagesUriService The GSP URI service to use
* @param grailsApplication The Grails application
* @param pluginManager The plugin manager
* @since 4.0.0
*/
MailMessageContentRenderer(GroovyPagesTemplateEngine groovyPagesTemplateEngine,
GroovyPagesUriService groovyPagesUriService,
GrailsApplication grailsApplication,
GrailsPluginManager pluginManager) {
this.groovyPagesTemplateEngine = groovyPagesTemplateEngine
this.groovyPagesUriService = groovyPagesUriService
this.grailsApplication = grailsApplication
this.pluginManager = pluginManager
}
MailMessageContentRender render(Writer out, String templateName, Map model, Locale locale, String pluginName = null) {
RenderEnvironment.with(grailsApplication.mainContext, out, locale) { RenderEnvironment env ->
Template template = createTemplate(templateName, env.controllerName, pluginName)
if (model instanceof Map) {
template.make(model).writeTo(out)
} else {
template.make().writeTo(out)
}
new MailMessageContentRender(out, template.metaInfo.contentType)
} as MailMessageContentRender
}
protected GroovyPageTemplate createTemplate(String templateName, String controllerName, String pluginName) {
if (templateName.startsWith('/')) {
if (!controllerName) {
controllerName = ''
}
} else {
if (!controllerName) {
throw new IllegalArgumentException('Mail views cannot be loaded from relative view paths when there is no current HTTP request')
}
}
String contextPath = getContextPath(pluginName)
String templateUri = contextPath ?
contextPath + groovyPagesUriService.getViewURI(controllerName, templateName) :
groovyPagesUriService.getDeployedViewURI(controllerName, templateName)
def template = groovyPagesTemplateEngine.createTemplateForUri(templateUri)
if (!template) {
if (pluginName) {
throw new IllegalArgumentException("Could not locate email view ${templateName} in plugin [$pluginName]")
} else {
throw new IllegalArgumentException("Could not locate mail body ${templateName}. Is it in a plugin? If so you must pass the plugin name in the [plugin] variable")
}
}
template as GroovyPageTemplate
}
protected String getContextPath(String pluginName) {
String contextPath = ''
if (pluginName) {
def plugin = pluginManager.getGrailsPlugin(pluginName)
if (plugin && !plugin.isBasePlugin()) {
contextPath = "${plugin.pluginPath}/grails-app/views"
}
}
contextPath
}
private static class RenderEnvironment {
final PrintWriter out
final Locale locale
final ApplicationContext applicationContext
final LinkGenerator grailsLinkGenerator
private GrailsWebRequest originalRequestAttributes
private GrailsWebRequest renderRequestAttributes
private HttpServletResponse originalWrappedResponse
RenderEnvironment(ApplicationContext applicationContext, Writer out, Locale locale = null) {
this.out = out instanceof PrintWriter ? out as PrintWriter : new PrintWriter(out)
this.locale = locale
this.applicationContext = applicationContext
this.grailsLinkGenerator = applicationContext.getBean('grailsLinkGenerator', LinkGenerator)
}
private void init() {
originalRequestAttributes = RequestContextHolder.getRequestAttributes() as GrailsWebRequest
originalWrappedResponse = WrappedResponseHolder.wrappedResponse
def renderLocale = Locale.getDefault()
if (locale) {
renderLocale = locale
} else if (originalRequestAttributes) {
renderLocale = RequestContextUtils.getLocale(originalRequestAttributes.request)
}
renderRequestAttributes = new GrailsWebRequest(
PageRenderRequestCreator.createInstance(grailsLinkGenerator.serverBaseURL, '/mail/render', renderLocale),
PageRenderResponseCreator.createInstance(out, renderLocale),
null,
applicationContext
)
if (originalRequestAttributes) {
renderRequestAttributes.controllerName = originalRequestAttributes.controllerName
}
RequestContextHolder.requestAttributes = renderRequestAttributes
renderRequestAttributes.request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new FixedLocaleResolver(defaultLocale: renderLocale))
renderRequestAttributes.setOut(out)
WrappedResponseHolder.wrappedResponse = renderRequestAttributes.currentResponse
}
private void close() {
RequestContextHolder.requestAttributes = originalRequestAttributes // null ok
WrappedResponseHolder.wrappedResponse = originalWrappedResponse
}
/**
* Establish an environment inheriting the locale of the current request if there is one
*/
static Object with(ApplicationContext applicationContext, Writer out, Closure block) {
with(applicationContext, out, null, block)
}
/**
* Establish an environment with a specific locale
*/
static Object with(ApplicationContext applicationContext, Writer out, Locale locale, Closure block) {
def env = new RenderEnvironment(applicationContext, out, locale)
env.init()
try {
block(env)
} finally {
env.close()
}
}
String getControllerName() {
renderRequestAttributes.controllerName
}
}
/*
* Creates the request object used during the GSP rendering pipeline for render operations outside a web request.
* Created dynamically to avoid issues with different servlet API spec versions.
*/
static class PageRenderRequestCreator {
static HttpServletRequest createInstance(final String serverBaseURL, final String requestURI, Locale localeToUse = Locale.getDefault()) {
final URI serverBaseURI = serverBaseURL != null ? new URI(serverBaseURL) : null
def params = new ConcurrentHashMap()
def attributes = new ConcurrentHashMap()
String contentType = null
String characterEncoding = 'UTF-8'
Proxy.newProxyInstance(HttpServletRequest.classLoader, [HttpServletRequest] as Class[], new InvocationHandler() {
Object invoke(proxy, Method method, Object[] args) {
String methodName = method.name
if (methodName == 'getContentType') {
return contentType
}
if (methodName == 'setContentType') {
contentType = args[0]
return null
}
if (methodName == 'getCharacterEncoding') {
return characterEncoding
}
if (methodName == 'setCharacterEncoding') {
characterEncoding = args[0]
return null
}
if (methodName == 'getRealPath') {
return requestURI
}
if (methodName == 'getLocalName') {
return 'localhost'
}
if (methodName == 'getLocalAddr') {
return '127.0.0.1'
}
if (methodName == 'getLocalPort') {
return 80
}
if (methodName == 'getCookies') {
return ([] as Cookie[])
}
if (methodName == 'getDateHeader' || methodName == 'getIntHeader') {
return -1
}
if (methodName == 'getMethod') {
return 'GET'
}
if (methodName == 'getContextPath' || methodName == 'getServletPath') {
return '/'
}
if (methodName in ['getPathInfo', 'getPathTranslated', 'getQueryString']) {
return ''
}
if (methodName == 'getRequestURL') {
return new StringBuffer(requestURI)
}
if (methodName == 'getRequestURI') {
return requestURI
}
if (methodName == 'isRequestedSessionIdValid') {
return true
}
if (methodName in [
'isRequestedSessionIdFromCookie', 'isRequestedSessionIdFromURL', 'isRequestedSessionIdFromUrl',
'authenticate', 'isUserInRole', 'isSecure', 'isAsyncStarted', 'isAsyncSupported']) {
return false
}
if (methodName == 'getSession') {
throw new UnsupportedOperationException('You cannot use the session in non-request rendering operations')
}
if (methodName == 'getInputStream') {
throw new UnsupportedOperationException('You cannot read the input stream in non-request rendering operations')
}
if (methodName == 'getProtocol') {
throw new UnsupportedOperationException('You cannot read the protocol in non-request rendering operations')
}
if (methodName == 'getScheme') {
if (serverBaseURI == null) {
throw new UnsupportedOperationException('You cannot read the scheme in non-request rendering operations')
}
return serverBaseURI.scheme
}
if (methodName == 'getServerName') {
if(serverBaseURI == null) {
throw new UnsupportedOperationException('You cannot read the servername in non-request rendering operations')
}
return serverBaseURI.host
}
if (methodName == 'getServerPort') {
if (serverBaseURI == null) {
throw new UnsupportedOperationException('You cannot read the server port in non-request rendering operations')
}
int port = serverBaseURI.port
if (port == -1) {
switch (serverBaseURI.scheme?.toLowerCase()) {
case 'https':
port = 443
break
default:
port = 80
}
}
return port
}
if (methodName == 'getReader') {
throw new UnsupportedOperationException('You cannot read input in non-request rendering operations')
}
if (methodName == 'getRemoteAddr') {
throw new UnsupportedOperationException('You cannot read the remote address in non-request rendering operations')
}
if (methodName == 'getRemoteHost') {
throw new UnsupportedOperationException('You cannot read the remote host in non-request rendering operations')
}
if (methodName == 'getRequestDispatcher') {
throw new UnsupportedOperationException('You cannot use the request dispatcher in non-request rendering operations')
}
if (methodName == 'getRemotePort') {
throw new UnsupportedOperationException('You cannot read the remote port in non-request rendering operations')
}
if (methodName == 'getParts') {
return []
}
if (methodName == 'getAttribute') {
return attributes[args[0]]
}
if (methodName == 'getAttributeNames') {
return attributes.keys()
}
if (methodName == 'setAttribute') {
String name = args[0]
Object o = args[1]
if (o == null) {
attributes.remove(name)
} else {
attributes[name] = o
}
return null
}
if (methodName == 'removeAttribute') {
attributes.remove(args[0])
return null
}
if (methodName == 'getLocale') {
return localeToUse
}
if (methodName == 'getLocales') {
def iterator = [localeToUse].iterator()
//noinspection UnnecessaryQualifiedReference
return PageRenderRequestCreator.iteratorAsEnumeration(iterator)
}
if (methodName == 'getParameter') {
return params[args[0]]
}
if (methodName == 'getParameterNames') {
return params.keys()
}
if (methodName == 'getParameterValues') {
return [] as String[]
}
if (methodName == 'getParameterMap') {
return params
}
if (methodName == 'getContentLength') {
return 0
}
if ('getHeaderNames' == methodName || 'getHeaders' == methodName) {
return Collections.enumeration(Collections.emptySet())
}
return null
}
}) as HttpServletRequest
}
private static Enumeration iteratorAsEnumeration(Iterator iterator) {
new Enumeration() {
@Override
boolean hasMoreElements() {
iterator.hasNext()
}
@Override
Object nextElement() {
iterator.next()
}
}
}
}
static class PageRenderResponseCreator {
static HttpServletResponse createInstance(final PrintWriter writer, Locale localeToUse = Locale.getDefault()) {
String characterEncoding = 'UTF-8'
String contentType = null
int bufferSize = 0
Proxy.newProxyInstance(HttpServletResponse.classLoader, [HttpServletResponse] as Class[], new InvocationHandler() {
Object invoke(proxy, Method method, Object[] args) {
String methodName = method.name
if (methodName == 'getContentType') {
return contentType
}
if (methodName == 'setContentType') {
contentType = args[0]
return null
}
if (methodName == 'getCharacterEncoding') {
return characterEncoding
}
if (methodName == 'setCharacterEncoding') {
characterEncoding = args[0]
return null
}
if (methodName == 'getBufferSize') {
return bufferSize
}
if (methodName == 'setBufferSize') {
bufferSize = args[0] as Integer
return null
}
if (methodName == 'containsHeader' || methodName == 'isCommitted') {
return false
}
if (methodName in ['encodeURL', 'encodeRedirectURL', 'encodeUrl', 'encodeRedirectUrl']) {
return args[0]
}
if (methodName == 'getWriter') {
return writer
}
if (methodName == 'getOutputStream') {
throw new UnsupportedOperationException('You cannot use the OutputStream in non-request rendering operations. Use getWriter() instead')
}
if (methodName == 'getHeaderNames') {
return []
}
if (methodName == 'getLocale') {
return localeToUse
}
if (methodName == 'getStatus') {
return 0
}
return null
}
}) as HttpServletResponse
}
}
}