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.apache.bridges.struts;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.HttpUtil;
020    import com.liferay.portlet.PortletResponseImpl;
021    import com.liferay.portlet.PortletURLImplWrapper;
022    
023    import org.apache.portals.bridges.struts.StrutsPortletURL;
024    
025    /**
026     * @author Michael Young
027     */
028    public class LiferayStrutsPortletURLImpl extends PortletURLImplWrapper {
029    
030            public LiferayStrutsPortletURLImpl(
031                    PortletResponseImpl portletResponseImpl, long plid, String lifecycle) {
032    
033                    super(portletResponseImpl, plid, lifecycle);
034            }
035    
036            @Override
037            public void setParameter(String name, String value) {
038                    super.setParameter(name, value);
039    
040                    // Add parameters from the query string because bridges passes these
041                    // through instead of setting them on the portlet URL
042    
043                    String decodedValue = HttpUtil.decodeURL(value);
044    
045                    try {
046                            if (name.equals(StrutsPortletURL.PAGE)) {
047                                    String[] urlComponents = decodedValue.split("\\?", 2);
048    
049                                    if (urlComponents.length != 2) {
050                                            return;
051                                    }
052    
053                                    String[] nameValue = urlComponents[1].split("\\&");
054    
055                                    for (int i = 0; i < nameValue.length; i++) {
056                                            String[] nameValuePair = nameValue[i].split("\\=", 2);
057    
058                                            if (nameValuePair.length == 2) {
059                                                    super.setParameter(nameValuePair[0], nameValuePair[1]);
060                                            }
061                                            else if (nameValuePair.length == 1) {
062                                                    super.setParameter(nameValuePair[0], "true");
063                                            }
064                                    }
065                            }
066                    }
067                    catch (Throwable t) {
068                            _log.error("Could not parse Struts page query string " + value, t);
069                    }
070            }
071    
072            private static Log _log = LogFactoryUtil.getLog(
073                    LiferayStrutsPortletURLImpl.class);
074    
075    }