// SPDX-FileCopyrightText: 2006 Istituto Nazionale di Fisica Nucleare
//
// SPDX-License-Identifier: Apache-2.0

package org.italiangrid.voms.request.impl;

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import org.italiangrid.voms.request.VOMSACRequest;
import org.italiangrid.voms.request.VOMSProtocol;
import org.italiangrid.voms.request.VOMSProtocolError;
import org.italiangrid.voms.request.VOMSProtocolListener;
import org.italiangrid.voms.request.VOMSResponse;
import org.italiangrid.voms.request.VOMSServerInfo;

import eu.emi.security.authn.x509.X509CertChainValidatorExt;
import eu.emi.security.authn.x509.X509Credential;
import eu.emi.security.authn.x509.impl.CertificateUtils;
import eu.emi.security.authn.x509.impl.FormatMode;
import eu.emi.security.authn.x509.impl.HostnameMismatchCallback2;

/**
 * Protocol implementing the legacy interface.
 * 
 * 
 */
public class LegacyProtocol extends AbstractVOMSProtocol implements
  VOMSProtocol, HostnameMismatchCallback2 {

  public LegacyProtocol(X509CertChainValidatorExt validator,
    VOMSProtocolListener listener, int connectTimeout, int readTimeout) {

    super(validator, listener, connectTimeout, readTimeout);
  }

  public synchronized VOMSResponse doRequest(VOMSServerInfo endpoint,
    X509Credential credential, VOMSACRequest request) {

    SSLSocketFactory sslSocketFactory = getSSLSocketFactory(credential);

    SSLSocket sslSocket = null;

    try {

      sslSocket = (SSLSocket) sslSocketFactory.createSocket();
      sslSocket.setSoTimeout(readTimeout);
      sslSocket.setEnabledProtocols(VOMS_LEGACY_ENABLED_PROTOCOLS);

      SocketAddress sa = new InetSocketAddress(endpoint.getURL().getHost(),
        endpoint.getURL().getPort());

      sslSocket.connect(sa, connectTimeout);

    } catch (Throwable t) {

      throw new VOMSProtocolError(t.getMessage(), endpoint, request,
        credential, t);

    }

    LegacyRequestSender protocol = LegacyRequestSender.instance(listener);

    VOMSResponse response = null;

    try {

      protocol.sendRequest(request, endpoint, sslSocket.getOutputStream());

      InputStream inputStream = sslSocket.getInputStream();

      response = new LegacyVOMSResponseParsingStrategy().parse(inputStream);

      sslSocket.close();

    } catch (IOException e) {

      throw new VOMSProtocolError(e.getMessage(), endpoint, request,
        credential, e);
    }

    listener.notifyReceivedResponse(response);
    return response;
  }

  @Override
  public void nameMismatch(X509Certificate peerCertificate, String hostName)
      throws CertificateException {

    String peerCertString = CertificateUtils.format(peerCertificate,
        FormatMode.MEDIUM_ONE_LINE);
      String message = String
        .format(
          "No subject alternative DNS name matching %s found. Peer certificate : %s",
          hostName, peerCertString);
      throw new CertificateException(message);
  }

}
