1 /**
2 * Copyright (C) 2005-2009 Alfresco Software Limited.
3 *
4 * This file is part of the Spring Surf Extension project.
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package org.springframework.extensions.webscripts.connector;
20
21 import java.io.ByteArrayInputStream;
22 import java.io.InputStream;
23 import java.io.UnsupportedEncodingException;
24
25 /**
26 * Representation of the response from a remote HTTP API call.
27 *
28 * @author Kevin Roast
29 */
30 public class Response
31 {
32 private String data;
33 private InputStream is;
34 private ResponseStatus status;
35 private String encoding = null;
36
37 /**
38 * Instantiates a new response.
39 *
40 * @param status the status
41 */
42 Response(ResponseStatus status)
43 {
44 this.status = status;
45 }
46
47 /**
48 * Instantiates a new response.
49 *
50 * @param data the data
51 * @param status the status
52 */
53 Response(String data, ResponseStatus status)
54 {
55 this.data = data;
56 this.status = status;
57 }
58
59 /**
60 * Instantiates a new response.
61 *
62 * @param is the is
63 * @param status the status
64 */
65 Response(InputStream is, ResponseStatus status)
66 {
67 this.is = is;
68 this.status = status;
69 }
70
71 /**
72 * Sets the encoding.
73 *
74 * @param encoding the new encoding
75 */
76 /*package*/ void setEncoding(String encoding)
77 {
78 this.encoding = encoding;
79 }
80
81 /**
82 * Gets the response.
83 *
84 * @return the data stream from the response object - will be null on error
85 * or if the response has already been streamed to an OutputStream.
86 */
87 public String getResponse()
88 {
89 return this.data;
90 }
91
92 /**
93 * Gets the text of the response.
94 *
95 * @return the text
96 */
97 public String getText()
98 {
99 return this.getResponse();
100 }
101
102 /**
103 * Gets the response stream.
104 *
105 * @return the response InputStream if set during construction, else will be null.
106 */
107 public InputStream getResponseStream()
108 {
109 try
110 {
111 return (this.is != null ? this.is : new ByteArrayInputStream(
112 this.data.getBytes(encoding)));
113 }
114 catch (UnsupportedEncodingException e)
115 {
116 throw new RuntimeException(
117 "UnsupportedEncodingException: " + encoding);
118 }
119 }
120
121 /**
122 * Gets the status.
123 *
124 * @return Status object representing the response status and any error information
125 */
126 public ResponseStatus getStatus()
127 {
128 return this.status;
129 }
130
131 /**
132 * Gets the encoding.
133 *
134 * @return the response encoding
135 */
136 public String getEncoding()
137 {
138 return this.encoding;
139 }
140
141 /*
142 * (non-Javadoc)
143 *
144 * @see java.lang.Object#toString()
145 */
146 @Override
147 public String toString()
148 {
149 return this.data;
150 }
151 }