001    /**
002     * Copyright (c) 2000-present 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    
049                    long totalSize = request.getContentLength();
050    
051                    if (totalSize < 0) {
052                            totalSize = GetterUtil.getLong(
053                                    request.getHeader(HttpHeaders.CONTENT_LENGTH), totalSize);
054                    }
055    
056                    _totalSize = totalSize;
057            }
058    
059            public ServletInputStream getCachedInputStream() {
060                    if (_totalSize < THRESHOLD_SIZE) {
061                            return this;
062                    }
063                    else {
064                            return new ServletInputStreamAdapter(
065                                    new UnsyncByteArrayInputStream(
066                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
067                    }
068            }
069    
070            @Override
071            public int read(byte[] b, int off, int len) throws IOException {
072                    int bytesRead = super.read(b, off, len);
073    
074                    if (bytesRead > 0) {
075                            _totalRead += bytesRead;
076                    }
077                    else {
078                            return bytesRead;
079                    }
080    
081                    int percent = (int)((_totalRead * 100L) / _totalSize);
082    
083                    if (_log.isDebugEnabled()) {
084                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
085                    }
086    
087                    if ((_totalSize > 0) && (_totalSize < THRESHOLD_SIZE)) {
088                            _cachedBytes.write(b, off, bytesRead);
089                    }
090    
091                    ProgressTracker progressTracker =
092                            (ProgressTracker)_session.getAttribute(LiferayFileUpload.PERCENT);
093    
094                    Integer curPercent = null;
095    
096                    if (progressTracker != null) {
097                            curPercent = progressTracker.getPercent();
098                    }
099    
100                    if ((curPercent == null) || ((percent - curPercent.intValue()) >= 1)) {
101                            if (progressTracker == null) {
102                                    progressTracker = new ProgressTracker(StringPool.BLANK);
103    
104                                    progressTracker.initialize(_session);
105                            }
106    
107                            progressTracker.setPercent(percent);
108                    }
109    
110                    return bytesRead;
111            }
112    
113            private static final Log _log = LogFactoryUtil.getLog(
114                    LiferayInputStream.class);
115    
116            private final UnsyncByteArrayOutputStream _cachedBytes =
117                    new UnsyncByteArrayOutputStream();
118            private final HttpSession _session;
119            private long _totalRead;
120            private final long _totalSize;
121    
122    }