mirror of
https://github.com/apache/struts.git
synced 2026-08-31 19:35:40 +00:00
Added interceptor for portlet apps that injects the portlet preferences.
A simple stub for servlet environment testing will be used if needed. WW-1676 git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@499708 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* $Id: ServletConfigInterceptor.java 471756 2006-11-06 15:01:43Z husted $
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.portlet.interceptor;
|
||||
|
||||
import javax.portlet.PortletPreferences;
|
||||
|
||||
|
||||
/**
|
||||
* All Actions that want to have access to the portlet preferences should
|
||||
* implement this interface. If running in a servlet environment, an
|
||||
* appropriate testing implementation will be provided.
|
||||
*/
|
||||
public interface PortletPreferencesAware {
|
||||
|
||||
/**
|
||||
* Sets the HTTP request object in implementing classes.
|
||||
*
|
||||
* @param request the HTTP request.
|
||||
*/
|
||||
public void setPortletPreferences(PortletPreferences prefs);
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* $Id: ServletConfigInterceptor.java 471756 2006-11-06 15:01:43Z husted $
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.portlet.interceptor;
|
||||
|
||||
import javax.portlet.PortletRequest;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.struts2.StrutsStatics;
|
||||
import org.apache.struts2.portlet.context.PortletActionContext;
|
||||
|
||||
import com.opensymphony.xwork2.ActionContext;
|
||||
import com.opensymphony.xwork2.ActionInvocation;
|
||||
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
|
||||
|
||||
|
||||
/**
|
||||
* <!-- START SNIPPET: description -->
|
||||
*
|
||||
* An interceptor which provides an implementation of PortletPreferences if the Action implements
|
||||
* PortletPreferencesAware.
|
||||
*
|
||||
* If running in a servlet environment, a testing implementation of PortletPreferences will be
|
||||
* created and provided, but it should not be used in a production environment.
|
||||
*
|
||||
* <!-- END SNIPPET: description -->
|
||||
*
|
||||
* <p/> <u>Interceptor parameters:</u>
|
||||
*
|
||||
* <!-- START SNIPPET: parameters -->
|
||||
*
|
||||
* <ul>
|
||||
*
|
||||
* <li>None</li>
|
||||
*
|
||||
* </ul>
|
||||
*
|
||||
* <!-- END SNIPPET: parameters -->
|
||||
*
|
||||
* <p/> <u>Extending the interceptor:</u>
|
||||
*
|
||||
* <p/>
|
||||
*
|
||||
* <!-- START SNIPPET: extending -->
|
||||
*
|
||||
* There are no known extension points for this interceptor.
|
||||
*
|
||||
* <!-- END SNIPPET: extending -->
|
||||
*
|
||||
* <p/> <u>Example code:</u>
|
||||
*
|
||||
* <pre>
|
||||
* <!-- START SNIPPET: example -->
|
||||
* <action name="someAction" class="com.examples.SomeAction">
|
||||
* <interceptor-ref name="portlet-preferences"/>
|
||||
* <interceptor-ref name="basicStack"/>
|
||||
* <result name="success">good_result.ftl</result>
|
||||
* </action>
|
||||
* <!-- END SNIPPET: example -->
|
||||
* </pre>
|
||||
*
|
||||
* @see PortletPreferencesAware
|
||||
*/
|
||||
public class PortletPreferencesInterceptor extends AbstractInterceptor implements StrutsStatics {
|
||||
|
||||
private static final Log LOG = LogFactory.getLog(PortletPreferencesInterceptor.class);
|
||||
|
||||
public String intercept(ActionInvocation invocation) throws Exception {
|
||||
final Object action = invocation.getAction();
|
||||
final ActionContext context = invocation.getInvocationContext();
|
||||
|
||||
if (action instanceof PortletPreferencesAware) {
|
||||
PortletRequest request = PortletActionContext.getRequest();
|
||||
PortletPreferencesAware awareAction = (PortletPreferencesAware) action;
|
||||
|
||||
// Check if running in a servlet environment
|
||||
if (request == null) {
|
||||
LOG.warn("This portlet preferences implementation should only be used during development");
|
||||
awareAction.setPortletPreferences(new ServletPortletPreferences(ActionContext.getContext().getSession()));
|
||||
} else {
|
||||
awareAction.setPortletPreferences(request.getPreferences());
|
||||
}
|
||||
}
|
||||
return invocation.invoke();
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* $Id: ServletConfigInterceptor.java 471756 2006-11-06 15:01:43Z husted $
|
||||
*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.apache.struts2.portlet.interceptor;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Vector;
|
||||
|
||||
import javax.portlet.PortletPreferences;
|
||||
import javax.portlet.ReadOnlyException;
|
||||
import javax.portlet.ValidatorException;
|
||||
|
||||
/**
|
||||
* Simple portlet preferences implementation that uses a map in the Session
|
||||
* as storage.
|
||||
*/
|
||||
public class ServletPortletPreferences implements PortletPreferences {
|
||||
|
||||
private Map session;
|
||||
private String PREFERENCES_KEY = "_portlet-preferences";
|
||||
|
||||
public ServletPortletPreferences(Map session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public Map getMap() {
|
||||
Map map = (Map) session.get(PREFERENCES_KEY);
|
||||
if (map == null) {
|
||||
map = new HashMap();
|
||||
session.put(PREFERENCES_KEY, map);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
public Enumeration getNames() {
|
||||
return new Vector(getMap().keySet()).elements();
|
||||
}
|
||||
|
||||
public String getValue(String key, String def) {
|
||||
String val = (String) getMap().get(key);
|
||||
if (val == null) {
|
||||
val = def;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public String[] getValues(String key, String[] def) {
|
||||
String[] val = (String[]) getMap().get(key);
|
||||
if (val == null) {
|
||||
val = def;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
public boolean isReadOnly(String arg0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
public void reset(String arg0) throws ReadOnlyException {
|
||||
session.put(PREFERENCES_KEY, new HashMap());
|
||||
}
|
||||
|
||||
public void setValue(String key, String value) throws ReadOnlyException {
|
||||
getMap().put(key, value);
|
||||
}
|
||||
|
||||
public void setValues(String key, String[] value) throws ReadOnlyException {
|
||||
getMap().put(key, value);
|
||||
}
|
||||
|
||||
public void store() throws IOException, ValidatorException {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,7 +12,18 @@
|
||||
<result-type name="freemarker" class="org.apache.struts2.views.freemarker.PortletFreemarkerResult"/>
|
||||
<result-type name="velocity" class="org.apache.struts2.portlet.result.PortletVelocityResult"/>
|
||||
</result-types>
|
||||
|
||||
|
||||
<interceptors>
|
||||
<interceptor name="portlet-preferences" class="org.apache.struts2.portlet.interceptor.PortletPreferencesInterceptor"/>
|
||||
<interceptor-stack name="portletDefaultStack">
|
||||
<interceptor-ref name="defaultStack"/>
|
||||
<interceptor-ref name="portlet-preferences" />
|
||||
</interceptor-stack>
|
||||
|
||||
</interceptors>
|
||||
|
||||
<default-interceptor-ref name="portletDefaultStack"/>
|
||||
|
||||
<action name="renderDirect" class="org.apache.struts2.portlet.dispatcher.DirectRenderFromEventAction">
|
||||
<result name="success">${location}</result>
|
||||
</action>
|
||||
|
||||
Reference in New Issue
Block a user