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.HeaderElement;
24 import org.apache.commons.httpclient.HttpClient;
25 import org.apache.commons.httpclient.HttpStatus;
26 import org.apache.commons.httpclient.methods.GetMethod;
27 import org.apache.commons.httpclient.methods.HeadMethod;
28 import org.apache.commons.httpclient.util.DateParseException;
29 import org.apache.commons.httpclient.util.DateUtil;
30 import org.joda.time.DateTime;
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 public HttpResource(String resource, ResourceFilter resourceFilter) {
67 super(resourceFilter);
68
69 resourceUrl = DatatypeHelper.safeTrimOrNullString(resource);
70 if (resourceUrl == null) {
71 throw new IllegalArgumentException("Resource URL may not be null or empty");
72 }
73
74 httpClient = new HttpClient();
75 }
76
77
78 public boolean exists() throws ResourceException {
79 HeadMethod headMethod = new HeadMethod(resourceUrl);
80
81 try {
82 httpClient.executeMethod(headMethod);
83 if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
84 return false;
85 }
86
87 return true;
88 } catch (IOException e) {
89 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
90 }
91 }
92
93
94 public InputStream getInputStream() throws ResourceException {
95 GetMethod getMethod = getResource();
96 try {
97 InputStream ins = getMethod.getResponseBodyAsStream();
98 if (getResourceFilter() != null) {
99 return getResourceFilter().applyFilter(ins);
100 } else {
101 return ins;
102 }
103 } catch (IOException e) {
104 throw new ResourceException("Unable to read response", e);
105 }
106 }
107
108
109 public DateTime getLastModifiedTime() throws ResourceException {
110 HeadMethod headMethod = new HeadMethod(resourceUrl);
111
112 try {
113 httpClient.executeMethod(headMethod);
114 if (headMethod.getStatusCode() != HttpStatus.SC_OK) {
115 throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
116 + ", received HTTP status code " + headMethod.getStatusCode());
117 }
118 Header lastModifiedHeader = headMethod.getResponseHeader("Last-Modified");
119 if (lastModifiedHeader != null && ! DatatypeHelper.isEmpty(lastModifiedHeader.getValue())) {
120 long lastModifiedTime = DateUtil.parseDate(lastModifiedHeader.getValue()).getTime();
121 return new DateTime(lastModifiedTime);
122 }
123
124 return new DateTime();
125 } catch (IOException e) {
126 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
127 } catch (DateParseException e) {
128 throw new ResourceException("Unable to parse last modified date for resource:" + resourceUrl, e);
129 }
130 }
131
132
133 public String getLocation() {
134 return resourceUrl;
135 }
136
137
138 public String toString() {
139 return getLocation();
140 }
141
142
143 public int hashCode() {
144 return getLocation().hashCode();
145 }
146
147
148 public boolean equals(Object o) {
149 if (o == this) {
150 return true;
151 }
152
153 if (o instanceof HttpResource) {
154 return getLocation().equals(((HttpResource) o).getLocation());
155 }
156
157 return false;
158 }
159
160
161
162
163
164
165
166
167 protected GetMethod getResource() throws ResourceException {
168 GetMethod getMethod = new GetMethod(resourceUrl);
169
170 try {
171 httpClient.executeMethod(getMethod);
172 if (getMethod.getStatusCode() != HttpStatus.SC_OK) {
173 throw new ResourceException("Unable to retrieve resource URL " + resourceUrl
174 + ", received HTTP status code " + getMethod.getStatusCode());
175 }
176 return getMethod;
177 } catch (IOException e) {
178 throw new ResourceException("Unable to contact resource URL: " + resourceUrl, e);
179 }
180 }
181 }