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  import java.io.Serializable;
34  
35  @ManagedBean
36  @RequestScoped
37  public class TobagoContext implements Serializable {
38  
39    public static final String BEAN_NAME = "tobagoContext";
40  
41    private static final Logger LOG = LoggerFactory.getLogger(TobagoContext.class);
42  
43    private static final TobagoResourceBundle RESOURCE_BUNDLE = new TobagoResourceBundle();
44    private static final TobagoMessageBundle MESSAGE_BUNDLE = new TobagoMessageBundle();
45  
46    private Theme theme;
47    private UserAgent userAgent;
48    private String focusId;
49    private String enctype;
50  
51    public TobagoResourceBundle getResourceBundle() {
52      return RESOURCE_BUNDLE;
53    }
54  
55    public TobagoMessageBundle getMessageBundle() {
56      return MESSAGE_BUNDLE;
57    }
58  
59    public TobagoConfig getTobagoConfig() {
60      return TobagoConfig.getInstance(FacesContext.getCurrentInstance());
61    }
62  
63    public Theme getTheme() {
64  
65      if (theme == null) {
66        final FacesContext facesContext = FacesContext.getCurrentInstance();
67        final ExternalContext externalContext = facesContext.getExternalContext();
68  
69        final String themeName;
70        final Object request = externalContext.getRequest();
71        if (request instanceof HttpServletRequest) {
72          themeName = CookieUtils.getThemeNameFromCookie((HttpServletRequest) request);
73        } else {
74          themeName = null;
75        }
76  
77        theme = getTobagoConfig().getTheme(themeName);
78        if (LOG.isDebugEnabled()) {
79          LOG.debug("theme='{}'", theme.getName());
80        }
81      }
82  
83      return theme;
84    }
85  
86    public void setTheme(final Theme theme) {
87      this.theme = theme;
88    }
89  
90    public UserAgent getUserAgent() {
91  
92      if (userAgent == null) {
93        final FacesContext facesContext = FacesContext.getCurrentInstance();
94        final ExternalContext externalContext = facesContext.getExternalContext();
95  
96        final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
97        userAgent = UserAgent.getInstance(requestUserAgent);
98        if (LOG.isDebugEnabled()) {
99          LOG.debug("userAgent='" + userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
100       }
101     }
102 
103     return userAgent;
104   }
105 
106   public void setUserAgent(final UserAgent userAgent) {
107     this.userAgent = userAgent;
108   }
109 
110   public String getFocusId() {
111     return focusId;
112   }
113 
114   public void setFocusId(final String focusId) {
115     this.focusId = focusId;
116   }
117 
118   public String getEnctype() {
119     return enctype;
120   }
121 
122   public void setEnctype(final String enctype) {
123     this.enctype = enctype;
124   }
125 
126   public static TobagoContext getInstance(final FacesContext facesContext) {
127     return (TobagoContext) VariableResolverUtils.resolveVariable(facesContext, BEAN_NAME);
128   }
129 }