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 implements Resource {
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          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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
112     public String getLocation() {
113         return resourceUrl;
114     }
115 
116     /** {@inheritDoc} */
117     public String toString() {
118         return getLocation();
119     }
120 
121     /** {@inheritDoc} */
122     public int hashCode() {
123         return getLocation().hashCode();
124     }
125 
126     /** {@inheritDoc} */
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      * Gets remote resource.
141      * 
142      * @return the remove resource
143      * 
144      * @throws ResourceException thrown if the resource could not be fetched
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 }