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.fabric.netty.fileserver;
016    
017    import com.liferay.portal.kernel.io.PathHolder;
018    import com.liferay.portal.kernel.util.HashUtil;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.io.Serializable;
022    
023    import java.nio.file.Path;
024    
025    /**
026     * @author Shuyang Zhou
027     */
028    public class FileResponse implements Serializable {
029    
030            public static final long FILE_NOT_FOUND = 0;
031    
032            public static final long FILE_NOT_MODIFIED = -1;
033    
034            public FileResponse(
035                    Path path, long size, long lastModifiedTime, boolean folder) {
036    
037                    _size = size;
038                    _lastModifiedTime = lastModifiedTime;
039                    _folder = folder;
040    
041                    _pathHolder = new PathHolder(path);
042            }
043    
044            @Override
045            public boolean equals(Object obj) {
046                    if (this == obj) {
047                            return true;
048                    }
049    
050                    if (!(obj instanceof FileResponse)) {
051                            return false;
052                    }
053    
054                    FileResponse fileResponse = (FileResponse)obj;
055    
056                    if ((_folder == fileResponse._folder) &&
057                            (_lastModifiedTime == fileResponse._lastModifiedTime) &&
058                            _pathHolder.equals(fileResponse._pathHolder) &&
059                            (_size == fileResponse._size)) {
060    
061                            return true;
062                    }
063    
064                    return false;
065            }
066    
067            public long getLastModifiedTime() {
068                    return _lastModifiedTime;
069            }
070    
071            public Path getLocalFile() {
072                    return _localFile;
073            }
074    
075            public Path getPath() {
076                    return _pathHolder.getPath();
077            }
078    
079            public long getSize() {
080                    return _size;
081            }
082    
083            @Override
084            public int hashCode() {
085                    int hash = HashUtil.hash(0, _folder);
086    
087                    hash = HashUtil.hash(hash, _lastModifiedTime);
088                    hash = HashUtil.hash(hash, _pathHolder);
089                    hash = HashUtil.hash(hash, _size);
090    
091                    return hash;
092            }
093    
094            public boolean isFileNotFound() {
095                    if (_size == FILE_NOT_FOUND) {
096                            return true;
097                    }
098    
099                    return false;
100            }
101    
102            public boolean isFileNotModified() {
103                    if (_size == FILE_NOT_MODIFIED) {
104                            return true;
105                    }
106    
107                    return false;
108            }
109    
110            public boolean isFolder() {
111                    return _folder;
112            }
113    
114            public void setLocalFile(Path localFile) {
115                    _localFile = localFile;
116            }
117    
118            @Override
119            public String toString() {
120                    StringBundler sb = new StringBundler(_size > 0 ? 11 : 10);
121    
122                    sb.append("{folder=");
123                    sb.append(_folder);
124                    sb.append(", lastModifiedTime=");
125                    sb.append(_lastModifiedTime);
126                    sb.append(", localFile=");
127                    sb.append(_localFile);
128                    sb.append(", pathHolder=");
129                    sb.append(_pathHolder);
130    
131                    if (_size == FILE_NOT_FOUND) {
132                            sb.append(", status=File Not Found");
133                    }
134                    else if (_size == FILE_NOT_MODIFIED) {
135                            sb.append(", status=File Not Modified");
136                    }
137                    else {
138                            sb.append(", size=");
139                            sb.append(_size);
140                    }
141    
142                    sb.append("}");
143    
144                    return sb.toString();
145            }
146    
147            private static final long serialVersionUID = 1L;
148    
149            private final boolean _folder;
150            private final long _lastModifiedTime;
151            private transient Path _localFile;
152            private final PathHolder _pathHolder;
153            private final long _size;
154    
155    }