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