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
25 /**
26 * Script / Template Model representing Web Script URLs
27 *
28 * This class is immutable.
29 *
30 * @author davidc
31 */
32 public final class URLModel
33 {
34 private final static Map<String, String> emptyArgs = Collections.emptyMap();
35 private final WebScriptRequest req;
36
37 /**
38 * Construct
39 *
40 * @param req
41 * @param res
42 */
43 URLModel(WebScriptRequest req)
44 {
45 this.req = req;
46 }
47
48 /**
49 * Gets the Server Path
50 *
51 * e.g. http://host:port
52 *
53 * @return server path
54 */
55 public String getServer()
56 {
57 return req.getServerPath();
58 }
59
60 /**
61 * Gets the Context Path
62 *
63 * e.g. /alfresco
64 *
65 * @return context path
66 */
67 public String getContext()
68 {
69 return req.getContextPath();
70 }
71
72 /**
73 * Gets the Service Context Path
74 *
75 * e.g. /alfresco/service
76 *
77 * @return service context path
78 */
79 public String getServiceContext()
80 {
81 return req.getServiceContextPath();
82 }
83
84 /**
85 * Gets the Service Path
86 *
87 * e.g. /alfresco/service/search/keyword
88 *
89 * @return service path
90 */
91 public String getService()
92 {
93 return req.getServicePath();
94 }
95
96 /**
97 * Gets the full path
98 *
99 * e.g. /alfresco/service/search/keyword?q=term
100 *
101 * @return service path
102 */
103 public String getFull()
104 {
105 return req.getURL();
106 }
107
108 /**
109 * Gets the URL arguments (query string)
110 *
111 * @return args (query string)
112 */
113 public String getArgs()
114 {
115 String args = req.getQueryString();
116 return (args == null) ? "" : args;
117 }
118
119 /**
120 * Gets the matching service path
121 *
122 * e.g.
123 * a) service registered path = /search/engine
124 * b) request path = /search/engine/external
125 *
126 * => /search/engine
127 *
128 * @return matching path
129 */
130 public String getMatch()
131 {
132 return req.getServiceMatch().getPath();
133 }
134
135 /**
136 * Gets the Service Extension Path
137 *
138 * e.g.
139 * a) service registered path = /search/engine
140 * b) request path = /search/engine/external
141 *
142 * => /external
143 *
144 * @return extension path
145 */
146 public String getExtension()
147 {
148 return req.getExtensionPath();
149 }
150
151 /**
152 * Gets the template form of this path
153 *
154 * @return template form of path
155 */
156 public String getTemplate()
157 {
158 return req.getServiceMatch().getTemplate();
159 }
160
161 /**
162 * Gets the values of template variables
163 *
164 * @return map of value indexed by variable name (or the empty map)
165 */
166 public Map<String, String> getTemplateArgs()
167 {
168 Map<String, String> args = req.getServiceMatch().getTemplateVars();
169 return (args == null) ? emptyArgs : args;
170 }
171 }