Liferay學習歷程二:搭建二次開發環境(第三部分)

在這裏將介紹如何集成Struts的DispatchAction多方法的功能,下面是代碼

packagecn.pisoft.portlet.struts;

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

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

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

importcn.pisoft.portlet.km.ExtActionKey;
importcn.pisoft.portlet.km.ServicePool;

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

publicclassDispatchPortletActionextendsPortletAction...{
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]);
發佈了0 篇原創文章 · 獲贊 0 · 訪問量 2782
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章