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.io.unsync.UnsyncByteArrayInputStream;
018    import com.liferay.portal.kernel.io.unsync.UnsyncByteArrayInputStreamWrapper;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.struts.StrutsUtil;
021    import com.liferay.portal.util.WebKeys;
022    
023    import java.io.IOException;
024    import java.io.InputStream;
025    
026    import java.util.Collections;
027    import java.util.Enumeration;
028    import java.util.HashMap;
029    import java.util.List;
030    import java.util.Map;
031    import java.util.Vector;
032    
033    import javax.servlet.ServletInputStream;
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletRequestWrapper;
036    
037    /**
038     * @author Michael Young
039     * @author Deepak Gothe
040     */
041    public class LiferayStrutsRequestImpl extends HttpServletRequestWrapper {
042    
043            public LiferayStrutsRequestImpl(HttpServletRequest request) {
044                    super(request);
045    
046                    _strutsAttributes = (Map<String, Object>)request.getAttribute(
047                            WebKeys.STRUTS_BRIDGES_ATTRIBUTES);
048    
049                    if (_strutsAttributes == null) {
050                            _strutsAttributes = new HashMap<String, Object>();
051    
052                            request.setAttribute(
053                                    WebKeys.STRUTS_BRIDGES_ATTRIBUTES, _strutsAttributes);
054                    }
055            }
056    
057            @Override
058            public Object getAttribute(String name) {
059                    Object value = null;
060    
061                    if (name.startsWith(StrutsUtil.STRUTS_PACKAGE) &&
062                            _strutsAttributes.containsKey(name)) {
063    
064                            value = _strutsAttributes.get(name);
065                    }
066                    else {
067                            value = super.getAttribute(name);
068                    }
069    
070                    return value;
071            }
072    
073            @Override
074            public Enumeration<String> getAttributeNames() {
075                    List<String> attributeNames = new Vector<String>();
076    
077                    Enumeration<String> enu = super.getAttributeNames();
078    
079                    while (enu.hasMoreElements()) {
080                            String name = enu.nextElement();
081    
082                            if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
083                                    attributeNames.add(name);
084                            }
085                    }
086    
087                    attributeNames.addAll(_strutsAttributes.keySet());
088    
089                    return Collections.enumeration(attributeNames);
090            }
091    
092            @Override
093            public ServletInputStream getInputStream() throws IOException {
094                    if (_bytes == null) {
095                            InputStream is = super.getInputStream();
096    
097                            _bytes = FileUtil.getBytes(is);
098                    }
099    
100                    return new UnsyncByteArrayInputStreamWrapper(
101                            new UnsyncByteArrayInputStream(_bytes));
102            }
103    
104            @Override
105            public void removeAttribute(String name) {
106                    if (name.startsWith(StrutsUtil.STRUTS_PACKAGE) &&
107                            _strutsAttributes.containsKey(name)) {
108    
109                            _strutsAttributes.remove(name);
110                    }
111                    else {
112                            super.removeAttribute(name);
113                    }
114            }
115    
116            @Override
117            public void setAttribute(String name, Object value) {
118                    if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
119                            _strutsAttributes.put(name, value);
120                    }
121                    else {
122                            super.setAttribute(name, value);
123                    }
124            }
125    
126            private byte[] _bytes;
127            private Map<String, Object> _strutsAttributes;
128    
129    }