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.jetspeed.container.url.impl;
18  
19  import java.io.UnsupportedEncodingException;
20  import java.util.Map;
21  import java.util.StringTokenizer;
22  
23  import javax.portlet.PortletMode;
24  import javax.portlet.WindowState;
25  import javax.servlet.http.HttpServletRequest;
26  
27  import org.apache.jetspeed.PortalContext;
28  import org.apache.jetspeed.PortalReservedParameters;
29  import org.apache.jetspeed.container.state.NavigationalState;
30  import org.apache.jetspeed.container.url.BasePortalURL;
31  import org.apache.jetspeed.desktop.JetspeedDesktop;
32  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
33  import org.apache.pluto.om.window.PortletWindow;
34  import org.apache.pluto.om.entity.PortletEntity;
35  import org.apache.pluto.om.portlet.PortletDefinition;
36  
37  /***
38   * DesktopEncodingPortalURL encodes action URLs to target desktop specific /action pipeline,
39   * and render URLs to target desktop specific /render pipeline
40   * 
41   * The query parameters "entity" and "portlet" are added to each url. These parameters are needed in a /render
42   * request and are used by the desktop javascript code for both /render and /action requests.
43   * 
44   * @author <a href="mailto:ate@apache.org">Ate Douma</a>
45   * @version $Id: PathInfoEncodingPortalURL.java 367856 2006-01-11 01:04:09Z taylor $
46   */
47  public class DesktopEncodingPortalURL extends AbstractPortalURL
48  {
49      private String baseActionPath = null;
50      private String baseRenderPath = null;
51      
52      private String desktopActionPipelinePath = null;
53      private String desktopRenderPipelinePath = null;
54      
55      
56      public DesktopEncodingPortalURL(NavigationalState navState, PortalContext portalContext, String desktopRenderPipelinePath, String desktopActionPipelinePath)
57      {
58          super(navState, portalContext);
59          initializePipelinePaths( desktopRenderPipelinePath, desktopActionPipelinePath );
60      }
61      
62      public DesktopEncodingPortalURL(NavigationalState navState, PortalContext portalContext, String desktopRenderPipelinePath, String desktopActionPipelinePath, BasePortalURL base)
63      {
64          super(navState, portalContext, base);
65          initializePipelinePaths( desktopRenderPipelinePath, desktopActionPipelinePath );
66      }
67  
68      public DesktopEncodingPortalURL(String characterEncoding, NavigationalState navState, PortalContext portalContext)
69      {
70          super(characterEncoding, navState, portalContext);
71          initializePipelinePaths( null, null );
72      }
73  
74      public DesktopEncodingPortalURL(HttpServletRequest request, String characterEncoding, NavigationalState navState, PortalContext portalContext)
75      {
76          super(request, characterEncoding, navState, portalContext);
77          initializePipelinePaths( null, null );
78      }
79      
80      private void initializePipelinePaths( String desktopRenderPipelinePath, String desktopActionPipelinePath )
81      {
82          if ( desktopActionPipelinePath == null || desktopActionPipelinePath.length() == 0 )
83              desktopActionPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_ACTION_PIPELINE_PATH;
84          if ( desktopActionPipelinePath.charAt( 0 ) != '/' )
85              desktopActionPipelinePath = "/" + desktopActionPipelinePath;
86          if ( desktopActionPipelinePath.length() > 1 && desktopActionPipelinePath.charAt( desktopActionPipelinePath.length() -1 ) == '/' )
87              desktopActionPipelinePath = desktopActionPipelinePath.substring( 0, desktopActionPipelinePath.length() -1 );
88  
89          if ( desktopRenderPipelinePath == null || desktopRenderPipelinePath.length() == 0 )
90              desktopRenderPipelinePath = JetspeedDesktop.DEFAULT_DESKTOP_RENDER_PIPELINE_PATH;
91          if ( desktopRenderPipelinePath.charAt( 0 ) != '/' )
92              desktopRenderPipelinePath = "/" + desktopRenderPipelinePath;
93          if ( desktopRenderPipelinePath.length() > 1 && desktopRenderPipelinePath.charAt( desktopRenderPipelinePath.length() -1 ) == '/' )
94              desktopRenderPipelinePath = desktopRenderPipelinePath.substring( 0, desktopRenderPipelinePath.length() -1 );
95          
96          this.desktopRenderPipelinePath = desktopRenderPipelinePath;
97          this.desktopActionPipelinePath = desktopActionPipelinePath;        
98      }
99  
100     protected void decodeBasePath(HttpServletRequest request)
101     {
102         super.decodeBasePath(request);
103         if ( this.baseActionPath == null )
104         {
105             this.baseActionPath = contextPath + this.desktopActionPipelinePath;
106             this.baseRenderPath = contextPath + this.desktopRenderPipelinePath;
107         }
108     }
109     
110     protected void decodePathAndNavigationalState(HttpServletRequest request)
111     {
112         String path = null;
113         String encodedNavState = null;
114 
115         String pathInfo = request.getPathInfo();
116         if (pathInfo != null)
117         {
118             StringTokenizer tokenizer = new StringTokenizer(request.getPathInfo(),"/");
119             StringBuffer buffer = new StringBuffer();
120             String token;
121             boolean foundNavState = false;
122             String navStatePrefix = getNavigationalStateParameterName() +":";
123             while (tokenizer.hasMoreTokens())
124             {
125                 token = tokenizer.nextToken();
126                 if (!foundNavState && token.startsWith(navStatePrefix))
127                 {
128                     foundNavState = true;
129                     if ( token.length() > navStatePrefix.length() )
130                     {
131                         encodedNavState = token.substring(navStatePrefix.length());
132                     }
133                 }
134                 else
135                 {
136                     buffer.append("/");
137                     buffer.append(token);
138                 }
139             }
140             if ( buffer.length() > 0 )
141             {
142                 path = buffer.toString();
143             }
144             else
145             {
146                 path = "/";
147             }
148         }
149         setPath(path);
150         setEncodedNavigationalState(encodedNavState);
151     }
152 
153     protected String createPortletURL(String encodedNavState, boolean secure)
154     {
155         return createPortletURL(encodedNavState, secure, null, false);
156     }
157     
158     protected String createPortletURL(String encodedNavState, boolean secure, PortletWindow window, boolean action)
159     {   
160         return createPortletURL(encodedNavState, secure, window, action, false, false);
161     }
162     
163     protected String createPortletURL(String encodedNavState, boolean secure, PortletWindow window, boolean action, boolean resource, boolean desktopRequestNotAjax)
164     {   
165         StringBuffer buffer = new StringBuffer("");
166         buffer.append(getBaseURL(secure));
167         if (action)
168         {
169             buffer.append(this.baseActionPath);
170         }
171         else
172         {
173             buffer.append(this.baseRenderPath);        
174         }            
175         if ( encodedNavState != null )
176         {
177             buffer.append("/");
178             buffer.append(getNavigationalStateParameterName());
179             buffer.append(":");
180             buffer.append(encodedNavState);
181         }
182         if ( getPath() != null )
183         {
184             buffer.append(getPath());
185         }
186         
187         if ( !resource )
188         {
189         	if ( ! desktopRequestNotAjax )
190             {
191         		PortletEntity pe = window.getPortletEntity();
192         		buffer.append( "?entity=" ).append( pe.getId() );
193             
194         		PortletDefinition portlet = pe.getPortletDefinition();
195         		MutablePortletApplication app = (MutablePortletApplication)portlet.getPortletApplicationDefinition();
196         		String uniqueName = app.getName() + "::" + portlet.getName();
197         		buffer.append( "&portlet=" ).append( uniqueName );
198             }
199         }
200         else
201         {
202             buffer.append("?encoder=desktop");
203         }
204 
205         return buffer.toString();
206     }        
207     
208     public String createPortletURL(PortletWindow window, Map parameters, PortletMode mode, WindowState state, boolean action, boolean secure)
209     {
210         try
211         {
212             boolean resource = !action && parameters.containsKey(PortalReservedParameters.PORTLET_RESOURCE_URL_REQUEST_PARAMETER);
213             boolean desktopRequestNotAjax = false;
214             if ( parameters.containsKey(JetspeedDesktop.DESKTOP_REQUEST_NOT_AJAX_PARAMETER) )
215             {
216             	desktopRequestNotAjax = true;
217             	parameters.remove(JetspeedDesktop.DESKTOP_REQUEST_NOT_AJAX_PARAMETER);
218             }
219             return createPortletURL(this.getNavigationalState().encode(window,parameters,mode,state,action), secure, window, action, resource, desktopRequestNotAjax);
220         }
221         catch (UnsupportedEncodingException e)
222         {
223             // should never happen
224             e.printStackTrace();
225             // to keep the compiler happy
226             return null;
227         }
228     }    
229 }