KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > crazybob > rss > UrlLoader


1 package org.crazybob.rss;
2
3 import java.io.*;
4 import java.util.*;
5 import java.net.*;
6
7 import org.apache.commons.httpclient.*;
8 import org.apache.commons.httpclient.methods.*;
9
10 /**
11  * Loads content from URL.
12  *
13  * @author Bob Lee (crazybob@crazybob.org)
14  */

15 public class UrlLoader {
16
17     private static final int TIMEOUT = 15000;
18     
19     /**
20      * Loads content from URL.
21      */

22     public Response load(String JavaDoc urlString, String JavaDoc lastModified)
23             throws IOException {
24         HttpClient client = new HttpClient();
25         client.setConnectionTimeout(TIMEOUT);
26         client.setTimeout(TIMEOUT);
27         GetMethod getMethod = new GetMethod(urlString);
28         if (lastModified != null)
29             getMethod.setRequestHeader(
30                 new Header("If-Modified-Since", lastModified));
31         client.executeMethod(getMethod);
32         if (getMethod.getStatusCode() == 304) {
33             Log.debug("Not modified. Skipping.");
34             return null;
35         }
36
37         Response response = new Response();
38         response.setBody(getMethod.getResponseBodyAsString());
39         Header lastModifiedHeader =
40             getMethod.getResponseHeader("Last-Modified");
41         if (lastModifiedHeader != null)
42             response.setLastModified(lastModifiedHeader.getValue());
43         return response;
44     }
45
46     public static class Response {
47         
48         private String JavaDoc body;
49         private String JavaDoc lastModified;
50
51         /**
52          * Get body.
53          *
54          * @return body as String.
55          */

56         public String JavaDoc getBody() {
57             return body;
58         }
59         
60         /**
61          * Set body.
62          *
63          * @param body the value to set.
64          */

65         public void setBody(String JavaDoc body) {
66             this.body = body;
67         }
68         
69         /**
70          * Get lastModified.
71          *
72          * @return lastModified as String.
73          */

74         public String JavaDoc getLastModified() {
75             return lastModified;
76         }
77         
78         /**
79          * Set lastModified.
80          *
81          * @param lastModified the value to set.
82          */

83         public void setLastModified(String JavaDoc lastModified) {
84             this.lastModified = lastModified;
85         }
86     }
87 }
88
Popular Tags