package com.mattrixwv.cipherstream.controller.monosubstitution; import java.util.List; import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.PropertySource; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect; import com.mattrixwv.cipherstream.monosubstitution.Baconian; import com.mattrixwv.cipherstream.utils.CipherInfoUtil; import com.mattrixwv.cipherstream.utils.CipherParameterUtil; import lombok.extern.slf4j.Slf4j; import tools.jackson.databind.node.ObjectNode; @Slf4j @RestController @RequestMapping("/baconian") @PropertySource("classpath:ciphers.properties") public class BaconianCipherController{ @Value("${cipher.mono.baconian.name}") private String baconianName; @Value("${cipher.mono.baconian.description}") private String baconianDescription; @Value("#{${cipher.mono.baconian.explanation}}") private List baconianExplanation; @Value("#{${cipher.mono.baconian.facts}}") private List baconianFacts; @GetMapping public ObjectNode getCipherInfo(){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baconianName); log.info("Getting info for {}", baconianName); return CipherInfoUtil.buildInfoNode(baconianName, baconianDescription, baconianExplanation, baconianFacts); } @PostMapping("/encode") public ObjectNode encodeBaconian(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baconianName); log.info("Encoding {}", baconianName); CipherParameterUtil.verifyBaconianParams(cipherParams); boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); Baconian baconian = new Baconian(preserveCapitals); String outputString = baconian.encode(inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } @PostMapping("/decode") public ObjectNode decodeBaconian(@RequestBody ObjectNode cipherParams){ MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, baconianName); log.info("Decoding {}", baconianName); CipherParameterUtil.verifyBaconianParams(cipherParams); boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean(); String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asString(); Baconian baconian = new Baconian(preserveCapitals); String outputString = baconian.decode(inputString); cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString); return cipherParams; } }