package com.google.enterprise.saml.server;

import com.google.enterprise.connector.manager.Manager;
import com.google.enterprise.sessionmanager.SessionManagerInterface;
import com.google.enterprise.saml.common.GsaConstants;

import java.util.logging.Logger;
import java.util.List;
import java.net.URLEncoder;
import java.io.UnsupportedEncodingException;

import org.opensaml.saml2.core.ArtifactResponse;
import org.opensaml.saml2.core.ArtifactResolve;
import org.opensaml.saml2.core.Response;
import org.opensaml.saml2.core.AuthzDecisionQuery;

/**
 * The implementation of the BackEnd interface for the Security Manager.
 *
 * At present, this implementation is very shallow and returns canned responses
 * for almost everything.
 */
public class BackEndImpl implements BackEnd {
  private static final Logger LOGGER =
      Logger.getLogger(BackEndImpl.class.getName());

  private SessionManagerInterface sm;
  private ArtifactResolver artifactResolver;
  private AuthzResponder authzResponder;
  private ArtifactStore artifacts;

  public BackEndImpl() {
    artifacts = new ArtifactStore();
  }
  public void setSessionManager(SessionManagerInterface sm) {
    this.sm = sm;
  }

  public SessionManagerInterface getSessionManager() {
    return sm;
  }

  public void setArtifactResolver(ArtifactResolver artifactResolver) {
    this.artifactResolver = artifactResolver;
  }

  public void setAuthzResponder(AuthzResponder authzResponder) {
    this.authzResponder = authzResponder;
  }

  public String loginRedirect(String referer, String relayState, String subject) {
    String urlEncodedRelayState = "";
    try {
      urlEncodedRelayState = URLEncoder.encode(relayState, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      LOGGER.warning("Could not encode RelayState");
    }

    String artifact = "foo";
    artifacts.put(artifact, subject);
    String redirectUrl = referer + GsaConstants.GSA_ARTIFACT_HANDLER_NAME
        + "?" + GsaConstants.GSA_ARTIFACT_PARAM_NAME + "=" + artifact
        + "&" + GsaConstants.GSA_RELAY_STATE_PARAM_NAME + "=" + urlEncodedRelayState;

    LOGGER.info("Referer: " + referer);
    LOGGER.info("RelayState: " + relayState);
    LOGGER.info("URLEncoded RelayState: " + urlEncodedRelayState);
    LOGGER.info("Redirect URL: " + redirectUrl);

    return redirectUrl;
  }

  public ArtifactResponse resolveArtifact(ArtifactResolve artifactResolve) {
    return artifactResolver.resolve(artifactResolve, artifacts);
  }

  public List<Response> authorize(List<AuthzDecisionQuery> authzDecisionQueries) {
    return authzResponder.authorizeBatch(authzDecisionQueries);
  }

  public Manager getConnectorManager() {
    // TODO Auto-generated method stub
    return null;
  }
}

@@ -1,172 +1,641 @@
-package com.google.enterprise.saml.server;%0A%0Aimport com.google.enterprise.connector.manager.Manager;%0Aimport com.google.enterprise.sessionmanager.SessionManagerInterface;
+// Copyright (C) 2008 Google Inc.%0A//%0A// Licensed under the Apache License, Version 2.0 (the %22License%22);%0A// you may not use this file except in compliance with the License.%0A// You may obtain a copy of the License at%0A//%0A//      http://www.apache.org/licenses/LICENSE-2.0%0A//%0A// Unless required by applicable law or agreed to in writing, software%0A// distributed under the License is distributed on an %22AS IS%22 BASIS,%0A// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A// See the License for the specific language governing permissions and%0A// limitations under the License.%0A%0Apackage com.google.enterprise.saml.server;%0A
 %0Aimp
@@ -690,326 +690,395 @@
 ts;%0A
-%0A
 import 
-java.util.logging.Logger;%0Aimport java.util.List;%0Aimport java.net.URLEncoder;%0Aimport java.io.UnsupportedEncodingException;%0A%0Aimport org.opensaml.saml2.core.ArtifactResponse;%0Aimport org.opensaml.saml2.core.ArtifactResolve;%0Aimport org.opensaml.saml2.core.Response;%0Aimport org.opensaml.saml2.core.AuthzDecisionQu
+com.google.enterprise.sessionmanager.SessionManagerInterface;%0A%0Aimport org.opensaml.saml2.core.ArtifactResolve;%0Aimport org.opensaml.saml2.core.ArtifactResponse;%0Aimport org.opensaml.saml2.core.AuthzDecisionQuery;%0Aimport org.opensaml.saml2.core.Response;%0A%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLEncoder;%0Aimport java.util.List;%0Aimport java.util.logging.Logg
 er
-y
 ;%0A%0A/
@@ -3163,109 +3163,7 @@
 %0A  %7D
-%0A%0A  public Manager getConnectorManager() %7B%0A    // TODO Auto-generated method stub%0A    return null;%0A  %7D
 %0A%7D%0A

Remove unused getConnectorManager method. Add copyright/license boilerplate.