2 * jFCPlib - GetResult.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.fcp.highlevel;
20 import java.io.InputStream;
21 import java.util.EventListener;
24 * A get result encapsulates the result of {@link FcpClient#getURI(String)}. It
25 * is used to allow synchronous retrieval of a file without resorting to
26 * {@link EventListener} interfaces to notify the application of intermediary
27 * results, such as redirects.
29 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
31 public class GetResult {
33 /** Whether the request was successful. */
34 private boolean success;
36 /** The error code, if an error occured. */
37 private int errorCode;
39 /** The exception, if an exception occured. */
40 private Throwable exception;
42 /** The real URI, if a redirect was found. */
43 private String realUri;
45 /** The content type of the file. */
46 private String contentType;
48 /** The length of the file. */
49 private long contentLength;
51 /** An input stream containing the data of the file. */
52 private InputStream inputStream;
55 * Returns whether the request was successful.
57 * @return {@code true} if the request was successful, {@code false}
60 public boolean isSuccess() {
65 * Sets whether the request was successful.
68 * {@code true} if the request was successful, {@code false}
70 * @return This result, to allow method chaning
72 GetResult success(boolean success) {
73 this.success = success;
78 * Returns the error code of the request. The error code is the error code
79 * that is transferred in FCP’s “GetFailed” message. The error code is not
80 * valid if {@link #isSuccess()} is {@code true}. If an exception occured
81 * (i.e. if {@link #getException()} returns a non-{@code null} value) the
82 * error code might also be invalid.
84 * @return The error code of the request
86 public int getErrorCode() {
91 * Sets the error code of the request.
94 * The error code of the request
95 * @return This result, to allow method chaining
97 GetResult errorCode(int errorCode) {
98 this.errorCode = errorCode;
103 * Returns the exception, if any occured.
105 * @return The occured exception, or {@code null} if there was no exception
107 public Throwable getException() {
112 * Sets the exception that occured.
115 * The occured exception
116 * @return This result, to allow method chaining
118 GetResult exception(Throwable exception) {
119 this.exception = exception;
124 * Returns the real URI in case of a redirect.
126 * @return The real URI, or {@code null} if there was no redirect
128 public String getRealUri() {
133 * Sets the real URI in case of a redirect.
137 * @return This result, to allow method chaining
139 GetResult realUri(String realUri) {
140 this.realUri = realUri;
145 * Returns the content type of the result.
147 * @return The content type of the result
149 public String getContentType() {
154 * Sets the content type of the result.
157 * The content type of the result
158 * @return This result, to allow method chaining
160 GetResult contentType(String contentType) {
161 this.contentType = contentType;
166 * Returns the content length of the result.
168 * @return The content length of the result
170 public long getContentLength() {
171 return contentLength;
175 * Sets the content length of the result.
177 * @param contentLength
178 * The content length of the result
179 * @return This result, to allow method chaining
181 GetResult contentLength(long contentLength) {
182 this.contentLength = contentLength;
191 public InputStream getInputStream() {
196 * Sets the input stream that will deliver the data.
199 * The input stream containing the data
200 * @return This result, to allow method chaining
202 GetResult inputStream(InputStream inputStream) {
203 this.inputStream = inputStream;
215 public String toString() {
216 return getClass().getName() + "[success=" + success + ",errorCode=" + errorCode + ",exception=" + exception + ",realUri=" + realUri + ",contentType=" + contentType + ",contentLength=" + contentLength + ",inputStream=" + inputStream + "]";