View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.jdiagnose.library.web.breadcrumb;
17  
18  import java.util.ArrayList;
19  import java.util.HashSet;
20  import java.util.List;
21  import java.util.Set;
22  import java.util.StringTokenizer;
23  
24  import javax.servlet.http.HttpServletRequest;
25  
26  /***
27   * @author jmccrindle
28   */
29  public class UriCrumbStrategy implements CrumbStrategy {
30  
31      private Set prefixes = new HashSet();
32      private String suffix = ".htm";
33  
34      /* (non-Javadoc)
35       * @see org.jdiagnose.library.web.breadcrumb.CrumbStrategy#getUrls(javax.servlet.http.HttpServletRequest)
36       */
37      public List getUrls(HttpServletRequest request) {
38          String requestURI = request.getRequestURI().substring(request.getContextPath().length());
39          int indexOfSecondSlash = requestURI.indexOf('/', 1);
40          String prefix = "";
41          if(indexOfSecondSlash >= 0) {
42              prefix = requestURI.substring(0, indexOfSecondSlash);
43              if(!prefixes.contains(prefix)) {
44                  prefix = "";
45              }
46          }
47          return pathToUrls(prefix, requestURI, suffix);
48      }
49      
50      /***
51       * e.g. /remote/hosts/agents/remotediagnostics.htm becomes:
52       * 
53       *  /remote/hosts.htm
54       *  /remote/hosts/agents.htm?host=${host}
55       * 
56       * @param path
57       * @return
58       */
59      public List pathToUrls(String prefix, String path, String suffix) {
60          int lastIndexOfSlash = path.lastIndexOf("/");
61          List result = new ArrayList();
62          if(lastIndexOfSlash > 0) {
63              String dirs = path.substring(0, lastIndexOfSlash);
64              StringTokenizer st = new StringTokenizer(dirs.substring(prefix.length()), "/");
65              StringBuffer root = new StringBuffer(prefix);
66              while(st.hasMoreTokens()) {
67                  String token = st.nextToken();
68                  root.append("/" + token);
69                  result.add(root.toString() + suffix);
70              }
71          }
72          result.add(path);
73          return result;
74      }
75  
76      public void setPrefixes(Set prefixes) {
77          this.prefixes = prefixes;
78      }
79      public void setSuffix(String suffix) {
80          this.suffix = suffix;
81      }
82  }