View Javadoc

1   /*
2    * Copyright [2006] [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.saml2.metadata.provider;
18  
19  import org.opensaml.xml.XMLObject;
20  import org.opensaml.xml.io.Unmarshaller;
21  import org.opensaml.xml.io.UnmarshallingException;
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.w3c.dom.Element;
25  
26  /**
27   * A <code>MetadataProvider</code> implementation that retrieves metadata from a DOM <code>Element</code> as
28   * supplied by the user.
29   * 
30   * It is the responsibility of the caller to re-initialize, via {@link #initialize()}, if any properties of this
31   * provider are changed.
32   */
33  public class DOMMetadataProvider extends AbstractObservableMetadataProvider implements MetadataProvider {
34  
35      /** Class logger. */
36      private final Logger log = LoggerFactory.getLogger(DOMMetadataProvider.class);
37  
38      /** Root metadata element exposed by this provider. */
39      private Element metadataElement;
40  
41      /** Unmarshalled metadata. */
42      private XMLObject metadata;
43  
44      /**
45       * Constructor.
46       * 
47       * @param mdElement the metadata element
48       */
49      public DOMMetadataProvider(Element mdElement) {
50          super();
51          metadataElement = mdElement;
52      }
53  
54      /** {@inheritDoc} */
55      public XMLObject getMetadata() {
56          return metadata;
57      }
58  
59      /**
60       * Initializes the provider and prepares it for use.
61       * 
62       * @throws MetadataProviderException thrown if the metadata element provided can not be read or is not valid
63       *             metadata
64       */
65      public synchronized void initialize() throws MetadataProviderException {
66          refreshMetadata();
67      }
68  
69      /**
70       * Reads the metadata element and re-applies the registered metadata filter.
71       * 
72       * @throws MetadataProviderException thrown if the metadata element provided can not be read or is not valid
73       *             metadata
74       */
75      private synchronized void refreshMetadata() throws MetadataProviderException {
76          try {
77              Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller(metadataElement);
78              XMLObject metadataTemp = unmarshaller.unmarshall(metadataElement);
79              filterMetadata(metadataTemp);
80              releaseMetadataDOM(metadataTemp);
81              metadata = metadataTemp;
82              emitChangeEvent();
83          } catch (UnmarshallingException e) {
84              String errorMsg = "Unable to unmarshall metadata element";
85              log.error(errorMsg, e);
86              throw new MetadataProviderException(errorMsg, e);
87          } catch (FilterException e) {
88              String errorMsg = "Unable to filter metadata";
89              log.error(errorMsg, e);
90              throw new MetadataProviderException(errorMsg, e);
91          }
92      }
93  }