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