1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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);
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 }