1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.opensaml.saml2.metadata.provider;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22
23 import org.joda.time.DateTime;
24 import org.opensaml.saml2.common.SAML2Helper;
25 import org.opensaml.xml.XMLObject;
26 import org.opensaml.xml.io.UnmarshallingException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37 public class FilesystemMetadataProvider extends AbstractObservableMetadataProvider {
38
39
40 private final Logger log = LoggerFactory.getLogger(FilesystemMetadataProvider.class);
41
42
43 private File metadataFile;
44
45
46 private boolean maintainExpiredMetadata;
47
48
49 private long lastUpdate;
50
51
52 private XMLObject cachedMetadata;
53
54
55
56
57
58
59
60
61
62 public FilesystemMetadataProvider(File metadata) throws MetadataProviderException {
63 super();
64
65 if (metadata == null) {
66 throw new MetadataProviderException("Give metadata file may not be null");
67 }
68
69 if (!metadata.exists()) {
70 throw new MetadataProviderException("Give metadata file, " + metadata.getAbsolutePath() + " does not exist");
71 }
72
73 if (!metadata.isFile()) {
74 throw new MetadataProviderException("Give metadata file, " + metadata.getAbsolutePath() + " is not a file");
75 }
76
77 if (!metadata.canRead()) {
78 throw new MetadataProviderException("Give metadata file, " + metadata.getAbsolutePath()
79 + " is not readable");
80 }
81
82 metadataFile = metadata;
83 maintainExpiredMetadata = true;
84 lastUpdate = -1;
85 }
86
87
88
89
90
91
92 public void initialize() throws MetadataProviderException {
93 refreshMetadata();
94 }
95
96
97
98
99
100
101 public boolean maintainExpiredMetadata() {
102 return maintainExpiredMetadata;
103 }
104
105
106
107
108
109
110 public void setMaintainExpiredMetadata(boolean maintain) {
111 maintainExpiredMetadata = maintain;
112 }
113
114
115 public XMLObject getMetadata() throws MetadataProviderException {
116 if (lastUpdate < metadataFile.lastModified()) {
117 refreshMetadata();
118 }
119
120 return cachedMetadata;
121 }
122
123
124
125
126
127
128 private synchronized void refreshMetadata() throws MetadataProviderException {
129
130 long metadataFileLastModified = metadataFile.lastModified();
131 if (lastUpdate >= metadataFileLastModified) {
132
133 return;
134 }
135
136 log.debug("Refreshing metadata from file {}", metadataFile);
137 try {
138 XMLObject metadata = unmarshallMetadata(new FileInputStream(metadataFile));
139 DateTime expirationTime = SAML2Helper.getEarliestExpiration(metadata);
140 if (expirationTime != null && !maintainExpiredMetadata() && expirationTime.isBeforeNow()) {
141 log.debug(
142 "Metadata from file {} is expired and provider is configured not to retain expired metadata.",
143 metadataFile);
144 cachedMetadata = null;
145 } else {
146 filterMetadata(metadata);
147 releaseMetadataDOM(metadata);
148 cachedMetadata = metadata;
149 }
150
151
152
153
154
155
156
157 lastUpdate = metadataFileLastModified;
158
159 emitChangeEvent();
160 } catch (FileNotFoundException e) {
161 String errorMsg = "Unable to read metadata file";
162 log.error(errorMsg, e);
163 throw new MetadataProviderException(errorMsg, e);
164 } catch (UnmarshallingException e) {
165 String errorMsg = "Unable to unmarshall metadata";
166 log.error(errorMsg, e);
167 throw new MetadataProviderException(errorMsg, e);
168 } catch (FilterException e) {
169 String errorMsg = "Unable to filter metadata";
170 log.error(errorMsg, e);
171 throw new MetadataProviderException(errorMsg, e);
172 }
173 }
174 }