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.enterprise.context.RequestScoped;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import javax.inject.Inject;
33  import javax.inject.Named;
34  import javax.servlet.http.HttpServletRequest;
35  import java.io.Serializable;
36  import java.lang.invoke.MethodHandles;
37  import java.util.Locale;
38  import java.util.ResourceBundle;
39  
40  @Named
41  @RequestScoped
42  public class TobagoContext implements Serializable {
43  
44    public static final String BEAN_NAME = "tobagoContext";
45  
46    private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
47  
48    @Inject
49    private TobagoConfig tobagoConfig;
50    private Theme theme;
51    private UserAgent userAgent;
52    private String focusId;
53    private String enctype;
54  
55    /**
56     * @deprecated since 5.0.0. Please use {@link org.apache.myfaces.tobago.util.ResourceUtils#getString} in Java or
57     * #{tobagoResourceBundle.key} in Facelets.
58     */
59    @Deprecated
60    public ResourceBundle getResourceBundle() {
61      final UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
62      final Locale locale = viewRoot != null
63          ? viewRoot.getLocale() : FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
64      return ResourceBundle.getBundle("tobagoResourceBundle", locale);
65    }
66  
67    /**
68     * @deprecated since 5.0.0. Please use {@link org.apache.myfaces.tobago.util.MessageUtils}.
69     */
70    @Deprecated
71    public ResourceBundle getMessageBundle() {
72      final UIViewRoot viewRoot = FacesContext.getCurrentInstance().getViewRoot();
73      final Locale locale = viewRoot != null
74          ? viewRoot.getLocale() : FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
75      return ResourceBundle.getBundle("org.apache.myfaces.tobago.context.TobagoMessage", locale);
76    }
77  
78    /**
79     * @deprecated since 5.0.0. Please get/inject {@link TobagoConfig} directly by CDI.
80     */
81    @Deprecated
82    public TobagoConfig getTobagoConfig() {
83      return tobagoConfig;
84    }
85  
86    public Theme getTheme() {
87  
88      if (theme == null) {
89        final FacesContext facesContext = FacesContext.getCurrentInstance();
90        final ExternalContext externalContext = facesContext.getExternalContext();
91  
92        final String themeName;
93        final Object request = externalContext.getRequest();
94        if (request instanceof HttpServletRequest) {
95          themeName = CookieUtils.getThemeNameFromCookie((HttpServletRequest) request);
96        } else {
97          themeName = null;
98        }
99  
100       theme = tobagoConfig.getTheme(themeName);
101       if (LOG.isDebugEnabled()) {
102         LOG.debug("theme='{}'", theme.getName());
103       }
104     }
105 
106     return theme;
107   }
108 
109   public void setTheme(final Theme theme) {
110     this.theme = theme;
111   }
112 
113   public UserAgent getUserAgent() {
114 
115     if (userAgent == null) {
116       final FacesContext facesContext = FacesContext.getCurrentInstance();
117       final ExternalContext externalContext = facesContext.getExternalContext();
118 
119       final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
120       userAgent = UserAgent.getInstance(requestUserAgent);
121       if (LOG.isDebugEnabled()) {
122         LOG.debug("userAgent='" + userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
123       }
124     }
125 
126     return userAgent;
127   }
128 
129   public void setUserAgent(final UserAgent userAgent) {
130     this.userAgent = userAgent;
131   }
132 
133   public String getFocusId() {
134     return focusId;
135   }
136 
137   public void setFocusId(final String focusId) {
138     this.focusId = focusId;
139   }
140 
141   public String getEnctype() {
142     return enctype;
143   }
144 
145   public void setEnctype(final String enctype) {
146     this.enctype = enctype;
147   }
148 
149   public static TobagoContext getInstance(final FacesContext facesContext) {
150     return (TobagoContext) VariableResolverUtils.resolveVariable(facesContext, BEAN_NAME);
151   }
152 }