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  package org.apache.myfaces.application.viewstate.token;
20  
21  import java.io.UnsupportedEncodingException;
22  import javax.faces.FacesException;
23  import javax.faces.context.FacesContext;
24  import org.apache.myfaces.shared.util.StateUtils;
25  
26  /**
27   *
28   * @author Thomas Andraschko
29   */
30  public class ServiceSideStateTokenProcessor extends StateTokenProcessor
31  {
32      @Override
33      public Object decode(FacesContext facesContext, String token)
34      {
35          if (STATELESS_TOKEN.equals(token))
36          {
37              // Should not happen, because ResponseStateManager.isStateless(context,viewId) should
38              // catch it first
39              return null;
40          }
41  
42          try
43          {
44              byte[] tokenBytes = token.getBytes(StateUtils.ZIP_CHARSET);
45              byte[] tokenBytesDecoded = StateUtils.decode(tokenBytes);
46              String tokenDecoded = new String(tokenBytesDecoded, StateUtils.ZIP_CHARSET);
47  
48              return tokenDecoded;
49          }
50          catch (UnsupportedEncodingException e)
51          {
52              throw new FacesException(e);
53          }
54  
55      }
56  
57      @Override
58      public String encode(FacesContext facesContext, Object savedStateObject)
59      {
60          if (facesContext.getViewRoot().isTransient())
61          {
62              return STATELESS_TOKEN;
63          }
64  
65          try
66          {
67              // string from #encodeSerializedState
68              String token = (String) savedStateObject;
69              byte[] tokenBytes = token.getBytes(StateUtils.ZIP_CHARSET);
70              byte[] tokenBytesEncoded = StateUtils.encode(tokenBytes);
71              String tokenEncoded = new String(tokenBytesEncoded, StateUtils.ZIP_CHARSET);
72  
73              return tokenEncoded;
74          }
75          catch (UnsupportedEncodingException e)
76          {
77              throw new FacesException(e);
78          }
79      }
80  }