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.trinidad.util;
20  
21  import java.lang.reflect.Method;
22  
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.UUID;
26  
27  import javax.faces.context.ExternalContext;
28  
29  import javax.portlet.faces.annotation.ExcludeFromManagedRequestScope;
30  
31  import javax.servlet.ServletRequest;
32  
33  /**
34   * TODO: get rid of this object
35   */
36   @ExcludeFromManagedRequestScope
37   public class RequestStateMap extends HashMap<String, Object>
38   {
39  
40     static public RequestStateMap getInstance(ServletRequest req)
41     {
42       RequestStateMap map = (RequestStateMap)req.getAttribute(_STATE_MAP);
43       
44       if(map == null)
45       {
46         map = new RequestStateMap();
47         req.setAttribute(_STATE_MAP, map);
48       }
49       
50       return map;
51     }
52     
53     static public RequestStateMap getInstance(ExternalContext ec)
54     {
55       Map<String, Object> reqMap = ec.getRequestMap();
56       RequestStateMap map = (RequestStateMap)reqMap.get(_STATE_MAP);
57       
58       //For now, always check this on a render so it can be removed from the session.
59       //This can be optimized to only save the state when request attributes are NOT preserved
60       if(!ExternalContextUtils.isRequestFromClient(ec))
61       {
62         String uuid = ec.getRequestParameterMap().get(_STATE_MAP);
63         if(uuid!= null)
64         {
65            RequestStateMap myMap= (RequestStateMap)ec.getSessionMap().remove(_STATE_MAP+"."+uuid);
66            if(map == null)
67            {
68              map = myMap;
69              reqMap.put(_STATE_MAP, map);
70            }
71            else
72            {
73              //TODO: put optimization code here
74            }
75         }
76       }
77       
78       if(map == null)
79       {
80         map = new RequestStateMap();
81         reqMap.put(_STATE_MAP, map);
82       }
83       
84       return map;
85     }
86     
87     private RequestStateMap(){};
88     
89     public void saveState(ExternalContext ec)
90     {
91       RequestType type = ExternalContextUtils.getRequestType(ec);
92       if(type.isPortlet() && !type.isResponseWritable())
93       {
94         try
95         {
96           //TODO: use reflection here but it can be replaced..
97           Object actionResp = ec.getResponse();
98           Method m = actionResp.getClass().getMethod("setRenderParameter", String.class, String.class);
99           String uuid = UUID.randomUUID().toString();
100 
101          ec.getSessionMap().put(_STATE_MAP+"."+uuid, this);
102          m.invoke(actionResp, _STATE_MAP, uuid);
103        }
104        catch(Throwable t)
105        {
106          //TODO: Log exception
107          t.printStackTrace();
108        }
109      }
110    }
111    
112    private static final String _STATE_MAP = RequestStateMap.class.getName();
113    private static final long serialVersionUID = 1L;
114  }