View Javadoc

1   /**
2    * 
3    */
4   package org.appfuse.webapp.server.services.impl;
5   
6   import java.io.Serializable;
7   import java.util.HashMap;
8   import java.util.Locale;
9   import java.util.Map;
10  
11  import javax.servlet.ServletContext;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  import javax.ws.rs.core.Context;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  import org.apache.cxf.jaxrs.ext.MessageContext;
19  import org.appfuse.model.User;
20  import org.appfuse.service.MailEngine;
21  import org.springframework.beans.factory.annotation.Autowired;
22  import org.springframework.beans.factory.annotation.Qualifier;
23  import org.springframework.context.MessageSource;
24  import org.springframework.mail.SimpleMailMessage;
25  import org.springframework.security.authentication.AuthenticationTrustResolver;
26  import org.springframework.security.authentication.AuthenticationTrustResolverImpl;
27  import org.springframework.security.core.Authentication;
28  import org.springframework.security.core.context.SecurityContextHolder;
29  import org.springframework.web.context.ServletContextAware;
30  
31  import com.google.web.bindery.requestfactory.server.RequestFactoryServlet;
32  
33  /**
34   * @author ivangsa
35   *
36   */
37  public abstract class AbstractBaseRequest implements ServletContextAware, CxfMessageContextAware {
38  
39      protected final transient Log log = LogFactory.getLog(getClass());
40  
41      @Autowired
42      @Qualifier("messageSource")
43      protected MessageSource messages;
44      @Autowired
45      protected MailEngine mailEngine;
46      @Autowired
47      protected SimpleMailMessage message;
48  
49      private ServletContext servletContext;
50      private final AuthenticationTrustResolver authenticationTrustResolver = new AuthenticationTrustResolverImpl();
51  
52      /**
53       * CXF JAX-RS MessageContext
54       */
55      public MessageContext messageContext;
56  
57      @Override
58      public void setServletContext(final ServletContext servletContext) {
59          this.servletContext = servletContext;
60      }
61  
62      @Override
63      @Context
64      public void setMessageContext(final MessageContext messageContext) {
65          this.messageContext = messageContext;
66      }
67  
68      /**
69       * Convenience method for getting a i18n key's value. Calling
70       * getMessageSourceAccessor() is used because the RequestContext variable is
71       * not set in unit tests b/c there's no DispatchServlet Request.
72       *
73       * @param msgKey
74       * @param locale
75       *            the current locale
76       * @return
77       */
78      public String getText(final String msgKey, final Locale locale) {
79          return messages.getMessage(msgKey, null, locale);
80      }
81  
82      /**
83       * Convenient method for getting a i18n key's value with a single string
84       * argument.
85       *
86       * @param msgKey
87       * @param arg
88       * @param locale
89       *            the current locale
90       * @return
91       */
92      public String getText(final String msgKey, final String arg, final Locale locale) {
93          return getText(msgKey, new Object[] { arg }, locale);
94      }
95  
96      /**
97       * Convenience method for getting a i18n key's value with arguments.
98       *
99       * @param msgKey
100      * @param args
101      * @param locale
102      *            the current locale
103      * @return
104      */
105     public String getText(final String msgKey, final Object[] args, final Locale locale) {
106         return messages.getMessage(msgKey, args, locale);
107     }
108 
109     /**
110      * Convenience message to send messages to users, includes app URL as
111      * footer.
112      * 
113      * @param user
114      *            the user to send a message to.
115      * @param msg
116      *            the message to send.
117      * @param url
118      *            the URL of the application.
119      */
120     protected void sendUserMessage(final User user, final String templateName, final String msg, final String url) {
121         if (log.isDebugEnabled()) {
122             log.debug("sending e-mail to user [" + user.getEmail() + "]...");
123         }
124 
125         message.setTo(user.getFullName() + "<" + user.getEmail() + ">");
126 
127         final Map<String, Serializable> model = new HashMap<String, Serializable>();
128         model.put("user", user);
129 
130         // TODO: once you figure out how to get the global resource bundle in
131         // WebWork, then figure it out here too. In the meantime, the Username
132         // and Password labels are hard-coded into the template.
133         // model.put("bundle", getTexts());
134         model.put("message", msg);
135         model.put("applicationURL", url);
136         mailEngine.sendMessage(message, templateName, model);
137     }
138 
139     protected String getCurrentUsername() {
140         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
141         if (authentication != null && !isAnonymousLogin()) {
142             return authentication.getName();
143         }
144         return null;
145     }
146 
147     protected boolean isAnonymousLogin() {
148         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
149         return authenticationTrustResolver.isAnonymous(authentication);
150     }
151 
152     protected boolean isRememberMeLogin() {
153         final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
154         return authenticationTrustResolver.isRememberMe(authentication);
155     }
156 
157     protected boolean isFullyAuthenticated() {
158         return !isAnonymousLogin() && !isRememberMeLogin();
159     }
160 
161     /**
162      * 
163      * @return
164      */
165     protected HttpServletRequest getServletRequest() {
166         final HttpServletRequest request = RequestFactoryServlet.getThreadLocalRequest();
167         if (request == null) { // jax-rs
168             return messageContext.getHttpServletRequest();
169         }
170         return request;
171     }
172 
173     /**
174      * 
175      * @return
176      */
177     protected HttpServletResponse getServletResponse() {
178         final HttpServletResponse response = RequestFactoryServlet.getThreadLocalResponse();
179         if (response == null) { // jax-rs
180             return messageContext.getHttpServletResponse();
181         }
182         return response;
183     }
184 
185     protected ServletContext getServletContext() {
186         return servletContext;
187     }
188 }