在这里将介绍如何集成Struts的DispatchAction
多方法的功能,下面是代码
package
cn.pisoft.portlet.struts;

import
java.lang.reflect.InvocationTargetException;
import
java.lang.reflect.Method;
import
java.security.MessageDigest;
import
java.security.NoSuchAlgorithmException;
import
java.util.ArrayList;
import
java.util.HashMap;
import
java.util.List;
import
java.util.Map;

import
javax.portlet.ActionRequest;
import
javax.portlet.ActionResponse;
import
javax.portlet.PortletConfig;
import
javax.portlet.PortletSession;
import
javax.portlet.RenderRequest;
import
javax.portlet.RenderResponse;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;

import
org.apache.commons.fileupload.FileItem;
import
org.apache.commons.fileupload.FileItemFactory;
import
org.apache.commons.fileupload.FileUploadException;
import
org.apache.commons.fileupload.portlet.PortletFileUpload;
import
org.apache.commons.logging.Log;
import
org.apache.commons.logging.LogFactory;
import
org.apache.struts.Globals;
import
org.apache.struts.action.ActionForm;
import
org.apache.struts.action.ActionForward;
import
org.apache.struts.action.ActionMapping;
import
org.apache.struts.util.MessageResources;

import
cn.pisoft.portlet.km.ExtActionKey;
import
cn.pisoft.portlet.km.ServicePool;

import
com.liferay.portal.model.Layout;
import
com.liferay.portal.model.User;
import
com.liferay.portal.model.impl.ResourceImpl;
import
com.liferay.portal.service.ResourceLocalServiceUtil;
import
com.liferay.portal.service.UserLocalServiceUtil;
import
com.liferay.portal.struts.PortletAction;
import
com.liferay.portal.util.PortalUtil;
import
com.liferay.portal.util.WebKeys;
import
com.liferay.portlet.ActionRequestImpl;


public
class
DispatchPortletAction
extends
PortletAction
...
{
protectedActionForwarddispatchMethod(ActionMappingmapping,
ActionFormform,HttpServletRequestrequest,

HttpServletResponseresponse,Stringname)throwsException...{

PortletConfigportletConfig=(PortletConfig)request
.getAttribute(WebKeys.JAVAX_PORTLET_CONFIG);

RenderRequestrenderRequest=(RenderRequest)request
.getAttribute(WebKeys.JAVAX_PORTLET_REQUEST);

RenderResponserenderResponse=(RenderResponse)request
.getAttribute(WebKeys.JAVAX_PORTLET_RESPONSE);


if(name==null)...{
returnthis.view(mapping,form,portletConfig,renderRequest,
renderResponse);
}

Methodmethod=null;


try...{
method=getMethod(name);


}catch(NoSuchMethodExceptione)...{
Stringmessage=messages.getMessage("dispatch.method",mapping
.getPath(),name);
log.error(message,e);

StringuserMsg=messages.getMessage("dispatch.method.user",
mapping.getPath());
thrownewNoSuchMethodException(userMsg);
}

ActionForwardforward=null;

try...{

Objectargs[]=...{mapping,form,portletConfig,renderRequest,
renderResponse};
forward=(ActionForward)method.invoke(this,args);


}catch(ClassCastExceptione)...{
Stringmessage=messages.getMessage("dispatch.return",mapping
.getPath(),name);
log.error(message,e);
throwe;


}catch(IllegalAccessExceptione)...{
Stringmessage=messages.getMessage("dispatch.error",mapping
.getPath(),name);
log.error(message,e);
throwe;


}catch(InvocationTargetExceptione)...{
Throwablet=e.getTargetException();

if(tinstanceofException)...{
throw((Exception)t);

}else...{
Stringmessage=messages.getMessage("dispatch.error",mapping
.getPath(),name);
log.error(message,e);
thrownewServletException(t);
}
}
return(forward);
}

protectedStringgetMethodName(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse,

Stringparameter)throwsException...{

StringmethodName=request.getParameter(parameter);

if(methodName==null||methodName.length()==0)...{
methodName=(String)request.getAttribute(parameter);
}
returnmethodName;
}

publicActionForwardview(ActionMappingmapping,ActionFormform,
PortletConfigconfig,RenderRequestreq,RenderResponseres)

throwsException...{
returnnull;
}


protectedMethodgetMethod(Stringname)throwsNoSuchMethodException...{

synchronized(methods)...{
Methodmethod=(Method)methods.get(name);


if(method==null)...{
method=clazz.getMethod(name,types);
methods.put(name,method);
}

return(method);
}
}


protectedvoidsaveToken(RenderRequestrequest)...{
Stringtoken=generateToken(request.getPortletSession().getId());

request.getPortletSession().setAttribute(Globals.TRANSACTION_TOKEN_KEY,
token);
}

publicsynchronizedbooleanisTokenValid(RenderRequestrequest,

booleanreset)...{
//Retrievethecurrentsessionforthisrequest
PortletSessionsession=request.getPortletSession(false);


if(session==null)...{
returnfalse;
}

//Retrievethetransactiontokenfromthissession,and
//resetitifrequested
Stringsaved=(String)session
.getAttribute(Globals.TRANSACTION_TOKEN_KEY);


if(saved==null)...{
returnfalse;
}


if(reset)...{
this.resetToken(request);
}

Stringtoken=request
.getParameter(org.apache.struts.taglib.html.Constants.TOKEN_KEY);


if(token==null)...{
returnfalse;
}

returnsaved.equals(token);
}


publicsynchronizedvoidresetToken(RenderRequestrequest)...{

request.getPortletSession().removeAttribute(
Globals.TRANSACTION_TOKEN_KEY);
}


publicsynchronizedStringgenerateToken(Stringid)...{

try...{
longcurrent=System.currentTimeMillis();


if(current==previous)...{
current++;
}

previous=current;

byte[]now=newLong(current).toString().getBytes();
MessageDigestmd=MessageDigest.getInstance("MD5");

md.update(id.getBytes());
md.update(now);

returntoHex(md.digest());

}catch(NoSuchAlgorithmExceptione)...{
returnnull;
}
}


privateStringtoHex(byte[]buffer)...{
StringBuffersb=newStringBuffer(buffer.length*2);


for(inti=0;i<buffer.length;i++)...{
sb.append(Character.forDigit((buffer[i]&0xf0)>>4,16));
sb.append(Character.forDigit(buffer[i]&0x0f,16));
}

returnsb.toString();
}



protectedObjectgetService(StringserviceId)...{
returnServicePool.getService(serviceId);
}


protectedvoidregisterModelResource(RenderRequestrequest,StringclassName,Stringid)...{

try...{
Layoutlayout=(Layout)request.getAttribute(WebKeys.LAYOUT);
StringuserId=request.getRemoteUser();
Useruser=UserLocalServiceUtil.getUserById(userId);
StringgroupId=PortalUtil.getPortletGroupId(layout.getPlid());
ResourceLocalServiceUtil.addModelResources(user.getCompanyId(),
groupId,userId,className,id,newString[1],
newString[1]);













































































































































































































































































