001/* 002 * Copyright 2011 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.web.renderingcontext; 018 019import org.apache.avalon.framework.component.Component; 020import org.apache.avalon.framework.context.Context; 021import org.apache.avalon.framework.context.ContextException; 022import org.apache.avalon.framework.context.Contextualizable; 023import org.apache.cocoon.components.ContextHelper; 024import org.apache.cocoon.environment.Request; 025 026/** Component for getting and setting current rendering context */ 027public class RenderingContextHandler implements Component, Contextualizable 028{ 029 /** Avalon role */ 030 public static final String ROLE = RenderingContextHandler.class.getName(); 031 032 private static final String __CONTEXT_ATTRIBUTE = "rendering-context"; 033 034 private Context _context; 035 036 @Override 037 public void contextualize(Context context) throws ContextException 038 { 039 _context = context; 040 } 041 042 /** 043 * Set the current rendering context. 044 * @param renderingMode the current rendering context. 045 */ 046 public void setRenderingContext(RenderingContext renderingMode) 047 { 048 Request request = ContextHelper.getRequest(_context); 049 request.setAttribute(__CONTEXT_ATTRIBUTE, renderingMode); 050 } 051 052 /** 053 * Retrieves the current rendering context. 054 * @return the current rendering context or <code>null</code> for the default one. 055 */ 056 public RenderingContext getRenderingContext() 057 { 058 Request request = ContextHelper.getRequest(_context); 059 RenderingContext context = (RenderingContext) request.getAttribute(__CONTEXT_ATTRIBUTE); 060 return context != null ? context : RenderingContext.BACK; 061 } 062}