View Javadoc

1   /*
2    * Copyright 2012 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.ci.comm.command;
17  
18  import java.io.Serializable;
19  
20  /**
21   * The result of the execution of a {@link Command}.
22   *
23   * @param <T> the type of the command this is a result for.
24   */
25  public final class CommandResult<T extends Command<T>> implements Serializable
26  {
27  
28    // ********************************* Fields *********************************
29  
30    // --- constants ------------------------------------------------------------
31  
32    /**
33     * The class version identifier.
34     * <p>
35     * The value of this constant is {@value}.
36     * </p>
37     */
38    private static final long serialVersionUID = 1L;
39  
40    // --- members --------------------------------------------------------------
41  
42    /**
43     * The return code.
44     *
45     * @serial
46     */
47    private final int code;
48  
49    /**
50     * The information about the HTTP status returned by the result.
51     *
52     * @serial
53     */
54    private final HttpStatus httpStatus;
55  
56    /**
57     * The page returned on the request. Since this may be a performance issue,
58     * this information is not collected by default. Therefore the value may be
59     * <code>null</code>.
60     *
61     * @serial
62     */
63    private final String pageContent;
64  
65    // ****************************** Initializer *******************************
66  
67    // ****************************** Constructors ******************************
68  
69    /**
70     * Default constructor.
71     *
72     * @param code the return code.
73     * @param httpStatus the information about the HTTP status returned by the
74     *          result.
75     * @param pageContent the page returned on the request.
76     */
77    public CommandResult(final int code, final HttpStatus httpStatus,
78        final String pageContent)
79    {
80      this.code = code;
81      this.httpStatus = httpStatus;
82      this.pageContent = pageContent;
83    }
84  
85    // ****************************** Inner Classes *****************************
86  
87    /**
88     * Provides information about the HTTP status returned by the result.
89     */
90    public static final class HttpStatus implements Serializable
91    {
92      // ******************************** Fields ********************************
93  
94      // --- constants ----------------------------------------------------------
95  
96      /**
97       * The class version identifier.
98       * <p>
99       * The value of this constant is {@value}.
100      * </p>
101      */
102     private static final long serialVersionUID = 1L;
103 
104     /**
105      * HTTP-status OK.
106      */
107     public static final int OK = 200;
108 
109     // --- members ------------------------------------------------------------
110 
111     /**
112      * The HTTP status code of the result.
113      *
114      * @serial
115      */
116     private final int code;
117 
118     /**
119      * The HTTP status text of the result.
120      *
121      * @serial
122      */
123     private final String text;
124 
125     // ***************************** Initializer ******************************
126 
127     // ***************************** Constructors *****************************
128 
129     /**
130      * Default constructor.
131      *
132      * @param code the HTTP status code of the result.
133      * @param text the HTTP status text of the result.
134      */
135     public HttpStatus(final int code, final String text)
136     {
137       this.code = code;
138       this.text = text;
139     }
140 
141     // ***************************** Inner Classes ****************************
142 
143     // ******************************** Methods *******************************
144 
145     // --- init ---------------------------------------------------------------
146 
147     // --- get&set ------------------------------------------------------------
148 
149     /**
150      * Returns the HTTP status code of the result.
151      *
152      * @return the HTTP status code of the result.
153      */
154     public int getCode()
155     {
156       return code;
157     }
158 
159     /**
160      * Returns the HTTP status text of the result.
161      *
162      * @return the HTTP status text of the result.
163      */
164     public String getText()
165     {
166       return text;
167     }
168 
169     // --- business -----------------------------------------------------------
170 
171     // --- object basics ------------------------------------------------------
172 
173     /**
174      * {@inheritDoc}
175      *
176      * @see java.lang.Object#toString()
177      */
178     @Override
179     public String toString()
180     {
181       return code + ": " + text;
182     }
183   }
184 
185   // ********************************* Methods ********************************
186 
187   // --- init -----------------------------------------------------------------
188 
189   // --- get&set --------------------------------------------------------------
190 
191   /**
192    * Returns the return code.
193    *
194    * @return the return code.
195    */
196   public int getCode()
197   {
198     return code;
199   }
200 
201   /**
202    * Returns the information about the HTTP status returned by the result.
203    *
204    * @return the information about the HTTP status returned by the result.
205    */
206   public HttpStatus getHttpStatus()
207   {
208     return httpStatus;
209   }
210 
211   /**
212    * Returns the page returned on the request. Since this may be a performance
213    * issue, this information is not collected by default. Therefore the value
214    * may be <code>null</code>.
215    *
216    * @return the page returned on the request.
217    */
218   public String getPageContent()
219   {
220     return pageContent;
221   }
222 
223   // --- business -------------------------------------------------------------
224 
225   // --- object basics --------------------------------------------------------
226 
227   /**
228    * {@inheritDoc}
229    *
230    * @see java.lang.Object#toString()
231    */
232   @Override
233   public String toString()
234   {
235     return httpStatus + " (" + code + ')';
236   }
237 }