001    /**
002     * Copyright (c) 2000-2011 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.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    }