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