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