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.util.VariableResolverUtils;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  import java.io.Serializable;
31  
32  /**
33   * <p>
34   * The ClientProperties contains data, which are individual for each user.
35   * It is stored in the session by default, but the application can override this in the faces-config.xml.
36   * </p>
37   * <p>
38   * The managed bean name which is Tobago using for the instance is {@link #MANAGED_BEAN_NAME}.
39   * </p>
40   * <p>
41   * Please use {@link ClientProperties#getInstance(javax.faces.context.FacesContext)}
42   * to access to the users client properties.
43   * </p>
44   */
45  
46  public class ClientProperties implements Serializable {
47  
48    /**
49     * The name of the managed bean
50     */
51    public static final String MANAGED_BEAN_NAME = "tobagoClientProperties";
52  
53    private static final long serialVersionUID = 2L;
54  
55    private static final Logger LOG = LoggerFactory.getLogger(ClientProperties.class);
56  
57    private String contentType = "html";
58    private Theme theme;
59    private UserAgent userAgent = UserAgent.DEFAULT;
60  
61    /**
62     * managed bean constructor
63     */
64    public ClientProperties() {
65      this(FacesContext.getCurrentInstance());
66    }
67  
68    private ClientProperties(final FacesContext facesContext) {
69  
70      final ExternalContext externalContext = facesContext.getExternalContext();
71  
72      // user agent
73      final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
74      this.userAgent = UserAgent.getInstance(requestUserAgent);
75      if (LOG.isDebugEnabled()) {
76        LOG.debug("userAgent='" + this.userAgent + "' from header " + "'User-Agent: " + requestUserAgent + "'");
77      }
78  
79      // theme
80      final String requestTheme = externalContext.getRequestParameterMap().get("tobago.theme");
81      final TobagoConfig config = TobagoConfig.getInstance(facesContext);
82      // TODO log error if tobago config is not initialized
83      this.theme = config.getTheme(requestTheme);
84      if (LOG.isDebugEnabled()) {
85        LOG.debug("theme='" + theme.getName() + "' from requestParameter " + "tobago.theme='" + requestTheme + "'");
86      }
87  
88      reset();
89    }
90  
91    /**
92     * Static method to access to the ClientProperties managed bean.
93     */
94    public static ClientProperties getInstance(final FacesContext facesContext) {
95      return (ClientProperties) VariableResolverUtils.resolveVariable(facesContext, MANAGED_BEAN_NAME);
96    }
97  
98    private void reset() {
99      ClientPropertiesKey.reset(FacesContext.getCurrentInstance());
100   }
101 
102   public String getContentType() {
103     return contentType;
104   }
105 
106   public void setContentType(final String contentType) {
107     this.contentType = contentType;
108     reset();
109   }
110 
111   public Theme getTheme() {
112     return theme;
113   }
114   
115   public void setTheme(final Theme theme) {
116     this.theme = theme;
117     reset();
118   }
119 
120   public UserAgent getUserAgent() {
121     return userAgent;
122   }
123 
124   public void setUserAgent(final UserAgent userAgent) {
125     this.userAgent = userAgent;
126     reset();
127   }
128 
129   public void updateUserAgent(final FacesContext facesContext) {
130     final ExternalContext externalContext = facesContext.getExternalContext();
131     final String requestUserAgent = externalContext.getRequestHeaderMap().get("User-Agent");
132     final UserAgent newUserAgent = UserAgent.getInstance(requestUserAgent);
133     if (newUserAgent != userAgent) {
134       userAgent = newUserAgent;
135       reset();
136     }
137   }
138 }