@@ -70,16 +70,22 @@ public class ServiceRequest {
7070 //**************************************************************************
7171 //** Constructor
7272 //**************************************************************************
73- public ServiceRequest (HttpServletRequest request ){
73+ /** Creates a new instance of this class using a JavaXT HttpServletRequest.
74+ */
75+ public ServiceRequest (javaxt .http .servlet .HttpServletRequest request ){
7476 this (null , request );
7577 }
7678
7779
7880 //**************************************************************************
7981 //** Constructor
8082 //**************************************************************************
81- public ServiceRequest (String service , HttpServletRequest request ){
82- this .service = service ;
83+ /** Creates a new instance of this class using a JavaXT HttpServletRequest.
84+ * @param service A path segment in the request URL used to designate the
85+ * start of the path variable. See setService() and setPath() for more info
86+ * on how the path variable is set.
87+ */
88+ public ServiceRequest (String service , javaxt .http .servlet .HttpServletRequest request ){
8389 this .request = request ;
8490 this .url = new javaxt .utils .URL (request .getURL ());
8591 this .parameters = url .getParameters ();
@@ -117,25 +123,77 @@ public ServiceRequest(String service, HttpServletRequest request){
117123
118124
119125
120- //Parse path, excluding servlet and service path
121- setPath (request .getPathInfo ());
122-
126+ //Update service (and path) after parsing params
127+ setService (service );
123128
124129
125130 //Get offset and limit
126131 updateOffsetLimit ();
127132 }
128133
129134
135+ //**************************************************************************
136+ //** Constructor
137+ //**************************************************************************
138+ /** Creates a new instance of this class using a "javax" HttpServletRequest
139+ * and HttpServlet.
140+ */
141+ public ServiceRequest (javax .servlet .http .HttpServletRequest request ,
142+ javax .servlet .http .HttpServlet servlet ){
143+ this (new javaxt .http .servlet .HttpServletRequest (request , servlet ));
144+ }
145+
146+
147+ //**************************************************************************
148+ //** Constructor
149+ //**************************************************************************
150+ /** Creates a new instance of this class using a "jakarta" HttpServletRequest
151+ * and HttpServlet.
152+ */
153+ public ServiceRequest (jakarta .servlet .http .HttpServletRequest request ,
154+ jakarta .servlet .http .HttpServlet servlet ){
155+ this (new javaxt .http .servlet .HttpServletRequest (request , servlet ));
156+ }
157+
158+
159+ //**************************************************************************
160+ //** setService
161+ //**************************************************************************
162+ /** Used to update the service and path variables associated with the
163+ * request.
164+ * @param service A segment in the request URL used to designate the start
165+ * of the path variable. The service segment should appear after the
166+ * servlet. For example, the service segment in
167+ * "http://localhost/servlet/service/a/b/c" is simply "service" and the
168+ * resultant path is "/a/b/c". If the servlet path is undefined/missing
169+ * (e.g. "http://localhost/service/a/b/c"), you can still specify "service"
170+ * as the service and the resultant path would be "/a/b/c". You can even
171+ * use multiple segments for the service parameter (e.g. "/path/to/service/"
172+ * for "http://localhost/servlet/path/to/service/a/b/c"). If the service
173+ * parameter is empty or null or simply doesn't exist in the URL, then the
174+ * path variable will be set to whatever is to the right of the servlet
175+ * path.
176+ */
177+ public void setService (String service ){
178+
179+
180+ if (service !=null ){
181+ service = service .trim ();
182+ if (service .isEmpty ()) service = null ;
183+ }
184+ this .service = service ;
185+
186+
187+ //Parse path, excluding servlet
188+ setPath (request .getPathInfo ());
189+ }
190+
191+
130192 //**************************************************************************
131193 //** getService
132194 //**************************************************************************
133- /** Returns the service name from the http request. Service requests follow
134- * the convention: "http://localhost/servlet/service/path". For example
135- * "http://localhost/myapp/admin/user". In this example, the servlet path
136- * is "myapp" and the service name is "admin". Note that the servlet path
137- * is optional and may include multiple "directories". This method makes it
138- * easier to find the service name from the url.
195+ /** Returns the service path in the request URL. Returns null if the service
196+ * is not set.
139197 */
140198 public String getService (){
141199 return service ;
@@ -145,25 +203,32 @@ public String getService(){
145203 //**************************************************************************
146204 //** getMethod
147205 //**************************************************************************
148- /** Returns a method name for the HTTP request. Examples:
206+ /** Returns a method name for the HTTP request using the first path segment
207+ * returned by getPath(0) and the HTTP request method (GET, POST, etc).
208+ * Examples:
149209 * <ul>
150210 * <li>GET "http://localhost/user" returns "getUser"</li>
151211 * <li>DELETE "http://localhost/user" returns "deleteUser"</li>
152212 * <li>POST or PUT "http://localhost/user" returns "saveUser"</li>
153213 * </ul>
154214 *
155- * If the request is read-only, "POST", "PUT", and "DELETE" requests will
156- * be re-mapped to "GET".
215+ * Returns an empty string if the path is empty.
216+ *
217+ * <p>
218+ * Note that the path starts after the servlet and service. Consider the
219+ * following URL: "http://localhost/myapp/admin/user". In this example, the
220+ * servlet is "myapp" and the service is "admin". The method name is
221+ * derived from the first path segment after the servlet and service (i.e.
222+ * "user"). An HTTP GET request to this URL would yield "getUser". See
223+ * setService() and setPath() for more information.
224+ * </p>
157225 *
158226 * <p>
159- * If the URL contains a "servlet" path or a "service" path, will return
160- * the first object in the path after the servlet and/or service. Consider
161- * this example: "http://localhost/myapp/admin/user" In this example, the
162- * servlet path is "myapp" and the service path is "admin" and and so the
163- * method name is derived from "user".
227+ * If the request is read-only, "POST", "PUT", and "DELETE" requests will
228+ * be re-mapped to "GET". See set setReadOnly().
164229 * </p>
165230 *
166- * Note that this method is used by the WebService class to map service
231+ * Note that the WebService class relies on this method to map service
167232 * requests to REST service endpoints and execute CRUD operations.
168233 */
169234 public String getMethod (){
@@ -174,9 +239,10 @@ public String getMethod(){
174239 //**************************************************************************
175240 //** setReadOnly
176241 //**************************************************************************
177- /** Used to disable insert, update, and delete operations. "POST", "PUT",
178- * and "DELETE" requests are remapped to "GET". See getMethod() for more
179- * information on how requests are mapped.
242+ /** Used to enable/disable insert, update, and delete operations by updating
243+ * the string returned by getMethod().
244+ * @param readOnly If true, "POST", "PUT", and "DELETE" requests are
245+ * remapped to "GET" for getMethod().
180246 */
181247 public void setReadOnly (boolean readOnly ){
182248 if (readOnly ==this .readOnly ) return ;
@@ -199,9 +265,11 @@ public boolean isReadOnly(){
199265 //**************************************************************************
200266 //** getPath
201267 //**************************************************************************
202- /** Returns a part of the url path at a given index AFTER the service
203- * name. For example, index 0 for "http://localhost/servlet/service/a/b/c"
204- * would yield "a".
268+ /** Returns a segment from the requested URL path at a given index. Note
269+ * that the path starts after the servlet and service. See setService() and
270+ * setPath() for more information on how the path variable is set.
271+ * @param i Index number, starting from 0. For example, index 0 for
272+ * "http://localhost/servlet/service/a/b/c" would yield "a".
205273 */
206274 public javaxt .utils .Value getPath (int i ){
207275 if (path ==null || i >=path .length ) return new javaxt .utils .Value (null );
@@ -212,8 +280,10 @@ public javaxt.utils.Value getPath(int i){
212280 //**************************************************************************
213281 //** getPath
214282 //**************************************************************************
215- /** Returns a part of the url path AFTER the service name. For example,
216- * "http://localhost/servlet/service/a/b/c" would yield "/a/b/c".
283+ /** Returns a part of the requested URL path after the servlet and service.
284+ * For example, "http://localhost/servlet/service/a/b/c" would yield
285+ * "/a/b/c". See setService() and setPath() for more information on how
286+ * the path variable is set.
217287 */
218288 public String getPath (){
219289 if (path ==null ) return null ;
@@ -229,16 +299,35 @@ public String getPath(){
229299 //**************************************************************************
230300 //** setPath
231301 //**************************************************************************
232- /** Used to update the path of the current URL. This method can be used to
233- * coerce a request to route to different web methods (see getMethod).
234- * @param path URL path, excluding servlet and service path. For example,
235- * if a URL follows the follows a pattern like
236- * "http://localhost/servlet/service/a/b/c" the path is "/a/b/c".
302+ /** Used to update the path variable associated with the request. The path
303+ * variable is essential for several methods in this class (e.g. getId and
304+ * getMethod) and is critical for routing requests to different web methods
305+ * in the WebService class. This method is called whenever the service is
306+ * updated (see setService).
307+ *
308+ * @param path URL path after the servlet path. For example, if a URL
309+ * follows a pattern like "http://localhost/servlet/service/a/b/c",
310+ * you should provide everything to the right of the servlet (i.e.
311+ * "/service/a/b/c"). If a service is defined, the resultant path would be
312+ * "/a/b/c". If a service is not defined, the resultant path would be
313+ * "/service/a/b/c".
237314 */
238315 public void setPath (String path ){
316+ this .path = null ;
317+
239318 if (path !=null ){
240- if ( path . startsWith ( "/" )) path = path . substring ( 1 );
319+
241320 boolean addPath = service ==null ;
321+
322+ //Special case for service
323+ if (service !=null && service .contains ("/" )){
324+ int idx = path .indexOf (service );
325+ if (idx >-1 ) path = path .substring (idx +service .length ());
326+ addPath = true ;
327+ }
328+
329+
330+ if (path .startsWith ("/" )) path = path .substring (1 );
242331 ArrayList <String > arr = new ArrayList <>();
243332 for (String str : path .split ("/" )){
244333 if (addPath ) arr .add (str );
@@ -247,7 +336,10 @@ public void setPath(String path){
247336 addPath = true ;
248337 }
249338 }
250- this .path = arr .toArray (new String [arr .size ()]);
339+
340+ if (!arr .isEmpty ()){
341+ this .path = arr .toArray (new String [arr .size ()]);
342+ }
251343 }
252344
253345
0 commit comments