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 ClientProperties#getInstance(javax.faces.context.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  
60    /** @deprecated since Tobago 1.5.14 */
61    @Deprecated
62    private boolean debugMode;
63  
64    private Measure verticalScrollbarWeight;
65    private Measure horizontalScrollbarWeight;
66  
67    private Measure pageWidth;
68    private Measure pageHeight;
69  
70    /** 
71     * managed bean constructor
72     */
73    public ClientProperties() {
74      this(FacesContext.getCurrentInstance());
75    }
76  
77    private ClientProperties(final FacesContext facesContext) {
78  
79      final ExternalContext externalContext = facesContext.getExternalContext();
80  
81      // user agent
82      final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
83      this.userAgent = UserAgent.getInstance(requestUserAgent);
84      if (LOG.isDebugEnabled()) {
85        LOG.debug("userAgent='" + this.userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
86      }
87  
88      // theme
89      final String requestTheme = externalContext.getRequestParameterMap().get("tobago.theme");
90      final TobagoConfig config = TobagoConfig.getInstance(facesContext);
91      // TODO log error if tobago config is not initialized
92      this.theme = config.getTheme(requestTheme);
93      if (LOG.isDebugEnabled()) {
94        LOG.debug("theme='" + theme.getName() + "' from requestParameter " + "tobago.theme='" + requestTheme + "'");
95      }
96  
97      reset();
98    }
99  
100   /**
101    * Static method to access to the ClientProperties managed bean.
102    */
103   public static ClientProperties getInstance(final FacesContext facesContext) {
104     return (ClientProperties) VariableResolverUtils.resolveVariable(facesContext, MANAGED_BEAN_NAME);
105   }
106 
107   private void reset() {
108     ClientPropertiesKey.reset(FacesContext.getCurrentInstance());
109   }
110 
111   public String getContentType() {
112     return contentType;
113   }
114 
115   public void setContentType(final String contentType) {
116     this.contentType = contentType;
117     reset();
118   }
119 
120   public Theme getTheme() {
121     return theme;
122   }
123   
124   public void setTheme(final Theme theme) {
125     this.theme = theme;
126     reset();
127   }
128 
129   public UserAgent getUserAgent() {
130     return userAgent;
131   }
132 
133   public void setUserAgent(final UserAgent userAgent) {
134     this.userAgent = userAgent;
135     reset();
136   }
137 
138   /** @deprecated since Tobago 1.5.14 */
139   @Deprecated
140   public boolean isDebugMode() {
141     return debugMode;
142   }
143 
144   /** @deprecated since Tobago 1.5.14 */
145   @Deprecated
146   public void setDebugMode(final boolean debugMode) {
147     this.debugMode = debugMode;
148   }
149 
150   /**
151    * @deprecated since 2.0.0, please use {@link javax.faces.component.UIViewRoot#getLocale()}
152    */
153   public Locale getLocale() {
154     Deprecation.LOG.warn("Please get locale via UIViewRoot.");
155     return FacesContext.getCurrentInstance().getViewRoot().getLocale();
156   }
157 
158   /**
159    * @deprecated since 2.0.0
160    * This setter should not be called from the application directly,
161    * but via {@link javax.faces.component.UIViewRoot#setLocale(Locale locale)}
162    */
163   public void setLocale(final Locale locale) {
164     Deprecation.LOG.warn("Please set locale via UIViewRoot.");
165     FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
166   }
167 
168   public Measure getVerticalScrollbarWeight() {
169     return verticalScrollbarWeight;
170   }
171 
172   public void setVerticalScrollbarWeight(final Measure verticalScrollbarWeight) {
173     this.verticalScrollbarWeight = verticalScrollbarWeight;
174   }
175 
176   public Measure getHorizontalScrollbarWeight() {
177     return horizontalScrollbarWeight;
178   }
179 
180   public void setHorizontalScrollbarWeight(final Measure horizontalScrollbarWeight) {
181     this.horizontalScrollbarWeight = horizontalScrollbarWeight;
182   }
183 
184   public Measure getPageWidth() {
185     return pageWidth;
186   }
187 
188   public void setPageWidth(final Measure pageWidth) {
189     this.pageWidth = pageWidth;
190   }
191 
192   public Measure getPageHeight() {
193     return pageHeight;
194   }
195 
196   public void setPageHeight(final Measure pageHeight) {
197     this.pageHeight = pageHeight;
198   }
199 
200   public void updateUserAgent(final FacesContext facesContext) {
201     final ExternalContext externalContext = facesContext.getExternalContext();
202     final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
203     final UserAgent newUserAgent = UserAgent.getInstance(requestUserAgent);
204     if (newUserAgent != userAgent) {
205       userAgent = newUserAgent;
206       reset();
207     }
208   }
209 }