View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.myfaces.tobago.context;
21  
22  import org.apache.myfaces.tobago.config.TobagoConfig;
23  import org.apache.myfaces.tobago.internal.util.CookieUtils;
24  import org.apache.myfaces.tobago.util.VariableResolverUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.bean.ManagedBean;
29  import javax.faces.bean.RequestScoped;
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import javax.servlet.http.HttpServletRequest;
33  
34  @ManagedBean
35  @RequestScoped
36  public class TobagoContext {
37  
38    public static final String BEAN_NAME = "tobagoContext";
39  
40    private static final Logger LOG = LoggerFactory.getLogger(TobagoContext.class);
41  
42    private static final TobagoResourceBundle RESOURCE_BUNDLE = new TobagoResourceBundle();
43    private static final TobagoMessageBundle MESSAGE_BUNDLE = new TobagoMessageBundle();
44  
45    private Theme theme;
46    private UserAgent userAgent;
47  
48    public TobagoResourceBundle getResourceBundle() {
49      return RESOURCE_BUNDLE;
50    }
51  
52    public TobagoMessageBundle getMessageBundle() {
53      return MESSAGE_BUNDLE;
54    }
55  
56    public TobagoConfig getTobagoConfig() {
57      return TobagoConfig.getInstance(FacesContext.getCurrentInstance());
58    }
59  
60    public Theme getTheme() {
61  
62      if (theme == null) {
63        final FacesContext facesContext = FacesContext.getCurrentInstance();
64        final ExternalContext externalContext = facesContext.getExternalContext();
65  
66        final String themeName;
67        Object request = externalContext.getRequest();
68        if (request instanceof HttpServletRequest) {
69          themeName = CookieUtils.getThemeNameFromCookie((HttpServletRequest) request);
70        } else {
71          themeName = null;
72        }
73  
74        theme = getTobagoConfig().getTheme(themeName);
75        if (LOG.isDebugEnabled()) {
76          LOG.debug("theme='{}'", theme.getName());
77        }
78      }
79  
80      return theme;
81    }
82  
83    public void setTheme(Theme theme) {
84      this.theme = theme;
85    }
86  
87    public UserAgent getUserAgent() {
88  
89      if (userAgent == null) {
90        final FacesContext facesContext = FacesContext.getCurrentInstance();
91        final ExternalContext externalContext = facesContext.getExternalContext();
92  
93        final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
94        userAgent = UserAgent.getInstance(requestUserAgent);
95        if (LOG.isDebugEnabled()) {
96          LOG.debug("userAgent='" + userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
97        }
98      }
99  
100     return userAgent;
101   }
102 
103   public void setUserAgent(UserAgent userAgent) {
104     this.userAgent = userAgent;
105   }
106 
107   public static TobagoContext getInstance(FacesContext facesContext) {
108     return (TobagoContext) VariableResolverUtils.resolveVariable(facesContext, BEAN_NAME);
109   }
110 }