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