package com.sun.identity.saml2.common;

import java.util.StringTokenizer;

public class NameIDInfoKey {
   private static final String DELIM = "|";
   private final String hostEntityId;
   private final String remoteEntityId;
   private final String nameIdValue;

   public NameIDInfoKey(String nameIDValue, String hostEntityID, String remoteEntityID) throws SAML2Exception {
      if (nameIDValue == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullNameIDValue"));
      }

      if (hostEntityID == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullHostEntityID"));
      }

      if (remoteEntityID == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullRemoteEntityID"));
      }

      this.nameIdValue = nameIDValue;
      this.hostEntityId = hostEntityID;
      this.remoteEntityId = remoteEntityID;
   }

   public String getNameIDValue() {
      return this.nameIdValue;
   }

   public String getHostEntityID() {
      return this.hostEntityId;
   }

   public String getRemoteEntityID() {
      return this.remoteEntityId;
   }

   public String toValueString() {
      return this.hostEntityId + "|" + this.remoteEntityId + "|" + this.nameIdValue;
   }

   public static NameIDInfoKey parse(String infoKey) throws SAML2Exception {
      if (infoKey == null) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("nullNameIDInfoKey"));
      }

      StringTokenizer st = new StringTokenizer(infoKey, "|");
      if (st.countTokens() != 3) {
         throw new SAML2Exception(SAML2Utils.BUNDLE.getString("inValidNameIDInfoKey"));
      }

      String hostID = st.nextToken();
      String remoteID = st.nextToken();
      String nameID = st.nextToken();
      return new NameIDInfoKey(nameID, hostID, remoteID);
   }
}
