001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public Object getAttribute(String name) {
058                    Object value = null;
059    
060                    if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
061                            value = _strutsAttributes.get(name);
062                    }
063                    else {
064                            value = super.getAttribute(name);
065                    }
066    
067                    return value;
068            }
069    
070            public void setAttribute(String name, Object value) {
071                    if (name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
072                            _strutsAttributes.put(name, value);
073                    }
074                    else {
075                            super.setAttribute(name, value);
076                    }
077            }
078    
079            public Enumeration<String> getAttributeNames() {
080                    List<String> attributeNames = new Vector<String>();
081    
082                    Enumeration<String> enu = super.getAttributeNames();
083    
084                    while (enu.hasMoreElements()) {
085                            String name = enu.nextElement();
086    
087                            if (!name.startsWith(StrutsUtil.STRUTS_PACKAGE)) {
088                                    attributeNames.add(name);
089                            }
090                    }
091    
092                    attributeNames.addAll(_strutsAttributes.keySet());
093    
094                    return Collections.enumeration(attributeNames);
095            }
096    
097            public ServletInputStream getInputStream() throws IOException {
098                    if (_bytes == null) {
099                            InputStream is = super.getInputStream();
100    
101                            _bytes = FileUtil.getBytes(is);
102    
103                            is.close();
104                    }
105    
106                    return new UnsyncByteArrayInputStreamWrapper(
107                            new UnsyncByteArrayInputStream(_bytes));
108            }
109    
110            private Map<String, Object> _strutsAttributes;
111            private byte[] _bytes;
112    
113    }