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.portlet;
21  
22  import java.io.Serializable;
23  import java.util.Enumeration;
24  import java.util.HashMap;
25  import java.util.Iterator;
26  import java.util.Map;
27  import javax.portlet.ActionRequest;
28  import javax.portlet.RenderRequest;
29  
30  /**
31   * This class saves the request attributes at the end of processAction.
32   *
33   * The attributes saved will be restored at the beginning of each render or 
34   * until the session times out.  Some portlet containers don't do this
35   * automatically, so it is done here.
36   *
37   * @author Stan Silvert
38   */
39  public class SavedRequestAttributes implements Serializable
40  {
41     private Map reqAttribs;
42     
43     /** Creates a new instance of SavedRequestAttributes */
44     public SavedRequestAttributes()
45     {
46     }
47     
48     public synchronized void saveRequestAttributes(ActionRequest request)
49     {
50        this.reqAttribs = new HashMap();
51        for (Enumeration i = request.getAttributeNames(); i.hasMoreElements();)
52        {
53           String name = (String)i.nextElement();
54           reqAttribs.put(name, request.getAttribute(name));
55        }
56     }
57     
58     public synchronized void resotreRequestAttributes(RenderRequest request)
59     {
60        for (Iterator i = this.reqAttribs.keySet().iterator(); i.hasNext();)
61        {
62           String name = (String)i.next();
63           Object attrib = request.getAttribute(name);
64           
65           // if somebody already set this attribute, don't overwrite
66           if (attrib == null)
67           {
68              request.setAttribute(name, this.reqAttribs.get(name));
69           }
70        }
71     }
72  }