KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > config > ConfigHelper


1 /*
2  * $Id: ConfigHelper.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 The Apache Software Foundation.
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.apache.struts.config;
20
21 import javax.servlet.ServletContext JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpServletResponse JavaDoc;
24 import javax.servlet.http.HttpSession JavaDoc;
25 import javax.sql.DataSource JavaDoc;
26
27 import org.apache.struts.Globals;
28 import org.apache.struts.action.ActionForm;
29 import org.apache.struts.action.ActionFormBean;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessages;
33 import org.apache.struts.upload.MultipartRequestWrapper;
34 import org.apache.struts.util.MessageResources;
35 import org.apache.struts.util.RequestUtils;
36
37 /**
38  * NOTE: THIS CLASS IS UNDER ACTIVE DEVELOPMENT.
39  * THE CURRENT CODE IS WRITTEN FOR CLARITY NOT EFFICIENCY.
40  * NOT EVERY API FUNCTION HAS BEEN IMPLEMENTED YET.
41  *
42  * A helper object to expose the Struts shared resources,
43  * which are be stored in the application, session, or
44  * request contexts, as appropriate.
45  *
46  * An instance should be created for each request
47  * processed. The methods which return resources from
48  * the request or session contexts are not thread-safe.
49  *
50  * Provided for use by other servlets in the application
51  * so they can easily access the Struts shared resources.
52  *
53  * The resources are stored under attributes in the
54  * application, session, or request contexts.
55  *
56  * The ActionConfig methods simply return the resources
57  * from under the context and key used by the Struts
58  * ActionServlet when the resources are created.
59  *
60  * @since Struts 1.1
61  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
62  */

63 public class ConfigHelper implements ConfigHelperInterface {
64
65     // -------------------------------------------------------- Properites
66

67     /**
68      * The application associated with this instance.
69      */

70     private ServletContext JavaDoc application = null;
71
72     /**
73      * Set the application associated with this instance.
74      * [servlet.getServletContext()]
75      */

76     public void setApplication(ServletContext JavaDoc application) {
77         this.application = application;
78     }
79
80     /**
81      * The session associated with this instance.
82      */

83     private HttpSession JavaDoc session = null;
84
85     /**
86      * Set the session associated with this instance.
87      */

88     public void setSession(HttpSession JavaDoc session) {
89         this.session = session;
90     }
91
92     /**
93      * The request associated with this instance.
94      */

95     private HttpServletRequest JavaDoc request = null;
96
97     /**
98      * Set the request associated with this object.
99      * Session object is also set or cleared.
100      */

101     public void setRequest(HttpServletRequest JavaDoc request) {
102         this.request = request;
103         if (this.request == null)
104             setSession(null);
105         else
106             setSession(this.request.getSession());
107     }
108
109     /**
110      * The response associated with this instance.
111      */

112     private HttpServletResponse JavaDoc response = null;
113
114     /**
115      * Set the response associated with this isntance.
116      * Session object is also set or cleared.
117      */

118     public void setResponse(HttpServletResponse JavaDoc response) {
119         this.response = response;
120     }
121
122     /**
123      * The forward associated with this instance.
124      */

125     private ActionForward forward = null;
126
127     /**
128      * Set the forward associated with this instance.
129      */

130     public void setForward(ActionForward forward) {
131         this.forward = forward;
132     }
133
134     /**
135      * Set the application and request for this object instance.
136      * The ServletContext can be set by any servlet in the application.
137      * The request should be the instant request.
138      * Most of the other methods retrieve their own objects
139      * by reference to the application, request, or session
140      * attributes.
141      * Do not call other methods without setting these first!
142      * This is also called by the convenience constructor.
143      *
144      * @param application - The associated ServletContext.
145      * @param request - The associated HTTP request.
146      * @param response - The associated HTTP response.
147      */

148     public void setResources(
149         ServletContext JavaDoc application,
150         HttpServletRequest JavaDoc request,
151         HttpServletResponse JavaDoc response) {
152         
153         setApplication(application);
154         setRequest(request);
155         setResponse(response);
156     }
157     
158     public ConfigHelper() {
159         super();
160     }
161
162     public ConfigHelper(
163         ServletContext JavaDoc application,
164         HttpServletRequest JavaDoc request,
165         HttpServletResponse JavaDoc response) {
166             
167         super();
168         this.setResources(application, request, response);
169     }
170     
171
172     // ------------------------------------------------ Application Context
173

174     /**
175      * The <strong>default</strong>
176      * configured data source (which must implement
177      * <code>javax.sql.DataSource</code>),
178      * if one is configured for this application.
179      */

180     public DataSource JavaDoc getDataSource() {
181
182         if (this.application == null)
183             return null;
184         return (DataSource JavaDoc) this.application.getAttribute(Globals.DATA_SOURCE_KEY);
185
186     }
187
188     public ActionMessages getActionMessages() {
189
190         if (this.application == null)
191             return null;
192         return (ActionMessages) this.application.getAttribute(Globals.MESSAGE_KEY);
193
194     }
195
196     /**
197      * The application resources for this application.
198      */

199     public MessageResources getMessageResources() {
200
201         if (this.application == null) {
202             return null;
203         }
204         return (MessageResources) this.application.getAttribute(Globals.MESSAGES_KEY);
205
206     }
207
208     /**
209      * The path-mapped pattern (<code>/action/*</code>) or
210      * extension mapped pattern ((<code>*.do</code>)
211      * used to determine our Action URIs in this application.
212      */

213     public String JavaDoc getServletMapping() {
214
215         if (this.application == null) {
216             return null;
217         }
218         return (String JavaDoc) this.application.getAttribute(Globals.SERVLET_KEY);
219
220     }
221
222     // ---------------------------------------------------- Session Context
223

224     /**
225      * The transaction token stored in this session, if it is used.
226      */

227     public String JavaDoc getToken() {
228
229         if (this.session == null) {
230             return null;
231         }
232         return (String JavaDoc) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY);
233
234     }
235
236     // ---------------------------------------------------- Request Context
237

238     /**
239      * The runtime JspException that may be been thrown by a Struts tag
240      * extension, or compatible presentation extension, and placed
241      * in the request.
242      */

243     public Throwable JavaDoc getException() {
244
245         if (this.request == null) {
246             return null;
247         }
248         return (Throwable JavaDoc) this.request.getAttribute(Globals.EXCEPTION_KEY);
249
250     }
251
252     /**
253      * The multipart object for this request.
254      */

255     public MultipartRequestWrapper getMultipartRequestWrapper() {
256
257         if (this.request == null) {
258             return null;
259         }
260         return (MultipartRequestWrapper) this.request.getAttribute(Globals.MULTIPART_KEY);
261     }
262
263     /**
264       * The <code>org.apache.struts.ActionMapping</code>
265       * instance for this request.
266       */

267     public ActionMapping getMapping() {
268
269         if (this.request == null) {
270             return null;
271         }
272         return (ActionMapping) this.request.getAttribute(Globals.MAPPING_KEY);
273
274     }
275
276     // ---------------------------------------------------- Utility Methods
277

278     /**
279      * Return true if a message string for the specified message key
280      * is present for the user's Locale.
281      *
282      * @param key Message key
283      */

284     public boolean isMessage(String JavaDoc key) {
285
286         // Look up the requested MessageResources
287
MessageResources resources = getMessageResources();
288
289         if (resources == null) {
290             return false;
291         }
292
293         // Return the requested message presence indicator
294
return resources.isPresent(RequestUtils.getUserLocale(request, null), key);
295
296     }
297
298     /*
299      * Retrieve and return the <code>ActionForm</code> bean associated with
300      * this mapping, creating and stashing one if necessary. If there is no
301      * form bean associated with this mapping, return <code>null</code>.
302      *
303      */

304     public ActionForm getActionForm() {
305
306         // Is there a mapping associated with this request?
307
ActionMapping mapping = getMapping();
308         if (mapping == null)
309             return (null);
310
311         // Is there a form bean associated with this mapping?
312
String JavaDoc attribute = mapping.getAttribute();
313         if (attribute == null)
314             return (null);
315
316         // Look up the existing form bean, if any
317
ActionForm instance = null;
318         if ("request".equals(mapping.getScope())) {
319             instance = (ActionForm) this.request.getAttribute(attribute);
320         } else {
321             instance = (ActionForm) this.session.getAttribute(attribute);
322         }
323
324         return instance;
325     }
326
327     /**
328      * Return the form bean definition associated with the specified
329      * logical name, if any; otherwise return <code>null</code>.
330      *
331      * @param name Logical name of the requested form bean definition
332      */

333     public ActionFormBean getFormBean(String JavaDoc name) {
334         return null;
335     }
336
337     /**
338      * Return the forwarding associated with the specified logical name,
339      * if any; otherwise return <code>null</code>.
340      *
341      * @param name Logical name of the requested forwarding
342      */

343     public ActionForward getActionForward(String JavaDoc name) {
344         return null;
345     }
346
347     /**
348      * Return the mapping associated with the specified request path, if any;
349      * otherwise return <code>null</code>.
350      *
351      * @param path Request path for which a mapping is requested
352      */

353     public ActionMapping getActionMapping(String JavaDoc path) {
354         return null;
355     }
356
357     /**
358      * Return the form action converted into an action mapping path. The
359      * value of the <code>action</code> property is manipulated as follows in
360      * computing the name of the requested mapping:
361      * <ul>
362      * <li>Any filename extension is removed (on the theory that extension
363      * mapping is being used to select the controller servlet).</li>
364      * <li>If the resulting value does not start with a slash, then a
365      * slash is prepended.</li>
366      * </ul>
367      */

368     public String JavaDoc getActionMappingName(String JavaDoc action) {
369
370         String JavaDoc value = action;
371         int question = action.indexOf("?");
372         if (question >= 0)
373             value = value.substring(0, question);
374         int slash = value.lastIndexOf("/");
375         int period = value.lastIndexOf(".");
376         if ((period >= 0) && (period > slash))
377             value = value.substring(0, period);
378         if (value.startsWith("/"))
379             return (value);
380         else
381             return ("/" + value);
382
383     }
384
385     /**
386      * Return the form action converted into a server-relative URL.
387      */

388     public String JavaDoc getActionMappingURL(String JavaDoc action) {
389
390         StringBuffer JavaDoc value = new StringBuffer JavaDoc(this.request.getContextPath());
391
392         // Use our servlet mapping, if one is specified
393
String JavaDoc servletMapping = getServletMapping();
394
395         if (servletMapping != null) {
396             String JavaDoc queryString = null;
397             int question = action.indexOf("?");
398             if (question >= 0)
399                 queryString = action.substring(question);
400             String JavaDoc actionMapping = getActionMappingName(action);
401             if (servletMapping.startsWith("*.")) {
402                 value.append(actionMapping);
403                 value.append(servletMapping.substring(1));
404             } else if (servletMapping.endsWith("/*")) {
405                 value.append(servletMapping.substring(0, servletMapping.length() - 2));
406                 value.append(actionMapping);
407             }
408             if (queryString != null)
409                 value.append(queryString);
410         }
411
412         // Otherwise, assume extension mapping is in use and extension is
413
// already included in the action property
414
else {
415             if (!action.startsWith("/"))
416                 value.append("/");
417             value.append(action);
418         }
419
420         // Return the completed value
421
return (value.toString());
422
423     }
424
425     /**
426      * Return the url encoded to maintain the user session, if any.
427      */

428     public String JavaDoc getEncodeURL(String JavaDoc url) {
429
430         if ((session != null) && (response != null)) {
431
432             boolean redirect = false;
433             if (forward != null)
434                 redirect = forward.getRedirect();
435
436             if (redirect)
437                 return response.encodeRedirectURL(url);
438             else
439                 return response.encodeURL(url);
440         } else
441             return (url);
442     }
443
444     // ------------------------------------------------ Presentation API
445

446     /**
447      * Renders the reference for a HTML <base> element
448      */

449     public String JavaDoc getOrigRef() {
450
451         // HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
452

453         if (request == null)
454             return null;
455         StringBuffer JavaDoc result = RequestUtils.requestToServerUriStringBuffer(request);
456         return result.toString();
457     }
458
459     /**
460      * Renders the reference for a HTML <base> element.
461      */

462     public String JavaDoc getBaseRef() {
463
464         if (request == null)
465             return null;
466
467         StringBuffer JavaDoc result = RequestUtils.requestToServerStringBuffer(request);
468         String JavaDoc path = null;
469         if (forward == null)
470             path = request.getRequestURI();
471         else
472             path = request.getContextPath() + forward.getPath();
473         result.append(path);
474
475         return result.toString();
476     }
477
478     /**
479      * Return the path for the specified forward,
480      * otherwise return <code>null</code>.
481      *
482      * @param name Name given to local or global forward.
483      */

484     public String JavaDoc getLink(String JavaDoc name) {
485
486         ActionForward forward = getActionForward(name);
487         if (forward == null)
488             return null;
489
490         StringBuffer JavaDoc path = new StringBuffer JavaDoc(this.request.getContextPath());
491         path.append(forward.getPath());
492
493         // :TODO: What about runtime parameters?
494

495         return getEncodeURL(path.toString());
496
497     }
498
499     /**
500      * Return the localized message for the specified key,
501      * otherwise return <code>null</code>.
502      *
503      * @param key Message key
504      */

505     public String JavaDoc getMessage(String JavaDoc key) {
506
507         MessageResources resources = getMessageResources();
508         if (resources == null)
509             return null;
510
511         return resources.getMessage(RequestUtils.getUserLocale(request, null), key);
512
513     }
514
515     /**
516      * Look up and return a message string, based on the specified parameters.
517      *
518      * @param key Message key to be looked up and returned
519      * @param args Replacement parameters for this message
520      */

521     public String JavaDoc getMessage(String JavaDoc key, Object JavaDoc args[]) {
522
523         MessageResources resources = getMessageResources();
524
525         if (resources == null)
526             return null;
527
528         // Return the requested message
529
if (args == null)
530             return resources.getMessage(
531                 RequestUtils.getUserLocale(request, null),
532                 key);
533         else
534             return resources.getMessage(
535                 RequestUtils.getUserLocale(request, null),
536                 key,
537                 args);
538
539     }
540
541     /**
542      * Return the URL for the specified ActionMapping,
543      * otherwise return <code>null</code>.
544      *
545      * @param path Name given to local or global forward.
546      */

547     public String JavaDoc getAction(String JavaDoc path) {
548         return getEncodeURL(getActionMappingURL(path));
549     }
550
551
552     // --------------------------------------------- Presentation Wrappers
553

554     /**
555      * Wrapper for getLink(String)
556      *
557      * @param name Name given to local or global forward.
558      */

559     public String JavaDoc link(String JavaDoc name) {
560         return getLink(name);
561     }
562
563     /**
564      * Wrapper for getMessage(String)
565      *
566      * @param key Message key
567      */

568     public String JavaDoc message(String JavaDoc key) {
569         return getMessage(key);
570     }
571
572     /**
573      * Wrapper for getMessage(String,Object[])
574      *
575      * @param key Message key to be looked up and returned
576      * @param args Replacement parameters for this message
577      */

578     public String JavaDoc message(String JavaDoc key, Object JavaDoc args[]) {
579         return getMessage(key, args);
580     }
581
582     /**
583      * Wrapper for getAction(String)
584      *
585      * @param path Name given to local or global forward.
586      */

587     public String JavaDoc action(String JavaDoc path) {
588         return getAction(path);
589     }
590
591
592
593
594
595
596 }
597
Popular Tags