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.components;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.CascadingRuntimeException;
022: import org.apache.avalon.framework.context.Context;
023: import org.apache.avalon.framework.context.ContextException;
024: import org.apache.cocoon.environment.ObjectModelHelper;
025: import org.apache.cocoon.environment.Request;
026: import org.apache.cocoon.environment.Response;
027:
028: /**
029: * A set of constants and methods to access the content of the context
030: * object. Some of the constants are defined in {@link org.apache.cocoon.Constants}.
031: *
032: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
033: * @version CVS $Id: ContextHelper.java 433543 2006-08-22 06:22:54Z crossley $
034: */
035:
036: public final class ContextHelper {
037:
038: /** Application <code>Context</code> Key for the current object model */
039: public static final String CONTEXT_OBJECT_MODEL = "object-model";
040:
041: /** Application <code>Context</code> Key for the current request object */
042: public static final String CONTEXT_REQUEST_OBJECT = CONTEXT_OBJECT_MODEL
043: + '.' + ObjectModelHelper.REQUEST_OBJECT;
044:
045: /** Application <code>Context</code> Key for the current response object */
046: public static final String CONTEXT_RESPONSE_OBJECT = CONTEXT_OBJECT_MODEL
047: + '.' + ObjectModelHelper.RESPONSE_OBJECT;
048:
049: /** Application <code>Context</code> Key for the current sitemap service manager */
050: public static final String CONTEXT_SITEMAP_SERVICE_MANAGER = "sitemap-service-manager";
051:
052: private ContextHelper() {
053: // Forbid instantiation
054: }
055:
056: /**
057: * Return the current request
058: * @param context The component context
059: * @return The request object
060: */
061: public static final Request getRequest(Context context) {
062: // the request object is always present
063: try {
064: return (Request) context.get(CONTEXT_REQUEST_OBJECT);
065: } catch (ContextException ce) {
066: throw new CascadingRuntimeException(
067: "Unable to get the request object from the context.",
068: ce);
069: }
070: }
071:
072: /**
073: * Return the current response
074: * @param context The component context
075: * @return The response
076: */
077: public static final Response getResponse(Context context) {
078: // the response object is always present
079: try {
080: return (Response) context.get(CONTEXT_RESPONSE_OBJECT);
081: } catch (ContextException ce) {
082: throw new CascadingRuntimeException(
083: "Unable to get the response object from the context.",
084: ce);
085: }
086: }
087:
088: /**
089: * Return the current object model
090: * @param context The component context
091: * @return The object model
092: */
093: public static final Map getObjectModel(Context context) {
094: // the object model is always present
095: try {
096: return (Map) context.get(CONTEXT_OBJECT_MODEL);
097: } catch (ContextException ce) {
098: throw new CascadingRuntimeException(
099: "Unable to get the object model from the context.",
100: ce);
101: }
102: }
103: }
|