View Javadoc

1   /*
2    * Copyright 2007 University Corporation for Advanced Internet Development, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * A resource representing a file retrieved from a URL using Apache Commons HTTPClient.
35   */
36  public class HttpResource extends AbstractFilteredResource {
37  
38      /** HTTP URL of the resource. */
39      private String resourceUrl;
40  
41      /** HTTP client. */
42      private HttpClient httpClient;
43  
44      /**
45       * Constructor.
46       * 
47       * @param resource HTTP(S) URL of the resource
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       * Constructor.
62       * 
63       * @param resource HTTP(S) URL of the resource
64       * @param resourceFilter filter to apply to this resource
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
133     public String getLocation() {
134         return resourceUrl;
135     }
136 
137     /** {@inheritDoc} */
138     public String toString() {
139         return getLocation();
140     }
141 
142     /** {@inheritDoc} */
143     public int hashCode() {
144         return getLocation().hashCode();
145     }
146 
147     /** {@inheritDoc} */
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      * Gets remote resource.
162      * 
163      * @return the remove resource
164      * 
165      * @throws ResourceException thrown if the resource could not be fetched
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 }