View Javadoc

1   /*
2    * Copyright 2007 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 edu.internet2.middleware.shibboleth.idp.profile;
18  
19  import java.io.File;
20  import java.io.OutputStreamWriter;
21  
22  import org.opensaml.Configuration;
23  import org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider;
24  import org.opensaml.ws.transport.InTransport;
25  import org.opensaml.ws.transport.OutTransport;
26  import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
27  import org.opensaml.xml.XMLObject;
28  import org.opensaml.xml.io.Marshaller;
29  import org.opensaml.xml.parse.ParserPool;
30  import org.opensaml.xml.util.DatatypeHelper;
31  import org.opensaml.xml.util.XMLHelper;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
36  import edu.internet2.middleware.shibboleth.common.profile.provider.AbstractRequestURIMappedProfileHandler;
37  
38  /**
39   * A simple profile handler that serves up the IdP's metadata. Eventually this handler should auto generate the metadata
40   * but, for now, it just provides information from a static file.
41   */
42  public class SAMLMetadataProfileHandler extends AbstractRequestURIMappedProfileHandler {
43  
44      /** Class logger. */
45      private final Logger log = LoggerFactory.getLogger(SAMLMetadataProfileHandler.class);
46  
47      /** Metadata provider. */
48      private FilesystemMetadataProvider metadataProvider;
49  
50      /**
51       * Constructor.
52       * 
53       * @param metadataFile the IdPs metadata file
54       * @param pool pool of XML parsers used to parse the metadata
55       */
56      public SAMLMetadataProfileHandler(String metadataFile, ParserPool pool) {
57          try {
58              metadataProvider = new FilesystemMetadataProvider(new File(metadataFile));
59              metadataProvider.setParserPool(pool);
60              metadataProvider.setMaintainExpiredMetadata(true);
61              metadataProvider.initialize();
62          } catch (Exception e) {
63              log.error("Unable to read metadata file " + metadataFile, e);
64          }
65      }
66  
67      /** {@inheritDoc} */
68      public void processRequest(InTransport in, OutTransport out) throws ProfileException {
69          XMLObject metadata;
70  
71          try {
72              String requestedEntity = DatatypeHelper.safeTrimOrNullString(((HttpServletRequestAdapter) in)
73                      .getParameterValue("entity"));
74              if (requestedEntity != null) {
75                  metadata = metadataProvider.getEntityDescriptor(requestedEntity);
76              } else {
77                  metadata = metadataProvider.getMetadata();
78              }
79  
80              if (metadata != null) {
81                  Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(metadata);
82                  XMLHelper.writeNode(marshaller.marshall(metadata), new OutputStreamWriter(out.getOutgoingStream()));
83              }
84          } catch (Exception e) {
85              log.error("Unable to retrieve and return metadata", e);
86              throw new ProfileException(e);
87          }
88      }
89  }