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