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 javax.faces.application;
20  
21  import java.net.MalformedURLException;
22  import java.net.URL;
23  import java.util.List;
24  import java.util.Map;
25  
26  import javax.el.ExpressionFactory;
27  import javax.el.ValueExpression;
28  import javax.faces.context.ExternalContext;
29  import javax.faces.context.FacesContext;
30  
31  /**
32   * @since 2.0
33   */
34  public class NavigationCase
35  {
36      private String _condition;
37      private String _fromAction;
38      private String _fromOutcome;
39      private String _fromViewId;
40      private String _toViewId;
41      private String _toFlowDocumentId;
42      private boolean _includeViewParams;
43      private boolean _redirect;
44      private Map<String,List<String>> _parameters;
45  
46      private ValueExpression _conditionExpression;
47      private ValueExpression _toViewIdExpression;
48  
49      public NavigationCase(String fromViewId, String fromAction, String fromOutcome, String condition, String toViewId,
50              Map<String,List<String>> parameters, boolean redirect, boolean includeViewParams)
51      {
52          _condition = condition;
53          _fromViewId = fromViewId;
54          _fromAction = fromAction;
55          _fromOutcome = fromOutcome;
56          _toViewId = toViewId;
57          _toFlowDocumentId = null;
58          _redirect = redirect;
59          _includeViewParams = includeViewParams;
60          _parameters = parameters;
61      }
62      
63      public NavigationCase(String fromViewId, String fromAction, String fromOutcome, String condition, String toViewId,
64                            String toFlowDocumentId, Map<String,List<String>> parameters, boolean redirect,
65                            boolean includeViewParams)
66      {
67          _condition = condition;
68          _fromViewId = fromViewId;
69          _fromAction = fromAction;
70          _fromOutcome = fromOutcome;
71          _toViewId = toViewId;
72          _toFlowDocumentId = toFlowDocumentId;
73          _redirect = redirect;
74          _includeViewParams = includeViewParams;
75          _parameters = parameters;
76  
77      }
78  
79      /**
80       * {@inheritDoc}
81       */
82      @Override
83      public boolean equals(Object o)
84      {
85          if (o == this)
86          {
87              return true;
88          }
89          else if (o instanceof NavigationCase)
90          {
91              NavigationCase c = (NavigationCase) o;
92  
93              return equals(_fromViewId, c._fromViewId) && equals(_fromAction, c._fromAction)
94                      && equals(_fromOutcome, c._fromOutcome) && equals(_condition, c._condition)
95                      && equals(_toViewId, c._toViewId) && _redirect == c._redirect
96                      && _includeViewParams == c._includeViewParams;
97          }
98          else
99          {
100             return false;
101         }
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public int hashCode()
109     {
110         return hash(_fromViewId) << 4 ^ hash(_fromAction) << 8 ^ hash(_fromOutcome) << 12 ^ hash(_condition) << 16
111                 ^ hash(_toViewId) << 20 ^ hash(Boolean.valueOf(_redirect)) << 24
112                 ^ hash(Boolean.valueOf(_includeViewParams));
113     }
114 
115     public URL getActionURL(FacesContext context) throws MalformedURLException
116     {
117         ExternalContext externalContext = context.getExternalContext();
118         return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(),
119                 externalContext.getRequestServerPort(),
120                 context.getApplication().getViewHandler().getActionURL(context, getToViewId(context)));
121     }
122 
123     public Boolean getCondition(FacesContext context)
124     {
125         if (_condition == null)
126         {
127             return null;
128         }
129 
130         ValueExpression expression = _getConditionExpression(context);
131 
132         return Boolean.TRUE.equals(expression.getValue(context.getELContext()));
133     }
134 
135     public String getFromAction()
136     {
137         return _fromAction;
138     }
139 
140     public String getFromOutcome()
141     {
142         return _fromOutcome;
143     }
144 
145     public String getFromViewId()
146     {
147         return _fromViewId;
148     }
149     
150     public URL getBookmarkableURL(FacesContext context) throws MalformedURLException
151     {
152         ExternalContext externalContext = context.getExternalContext();
153         return new URL(externalContext.getRequestScheme(),
154                 externalContext.getRequestServerName(),
155                 externalContext.getRequestServerPort(),
156                 context.getApplication().getViewHandler().getBookmarkableURL(context, getToViewId(context), 
157                         _NavigationUtils.getEvaluatedNavigationParameters(context,
158                              getParameters()), isIncludeViewParams()));
159     }
160 
161     public URL getResourceURL(FacesContext context) throws MalformedURLException
162     {
163         ExternalContext externalContext = context.getExternalContext();
164         return new URL(externalContext.getRequestScheme(), externalContext.getRequestServerName(),
165                        externalContext.getRequestServerPort(),
166                        context.getApplication().getViewHandler().getResourceURL(context, getToViewId(context)));
167     }
168     
169     public URL getRedirectURL(FacesContext context) throws MalformedURLException
170     {
171         ExternalContext externalContext = context.getExternalContext();
172         return new URL(externalContext.getRequestScheme(),
173                 externalContext.getRequestServerName(),
174                 externalContext.getRequestServerPort(),
175                 context.getApplication().getViewHandler().getRedirectURL(context, getToViewId(context), 
176                         _NavigationUtils.getEvaluatedNavigationParameters(context,
177                              getParameters()), isIncludeViewParams()));
178     }
179     
180     public Map<String,List<String>> getParameters()
181     {
182         return _parameters;        
183     }
184 
185     public String getToViewId(FacesContext context)
186     {
187         if (_toViewId == null)
188         {
189             return null;
190         }
191 
192         ValueExpression expression = _getToViewIdExpression(context);
193         
194         return (String)( (expression.isLiteralText()) ? 
195                 expression.getExpressionString() : expression.getValue(context.getELContext()) );
196     }
197 
198     public boolean hasCondition()
199     {
200         return _condition != null && _condition.length() > 0;
201     }
202 
203     public boolean isIncludeViewParams()
204     {
205         return _includeViewParams;
206     }
207 
208     public boolean isRedirect()
209     {
210         return _redirect;
211     }
212     
213     /**
214      * @since 2.2
215      * @return 
216      */
217     public String getToFlowDocumentId()
218     {
219         return _toFlowDocumentId;
220     }
221     
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String toString()
227     {
228         StringBuilder builder = new StringBuilder(128);
229         builder.append("<navigation-case>\n");
230         
231         if (_fromViewId != null)
232         {
233             builder.append("  ");
234             builder.append("<from-view-id>");
235             builder.append(_fromViewId);
236             builder.append("</from-view-id>\n");
237         }
238         
239         if (_fromAction != null)
240         {
241             builder.append("  ");
242             builder.append("<from-action>");
243             builder.append(_fromAction);
244             builder.append("</from-action>\n");
245         }
246         
247         if (_fromOutcome != null)
248         {
249             builder.append("  ");
250             builder.append("<from-outcome>");
251             builder.append(_fromOutcome);
252             builder.append("</from-outcome>\n");
253         }
254         
255         if (_condition != null)
256         {
257             builder.append("  ");
258             builder.append("<if>");
259             builder.append(_condition);
260             builder.append("</if>\n");
261         }
262         
263         builder.append("  ");
264         builder.append("<to-view-id>");
265         builder.append(_toViewId);
266         builder.append("</to-view-id>\n");
267         
268         if (_toFlowDocumentId != null)
269         {
270             builder.append("  ");
271             builder.append("<to-flow-document-id>");
272             builder.append(_toFlowDocumentId);
273             builder.append("</to-flow-document-id>\n");
274         }
275         
276         if (_redirect)
277         {
278             builder.append("  ");
279             builder.append("<redirect include-view-params=\"");
280             builder.append(_includeViewParams);
281             if (_parameters != null && _parameters.size() != 0)
282             {
283                 builder.append("\">\n");
284                 for (Map.Entry<String, List<String>> entry : _parameters.entrySet())
285                 {
286                     String name = entry.getKey();
287                     for (String value : entry.getValue())
288                     {
289                         builder.append("    <view-param>\n");
290                         builder.append("      <name>");
291                         builder.append(name);
292                         builder.append("</name>\n");
293                         builder.append("      <value>");
294                         builder.append(value);
295                         builder.append("</value>\n");
296                         builder.append("    </view-param>\n");
297                     }
298                 }
299                 builder.append("  </redirect>\n");
300             }
301             else
302             {
303                 builder.append("\"/>\n");
304             }
305         }
306         
307         builder.append("</navigation-case>");
308         
309         return builder.toString();
310     }
311 
312     private ValueExpression _getConditionExpression(FacesContext context)
313     {
314         assert _condition != null;
315 
316         if (_conditionExpression == null)
317         {
318             ExpressionFactory factory = context.getApplication().getExpressionFactory();
319             _conditionExpression = factory.createValueExpression(context.getELContext(), _condition, Boolean.class);
320         }
321 
322         return _conditionExpression;
323     }
324 
325     private ValueExpression _getToViewIdExpression(FacesContext context)
326     {
327         assert _toViewId != null;
328 
329         if (_toViewIdExpression == null)
330         {
331             ExpressionFactory factory = context.getApplication().getExpressionFactory();
332             _toViewIdExpression = factory.createValueExpression(context.getELContext(), _toViewId, String.class);
333         }
334 
335         return _toViewIdExpression;
336     }
337 
338     private boolean equals(Object o1, Object o2)
339     {
340         if (o1 == null)
341         {
342             return o2 == null;
343         }
344         else
345         {
346             return o1.equals(o2);
347         }
348     }
349 
350     private int hash(Object o)
351     {
352         return o == null ? 0 : o.hashCode();
353     }
354 }