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.lifecycle;
20  
21  import java.util.HashMap;
22  import java.util.Map;
23  import javax.faces.context.FacesContext;
24  import javax.faces.lifecycle.ClientWindow;
25  import javax.faces.render.ResponseStateManager;
26  
27  /**
28   *
29   * @author lu4242
30   */
31  public class UrlClientWindow extends ClientWindow
32  {
33      private String windowId;
34  
35      private TokenGenerator tokenGenerator;
36      
37      private Map<String,String> queryParamsMap;
38      
39      public UrlClientWindow(TokenGenerator tokenGenerator)
40      {
41          this.tokenGenerator = tokenGenerator;
42      }
43          
44      @Override
45      public void decode(FacesContext context)
46      {
47          String windowId = calculateWindowId(context);
48  
49          if (windowId != null)
50          {
51              // Store the current windowId.
52              setId(windowId);
53          }
54          else
55          {
56              //Generate a new windowId
57              setId(tokenGenerator._getNextToken());
58          }
59      }
60      
61      protected String calculateWindowId(FacesContext context)
62      {
63          //1. If it comes as parameter, it takes precedence over any other choice, because
64          //   no browser is capable to do a POST and create a new window at the same time.
65          String windowId = context.getExternalContext().getRequestParameterMap().get(
66                  ResponseStateManager.CLIENT_WINDOW_PARAM);
67          if (windowId != null)
68          {
69              return windowId;
70          }
71          windowId = context.getExternalContext().getRequestParameterMap().get(
72                  ResponseStateManager.CLIENT_WINDOW_URL_PARAM);
73          if (windowId != null)
74          {
75              return windowId;
76          }
77          return null;
78      }
79  
80      @Override
81      public String getId()
82      {
83          return windowId;
84      }
85      
86      public void setId(String id)
87      {
88          windowId = id;
89          queryParamsMap = null;
90      }
91  
92      @Override
93      public Map<String, String> getQueryURLParameters(FacesContext context)
94      {
95          if (queryParamsMap == null)
96          {
97              String id = context.getExternalContext().getClientWindow().getId();
98              if (id != null)
99              {
100                 queryParamsMap = new HashMap<String, String>(2,1);
101                 queryParamsMap.put(ResponseStateManager.CLIENT_WINDOW_URL_PARAM, id);
102             }
103         }
104         return queryParamsMap;
105     }
106     
107 }