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.upload;
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.io.unsync.UnsyncByteArrayOutputStream;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.servlet.HttpHeaders;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.util.PropsUtil;
025    import com.liferay.util.servlet.ServletInputStreamWrapper;
026    
027    import java.io.IOException;
028    
029    import javax.servlet.ServletInputStream;
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpSession;
032    
033    /**
034     * @author Brian Myunghun Kim
035     * @author Brian Wing Shun Chan
036     * @author Harry Mark
037     */
038    public class LiferayInputStream extends ServletInputStreamWrapper {
039    
040            public static final long THRESHOLD_SIZE = GetterUtil.getLong(
041                    PropsUtil.get(LiferayInputStream.class.getName() + ".threshold.size"));
042    
043            public LiferayInputStream(HttpServletRequest request) throws IOException {
044                    super(request.getInputStream());
045    
046                    _session = request.getSession();
047                    _totalSize = request.getContentLength();
048    
049                    if (_totalSize < 0) {
050                            _totalSize = GetterUtil.getLong(
051                                    request.getHeader(HttpHeaders.CONTENT_LENGTH), _totalSize);
052                    }
053            }
054    
055            public ServletInputStream getCachedInputStream() {
056                    if (_totalSize < THRESHOLD_SIZE) {
057                            return this;
058                    }
059                    else {
060                            return new UnsyncByteArrayInputStreamWrapper(
061                                    new UnsyncByteArrayInputStream(
062                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
063                    }
064            }
065    
066            @Override
067            public int read(byte[] b, int off, int len) throws IOException {
068                    int bytesRead = super.read(b, off, len);
069    
070                    if (bytesRead > 0) {
071                            _totalRead += bytesRead;
072                    }
073                    else {
074                            return bytesRead;
075                    }
076    
077                    int percent = (int)((_totalRead * 100L) / _totalSize);
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
081                    }
082    
083                    if ((_totalSize > 0) && (_totalSize < THRESHOLD_SIZE)) {
084                            _cachedBytes.write(b, off, bytesRead);
085                    }
086    
087                    Integer curPercent = (Integer)_session.getAttribute(
088                            LiferayFileUpload.PERCENT);
089    
090                    if ((curPercent == null) || ((percent - curPercent.intValue()) >= 1)) {
091                            _session.setAttribute(
092                                    LiferayFileUpload.PERCENT, new Integer(percent));
093                    }
094    
095                    return bytesRead;
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
099    
100            private UnsyncByteArrayOutputStream _cachedBytes =
101                    new UnsyncByteArrayOutputStream();
102            private HttpSession _session;
103            private long _totalRead;
104            private long _totalSize;
105    
106    }