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.util.StringTokenizer;
20  
21  import javax.servlet.http.HttpServletRequest;
22  
23  import org.apache.jetspeed.PortalContext;
24  import org.apache.jetspeed.container.state.NavigationalState;
25  import org.apache.jetspeed.container.url.BasePortalURL;
26  
27  /***
28   * PathInfoEncodingPortalURL encodes the NavigationalState as PathInfo element
29   * *
30   * @author <a href="mailto:ate@apache.org">Ate Douma</a>
31   * @version $Id: PathInfoEncodingPortalURL.java 605989 2007-12-20 18:26:54Z ate $
32   */
33  public class PathInfoEncodingPortalURL extends AbstractPortalURL
34  {
35      public PathInfoEncodingPortalURL(NavigationalState navState, PortalContext portalContext, BasePortalURL base)
36      {
37          super(navState, portalContext, base);
38      }
39  
40      public PathInfoEncodingPortalURL(NavigationalState navState, PortalContext portalContext)
41      {
42          super(navState, portalContext);
43      }
44  
45      public PathInfoEncodingPortalURL(String characterEncoding, NavigationalState navState, PortalContext portalContext)
46      {
47          super(characterEncoding, navState, portalContext);
48      }
49  
50      public PathInfoEncodingPortalURL(HttpServletRequest request, String characterEncoding, NavigationalState navState, PortalContext portalContext)
51      {
52          super(request, characterEncoding, navState, portalContext);
53      }
54  
55      protected void decodePathAndNavigationalState(HttpServletRequest request)
56      {
57          String path = null;
58          String encodedNavState = null;
59  
60          String pathInfo = request.getPathInfo();
61          if (pathInfo != null)
62          {
63              StringTokenizer tokenizer = new StringTokenizer(request.getPathInfo(),"/");
64              StringBuffer buffer = new StringBuffer();
65              String token;
66              boolean foundNavState = false;
67              String navStatePrefix = getNavigationalStateParameterName() +":";
68              while (tokenizer.hasMoreTokens())
69              {
70                  token = tokenizer.nextToken();
71                  if (!foundNavState && token.startsWith(navStatePrefix))
72                  {
73                      foundNavState = true;
74                      if ( token.length() > navStatePrefix.length() )
75                      {
76                          encodedNavState = token.substring(navStatePrefix.length());
77                      }
78                  }
79                  else
80                  {
81                      buffer.append("/");
82                      buffer.append(token);
83                  }
84              }
85              if ( buffer.length() > 0 )
86              {
87                  path = buffer.toString();
88              }
89              else
90              {
91                  path = "/";
92              }
93          }
94          setPath(path);
95          setEncodedNavigationalState(encodedNavState);
96      }
97  
98      protected String createPortletURL(String encodedNavState, boolean secure)
99      {
100         StringBuffer buffer = new StringBuffer(getBaseURL(secure));
101         buffer.append(getBasePath());
102         if ( encodedNavState != null )
103         {
104             buffer.append("/");
105             buffer.append(getNavigationalStateParameterName());
106             buffer.append(":");
107             buffer.append(encodedNavState);
108         }
109         if ( getPath() != null )
110         {
111             buffer.append(getPath());
112         }
113         return buffer.toString();
114     }
115 
116     public boolean isPathInfoEncodingNavState()
117     {
118         return true;
119     }        
120 }