001: /*
002: * $Id: $
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.PortletContext;
024: import javax.portlet.PortletRequest;
025: import javax.portlet.PortletResponse;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.struts2.StrutsStatics;
030: import org.apache.struts2.interceptor.PrincipalAware;
031: import org.apache.struts2.portlet.PortletActionConstants;
032:
033: import com.opensymphony.xwork2.ActionContext;
034: import com.opensymphony.xwork2.ActionInvocation;
035: import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
036:
037: public class PortletAwareInterceptor extends AbstractInterceptor
038: implements PortletActionConstants, StrutsStatics {
039:
040: private static final long serialVersionUID = 2476509721059587700L;
041:
042: private static final Log LOG = LogFactory
043: .getLog(PortletAwareInterceptor.class);
044:
045: /**
046: * Sets action properties based on the interfaces an action implements. Things like application properties,
047: * parameters, session attributes, etc are set based on the implementing interface.
048: *
049: * @param invocation an encapsulation of the action execution state.
050: * @throws Exception if an error occurs when setting action properties.
051: */
052: public String intercept(ActionInvocation invocation)
053: throws Exception {
054: final Object action = invocation.getAction();
055: final ActionContext context = invocation.getInvocationContext();
056:
057: if (action instanceof PortletRequestAware) {
058: PortletRequest request = (PortletRequest) context
059: .get(REQUEST);
060: ((PortletRequestAware) action).setPortletRequest(request);
061: }
062:
063: if (action instanceof PortletResponseAware) {
064: PortletResponse response = (PortletResponse) context
065: .get(RESPONSE);
066: ((PortletResponseAware) action)
067: .setPortletResponse(response);
068: }
069: if (action instanceof PrincipalAware) {
070: PortletRequest request = (PortletRequest) context
071: .get(REQUEST);
072: ((PrincipalAware) action)
073: .setPrincipalProxy(new PortletPrincipalProxy(
074: request));
075: }
076: if (action instanceof PortletContextAware) {
077: PortletContext portletContext = (PortletContext) context
078: .get(STRUTS_PORTLET_CONTEXT);
079: ((PortletContextAware) action)
080: .setPortletContext(portletContext);
081: }
082: if (action instanceof PortletPreferencesAware) {
083: PortletRequest request = (PortletRequest) context
084: .get(REQUEST);
085:
086: // Check if running in a servlet environment
087: if (request == null) {
088: LOG
089: .warn("This portlet preferences implementation should only be used during development");
090: ((PortletPreferencesAware) action)
091: .setPortletPreferences(new ServletPortletPreferences(
092: ActionContext.getContext().getSession()));
093: } else {
094: ((PortletPreferencesAware) action)
095: .setPortletPreferences(request.getPreferences());
096: }
097: }
098: return invocation.invoke();
099: }
100: }
|