001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment;
018:
019: import java.util.Map;
020:
021: import org.apache.cocoon.util.Deprecation;
022:
023: /**
024: * A set of constants and methods to access the content of the object model.
025: * <p>
026: * The object model is a <code>Map</code> used to pass information about the
027: * calling environment to the sitemap and its components (matchers, actions,
028: * transformers, etc).
029: * <p>
030: * This class provides accessors only for the objects in the object model that are
031: * common to every environment and which can thus be used safely. Some environments
032: * provide additional objects, but they are not described here and accessing them
033: * should be done in due cause since this ties the application to that particular
034: * environment.
035: *
036: * @author <a href="mailto:sylvain@apache.org">Sylvain Wallez</a>
037: * @version CVS $Id: ObjectModelHelper.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039:
040: public final class ObjectModelHelper {
041:
042: /** Key for the environment {@link Request} in the object model. */
043: public final static String REQUEST_OBJECT = "request";
044:
045: /** Key for the environment {@link Response} in the object model. */
046: public final static String RESPONSE_OBJECT = "response";
047:
048: /** Key for the environment {@link Context} in the object model. */
049: public final static String CONTEXT_OBJECT = "context";
050:
051: /** Key for the expiration value (Long) in the object model. */
052: public final static String EXPIRES_OBJECT = "expires";
053:
054: /** Key for the throwable object, only available within a <map:handle-errors>. */
055: public final static String THROWABLE_OBJECT = "throwable";
056:
057: /**
058: * Key for a {@link Map} containing information from
059: * a parent request provided to a sub-request (internal processing)
060: */
061: public final static String PARENT_CONTEXT = "parent-context";
062:
063: private ObjectModelHelper() {
064: // Forbid instantiation
065: }
066:
067: public static final Request getRequest(Map objectModel) {
068: return (Request) objectModel.get(REQUEST_OBJECT);
069: }
070:
071: public static final Response getResponse(Map objectModel) {
072: return (Response) objectModel.get(RESPONSE_OBJECT);
073: }
074:
075: public static final Context getContext(Map objectModel) {
076: return (Context) objectModel.get(CONTEXT_OBJECT);
077: }
078:
079: public static final Long getExpires(Map objectModel) {
080: return (Long) objectModel.get(EXPIRES_OBJECT);
081: }
082:
083: public static final Throwable getThrowable(Map objectModel) {
084: return (Throwable) objectModel.get(THROWABLE_OBJECT);
085: }
086:
087: /**
088: * @deprecated Don't use this method which should never have been there
089: * @since 2.1.7
090: */
091: public static Cookie getCookie(Map objectModel, String cookieName,
092: int cookieIndex) {
093: Deprecation.logger
094: .error("ObjectModelHelper.getCookie() should not be used, and will be removed in the next release");
095: boolean retrieveByName = false;
096: boolean retrieveByIndex = false;
097: boolean matchFound = false;
098:
099: 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: }
|