<commit_before>package com.identityblitz.login.authn.cmd

import com.identityblitz.login.transport.{OutboundTransport, InboundTransport}
import com.identityblitz.json.{Json, JVal}
import com.identityblitz.login.LoggingUtils._
import com.identityblitz.login.error.CommandException
import org.apache.commons.codec.binary.Base64

/**
  */
trait Command {

  def name: String

  def execute(implicit iTr: InboundTransport, oTr: OutboundTransport): Either[CommandException, Option[Command]]

  def saveState: JVal

  final def asString(): String =
    Base64.encodeBase64String(Json.obj("name" -> name, "state" -> saveState).toJson.getBytes("UTF-8"))

}

object Command {

  private val cmdMap = Map[String, (JVal => Command)](
    BindCommand.COMMAND_NAME -> {(state: JVal) => BindCommand(state)}
  )

  def apply[T <: Command](cmdStr: String): T = {
    val jval = JVal.parseStr(new String(Base64.decodeBase64(cmdStr), "UTF-8"))
    (jval \ "name").asOpt[String].fold[T]({
      val err = s"Deserialization of the command [$cmdStr] failed: the name attribute is not found"
      logger.error(err)
      throw new IllegalArgumentException(err)
    })(name => cmdMap.get(name).map(_(jval \ "state")).getOrElse({
      val err = s"Deserialization of the command [$cmdStr] failed: unknown command`s name [$name]"
      logger.error(err)
      throw new IllegalArgumentException(err)
    }).asInstanceOf)
  }

}<commit_msg>move to command pattern (partial commit)<commit_after>package com.identityblitz.login.authn.cmd

import com.identityblitz.login.transport.{OutboundTransport, InboundTransport}
import com.identityblitz.json.{Json, JVal}
import com.identityblitz.login.LoggingUtils._
import com.identityblitz.login.error.CommandException
import org.apache.commons.codec.binary.Base64

/**
  */
trait Command {

  def name: String

  def execute(implicit iTr: InboundTransport, oTr: OutboundTransport): Either[CommandException, Option[Command]]

  def saveState: JVal

  final def asString(): String =
    Base64.encodeBase64String(Json.obj("name" -> name, "state" -> saveState).toJson.getBytes("UTF-8"))

}

object Command {

  private val cmdMap = Map[String, (JVal => Command)](
    BindCommand.COMMAND_NAME -> {(state: JVal) => BindCommand(state)}
  )

  def apply[T <: Command](cmdStr: String): T = {
    val jval = JVal.parseStr(new String(Base64.decodeBase64(cmdStr), "UTF-8"))
    (jval \ "name").asOpt[String].fold[T]({
      val err = s"Deserialization of the command [$cmdStr] failed: the name attribute is not found"
      logger.error(err)
      throw new IllegalArgumentException(err)
    })(name => cmdMap.get(name).map(_(jval \ "state")).getOrElse({
      val err = s"Deserialization of the command [$cmdStr] failed: unknown command`s name [$name]"
      logger.error(err)
      throw new IllegalArgumentException(err)
    }).asInstanceOf[T])
  }

}
{'fraction_non_alphanumeric': 0.13496503496503495, 'fraction_numerical': 0.008391608391608392, 'mean_word_length': 4.43916349809886, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226745', 'n_tokens_mistral': 936, 'n_tokens_neox': 897, 'n_words': 205}