View Javadoc

    1   /*
    2    * Licensed to the University Corporation for Advanced Internet Development,
    3    * Inc. (UCAID) under one or more contributor license agreements.  See the
    4    * NOTICE file distributed with this work for additional information regarding
    5    * copyright ownership. The UCAID licenses this file to You under the Apache
    6    * License, Version 2.0 (the "License"); you may not use this file except in
    7    * compliance with the License.  You may obtain a copy of the License at
    8    *
    9    *    http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package edu.internet2.middleware.shibboleth.idp.profile;
    19
    20  import java.io.File;
    21  import java.io.OutputStreamWriter;
    22
    23  import javax.servlet.http.HttpServletRequest;
    24  import javax.servlet.http.HttpServletResponse;
    25
    26  import org.opensaml.Configuration;
    27  import org.opensaml.saml2.metadata.provider.FilesystemMetadataProvider;
    28  import org.opensaml.ws.transport.InTransport;
    29  import org.opensaml.ws.transport.OutTransport;
    30  import org.opensaml.ws.transport.http.HttpServletRequestAdapter;
    31  import org.opensaml.ws.transport.http.HttpServletResponseAdapter;
    32  import org.opensaml.xml.XMLObject;
    33  import org.opensaml.xml.io.Marshaller;
    34  import org.opensaml.xml.parse.ParserPool;
    35  import org.opensaml.xml.util.DatatypeHelper;
    36  import org.opensaml.xml.util.XMLHelper;
    37  import org.slf4j.Logger;
    38  import org.slf4j.LoggerFactory;
    39
    40  import edu.internet2.middleware.shibboleth.common.profile.ProfileException;
    41  import edu.internet2.middleware.shibboleth.common.profile.provider.AbstractRequestURIMappedProfileHandler;
    42
    43  /**
    44   * A simple profile handler that serves up the IdP's metadata. Eventually this handler should auto generate the metadata
    45   * but, for now, it just provides information from a static file.
    46   */
    47  public class SAMLMetadataProfileHandler extends AbstractRequestURIMappedProfileHandler {
    48
    49      /** Class logger. */
    50      private final Logger log = LoggerFactory.getLogger(SAMLMetadataProfileHandler.class);
    51
    52      /** Metadata provider. */
    53      private FilesystemMetadataProvider metadataProvider;
    54
    55      /**
    56       * Constructor.
    57       *
    58       * @param metadataFile the IdPs metadata file
    59       * @param pool pool of XML parsers used to parse the metadata
    60       */
    61      public SAMLMetadataProfileHandler(String metadataFile, ParserPool pool) {
    62          try {
    63              metadataProvider = new FilesystemMetadataProvider(new File(metadataFile));
    64              metadataProvider.setParserPool(pool);
    65              metadataProvider.setRequireValidMetadata(false);
    66              metadataProvider.initialize();
    67          } catch (Exception e) {
    68              log.error("Unable to read metadata file " + metadataFile, e);
    69          }
    70      }
    71
    72      /** {@inheritDoc} */
    73      public void processRequest(InTransport in, OutTransport out) throws ProfileException {
    74          XMLObject metadata;
    75
    76          HttpServletRequest httpRequest = ((HttpServletRequestAdapter)in).getWrappedRequest();
    77          HttpServletResponse httpResponse = ((HttpServletResponseAdapter)out).getWrappedResponse();
    78
    79          String acceptHeder = DatatypeHelper.safeTrimOrNullString(httpRequest.getHeader("Accept"));
    80          if(acceptHeder != null && !acceptHeder.contains("application/samlmetadata+xml")){
    81              httpResponse.setContentType("application/xml");
    82          }else{
    83              httpResponse.setContentType("application/samlmetadata+xml");
    84          }
    85
    86          try {
    87              String requestedEntity = DatatypeHelper.safeTrimOrNullString(((HttpServletRequestAdapter) in)
    88                      .getParameterValue("entity"));
    89              if (requestedEntity != null) {
    90                  metadata = metadataProvider.getEntityDescriptor(requestedEntity);
    91              } else {
    92                  metadata = metadataProvider.getMetadata();
    93              }
    94
    95              if (metadata != null) {
    96                  Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(metadata);
    97                  XMLHelper.writeNode(marshaller.marshall(metadata), new OutputStreamWriter(out.getOutgoingStream()));
    98              }
    99          } catch (Exception e) {
    100             log.error("Unable to retrieve and return metadata", e);
    101             throw new ProfileException(e);
    102         }
    103     }
    104 }