1

I ran into a little problem with a hook. Szenario:

  • The Hook should override struts action /document_library/edit_file_entry which is called, whenever a user uploads a document into the document library.
  • The goal is to check the title of the document and rename it following a given naming-scheme.

My solution:

@Override
public void processAction(
        StrutsPortletAction originalStrutsPortletAction,
        PortletConfig portletConfig, ActionRequest actionRequest,
        ActionResponse actionResponse)
    throws Exception {

    //Get old title - set new title
    String oldTitle =  ParamUtil.getString(actionRequest, "title");
    String newTitle = "Test";

    //wrap request to set param
    DynamicActionRequest actionRequestNew = new DynamicActionRequest(actionRequest);
    actionRequestNew.setParameter("title", newTitle );

    //call original struts action with modified title
    originalStrutsPortletAction.processAction(originalStrutsPortletAction, portletConfig,  actionRequestNew, actionResponse);

}

The Problem is that the original Struts action in portal-impl/src/com/liferay/portlet/documentlibrary/action/EditFileEntryAction.java uses PortalUtil.getUploadPortletRequest(actionRequest); which expects a PortletRequestImpl.

But DynamicActionRequest cannot be cast to PortletRequestImpl.

See:

12:07:04,466 ERROR [http-bio-8082-exec-44][render_portlet_jsp:154] java.lang.ClassCastException: com.liferay.portal.kernel.portlet.DynamicActionRequest cannot be cast to com.liferay.portlet.PortletRequestImpl
    at com.liferay.portal.util.PortalImpl.getUploadPortletRequest(PortalImpl.java:4067)
    at com.liferay.portal.util.PortalUtil.getUploadPortletRequest(PortalUtil.java:1253)
    at com.liferay.portlet.documentlibrary.action.EditFileEntryAction.updateFileEntry(EditFileEntryAction.java:653)
    at com.liferay.portlet.documentlibrary.action.EditFileEntryAction.processAction(EditFileEntryAction.java:129)
    at com.liferay.portal.struts.StrutsPortletActionAdapter.processAction(StrutsPortletActionAdapter.java:51)
    at com.liferay.portal.kernel.struts.BaseStrutsPortletAction.processAction(BaseStrutsPortletAction.java:42)
    at com.foo.hook.portlet.sites.action.MyEditFileEntryAction.processAction(MyEditFileEntryAction.java:83)
    at com.liferay.portal.kernel.bean.ClassLoaderBeanHandler.invoke(ClassLoaderBeanHandler.java:67)
    at com.liferay.portal.struts.PortletActionAdapter.processAction(PortletActionAdapter.java:55)
    at com.liferay.portal.struts.PortletRequestProcessor.process(PortletRequestProcessor.java:169)
    at com.liferay.portlet.StrutsPortlet.processAction(StrutsPortlet.java:212)
    at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:70)
    at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
    at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:548)
    at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:579)
    at com.liferay.portlet.InvokerPortletImpl.processAction(InvokerPortletImpl.java:294)
    at com.liferay.portal.action.LayoutAction.processPortletRequest(LayoutAction.java:944)
    at com.liferay.portal.action.LayoutAction.processLayout(LayoutAction.java:688)
    at com.liferay.portal.action.LayoutAction.execute(LayoutAction.java:249)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)

How can I change the parameter without using DynmicActionRequest? Any suggestions?

I'm running Liferay-Portal 6.1.20 EE.

Thanks in advance.

FeinesFabi
  • 1,147
  • 2
  • 12
  • 25

2 Answers2

-1

There are two approaches I can think of.

1) Create ActionRequestWrapper object and add a parameter. This would probably solve your issue.

2) Create a subclass of LR's action class. In that make the needed code changes. Create a hook and make the new action class available to LR.

I hope this helps.

Pankaj Kathiriya
  • 4,210
  • 2
  • 19
  • 26
Gaurav
  • 72
  • 4
  • 1) I don't see how this would work. I would have to make a wrapper for the 'PortletRequestImpl'. That would add a depencency for the `portal-impl' which is *22MB* and should never be added to a hook/portlet/etc. – FeinesFabi Mar 09 '15 at 18:36
  • 2) Sounds like writing an EXT which modifies the action class. I have to check if I'm allowed to introduce exts - as the are a maintenance risk. – FeinesFabi Mar 09 '15 at 18:38
  • 1) I was thinking to have a portlet filter which gets invoked for your upload request. You just override "getParameter" method to return the value of your desire for "title" param. I dont think you need ot bundly portal-impl in your hook. – Gaurav Mar 10 '15 at 10:51
  • Another approach can be to have a workflow. Create a workflow which gets kicked off on document upload. In your workflow action, you can modify the title of the document. – Gaurav Mar 10 '15 at 10:52
  • I really like the workflow idea. I haven't used workflows yet, so I'll give it a shot. In the meantime I have solved the problem by overriding the upload.js – FeinesFabi Mar 11 '15 at 12:24
-4

You can set the parameter in existing actionRequest:

actionRequest.setParameter("title", newTitle);

It will updated with new value.

Dani Sancas
  • 1,365
  • 11
  • 27