View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.portletbridge.portlet;
17  
18  import java.io.Serializable;
19  import java.net.URI;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import javax.portlet.PortletURL;
24  import javax.portlet.RenderResponse;
25  
26  /***
27   * @author JMcCrindle
28   */
29  public class DefaultPortletBridgeMemento implements PortletBridgeMemento, Serializable {
30  
31      /***
32       * default serial version id 
33       */
34      private static final long serialVersionUID = -5751042731400361166L;
35  
36      private Map idToRequests = new HashMap();
37      private Map dataToRequests = new HashMap();
38      private Map mementos = new HashMap();
39      private final String idParamKey;
40      private final BridgeAuthenticator bridgeAuthenticator;
41      
42      public DefaultPortletBridgeMemento(String idParamKey, BridgeAuthenticator bridgeAuthenticator) {
43          this.idParamKey = idParamKey;
44          this.bridgeAuthenticator = bridgeAuthenticator;
45      }
46  
47      /* (non-Javadoc)
48       * @see org.portletbridge.portlet.PortletBridgeMemento#getBridgeRequest(java.lang.String)
49       */
50      public BridgeRequest getBridgeRequest(String id) {
51          return (BridgeRequest) idToRequests.get(id);
52      }
53  
54      /* (non-Javadoc)
55       * @see org.portletbridge.portlet.PortletBridgeMemento#getPerPortletMemento(java.lang.String)
56       */
57      public PerPortletMemento getPerPortletMemento(String portletId) {
58          PerPortletMemento memento = (PerPortletMemento) mementos.get(portletId);
59          if(memento == null) {
60              memento = new DefaultPerPortletMemento(bridgeAuthenticator);
61              mementos.put(portletId, memento);
62          }
63          return memento;
64      }
65  
66      public BridgeRequest createBridgeRequest(RenderResponse response, String id, URI url) {
67          PortletURL pageUrl = response.createRenderURL();
68          String namespace = response.getNamespace();
69          String key = namespace + pageUrl.toString() + url.toString();
70          BridgeRequest request = (BridgeRequest) dataToRequests.get(key);
71          if(request != null) {
72              return request;
73          } else {
74              pageUrl.setParameter(idParamKey, id);
75              DefaultBridgeRequest bridgeRequest = new DefaultBridgeRequest(id, namespace, pageUrl.toString(), url);
76              idToRequests.put(id, bridgeRequest);
77              dataToRequests.put(key, bridgeRequest);
78              return bridgeRequest;
79          }
80      }
81  
82  }