KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > chain > web > servlet > ServletPathMapper


1 /*
2  * Copyright 1999-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.apache.commons.chain.web.servlet;
17
18
19 import javax.servlet.http.HttpServletRequest JavaDoc;
20 import org.apache.commons.chain.Catalog;
21 import org.apache.commons.chain.Command;
22 import org.apache.commons.chain.Context;
23
24
25 /**
26  * <p>{@link Command} that uses the "servlet path" component of the request URI
27  * to select a {@link Command} from the appropriate {@link Catalog}, and
28  * execute it. To use this command, you would typically map an instance
29  * of {@link ChainProcessor} to an extension pattern like "*.execute" and
30  * then arrange that this is the default command to be executed. In such
31  * an environment, a request for a context relative URI of "/foo.execute"
32  * would cause the "/foo.execute" command to be loaded and executed.</p>
33  *
34  * @author Craig R. McClanahan
35  */

36
37 public class ServletPathMapper implements Command {
38
39
40     // ------------------------------------------------------ Instance Variables
41

42
43     private String JavaDoc catalogKey = ChainProcessor.CATALOG_DEFAULT;
44
45
46     // -------------------------------------------------------------- Properties
47

48
49     /**
50      * <p>Return the context key under which our {@link Catalog} has been
51      * stored.</p>
52      */

53     public String JavaDoc getCatalogKey() {
54
55         return (this.catalogKey);
56
57     }
58
59
60     /**
61      * <p>Set the context key under which our {@link Catalog} has been
62      * stored.</p>
63      *
64      * @param catalogKey The new catalog key
65      */

66     public void setCatalogKey(String JavaDoc catalogKey) {
67
68         this.catalogKey = catalogKey;
69
70     }
71
72
73     // --------------------------------------------------------- Command Methods
74

75
76     /**
77      * <p>Look up the servlet path information for this request, and use it to
78      * select an appropriate {@link Command} to be executed.
79      *
80      * @param context Context for the current request
81      */

82     public boolean execute(Context context) throws Exception JavaDoc {
83
84         // Look up the servlet path for this request
85
ServletWebContext swcontext = (ServletWebContext) context;
86         HttpServletRequest JavaDoc request = swcontext.getRequest();
87         String JavaDoc servletPath = (String JavaDoc)
88             request.getAttribute("javax.servlet.include.servlet_path");
89         if (servletPath == null) {
90             servletPath = request.getServletPath();
91         }
92
93         // Map to the Command specified by the extra path info
94
Catalog catalog = (Catalog) context.get(getCatalogKey());
95         Command command = catalog.getCommand(servletPath);
96         return (command.execute(context));
97
98     }
99
100
101 }
102
Popular Tags