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.context.ClientPropertiesKey;
24  import org.apache.myfaces.tobago.internal.util.Deprecation;
25  import org.apache.myfaces.tobago.layout.Measure;
26  import org.apache.myfaces.tobago.util.VariableResolverUtils;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import javax.faces.context.ExternalContext;
31  import javax.faces.context.FacesContext;
32  import java.io.Serializable;
33  import java.util.Locale;
34  
35  /**
36   * The ClientProperties contains data, which are individual for each user.
37   * It is stored in the session by default, but the application can override this in the faces-config.xml.
38   * <p/>
39   * The managed bean name which is Tobago using for the instance is {@link #MANAGED_BEAN_NAME}.
40   * <p/>
41   * Please use {@link org.apache.myfaces.tobago.util.VariableResolverUtils#resolveClientProperties(FacesContext)}
42   * to access to the users client properties.
43   */
44  
45  public class ClientProperties implements Serializable {
46  
47    /**
48     * The name of the managed bean
49     */
50    public static final String MANAGED_BEAN_NAME = "tobagoClientProperties";
51  
52    private static final long serialVersionUID = 2L;
53  
54    private static final Logger LOG = LoggerFactory.getLogger(ClientProperties.class);
55  
56    private String contentType = "html";
57    private Theme theme;
58    private UserAgent userAgent = UserAgent.DEFAULT;
59    private boolean debugMode;
60  
61    private Measure verticalScrollbarWeight;
62    private Measure horizontalScrollbarWeight;
63  
64    private Measure pageWidth;
65    private Measure pageHeight;
66  
67    /** 
68     * managed bean constructor
69     */
70    public ClientProperties() {
71      this(FacesContext.getCurrentInstance());
72    }
73  
74    private ClientProperties(final FacesContext facesContext) {
75  
76      final ExternalContext externalContext = facesContext.getExternalContext();
77  
78      // user agent
79      final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
80      this.userAgent = UserAgent.getInstance(requestUserAgent);
81      if (LOG.isDebugEnabled()) {
82        LOG.debug("userAgent='" + this.userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
83      }
84  
85      // theme
86      final String requestTheme = externalContext.getRequestParameterMap().get("tobago.theme");
87      final TobagoConfig config = TobagoConfig.getInstance(facesContext);
88      // TODO log error if tobago config is not initialized
89      this.theme = config.getTheme(requestTheme);
90      if (LOG.isDebugEnabled()) {
91        LOG.debug("theme='" + theme.getName() + "' from requestParameter " + "tobago.theme='" + requestTheme + "'");
92      }
93  
94      reset();
95    }
96  
97    public static ClientProperties getInstance(final FacesContext facesContext) {
98      return (ClientProperties) VariableResolverUtils.resolveVariable(facesContext, MANAGED_BEAN_NAME);
99    }
100 
101   private void reset() {
102     ClientPropertiesKey.reset(FacesContext.getCurrentInstance());
103   }
104 
105   public String getContentType() {
106     return contentType;
107   }
108 
109   public void setContentType(final String contentType) {
110     this.contentType = contentType;
111     reset();
112   }
113 
114   public Theme getTheme() {
115     return theme;
116   }
117   
118   public void setTheme(final Theme theme) {
119     this.theme = theme;
120     reset();
121   }
122 
123   public UserAgent getUserAgent() {
124     return userAgent;
125   }
126 
127   public void setUserAgent(final UserAgent userAgent) {
128     this.userAgent = userAgent;
129     reset();
130   }
131 
132   public boolean isDebugMode() {
133     return debugMode;
134   }
135 
136   public void setDebugMode(final boolean debugMode) {
137     this.debugMode = debugMode;
138   }
139 
140   /**
141    * @deprecated since 2.0.0, please use {@link javax.faces.component.UIViewRoot#getLocale()}
142    */
143   public Locale getLocale() {
144     Deprecation.LOG.warn("Please get locale via UIViewRoot.");
145     return FacesContext.getCurrentInstance().getViewRoot().getLocale();
146   }
147 
148   /**
149    * @deprecated since 2.0.0
150    * This setter should not be called from the application directly,
151    * but via {@link javax.faces.component.UIViewRoot#setLocale(Locale locale)}
152    */
153   public void setLocale(final Locale locale) {
154     Deprecation.LOG.warn("Please set locale via UIViewRoot.");
155     FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
156   }
157 
158   public Measure getVerticalScrollbarWeight() {
159     return verticalScrollbarWeight;
160   }
161 
162   public void setVerticalScrollbarWeight(final Measure verticalScrollbarWeight) {
163     this.verticalScrollbarWeight = verticalScrollbarWeight;
164   }
165 
166   public Measure getHorizontalScrollbarWeight() {
167     return horizontalScrollbarWeight;
168   }
169 
170   public void setHorizontalScrollbarWeight(final Measure horizontalScrollbarWeight) {
171     this.horizontalScrollbarWeight = horizontalScrollbarWeight;
172   }
173 
174   public Measure getPageWidth() {
175     return pageWidth;
176   }
177 
178   public void setPageWidth(Measure pageWidth) {
179     this.pageWidth = pageWidth;
180   }
181 
182   public Measure getPageHeight() {
183     return pageHeight;
184   }
185 
186   public void setPageHeight(Measure pageHeight) {
187     this.pageHeight = pageHeight;
188   }
189 
190   public void updateUserAgent(final FacesContext facesContext) {
191     final ExternalContext externalContext = facesContext.getExternalContext();
192     final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
193     final UserAgent newUserAgent = UserAgent.getInstance(requestUserAgent);
194     if (newUserAgent != userAgent) {
195       userAgent = newUserAgent;
196       reset();
197     }
198   }
199 }