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