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.internal.context;
21  
22  import org.apache.myfaces.tobago.context.ClientProperties;
23  import org.apache.myfaces.tobago.context.Theme;
24  import org.apache.myfaces.tobago.context.UserAgent;
25  import org.apache.myfaces.tobago.util.VariableResolverUtils;
26  
27  import javax.faces.component.UIViewRoot;
28  import javax.faces.context.FacesContext;
29  import java.io.Serializable;
30  import java.util.Locale;
31  import java.util.Map;
32  
33  public final class ClientPropertiesKey implements Serializable {
34  
35    private static final String KEY_IN_REQUEST = ClientPropertiesKey.class.getName();
36    
37    private final String contentType;
38    private final Theme theme;
39    private final UserAgent userAgent;
40    private final Locale locale;
41  
42    private final int hashCode;
43  
44    public static ClientPropertiesKey get(FacesContext facesContext) {
45      // todo later: refactor when having JSF 2.0: using attributes of facesContext
46      Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
47      ClientPropertiesKey key = (ClientPropertiesKey) requestMap.get(KEY_IN_REQUEST);
48      if (key == null) {
49        ClientProperties clientProperties = VariableResolverUtils.resolveClientProperties(facesContext);
50        key = new ClientPropertiesKey(clientProperties, facesContext.getViewRoot());
51        requestMap.put(KEY_IN_REQUEST, key);
52      }
53  
54      return key;
55    }
56  
57    public static void reset(FacesContext facesContext) {
58      Map<String, Object> requestMap = facesContext.getExternalContext().getRequestMap();
59      requestMap.remove(KEY_IN_REQUEST);
60    }
61    
62    private ClientPropertiesKey(final ClientProperties clientProperties, final UIViewRoot viewRoot) {
63      contentType = clientProperties.getContentType();
64      theme = clientProperties.getTheme();
65      userAgent = clientProperties.getUserAgent();
66      locale = viewRoot.getLocale();
67      hashCode = calcHashCode();
68    }
69  
70    @Override
71    public boolean equals(Object o) {
72      if (this == o) {
73        return true;
74      }
75      if (o == null || getClass() != o.getClass()) {
76        return false;
77      }
78  
79      ClientPropertiesKey that = (ClientPropertiesKey) o;
80  
81      if (!contentType.equals(that.contentType)) {
82        return false;
83      }
84      if (locale == null) {
85        if (that.locale != null) {
86          return false;
87        }
88      } else {
89        if (!locale.equals(that.locale)) {
90          return false;
91        }
92      }
93      if (!theme.equals(that.theme)) {
94        return false;
95      }
96      if (!userAgent.equals(that.userAgent)) {
97        return false;
98      }
99  
100     return true;
101   }
102 
103   private int calcHashCode() {
104     int result = contentType.hashCode();
105     result = 31 * result + theme.hashCode();
106     result = 31 * result + userAgent.hashCode();
107     if (locale != null) {
108       result = 31 * result + locale.hashCode();
109     }
110     return result;
111   }
112 
113   @Override
114   public int hashCode() {
115     return hashCode;
116   }
117 
118   @Override
119   public String toString() {
120     return "ClientPropertiesKey(" + contentType + "," + theme + "," + userAgent + "," + locale + "," + hashCode + ')';
121   }
122 
123   public String getContentType() {
124     return contentType;
125   }
126 
127   public Theme getTheme() {
128     return theme;
129   }
130 
131   public UserAgent getUserAgent() {
132     return userAgent;
133   }
134 
135   public Locale getLocale() {
136     return locale;
137   }
138 }