001    /**
002     * Copyright (c) 2000-2010 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            public void setParameter(String name, String value) {
041                    if (name.equals("struts_action")) {
042                            if (!value.startsWith(_strutsPath)) {
043                                    int pos = value.lastIndexOf(CharPool.SLASH);
044    
045                                    value = _strutsPath + value.substring(pos + 1, value.length());
046                            }
047                    }
048    
049                    super.setParameter(name, value);
050            }
051    
052            public void setParameters(Map<String, String[]> params) {
053                    for (Map.Entry<String, String[]> entry : params.entrySet()) {
054                            String name = entry.getKey();
055                            String[] values = entry.getValue();
056    
057                            if (name.equals("struts_action")) {
058                                    for (int i = 0; i < values.length; i++) {
059                                            String value = values[i];
060    
061                                            if (!value.startsWith(_strutsPath)) {
062                                                    int pos = value.lastIndexOf(CharPool.SLASH);
063    
064                                                    value =
065                                                            _strutsPath +
066                                                                    value.substring(pos + 1, value.length());
067    
068                                                    values[i] = value;
069                                            }
070                                    }
071                            }
072                    }
073    
074                    super.setParameters(params);
075            }
076    
077            private Portlet _portlet;
078            private String _strutsPath;
079    
080    }