This project has retired. For details please refer to its Attic page.
VelocityMailGenerator xref
View Javadoc

1   package org.apache.archiva.redback.integration.mail;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * Mail generator component implementation using velocity.
42   *
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
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      // FIXME use the spring directly 
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 }