1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
72 BasicParserPool ppMgr = new BasicParserPool( );
73 ppMgr.setNamespaceAware( true );
74
75 try
76 {
77
78 Document inCommonMDDoc = ppMgr.parse( stream );
79 Element metadataRoot = inCommonMDDoc.getDocumentElement( );
80
81
82 UnmarshallerFactory unmarshallerFactory = Configuration.getUnmarshallerFactory( );
83 Unmarshaller unmarshaller = unmarshallerFactory.getUnmarshaller( metadataRoot );
84
85
86
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
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
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 }