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.keys.AuthenticationKey;
23 import org.apache.velocity.VelocityContext;
24 import org.apache.velocity.app.VelocityEngine;
25 import org.apache.velocity.exception.MethodInvocationException;
26 import org.apache.velocity.exception.ParseErrorException;
27 import org.apache.velocity.exception.ResourceNotFoundException;
28 import org.apache.archiva.redback.configuration.UserConfiguration;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.io.StringWriter;
36 import java.text.SimpleDateFormat;
37 import java.util.Locale;
40 * Mail generator component implementation using velocity.
42 * @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( "application.url", appUrl ) );
99 String feedback = config.getString( "email.feedback.path" );
101 if ( feedback != null )
103 if ( feedback.startsWith( "/" ) )
105 feedback = appUrl + feedback;
108 context.put( "feedback", feedback );
111 context.put( "urlPath", config.getString( "email.url.path", "security/login!login.action" ) );
113 context.put( "authkey", authkey.getKey() );
115 context.put( "accountId", authkey.getForPrincipal() );
117 SimpleDateFormat dateformatter = new SimpleDateFormat( config.getString( "application.timestamp" ), Locale.US );
119 context.put( "requestedOn", dateformatter.format( authkey.getDateCreated() ) );
121 if ( authkey.getDateExpires() != null )
123 context.put( "expiresOn", dateformatter.format( authkey.getDateExpires() ) );
127 context.put( "expiresOn", "(does not expire)" );
133 public UserConfiguration getConfig()
138 public void setConfig( UserConfiguration config )
140 this.config = config;
143 public VelocityEngine getVelocityEngine()
145 return velocityEngine;
148 public void setVelocityEngine( VelocityEngine velocityEngine )
150 this.velocityEngine = velocityEngine;