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