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