001: /*
002: * $Id: PortletPreferencesInterceptor.java 502294 2007-02-01 17:28:00Z niallp $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.portlet.interceptor;
022:
023: import javax.portlet.PortletRequest;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.struts2.StrutsStatics;
028: import org.apache.struts2.portlet.context.PortletActionContext;
029:
030: import com.opensymphony.xwork2.ActionContext;
031: import com.opensymphony.xwork2.ActionInvocation;
032: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
033:
034: /**
035: * <!-- START SNIPPET: description -->
036: *
037: * An interceptor which provides an implementation of PortletPreferences if the Action implements
038: * PortletPreferencesAware.
039: *
040: * If running in a servlet environment, a testing implementation of PortletPreferences will be
041: * created and provided, but it should not be used in a production environment.
042: *
043: * <!-- END SNIPPET: description -->
044: *
045: * <p/> <u>Interceptor parameters:</u>
046: *
047: * <!-- START SNIPPET: parameters -->
048: *
049: * <ul>
050: *
051: * <li>None</li>
052: *
053: * </ul>
054: *
055: * <!-- END SNIPPET: parameters -->
056: *
057: * <p/> <u>Extending the interceptor:</u>
058: *
059: * <p/>
060: *
061: * <!-- START SNIPPET: extending -->
062: *
063: * There are no known extension points for this interceptor.
064: *
065: * <!-- END SNIPPET: extending -->
066: *
067: * <p/> <u>Example code:</u>
068: *
069: * <pre>
070: * <!-- START SNIPPET: example -->
071: * <action name="someAction" class="com.examples.SomeAction">
072: * <interceptor-ref name="portlet-preferences"/>
073: * <interceptor-ref name="basicStack"/>
074: * <result name="success">good_result.ftl</result>
075: * </action>
076: * <!-- END SNIPPET: example -->
077: * </pre>
078: *
079: * @see PortletPreferencesAware
080: */
081: public class PortletPreferencesInterceptor extends AbstractInterceptor
082: implements StrutsStatics {
083:
084: private static final Log LOG = LogFactory
085: .getLog(PortletPreferencesInterceptor.class);
086:
087: public String intercept(ActionInvocation invocation)
088: throws Exception {
089: final Object action = invocation.getAction();
090: final ActionContext context = invocation.getInvocationContext();
091:
092: if (action instanceof PortletPreferencesAware) {
093: PortletRequest request = PortletActionContext.getRequest();
094: PortletPreferencesAware awareAction = (PortletPreferencesAware) action;
095:
096: // Check if running in a servlet environment
097: if (request == null) {
098: LOG
099: .warn("This portlet preferences implementation should only be used during development");
100: awareAction
101: .setPortletPreferences(new ServletPortletPreferences(
102: ActionContext.getContext().getSession()));
103: } else {
104: awareAction.setPortletPreferences(request
105: .getPreferences());
106: }
107: }
108: return invocation.invoke();
109: }
110: }
|