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

import com.sun.identity.saml2.assertion.Issuer;
import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.saml2.ecp.ECPRequest;
import com.sun.identity.saml2.protocol.IDPList;

public class ECPRequestImpl implements ECPRequest {
   private static final String REQUEST = "Request";
   private Issuer issuer;
   private IDPList idpList;
   private Boolean mustUnderstand;
   private String actor;
   private String providerName;
   private Boolean isPassive;
   private boolean isMutable;

   @Override
   public Issuer getIssuer() {
      return this.issuer;
   }

   @Override
   public void setIssuer(Issuer issuer) throws SAML2Exception {
      if (this.isMutable) {
         this.issuer = issuer;
      } else {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }
   }

   @Override
   public IDPList getIDPList() {
      return this.idpList;
   }

   @Override
   public void setIDPList(IDPList idpList) throws SAML2Exception {
      if (this.isMutable) {
         this.idpList = idpList;
      } else {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }
   }

   @Override
   public Boolean isMustUnderstand() {
      return this.mustUnderstand;
   }

   @Override
   public void setMustUnderstand(Boolean mustUnderstand) throws SAML2Exception {
      if (!this.isMutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.mustUnderstand = mustUnderstand;
   }

   @Override
   public String getActor() {
      return this.actor;
   }

   @Override
   public void setActor(String actor) throws SAML2Exception {
      if (!this.isMutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.actor = actor;
   }

   @Override
   public String getProviderName() {
      return this.providerName;
   }

   @Override
   public void setProviderName(String providerName) throws SAML2Exception {
      if (!this.isMutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.providerName = providerName;
   }

   @Override
   public Boolean isPassive() {
      return this.isPassive;
   }

   @Override
   public void setIsPassive(Boolean isPassive) throws SAML2Exception {
      if (!this.isMutable) {
         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("objectImmutable"));
      }

      this.isPassive = isPassive;
   }

   @Override
   public String toXMLString() throws SAML2Exception {
      return this.toXMLString(true, false);
   }

   @Override
   public String toXMLString(boolean includeNSPrefix, boolean declareNS) throws SAML2Exception {
      this.validateData();
      StringBuffer xml = new StringBuffer(300);
      xml.append("<");
      if (includeNSPrefix) {
         xml.append("ecp:");
      }

      xml.append("Request");
      if (declareNS) {
         xml.append(" ")
            .append("xmlns:ecp=\"urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp\"")
            .append(" ")
            .append("xmlns:soap-env=\"http://schemas.xmlsoap.org/soap/envelope/\"");
      }

      xml.append(" ")
         .append("soap-env:")
         .append("mustUnderstand")
         .append("=")
         .append("\"")
         .append(this.mustUnderstand.toString())
         .append("\"")
         .append(" ")
         .append("soap-env:")
         .append("actor")
         .append("=")
         .append("\"")
         .append(this.actor)
         .append("\"");
      if (this.providerName != null) {
         xml.append(" ").append("ProviderName").append("=").append("\"").append(this.providerName).append("\"");
      }

      if (this.isPassive != null) {
         xml.append(" ").append("IsPassive").append("=").append("\"").append(this.isPassive).append("\"");
      }

      xml.append(">").append("\n").append(this.issuer.toXMLString(includeNSPrefix, declareNS));
      if (this.idpList != null) {
         xml.append("\n").append(this.idpList.toXMLString(includeNSPrefix, declareNS));
      }

      xml.append("\n").append("</ecp:").append("Request").append(">");
      return xml.toString();
   }

   @Override
   public void makeImmutable() {
      if (this.isMutable) {
         this.isMutable = false;
      }
   }

   @Override
   public boolean isMutable() {
      return this.isMutable;
   }

   protected void validateData() throws SAML2Exception {
      if (this.mustUnderstand == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("ECPRequestImpl.validateData: mustUnderstand is missing in the ecp:Request");
         }

         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missingMustUnderstandECPRequest"));
      } else if (this.actor == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("ECPRequestImpl.validateData: actor is missing in the ecp:Request");
         }

         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missingActorECPRequest"));
      } else if (this.issuer == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("ECPRequestImpl.validateData: Issuer is missing in the ecp:Request");
         }

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