KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > environment > ObjectModelHelper


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.cocoon.environment;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.cocoon.util.Deprecation;
21
22 /**
23  * A set of constants and methods to access the content of the object model.
24  * <p>
25  * The object model is a <code>Map</code> used to pass information about the
26  * calling environment to the sitemap and its components (matchers, actions,
27  * transformers, etc).
28  * <p>
29  * This class provides accessors only for the objects in the object model that are
30  * common to every environment and which can thus be used safely. Some environments
31  * provide additional objects, but they are not described here and accessing them
32  * should be done in due cause since this ties the application to that particular
33  * environment.
34  *
35  * @author <a HREF="mailto:sylvain@apache.org">Sylvain Wallez</a>
36  * @version CVS $Id: ObjectModelHelper.java 312661 2005-10-10 14:58:44Z sylvain $
37  */

38
39 public final class ObjectModelHelper {
40
41     /** Key for the environment {@link Request} in the object model. */
42     public final static String JavaDoc REQUEST_OBJECT = "request";
43
44     /** Key for the environment {@link Response} in the object model. */
45     public final static String JavaDoc RESPONSE_OBJECT = "response";
46
47     /** Key for the environment {@link Context} in the object model. */
48     public final static String JavaDoc CONTEXT_OBJECT = "context";
49
50     /** Key for the expiration value (Long) in the object model. */
51     public final static String JavaDoc EXPIRES_OBJECT = "expires";
52     
53     /** Key for the throwable object, only available within a &lt;map:handle-errors>. */
54     public final static String JavaDoc THROWABLE_OBJECT = "throwable";
55
56     /**
57      * Key for a {@link Map} containing information from
58      * a parent request provided to a sub-request (internal processing)
59      */

60     public final static String JavaDoc PARENT_CONTEXT = "parent-context";
61
62
63     private ObjectModelHelper() {
64         // Forbid instantiation
65
}
66
67     public static final Request getRequest(Map JavaDoc objectModel) {
68         return (Request)objectModel.get(REQUEST_OBJECT);
69     }
70
71     public static final Response getResponse(Map JavaDoc objectModel) {
72         return (Response)objectModel.get(RESPONSE_OBJECT);
73     }
74
75     public static final Context getContext(Map JavaDoc objectModel) {
76         return (Context)objectModel.get(CONTEXT_OBJECT);
77     }
78
79     public static final Long JavaDoc getExpires(Map JavaDoc objectModel) {
80         return (Long JavaDoc)objectModel.get(EXPIRES_OBJECT);
81     }
82     
83     public static final Throwable JavaDoc getThrowable(Map JavaDoc objectModel) {
84         return (Throwable JavaDoc)objectModel.get(THROWABLE_OBJECT);
85     }
86     
87     /**
88      * @deprecated Don't use this method which should never have been there
89      * @since 2.1.7
90      */

91     public static Cookie getCookie(Map JavaDoc objectModel,
92                                    String JavaDoc cookieName,
93                                    int cookieIndex) {
94         Deprecation.logger.error("ObjectModelHelper.getCookie() should not be used, and will be removed in the next release");
95         boolean retrieveByName = false;
96         boolean retrieveByIndex = false;
97         boolean matchFound = false;
98
99         int count = 0;
100
101         Request request = ObjectModelHelper.getRequest(objectModel);
102         Cookie currentCookie = null;
103
104         if (cookieName != null) {
105             retrieveByName = true;
106         } else if (cookieIndex >=0) {
107             retrieveByIndex = true;
108         }
109
110         Cookie[] cookies = request.getCookies();
111         if (cookies != null && retrieveByName) {
112             for(count = 0; count < cookies.length; count++) {
113                 currentCookie = cookies[count];
114                 if (currentCookie.getName().equals(cookieName)) {
115                     matchFound = true;
116                     break;
117                 }
118             }
119         } else if(cookies != null && retrieveByIndex) {
120             if(cookies.length > cookieIndex) {
121                 currentCookie = cookies[cookieIndex];
122                 matchFound = true;
123             }
124         }
125
126         if (matchFound) {
127             return currentCookie;
128         }
129         return null;
130     }
131
132 }
133
Popular Tags