001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.struts;
016    
017    import com.liferay.portal.kernel.struts.BaseStrutsPortletAction;
018    import com.liferay.portal.kernel.util.ClassLoaderUtil;
019    
020    import javax.portlet.ActionRequest;
021    import javax.portlet.ActionResponse;
022    import javax.portlet.PortletConfig;
023    import javax.portlet.RenderRequest;
024    import javax.portlet.RenderResponse;
025    import javax.portlet.ResourceRequest;
026    import javax.portlet.ResourceResponse;
027    
028    import org.apache.struts.action.ActionForm;
029    import org.apache.struts.action.ActionForward;
030    import org.apache.struts.action.ActionMapping;
031    
032    /**
033     * @author Mika Koivisto
034     */
035    public class StrutsPortletActionAdapter extends BaseStrutsPortletAction {
036    
037            public StrutsPortletActionAdapter(
038                    PortletAction portletAction, ActionMapping actionMapping,
039                    ActionForm actionForm) {
040    
041                    _portletAction = portletAction;
042                    _actionMapping = actionMapping;
043                    _actionForm = actionForm;
044            }
045    
046            @Override
047            public boolean isCheckMethodOnProcessAction() {
048                    return _portletAction.isCheckMethodOnProcessAction();
049            }
050    
051            @Override
052            public void processAction(
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    Thread currentThread = Thread.currentThread();
058    
059                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
060    
061                    currentThread.setContextClassLoader(
062                            ClassLoaderUtil.getPortalClassLoader());
063    
064                    try {
065                            _portletAction.processAction(
066                                    _actionMapping, _actionForm, portletConfig, actionRequest,
067                                    actionResponse);
068                    }
069                    finally {
070                            currentThread.setContextClassLoader(contextClassLoader);
071                    }
072            }
073    
074            @Override
075            public String render(
076                            PortletConfig portletConfig, RenderRequest renderRequest,
077                            RenderResponse renderResponse)
078                    throws Exception {
079    
080                    Thread currentThread = Thread.currentThread();
081    
082                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
083    
084                    currentThread.setContextClassLoader(
085                            ClassLoaderUtil.getPortalClassLoader());
086    
087                    try {
088                            ActionForward actionForward = _portletAction.render(
089                                    _actionMapping, _actionForm, portletConfig, renderRequest,
090                                    renderResponse);
091    
092                            if (actionForward != null) {
093                                    return actionForward.getPath();
094                            }
095    
096                            return null;
097                    }
098                    finally {
099                            currentThread.setContextClassLoader(contextClassLoader);
100                    }
101            }
102    
103            @Override
104            public void serveResource(
105                            PortletConfig portletConfig, ResourceRequest resourceRequest,
106                            ResourceResponse resourceResponse)
107                    throws Exception {
108    
109                    Thread currentThread = Thread.currentThread();
110    
111                    ClassLoader contextClassLoader = currentThread.getContextClassLoader();
112    
113                    currentThread.setContextClassLoader(
114                            ClassLoaderUtil.getPortalClassLoader());
115    
116                    try {
117                            _portletAction.serveResource(
118                                    _actionMapping, _actionForm, portletConfig, resourceRequest,
119                                    resourceResponse);
120                    }
121                    finally {
122                            currentThread.setContextClassLoader(contextClassLoader);
123                    }
124            }
125    
126            private final ActionForm _actionForm;
127            private final ActionMapping _actionMapping;
128            private final PortletAction _portletAction;
129    
130    }