1 package org.apache.archiva.redback.integration.mail;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
41 * Mail generator component implementation using velocity.
43 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45 @Service("mailGenerator#velocity")
46 public class VelocityMailGenerator
47 implements MailGenerator
49 private Logger log = LoggerFactory.getLogger( VelocityMailGenerator.class );
52 @Named(value = "userConfiguration")
53 private UserConfiguration config;
55 // FIXME use the spring directly
57 @Named(value = "velocityEngine#redback")
58 private VelocityEngine velocityEngine;
60 public String generateMail( String templateName, AuthenticationKey authkey, String baseUrl )
62 VelocityContext context = createVelocityContext( authkey, baseUrl );
64 String packageName = getClass().getPackage().getName().replace( '.', '/' );
65 String templateFile = packageName + "/template/" + templateName + ".vm";
67 StringWriter writer = new StringWriter();
71 velocityEngine.mergeTemplate( templateFile, context, writer );
73 catch ( ResourceNotFoundException e )
75 log.error( "No such template: '{}'.", templateFile );
77 catch ( ParseErrorException e )
79 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
81 catch ( MethodInvocationException e )
83 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
87 log.error( "Unable to generate email for template '" + templateFile + "': " + e.getMessage(), e );
90 return writer.getBuffer().toString();
93 private VelocityContext createVelocityContext( AuthenticationKey authkey, String appUrl )
95 VelocityContext context = new VelocityContext();
97 context.put( "applicationUrl", config.getString( UserConfigurationKeys.APPLICATION_URL, appUrl ) );
99 String feedback = config.getString( UserConfigurationKeys.EMAIL_FEEDBACK_PATH );
101 if ( feedback != null )
103 if ( feedback.startsWith( "/" ) )
105 feedback = appUrl + feedback;
108 context.put( "feedback", feedback );
111 context.put( "urlPath",
112 config.getString( UserConfigurationKeys.EMAIL_URL_PATH, "security/login!login.action" ) );
114 context.put( "authkey", authkey.getKey() );
116 context.put( "accountId", authkey.getForPrincipal() );
118 SimpleDateFormat dateformatter =
119 new SimpleDateFormat( config.getString( UserConfigurationKeys.APPLICATION_TIMESTAMP ), Locale.US );
121 context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) );
123 if ( authkey.getDateExpires() != null )
125 context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) );
129 context.put( "expiresOn", "(does not expire)" );
135 public UserConfiguration getConfig()
140 public void setConfig( UserConfiguration config )
142 this.config = config;
145 public VelocityEngine getVelocityEngine()
147 return velocityEngine;
150 public void setVelocityEngine( VelocityEngine velocityEngine )
152 this.velocityEngine = velocityEngine;