View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.mylutece.modules.saml.authentication.metadata;
35  
36  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.Constants;
37  import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLParsingException;
38  import fr.paris.lutece.portal.service.util.AppException;
39  
40  import org.apache.log4j.Logger;
41  
42  import org.opensaml.Configuration;
43  
44  import org.opensaml.saml2.metadata.EntityDescriptor;
45  import org.opensaml.saml2.metadata.validator.EntityDescriptorSchemaValidator;
46  
47  import org.opensaml.xml.io.Unmarshaller;
48  import org.opensaml.xml.io.UnmarshallerFactory;
49  import org.opensaml.xml.io.UnmarshallingException;
50  import org.opensaml.xml.parse.BasicParserPool;
51  import org.opensaml.xml.parse.XMLParserException;
52  import org.opensaml.xml.validation.ValidationException;
53  
54  import org.w3c.dom.Document;
55  import org.w3c.dom.Element;
56  
57  import java.io.InputStream;
58  
59  
60  public abstract class MetadataManager implements Constants
61  {
62      private static Logger _logger = Logger.getLogger( MetadataManager.class );
63      protected EntityDescriptor metaData = null;
64  
65      public MetadataManager(  )
66      {
67      }
68  
69      protected void loadMetadata( InputStream stream )
70      {
71          // Get parser pool manager
72          BasicParserPool ppMgr = new BasicParserPool(  );
73          ppMgr.setNamespaceAware( true );
74  
75          try
76          {
77              // Parse metadata file
78              Document inCommonMDDoc = ppMgr.parse( stream );
79              Element metadataRoot = inCommonMDDoc.getDocumentElement(  );
80  
81              // Get apropriate unmarshaller
82              UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory(  );
83              Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller( metadataRoot );
84  
85              // Unmarshall using the document root element, an EntitiesDescriptor
86              // in this case
87              metaData = (EntityDescriptor) unmarshaller.unmarshall( metadataRoot );
88          }
89          catch ( XMLParserException xe )
90          {
91              String message = "Erreur de parsing des metadonn�es " + xe.getLocalizedMessage(  );
92              _logger.warn( message );
93              throw new AppException( message, xe );
94          }
95          catch ( UnmarshallingException ue )
96          {
97              String message = "Erreur d'unmarshalling des metadonn�es " + ue.getLocalizedMessage(  );
98              _logger.warn( message );
99              throw new AppException( message, ue );
100         }
101 
102         // Validation Sch�ma
103         try
104         {
105             EntityDescriptorSchemaValidator schemaValidator = new EntityDescriptorSchemaValidator(  );
106             schemaValidator.validate( metaData );
107         }
108         catch ( ValidationException e )
109         {
110             String message = "Erreur de validation des metadonn�es " + e.getLocalizedMessage(  );
111             _logger.warn( message );
112             throw new AppException( message, e );
113         }
114 
115         // Validation contenu "Metier"
116         try
117         {
118             this.validateContent(  );
119         }
120         catch ( SAMLParsingException e )
121         {
122             String message = "Erreur de validation des metadonn�es " + e.getLocalizedMessage(  );
123             _logger.warn( message );
124             throw new AppException( message, e );
125         }
126     }
127 
128     public abstract void loadMetadata( String inFilePath );
129 
130     protected abstract void validateContent(  ) throws SAMLParsingException;
131 
132     public EntityDescriptor getMetaData(  )
133     {
134         return metaData;
135     }
136 }