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

import com.sun.identity.saml2.common.SAML2Exception;
import com.sun.identity.saml2.common.SAML2SDKUtils;
import com.sun.identity.saml2.ecp.ECPResponse;

public class ECPResponseImpl implements ECPResponse {
   private static final String RESPONSE = "Response";
   private Boolean mustUnderstand;
   private String actor;
   private String assertionConsumerServiceURL;
   private boolean isMutable = true;

   @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 getAssertionConsumerServiceURL() {
      return this.assertionConsumerServiceURL;
   }

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

      this.assertionConsumerServiceURL = url;
   }

   @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("Response");
      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("\"")
         .append(" ")
         .append("AssertionConsumerServiceURL")
         .append("=")
         .append("\"")
         .append(this.assertionConsumerServiceURL)
         .append("\"")
         .append(">")
         .append("</ecp:")
         .append("Response")
         .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.assertionConsumerServiceURL == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("ECPResponseImpl.validateData: AssertionConsumerServiceURL is missing in the ecp:Response");
         }

         throw new SAML2Exception(SAML2SDKUtils.BUNDLE.getString("missingAssertionConsumerServiceURLECPResponse"));
      } else if (this.mustUnderstand == null) {
         if (SAML2SDKUtils.DEBUG.messageEnabled()) {
            SAML2SDKUtils.DEBUG.message("ECPResponseImpl.validateData: mustUnderstand is missing in the ecp:Response");
         }

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

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