<commit_before>// Copyright (C) 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.saml.server;

import org.opensaml.Configuration;
import org.opensaml.common.SAMLObject;
import org.opensaml.saml2.core.ArtifactResponse;
import org.opensaml.xml.io.Marshaller;
import org.opensaml.xml.io.MarshallerFactory;
import org.w3c.dom.Element;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handler for SAML ArtifactResolve requests.
 */
public class SamlArtifactResolve extends HttpServlet {

  private static final Logger LOGGER =
      Logger.getLogger(SamlArtifactResolve.class.getName());

  private BackEnd backend;

  public SamlArtifactResolve() {
    this(BackEndImpl.getInstance());
  }

  /**
   * Available for testing.
   * @param backend
   */
  public SamlArtifactResolve(BackEnd backend) {
    super();
    this.backend = backend;
  }

  /**
   * For testing.
   */
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
  }

  protected void handlePost(HttpServletRequest request,
                     HttpServletResponse response)
      throws IOException {

    ArtifactResponse artifactResp = backend.resolveArtifact(null);

    SOAPMessage soapMsg = soapify(artifactResp);

    if (null == soapMsg) {
      LOGGER.severe("Error in constructing SOAP message, no response will be"
                    + "sent");
      return;
    }
    
    try {
      soapMsg.writeTo(response.getOutputStream());
    } catch (SOAPException e) {
      LOGGER.log(Level.SEVERE, "Error writing out SOAP message.", e);
    }

  }

  /**
   * For a given SAMLObject, generate a SOAP message that envelopes the
   * SAMLObject.
   *
   * @param samlObject a SAMLObject
   * @return a SOAPMessage, or null on failure
   */<commit_msg>Bugfix in SamlArtifactResolve to serve POST requests.<commit_after>// Copyright (C) 2008 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.enterprise.saml.server;

import org.opensaml.Configuration;
import org.opensaml.common.SAMLObject;
import org.opensaml.saml2.core.ArtifactResponse;
import org.opensaml.xml.io.Marshaller;
import org.opensaml.xml.io.MarshallerFactory;
import org.w3c.dom.Element;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Handler for SAML ArtifactResolve requests.
 */
public class SamlArtifactResolve extends HttpServlet {

  private static final Logger LOGGER =
      Logger.getLogger(SamlArtifactResolve.class.getName());

  private BackEnd backend;

  public SamlArtifactResolve() {
    this(BackEndImpl.getInstance());
  }

  /**
   * Available for testing.
   * @param backend
   */
  public SamlArtifactResolve(BackEnd backend) {
    super();
    this.backend = backend;
  }

  /**
   * For testing.
   */
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }

  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    handlePost(request, response);
  }

  protected void handlePost(HttpServletRequest request,
                     HttpServletResponse response)
      throws IOException {

    ArtifactResponse artifactResp = backend.resolveArtifact(null);

    SOAPMessage soapMsg = soapify(artifactResp);

    if (null == soapMsg) {
      LOGGER.severe("Error in constructing SOAP message, no response will be"
                    + "sent");
      return;
    }
    
    try {
      soapMsg.writeTo(response.getOutputStream());
    } catch (SOAPException e) {
      LOGGER.log(Level.SEVERE, "Error writing out SOAP message.", e);
    }

  }

  /**
   * For a given SAMLObject, generate a SOAP message that envelopes the
   * SAMLObject.
   *
   * @param samlObject a SAMLObject
   * @return a SOAPMessage, or null on failure
   */
{'fraction_non_alphanumeric': 0.09131403118040089, 'fraction_numerical': 0.0034264176803152305, 'mean_word_length': 3.330860534124629, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 10}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3961990', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1605, 'n_words': 480}