001    /**
002     * Copyright (c) 2000-2010 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.StringPool;
018    import com.liferay.portal.model.Portlet;
019    import com.liferay.portlet.PortletResponseImpl;
020    import com.liferay.portlet.PortletURLImplWrapper;
021    
022    import java.util.Map;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class StrutsActionPortletURL extends PortletURLImplWrapper {
028    
029            public StrutsActionPortletURL(
030                    PortletResponseImpl portletResponseImpl, long plid, String lifecycle) {
031    
032                    super(portletResponseImpl, plid, lifecycle);
033    
034                    _portlet = portletResponseImpl.getPortlet();
035                    _strutsPath =
036                            StringPool.SLASH + _portlet.getStrutsPath() + StringPool.SLASH;
037            }
038    
039            public void setParameter(String name, String value) {
040                    if (name.equals("struts_action")) {
041                            if (!value.startsWith(_strutsPath)) {
042                                    int pos = value.lastIndexOf(StringPool.SLASH);
043    
044                                    value = _strutsPath + value.substring(pos + 1, value.length());
045                            }
046                    }
047    
048                    super.setParameter(name, value);
049            }
050    
051            public void setParameters(Map<String, String[]> params) {
052                    for (Map.Entry<String, String[]> entry : params.entrySet()) {
053                            String name = entry.getKey();
054                            String[] values = entry.getValue();
055    
056                            if (name.equals("struts_action")) {
057                                    for (int i = 0; i < values.length; i++) {
058                                            String value = values[i];
059    
060                                            if (!value.startsWith(_strutsPath)) {
061                                                    int pos = value.lastIndexOf(StringPool.SLASH);
062    
063                                                    value =
064                                                            _strutsPath +
065                                                                    value.substring(pos + 1, value.length());
066    
067                                                    values[i] = value;
068                                            }
069                                    }
070                            }
071                    }
072    
073                    super.setParameters(params);
074            }
075    
076            private Portlet _portlet;
077            private String _strutsPath;
078    
079    }