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.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   * A resource representing a file retrieved from a URL using Apache Commons HTTPClient.
34   */
35  public class HttpResource extends AbstractFilteredResource {
36  
37      /** HTTP URL of the resource. */
38      private String resourceUrl;
39  
40      /** HTTP client. */
41      private HttpClient httpClient;
42  
43      /**
44       * Constructor.
45       * 
46       * @param resource HTTP(S) URL of the resource
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       * Constructor.
61       * 
62       * @param resource HTTP(S) URL of the resource
63       * @param resourceFilter filter to apply to this resource
64       * 
65       * @deprecated use {@link #setResourceFilter(ResourceFilter)} instead
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      /** {@inheritDoc} */
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      /** {@inheritDoc} */
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     /** {@inheritDoc} */
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     /** {@inheritDoc} */
129     public String getLocation() {
130         return resourceUrl;
131     }
132 
133     /** {@inheritDoc} */
134     public String toString() {
135         return getLocation();
136     }
137 
138     /** {@inheritDoc} */
139     public int hashCode() {
140         return getLocation().hashCode();
141     }
142 
143     /** {@inheritDoc} */
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      * Gets remote resource.
158      * 
159      * @return the remove resource
160      * 
161      * @throws ResourceException thrown if the resource could not be fetched
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 }