1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.util.resource;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21
22 import org.apache.commons.httpclient.Header;
23 import org.apache.commons.httpclient.HttpClient;
24 import org.apache.commons.httpclient.HttpStatus;
25 import org.apache.commons.httpclient.methods.GetMethod;
26 import org.apache.commons.httpclient.methods.HeadMethod;
27 import org.apache.commons.httpclient.util.DateParseException;
28 import org.apache.commons.httpclient.util.DateUtil;
29 import org.joda.time.DateTime;
30 import org.joda.time.chrono.ISOChronology;
31 import org.opensaml.xml.util.DatatypeHelper;
32
33
34
35
36 public class HttpResource extends AbstractFilteredResource {
37
38
39 private String resourceUrl;
40
41
42 private HttpClient httpClient;
43
44
45
46
47
48
49 public HttpResource(String resource) {
50 super();
51
52 resourceUrl = DatatypeHelper.safeTrimOrNullString(resource);
53 if (resourceUrl == null) {
54 throw new IllegalArgumentException("Resource URL may not be null or empty");
55 }
56
57 httpClient = new HttpClient();
58 }
59
60
61
62
63
64
65
66
67
68 public HttpResource(String resource, ResourceFilter resourceFilter) {
69 super(resourceFilter);
70
71 resourceUrl = DatatypeHelper.safeTrimOrNullString(resource);
72 if (resourceUrl == null) {
73 throw new IllegalArgumentException("Resource URL may not be null or empty");
74 }
75
76 httpClient = new HttpClient();
77 }
78
79
80 public boolean exists() throws ResourceException {
81 HeadMethod headMethod = new HeadMethod(resourceUrl);
82
83 try {
84 httpClient.executeMethod(headMethod);
85 if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
86 return false;
87 }
88
89 return true;
90 } catch (IOException e) {
91 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
92 }
93 }
94
95
96 public InputStream getInputStream() throws ResourceException {
97 GetMethod getMethod = getResource();
98 try {
99 return applyFilter(getMethod.getResponseBodyAsStream());
100 } catch (IOException e) {
101 throw new ResourceException("Unable to read response", e);
102 }
103 }
104
105
106 public DateTime getLastModifiedTime() throws ResourceException {
107 HeadMethod headMethod = new HeadMethod(resourceUrl);
108
109 try {
110 httpClient.executeMethod(headMethod);
111 if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
112 throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
113 + ", received HTTP status code " + headMethod.getStatusCode());
114 }
115 Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified");
116 if (lastModifiedHeader != null && ! DatatypeHelper.isEmpty(lastModifiedHeader.getValue())) {
117 long lastModifiedTime = DateUtil.parseDate(lastModifiedHeader.getValue()).getTime();
118 return new DateTime(lastModifiedTime, ISOChronology.getInstanceUTC());
119 }
120
121 return new DateTime();
122 } catch (IOException e) {
123 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
124 } catch (DateParseException e) {
125 throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e);
126 }
127 }
128
129
130 public String getLocation() {
131 return resourceUrl;
132 }
133
134
135 public String toString() {
136 return getLocation();
137 }
138
139
140 public int hashCode() {
141 return getLocation().hashCode();
142 }
143
144
145 public boolean equals(Object o) {
146 if (o == this) {
147 return true;
148 }
149
150 if (o instanceof HttpResource) {
151 return getLocation().equals(((HttpResource) o).getLocation());
152 }
153
154 return false;
155 }
156
157
158
159
160
161
162
163
164 protected GetMethod getResource() throws ResourceException {
165 GetMethod getMethod = new GetMethod(resourceUrl);
166
167 try {
168 httpClient.executeMethod(getMethod);
169 if (getMethod.getStatusCode() != HttpStatus.SC_OK) {
170 throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
171 + ", received HTTP status code " + getMethod.getStatusCode());
172 }
173 return getMethod;
174 } catch (IOException e) {
175 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
176 }
177 }
178 }