View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.common.content;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.net.URL;
24  import java.net.URLConnection;
25  
26  import javax.xml.transform.Source;
27  import javax.xml.transform.stream.StreamSource;
28  
29  import org.apache.commons.io.FilenameUtils;
30  import org.xml.sax.InputSource;
31  
32  /**
33   * @author Thomas Scheffler (yagee)
34   *
35   */
36  public class MCRURLContent extends MCRContent {
37  
38      private URL url;
39  
40      public MCRURLContent(URL url) {
41          super();
42          this.url = url;
43          this.setSystemId(url.toString());
44          String fileName = url.getPath();
45          if (fileName.endsWith("/")) {
46              fileName = FilenameUtils.getPathNoEndSeparator(fileName); //removes final '/';
47          }
48          setName(FilenameUtils.getName(fileName));
49      }
50  
51      @Override
52      public InputStream getInputStream() throws IOException {
53          return url.openStream();
54      }
55  
56      @Override
57      public Source getSource() throws IOException {
58          return new StreamSource(getSystemId());
59      }
60  
61      @Override
62      public InputSource getInputSource() throws IOException {
63          return new InputSource(getSystemId());
64      }
65  
66      @Override
67      public long length() throws IOException {
68          return url.openConnection().getContentLengthLong();
69      }
70  
71      @Override
72      public long lastModified() throws IOException {
73          return url.openConnection().getLastModified();
74      }
75  
76      @Override
77      public String getETag() throws IOException {
78          URLConnection openConnection = url.openConnection();
79          openConnection.connect();
80          String eTag = openConnection.getHeaderField("ETag");
81          if (eTag != null) {
82              return eTag;
83          }
84          long lastModified = openConnection.getLastModified();
85          long length = openConnection.getContentLengthLong();
86          eTag = getSimpleWeakETag(url.toString(), length, lastModified);
87          return eTag == null ? null : eTag.substring(2);
88      }
89  
90      @Override
91      public String getMimeType() throws IOException {
92          return super.getMimeType() == null ? url.openConnection().getContentType() : super.getMimeType();
93      }
94  
95  }