001    /**
002     * Copyright (c) 2000-2013 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            @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);
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 = _strutsPath + value.substring(pos + 1);
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    }