View Javadoc
1   package emissary.util.web;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import javax.annotation.Nullable;
6   
7   /**
8    * The consolidated output from a web page retrieval
9    *
10   * @author mjf, 2000-08-01
11   */
12  public class UrlData {
13  
14      String password;
15      List<UrlRequestProperty> props;
16      String referer;
17      int responseCode;
18      byte[] theContent;
19      String theUrl;
20      int theMethod;
21      String userName;
22      String userAgent;
23  
24      /*
25       * The request properties array sent into this constructor is copied. Note that a deep copy is not performed. If the
26       * properties inside the list are changed, this class will see the changes.
27       */
28      public UrlData(final String theUrl, final byte[] theContent, final int responseCode, final List<UrlRequestProperty> props) {
29          this.theUrl = theUrl;
30          this.theContent = theContent;
31          this.responseCode = responseCode;
32          this.theMethod = Url.GET;
33          this.props = new ArrayList<>(props);
34      }
35  
36      public UrlData() {}
37  
38      public UrlData(final String theUrl) {
39          this.theUrl = theUrl;
40      }
41  
42      /**
43       * Get the value of responseCode.
44       * 
45       * @return Value of responseCode.
46       */
47      public int getResponseCode() {
48          return this.responseCode;
49      }
50  
51      /**
52       * Set the value of responseCode.
53       * 
54       * @param v Value to assign to responseCode.
55       */
56      public void setResponseCode(final int v) {
57          this.responseCode = v;
58      }
59  
60      /**
61       * Get the value of theUrl.
62       * 
63       * @return Value of theUrl.
64       */
65      public String getTheUrl() {
66          return this.theUrl;
67      }
68  
69      /**
70       * Set the value of theUrl.
71       * 
72       * @param v Value to assign to theUrl.
73       */
74      public void setTheUrl(final String v) {
75          this.theUrl = v;
76      }
77  
78      /**
79       * Get the value of theContent.
80       * 
81       * @return Value of theContent.
82       */
83      public byte[] getTheContent() {
84          return this.theContent;
85      }
86  
87      public int getContentLength() {
88          return (this.theContent == null) ? 0 : this.theContent.length;
89      }
90  
91      /**
92       * Set the value of theContent.
93       * 
94       * @param v Value to assign to theContent.
95       */
96      public void setTheContent(final byte[] v) {
97          this.theContent = v;
98      }
99  
100     /**
101      * Get the value of referer.
102      * 
103      * @return Value of referer.
104      */
105     public String getReferer() {
106         return this.referer;
107     }
108 
109     /**
110      * Set the value of referer.
111      * 
112      * @param v Value to assign to referer.
113      */
114     public void setReferer(final String v) {
115         this.referer = v;
116     }
117 
118     /**
119      * Get the value of userAgent.
120      * 
121      * @return Value of userAgent.
122      */
123     public String getUserAgent() {
124         return this.userAgent;
125     }
126 
127     /**
128      * Set the value of userAgent.
129      * 
130      * @param v Value to assign to userAgent.
131      */
132     public void setUserAgent(final String v) {
133         this.userAgent = v;
134     }
135 
136     /**
137      * Get the value of userName.
138      * 
139      * @return Value of userName.
140      */
141     public String getUserName() {
142         return this.userName;
143     }
144 
145     /**
146      * Set the value of userName.
147      * 
148      * @param v Value to assign to userName.
149      */
150     public void setUserName(final String v) {
151         this.userName = v;
152     }
153 
154     /**
155      * Get the value of password.
156      * 
157      * @return Value of password.
158      */
159     public String getPassword() {
160         return this.password;
161     }
162 
163     /**
164      * Set the value of password.
165      * 
166      * @param v Value to assign to password.
167      */
168     public void setPassword(final String v) {
169         this.password = v;
170     }
171 
172     /**
173      * Get the value of theMethod.
174      * 
175      * @return Value of theMethod.
176      */
177     public int getTheMethod() {
178         return this.theMethod;
179     }
180 
181     /**
182      * Get the value of props.
183      * 
184      * @return Value of props.
185      */
186     @Nullable
187     public List<UrlRequestProperty> getProps() {
188         return this.props == null ? null : new ArrayList<>(this.props);
189     }
190 
191     public int getNumberOfProperties() {
192         return (this.props == null) ? 0 : this.props.size();
193     }
194 
195     /**
196      * Set the value of props.
197      * 
198      * @param v Value to assign to props.
199      */
200     public void setProps(final List<UrlRequestProperty> v) {
201         this.props = new ArrayList<>(v);
202     }
203 
204     /**
205      * Add one more property to this request
206      * 
207      * @param v the new UrlRequestProperty to add
208      */
209     public void addProp(@Nullable final UrlRequestProperty v) {
210 
211         if (v == null) {
212             return;
213         }
214 
215         if (this.props == null) {
216             this.props = new ArrayList<>();
217         }
218 
219         this.props.add(v);
220     }
221 
222     /**
223      * Add a bunch more properties to this request
224      * 
225      * @param v array of new UrlRequestProperty to add
226      */
227     public void addProps(@Nullable final List<UrlRequestProperty> v) {
228         if (v == null) {
229             return;
230         }
231         if (v.isEmpty()) {
232             return;
233         }
234 
235         if (this.props == null) {
236             this.props = new ArrayList<>();
237         }
238 
239         this.props.addAll(v);
240     }
241 
242     /**
243      * Set the value of theMethod.
244      * 
245      * @param v Value to assign to theMethod.
246      */
247     public void setTheMethod(final int v) {
248         this.theMethod = v;
249     }
250 
251     /**
252      * As a helper to the toString methods
253      */
254     private StringBuilder toStringBuilder(final boolean headers) {
255         final StringBuilder sb = new StringBuilder();
256         sb.append(Url.theMethodString[this.theMethod]).append(": ").append(this.theUrl).append(" ").append(this.responseCode).append("/")
257                 .append(this.theContent.length);
258         if (headers && (this.props != null)) {
259             for (UrlRequestProperty prop : this.props) {
260                 sb.append(prop.toString());
261             }
262         }
263         sb.append("\n\n").append(new String(this.theContent)).append("\n");
264         return sb;
265     }
266 
267     @Override
268     public String toString() {
269         return this.toStringBuilder(false).toString();
270     }
271 
272     public String toString(final boolean headers) {
273         return this.toStringBuilder(headers).toString();
274     }
275 }