package PACKAGENAME; // <- CHANGE THIS TO THE PACKAGE WHERE THE CLASS WILL BE ADDED


import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.InetAddress;

// Java class for implementing basic authentication with the plugtest portal.
// for loading this class, add in your java code shown below in this comments area, 
// substituting the string value of the argument with the complete name
// of the class once it has been incorporated in one of your packages. 
//
// Class.forName("PACKAGENAME.EtsiTestAuthenticator"); // <- CHANGE THIS TO THE COMPLETE NAME OF THE CLASS
//
//

public class EtsiTestAuthenticator
extends Authenticator {

  static {
   // Install the custom authenticator
   Authenticator.setDefault(new EtsiTestAuthenticator());   
  }

  //
  // This method is called when a password-protected URL is accessed
  protected PasswordAuthentication getPasswordAuthentication() {
    // Get information about the request
    String promptString = getRequestingPrompt();
    InetAddress ipaddr = getRequestingSite();
    
    // String hostname = getRequestingHost();
    String hostname = ipaddr.getHostName();
         
    int port = getRequestingPort();

    if (hostname.equalsIgnoreCase("xades-portal.etsi.org")) {
      String username = "xxxx"; // <-- CHANGE THIS TO YOUR LOGIN NAME
      String password = "yyy"; // <-- CHANGE THIS TO YOUR PASSWORD
      return new PasswordAuthentication(username, password.toCharArray());
    } else {
      return null;
    }
  }

  private static String uname;
  public static void setUname(String un){
    uname = un;
  }
  
  private static String pwd;
  public static void setPwd(String apwd){
    pwd = apwd;
  }
}