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.ServletInputStreamAdapter;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.ProgressTracker;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.util.PropsUtil;
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 ServletInputStreamAdapter {
039    
040            public static final int THRESHOLD_SIZE = GetterUtil.getInteger(
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    
050            public ServletInputStream getCachedInputStream() {
051                    if (_totalSize < THRESHOLD_SIZE) {
052                            return this;
053                    }
054                    else {
055                            return new ServletInputStreamAdapter(
056                                    new UnsyncByteArrayInputStream(
057                                            _cachedBytes.unsafeGetByteArray(), 0, _cachedBytes.size()));
058                    }
059            }
060    
061            @Override
062            public int read(byte[] b, int off, int len) throws IOException {
063                    int bytesRead = super.read(b, off, len);
064    
065                    if (bytesRead > 0) {
066                            _totalRead += bytesRead;
067                    }
068                    else {
069                            return bytesRead;
070                    }
071    
072                    int percent = (int)((_totalRead * 100L) / _totalSize);
073    
074                    if (_log.isDebugEnabled()) {
075                            _log.debug(bytesRead + "/" + _totalRead + "=" + percent);
076                    }
077    
078                    if ((_totalSize > 0) && (_totalSize < THRESHOLD_SIZE)) {
079                            _cachedBytes.write(b, off, bytesRead);
080                    }
081    
082                    ProgressTracker progressTracker =
083                            (ProgressTracker)_session.getAttribute(LiferayFileUpload.PERCENT);
084    
085                    Integer curPercent = null;
086    
087                    if (progressTracker != null) {
088                            curPercent = progressTracker.getPercent();
089                    }
090    
091                    if ((curPercent == null) || ((percent - curPercent.intValue()) >= 1)) {
092                            if (progressTracker == null) {
093                                    progressTracker = new ProgressTracker(
094                                            _session, StringPool.BLANK);
095    
096                                    progressTracker.initialize();
097                            }
098    
099                            progressTracker.setPercent(percent);
100                    }
101    
102                    return bytesRead;
103            }
104    
105            private static Log _log = LogFactoryUtil.getLog(LiferayInputStream.class);
106    
107            private UnsyncByteArrayOutputStream _cachedBytes =
108                    new UnsyncByteArrayOutputStream();
109            private HttpSession _session;
110            private int _totalRead;
111            private int _totalSize;
112    
113    }