Files
CipherStreamAPI/src/main/java/com/mattrixwv/cipherstream/controller/monosubstitution/CaesarCipherController.java
Matthew Ellison fcc04b3416 Updated CORS
2024-04-15 16:56:59 -04:00

78 lines
3.1 KiB
Java

package com.mattrixwv.cipherstream.controller.monosubstitution;
import org.slf4j.MDC;
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.fasterxml.jackson.databind.node.ObjectNode;
import com.mattrixwv.cipherstream.aspect.CipherStreamLoggingAspect;
import com.mattrixwv.cipherstream.monosubstitution.Caesar;
import com.mattrixwv.cipherstream.utils.CipherInfoUtil;
import com.mattrixwv.cipherstream.utils.CipherParameterUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@RequestMapping("/cipherStream/caesar")
public class CaesarCipherController{
@GetMapping
public ObjectNode getCipherInfo(){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.CAESAR_CIPHER_NAME);
log.info("Getting info for {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
return CipherInfoUtil.buildInfoNode(CipherInfoUtil.CAESAR_CIPHER_NAME, CipherInfoUtil.CAESAR_CIPHER_DESCRIPTION);
}
@PostMapping("/encode")
public ObjectNode encodeCaesar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.CAESAR_CIPHER_NAME);
log.info("Encoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
CipherParameterUtil.verifyCaesarParams(cipherParams);
boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean();
boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean();
boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean();
int shiftAmount = cipherParams.get(CipherParameterUtil.CAESAR_SHIFT_AMOUNT).asInt();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Caesar caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = caesar.encode(shiftAmount, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
@PostMapping("/decode")
public ObjectNode decodeCaesar(@RequestBody ObjectNode cipherParams){
MDC.put(CipherStreamLoggingAspect.CIPHER_NAME_LOGGING, CipherInfoUtil.CAESAR_CIPHER_NAME);
log.info("Decoding {}", CipherInfoUtil.CAESAR_CIPHER_NAME);
CipherParameterUtil.verifyCaesarParams(cipherParams);
boolean preserveCapitals = cipherParams.get(CipherParameterUtil.PRESERVE_CAPITALS).asBoolean();
boolean preserveWhitespace = cipherParams.get(CipherParameterUtil.PRESERVE_WHITESPACE).asBoolean();
boolean preserveSymbols = cipherParams.get(CipherParameterUtil.PRESERVE_SYMBOLS).asBoolean();
int shiftAmount = cipherParams.get(CipherParameterUtil.CAESAR_SHIFT_AMOUNT).asInt();
String inputString = cipherParams.get(CipherParameterUtil.INPUT_STRING).asText();
Caesar caesar = new Caesar(preserveCapitals, preserveWhitespace, preserveSymbols);
String outputString = caesar.decode(shiftAmount, inputString);
cipherParams.put(CipherParameterUtil.OUTPUT_STRING, outputString);
return cipherParams;
}
}