View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.portals.bridges.frameworks;
18  
19  import java.util.StringTokenizer;
20  
21  import javax.portlet.PortletMode;
22  import javax.portlet.PortletModeException;
23  import javax.portlet.PortletURL;
24  import javax.portlet.RenderRequest;
25  import javax.portlet.RenderResponse;
26  import javax.portlet.WindowState;
27  import javax.portlet.WindowStateException;
28  
29  import org.apache.portals.bridges.frameworks.model.PortletApplicationModel;
30  
31  
32  /***
33   * Forwarder
34   * 
35   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
36   * @version $Id: Forwarder.java 517068 2007-03-12 01:44:37Z ate $
37   */
38  public class Forwarder
39  {
40      PortletApplicationModel model;
41      RenderRequest  request;
42      RenderResponse response;    
43      
44      public Forwarder(PortletApplicationModel model,
45                       RenderRequest  request,
46                       RenderResponse response)
47      {
48          this.model = model;
49          this.request = request;        
50          this.response = response;
51      }
52      
53      public String toString()
54      {
55          return response.createRenderURL().toString();        
56      }
57      
58      private Forwarder()
59      {        
60      }
61      
62      /***
63       * Get a link from a view name plus optional comma separated mode, window state 
64       * Supports syntax from forwards
65       * Examples of viewName parameter:
66       *   "myview" 
67       *   "myview"
68       *   "myview,state:maximized"
69       *   "myview,state:normal"
70       *   "myview,mode:view,state:maximized"
71       *   "myview,mode:edit,state:normal"
72       * 
73       * @param actionForward
74       * @return
75       */    
76      public PortletURL getView(String viewName)
77      {
78          PortletURL url = response.createRenderURL();        
79          buildLink(viewName, url);
80          return url;
81      }
82      
83      /***
84       * Get a link from a action forward logical name
85       * in the form of view:action where action can be
86       * "success" or "failure"
87       *  
88       * @param actionForward
89       * @return
90       */
91      public PortletURL getLink(String actionForward)
92      {
93          String forwardName = model.getForward(actionForward);
94          PortletURL url = response.createRenderURL();
95          buildLink(forwardName, url);
96          return url;
97      }
98  
99      /***
100      * Get a link from a action forward logical name
101      * for the given action
102      * 
103      * @param actionForward
104      * @return
105      */    
106     public PortletURL getLink(String forward, String action)
107     {
108         String actionForward = model.getForward(forward, action);
109         PortletURL url = response.createRenderURL();
110         buildLink(actionForward, url);
111         return url;
112     }
113     
114     // TODO: signatures of getLink with 'dynamic' parameters i.e. pass in a map of runtime binding parameters
115     
116     private void buildLink(String actionForward, PortletURL url)
117     {
118         if (actionForward == null)
119         {
120             return; // no parameters
121         }
122         
123         PortletMode mode = null;
124         StringTokenizer tokenizer = new StringTokenizer(actionForward, ForwardConstants.DELIMITER);
125         while (tokenizer.hasMoreTokens())
126         {
127             String token = tokenizer.nextToken();
128             if (token.startsWith(ForwardConstants.MODE_PREFIX))
129             {
130                 mode = setPortletMode(token.substring(ForwardConstants.MODE_PREFIX.length()), url);
131             }
132             else if (token.startsWith(ForwardConstants.STATE_PREFIX))
133             {
134                 setWindowState(token.substring(ForwardConstants.STATE_PREFIX.length()), url);                
135             }
136             else
137             {
138                 if (mode == null)
139                 {
140                     mode = request.getPortletMode();
141                 }
142                 if (mode.equals(PortletMode.VIEW))
143                 {
144                     url.setParameter(FrameworkConstants.VIEW_VIEW_MODE, token);
145                 }
146                 else if (mode.equals(PortletMode.EDIT))
147                 {
148                     url.setParameter(FrameworkConstants.VIEW_EDIT_MODE, token);                    
149                 }
150                 else if (mode.equals(PortletMode.HELP))
151                 {
152                     url.setParameter(FrameworkConstants.VIEW_HELP_MODE, token);                    
153                 }
154             }
155         }                                        
156     }
157     
158     private void setWindowState(String forward, PortletURL url)
159     {
160         try
161         {
162             if (forward.equals(ForwardConstants.MAXIMIZED))
163             {
164                 url.setWindowState(WindowState.MAXIMIZED);
165             }
166             else if (forward.equals(ForwardConstants.MINIMIZED))
167             {
168                 url.setWindowState(WindowState.MINIMIZED);
169             }
170             else if (forward.equals(ForwardConstants.NORMAL))
171             {
172                 url.setWindowState(WindowState.NORMAL);
173             }
174         }
175         catch (WindowStateException e)
176         {
177         }
178     }
179     
180     private PortletMode setPortletMode(String forward, PortletURL url)
181     {
182         PortletMode mode = null;
183         try
184         {
185             if (forward.equals(ForwardConstants.VIEW))
186             {
187                 url.setPortletMode(PortletMode.VIEW);
188                 mode = PortletMode.VIEW;
189             }
190             else if (forward.equals(ForwardConstants.EDIT))
191             {
192                 url.setPortletMode(PortletMode.EDIT);
193                 mode = PortletMode.EDIT;                
194             }
195             else if (forward.equals(ForwardConstants.HELP))
196             {
197                 url.setPortletMode(PortletMode.HELP);
198                 mode = PortletMode.HELP;                
199             }            
200         }
201         catch (PortletModeException e)
202         {
203         }
204         return mode;
205     }
206     
207 }