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  
24  import org.apache.commons.collections.map.UnmodifiableMap;
25  
26  
27  /**
28   * Represents a URI to Web Script match
29   * 
30   * This class is immutable.
31   * 
32   * @author davidc
33   */
34  public final class Match
35  {
36      final private String templatePath;
37      final private Map<String, String> templateVars;
38      final private String matchPath;
39      final private WebScript script;
40      final private Kind kind;
41  
42      /**
43       * Kind of Match
44       */
45      public enum Kind
46      {
47          /** URL request matches on URI only */
48          URI,
49          /** URL request matches on URI and Method */
50          FULL
51      };
52  
53      /**
54       * Construct
55       * 
56       * @param templateVars
57       * @param script
58       */
59      public Match(String templatePath, Map<String, String> templateVars, String matchPath, WebScript script)
60      {
61          this.kind = Kind.FULL;
62          this.templatePath = templatePath;
63          this.templateVars = Collections.unmodifiableMap(templateVars);
64          this.matchPath = matchPath;
65          this.script = script;
66      }
67      
68      /**
69       * Construct
70       * 
71       * @param templatePath
72       */
73      public Match(String templatePath, Map<String, String> templateVars, String matchPath)
74      {
75          this.kind = Kind.URI;
76          this.templatePath = templatePath;
77          this.templateVars = Collections.unmodifiableMap(templateVars);
78          this.matchPath = matchPath;
79          this.script = null;
80      }
81  
82      /**
83       * Gets the kind of Match
84       */
85      public Kind getKind()
86      {
87          return this.kind;
88      }
89      
90      /**
91       * Gets the template request URL that matched the Web Script URL Template
92       * 
93       * @return  matching url template
94       */
95      public String getTemplate()
96      {
97          return templatePath;
98      }
99  
100     /**
101      * Gets the template variable substitutions
102      * 
103      * @return  template variable values (value indexed by name)
104      */
105     public Map<String, String> getTemplateVars()
106     {
107         return templateVars;
108     }
109     
110     /**
111      * Gets the static (i.e. without tokens) part of the request URL that matched
112      * the Web Script URL Template
113      * 
114      * @return  matching static url path
115      */
116     public String getPath()
117     {
118         return matchPath;
119     }
120     
121     /**
122      * Gets the matching web script
123      * 
124      * @return  service (or null, if match kind is URI)
125      */
126     public WebScript getWebScript()
127     {
128         return script;
129     }
130 
131     @Override
132     public String toString()
133     {
134         return templatePath;
135     }
136 }