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