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