package com.sun.identity.saml2.protocol.impl;

import com.sun.identity.saml2.assertion.impl.EncryptedElementImpl;
import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.saml2.protocol.NewEncryptedID;
import com.sun.identity.saml2.protocol.NewID;
import com.sun.identity.saml2.protocol.ProtocolFactory;
import com.sun.identity.saml2.xmlenc.EncManager;
import com.sun.identity.shared.xml.XMLUtils;
import java.security.Key;
import org.w3c.dom.Element;

public class NewEncryptedIDImpl extends EncryptedElementImpl implements NewEncryptedID {
   private static final String ELEMENT_NAME = "NewEncryptedID";

   private void parseElement(Element element) throws SAML2Exception {
      if (element == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("NewEncryptedIDImpl.parseElement: Input is null.");
         }

         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("nullInput"));
      } else {
         String tag = element.getLocalName();
         if (tag == null || !tag.equals("NewEncryptedID")) {
            if (SAML2SDKUtils.DEBUG.messageEnabled()) {
               SAML2SDKUtils.DEBUG.message("NewEncryptedIDImpl.parseElement: not EncryptedIDImpl.");
            }

            throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("wrongInput"));
         }
      }
   }

   public NewEncryptedIDImpl(Element element) throws SAML2Exception {
      this.parseElement(element);
      this.xmlString = XMLUtils.print(element);
   }

   @Override
   public NewID decrypt(Key recipientPrivateKey) throws SAML2Exception {
      Element el = EncManager.getEncInstance().decrypt(this.xmlString, recipientPrivateKey);
      return ProtocolFactory.getInstance().createNewID(el);
   }
}
