View Javadoc

1   /*
2    * Copyright 2008 the original author or authors.
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.springframework.ws.transport.http;
18  
19  import java.io.File;
20  import java.net.URI;
21  import java.net.URISyntaxException;
22  import javax.xml.transform.Source;
23  import javax.xml.transform.dom.DOMSource;
24  
25  import org.w3c.dom.Document;
26  
27  import org.springframework.util.StringUtils;
28  import org.springframework.xml.transform.TraxUtils;
29  
30  /**
31   * Utility class that determines the last modified date of a given {@link Source}.
32   *
33   * @author Arjen Poutsma
34   * @since 1.5.3
35   */
36  class LastModifiedHelper {
37  
38      private LastModifiedHelper() {
39      }
40  
41      /**
42       * Returns the last modified date of the given {@link Source}.
43       *
44       * @param source the source
45       * @return the last modified date, as a long
46       */
47      static long getLastModified(Source source) {
48          if (source instanceof DOMSource) {
49              Document document = TraxUtils.getDocument((DOMSource) source);
50              return document != null ? getLastModified(document.getDocumentURI()) : -1;
51          }
52          else {
53              return getLastModified(source.getSystemId());
54          }
55      }
56  
57      private static long getLastModified(String systemId) {
58          if (StringUtils.hasText(systemId)) {
59              try {
60                  URI systemIdUri = new URI(systemId);
61                  if ("file".equals(systemIdUri.getScheme())) {
62                      File documentFile = new File(systemIdUri);
63                      if (documentFile.exists()) {
64                          return documentFile.lastModified();
65                      }
66                  }
67              }
68              catch (URISyntaxException e) {
69                  // ignore
70              }
71          }
72          return -1;
73      }
74  
75  }