001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.struts;
016    
017    import com.liferay.portal.kernel.util.CharPool;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.model.Portlet;
020    import com.liferay.portlet.PortletResponseImpl;
021    import com.liferay.portlet.PortletURLImplWrapper;
022    
023    import java.util.Map;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class StrutsActionPortletURL extends PortletURLImplWrapper {
029    
030            public StrutsActionPortletURL(
031                    PortletResponseImpl portletResponseImpl, long plid, String lifecycle) {
032    
033                    super(portletResponseImpl, plid, lifecycle);
034    
035                    _portlet = portletResponseImpl.getPortlet();
036                    _strutsPath =
037                            StringPool.SLASH + _portlet.getStrutsPath() + StringPool.SLASH;
038            }
039    
040            @Override
041            public void setParameter(String name, String value) {
042                    if (name.equals("struts_action")) {
043                            if (!value.startsWith(_strutsPath)) {
044                                    int pos = value.lastIndexOf(CharPool.SLASH);
045    
046                                    value = _strutsPath + value.substring(pos + 1, value.length());
047                            }
048                    }
049    
050                    super.setParameter(name, value);
051            }
052    
053            @Override
054            public void setParameters(Map<String, String[]> params) {
055                    for (Map.Entry<String, String[]> entry : params.entrySet()) {
056                            String name = entry.getKey();
057                            String[] values = entry.getValue();
058    
059                            if (name.equals("struts_action")) {
060                                    for (int i = 0; i < values.length; i++) {
061                                            String value = values[i];
062    
063                                            if (!value.startsWith(_strutsPath)) {
064                                                    int pos = value.lastIndexOf(CharPool.SLASH);
065    
066                                                    value =
067                                                            _strutsPath +
068                                                                    value.substring(pos + 1, value.length());
069    
070                                                    values[i] = value;
071                                            }
072                                    }
073                            }
074                    }
075    
076                    super.setParameters(params);
077            }
078    
079            private Portlet _portlet;
080            private String _strutsPath;
081    
082    }