View Javadoc

1   /**
2    * Copyright (C) 2005-2009 Alfresco Software Limited.
3    *
4    * This file is part of the Spring Surf Extension project.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.springframework.extensions.webscripts;
20  
21  import java.util.Collections;
22  import java.util.Map;
23  import java.util.TreeMap;
24  
25  
26  /**
27   * Uri index supporting simple URI templates where matching is performed
28   * on static prefix of the URI.
29   * 
30   * e.g. /a/{b} is matched on /a
31   * 
32   * Note: this index was used until Alfresco v3.0
33   * 
34   * @author davidc
35   */
36  public class PrefixTemplateUriIndex implements UriIndex
37  {
38      // map of web scripts by url
39      // NOTE: The map is sorted by url (descending order)
40      private Map<String, IndexEntry> index = new TreeMap<String, IndexEntry>(Collections.reverseOrder());
41      
42      /* (non-Javadoc)
43       * @see org.alfresco.web.scripts.UriIndex#clear()
44       */
45      public void clear()
46      {
47          index.clear();
48      }
49  
50      /* (non-Javadoc)
51       * @see org.alfresco.web.scripts.UriIndex#getSize()
52       */
53      public int getSize()
54      {
55          return index.size();
56      }
57  
58      /* (non-Javadoc)
59       * @see org.alfresco.web.scripts.UriIndex#registerUri(org.alfresco.web.scripts.WebScript, java.lang.String)
60       */
61      public void registerUri(WebScript script, String uri)
62      {
63          Description desc = script.getDescription();
64          
65          // establish static part of url template
66          boolean wildcard = false;
67          boolean extension = true;
68          int queryArgIdx = uri.indexOf('?');
69          if (queryArgIdx != -1)
70          {
71              uri = uri.substring(0, queryArgIdx);
72          }
73          int tokenIdx = uri.indexOf('{');
74          if (tokenIdx != -1)
75          {
76              uri = uri.substring(0, tokenIdx);
77              wildcard = true;
78          }
79          if (desc.getFormatStyle() != Description.FormatStyle.argument)
80          {
81              int extIdx = uri.lastIndexOf(".");
82              if (extIdx != -1)
83              {
84                  uri = uri.substring(0, extIdx);
85              }
86              extension = false;
87          }
88          
89          // index service by static part of url (ensuring no other service has already claimed the url)
90          String uriIdx = desc.getMethod() + ":" + uri;
91          if (index.containsKey(uriIdx))
92          {
93              IndexEntry urlIndex = index.get(uriIdx);
94              WebScript existingService = urlIndex.script;
95              if (!existingService.getDescription().getId().equals(desc.getId()))
96              {
97                  String msg = "Web Script document " + desc.getDescPath() + " is attempting to define the url '" + uriIdx + "' already defined by " + existingService.getDescription().getDescPath();
98                  throw new WebScriptException(msg);
99              }
100         }
101         else
102         {
103             IndexEntry urlIndex = new IndexEntry(uri, wildcard, extension, script);
104             index.put(uriIdx, urlIndex);
105         }
106     }
107 
108     /* (non-Javadoc)
109      * @see org.alfresco.web.scripts.UriIndex#findWebScript(java.lang.String, java.lang.String)
110      */
111     public Match findWebScript(String method, String uri)
112     {
113         String matchedPath = null;
114         Match scriptMatch = null;
115         String match = method.toUpperCase() + ":" + uri;
116         String matchNoExt = method.toUpperCase() + ":" + ((uri.indexOf('.') != -1) ? uri.substring(0, uri.indexOf('.')) : uri);
117         
118         // locate full match - on URI and METHOD
119         for (Map.Entry<String, IndexEntry> entry : index.entrySet())
120         {
121             IndexEntry urlIndex = entry.getValue();
122             String index = entry.getKey();
123             String test = urlIndex.includeExtension ? match : matchNoExt; 
124             if ((urlIndex.wildcardPath && test.startsWith(index)) || (!urlIndex.wildcardPath && test.equals(index)))
125             {
126                 scriptMatch = new Match(urlIndex.path, null, urlIndex.path, urlIndex.script); 
127                 break;
128             }
129             else if ((urlIndex.wildcardPath && uri.startsWith(urlIndex.path)) || (!urlIndex.wildcardPath && uri.equals(urlIndex.path)))
130             {
131                 matchedPath = urlIndex.path;
132             }
133         }
134         
135         // locate URI match
136         if (scriptMatch == null && matchedPath != null)
137         {
138             scriptMatch = new Match(matchedPath, null, matchedPath);
139         }
140         
141         return scriptMatch;
142     }
143     
144     /**
145      * Index Entry
146      */
147     private static class IndexEntry
148     {
149         private IndexEntry(String path, boolean wildcardPath, boolean includeExtension, WebScript script)
150         {
151             this.path = path;
152             this.wildcardPath = wildcardPath;
153             this.includeExtension = includeExtension;
154             this.script = script;
155         }
156         
157         private String path;
158         private boolean wildcardPath;
159         private boolean includeExtension;
160         private WebScript script;
161     }
162 
163 }