This project has retired. For details please refer to its
Attic page.
VelocityMailGenerator xref
1 package org.apache.archiva.redback.integration.mail;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.archiva.redback.configuration.UserConfiguration;
23 import org.apache.archiva.redback.configuration.UserConfigurationKeys;
24 import org.apache.archiva.redback.keys.AuthenticationKey;
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.app.VelocityEngine;
27 import org.apache.velocity.exception.MethodInvocationException;
28 import org.apache.velocity.exception.ParseErrorException;
29 import org.apache.velocity.exception.ResourceNotFoundException;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.springframework.stereotype.Service;
33
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import java.io.StringWriter;
37 import java.text.SimpleDateFormat;
38 import java.util.Locale;
39
40
41
42
43
44
45 @Service("mailGenerator#velocity")
46 public class VelocityMailGenerator
47 implements MailGenerator
48 {
49 private Logger log = LoggerFactory.getLogger( VelocityMailGenerator.class );
50
51 @Inject
52 @Named(value = "userConfiguration#default")
53 private UserConfiguration config;
54
55
56 @Inject
57 @Named(value = "velocityEngine#redback")
58 private VelocityEngine velocityEngine;
59
60 public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl )
61 {
62 VelocityContext context = createVelocityContext( authkey, baseUrl );
63
64 String packageName = getClass().getPackage().getName().replace( '.', '/' );
65 String templateFile = packageName + "/template/" + templateName + ".vm";
66
67 StringWriter writer = new StringWriter();
68
69 try
70 {
71 velocityEngine.mergeTemplate( templateFile, context, writer );
72 }
73 catch ( ResourceNotFoundException e )
74 {
75 log.error( "No such template: '{}'.", templateFile );
76 }
77 catch ( ParseErrorException e )
78 {
79 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
80 }
81 catch ( MethodInvocationException e )
82 {
83 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
84 }
85 catch ( Exception e )
86 {
87 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
88 }
89
90 return writer.getBuffer().toString();
91 }
92
93 private VelocityContext createVelocityContext( AuthenticationKey authkey, String appUrl )
94 {
95 VelocityContext context = new VelocityContext();
96
97 context.put( "applicationUrl", config.getString( UserConfigurationKeys.APPLICATION_URL, appUrl ) );
98
99 String feedback = config.getString( UserConfigurationKeys.EMAIL_FEEDBACK_PATH );
100
101 if ( feedback != null )
102 {
103 if ( feedback.startsWith( "/" ) )
104 {
105 feedback = appUrl + feedback;
106 }
107
108 context.put( "feedback", feedback );
109 }
110
111 context.put( "urlPath",
112 config.getString( UserConfigurationKeys.EMAIL_URL_PATH, "security/login!login.action" ) );
113
114 context.put( "authkey", authkey.getKey() );
115
116 context.put( "accountId", authkey.getForPrincipal() );
117
118 SimpleDateFormat dateformatter =
119 new SimpleDateFormat( config.getString( UserConfigurationKeys.APPLICATION_TIMESTAMP ), Locale.US );
120
121 context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) );
122
123 if ( authkey.getDateExpires() != null )
124 {
125 context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) );
126 }
127 else
128 {
129 context.put( "expiresOn", "(does not expire)" );
130 }
131 return context;
132 }
133
134
135 public UserConfiguration getConfig()
136 {
137 return config;
138 }
139
140 public void setConfig( UserConfiguration config )
141 {
142 this.config = config;
143 }
144
145 public VelocityEngine getVelocityEngine()
146 {
147 return velocityEngine;
148 }
149
150 public void setVelocityEngine( VelocityEngine velocityEngine )
151 {
152 this.velocityEngine = velocityEngine;
153 }
154 }