Commit 37395020 by Facundo Javier Gelatti

Reuse Presenter in Cli, removing duplication

1 parent b15155dd
package org.stegosuite;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import org.stegosuite.ui.cli.CliParser;
import org.stegosuite.ui.gui.Gui;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
public class Stegosuite {
public static void main(String[] args) {
......@@ -15,7 +14,7 @@ public class Stegosuite {
if (args.length == 0) {
new Gui(null);
} else if (args[0].startsWith("-")) {
new CliParser(args);
new CliParser(args).parse();
} else {
new Gui(args[0]);
}
......
......@@ -2,6 +2,10 @@ package org.stegosuite.application;
import org.eclipse.swt.graphics.ImageData;
import org.stegosuite.application.block_processing.BlockProcessor;
import org.stegosuite.application.embedding.Embedding;
import org.stegosuite.application.embedding.EmbeddingDoneListener;
import org.stegosuite.application.embedding.EmbeddingFactory;
import org.stegosuite.application.embedding.ExtractingDoneListener;
import org.stegosuite.image.embedding.EmbeddingMethod;
import org.stegosuite.image.embedding.EmbeddingProgress;
import org.stegosuite.image.embedding.Visualizer;
......@@ -12,10 +16,6 @@ import org.stegosuite.model.exception.SteganoImageException;
import org.stegosuite.model.payload.Payload;
import org.stegosuite.model.payload.block.FileBlock;
import org.stegosuite.model.payload.block.MessageBlock;
import org.stegosuite.application.embedding.Embedding;
import org.stegosuite.application.embedding.EmbeddingDoneListener;
import org.stegosuite.application.embedding.EmbeddingFactory;
import org.stegosuite.application.embedding.ExtractingDoneListener;
import org.stegosuite.util.FileUtils;
import java.io.File;
......@@ -95,10 +95,10 @@ public class StegosuitePresenter implements EmbeddingDoneListener, ExtractingDon
public void extractNotifying(EmbeddingProgress progressListener, String password) {
this.progressListener = progressListener;
extractWith(password);
extractUsing(password);
}
private void extractWith(String password) {
public void extractUsing(String password) {
payload.setPassword(password);
try {
embedding.extract(payload, progressListener, this);
......
package org.stegosuite.ui.cli;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import org.apache.commons.cli.CommandLine;
import org.eclipse.swt.graphics.ImageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.stegosuite.application.StegosuitePresenter;
import org.stegosuite.application.StegosuiteUI;
import org.stegosuite.image.embedding.Visualizer;
import org.stegosuite.image.format.ImageFormat;
import org.stegosuite.model.exception.SteganoEmbedException;
import org.stegosuite.model.exception.SteganoExtractException;
import org.stegosuite.model.exception.SteganoImageException;
import org.stegosuite.model.payload.Payload;
import org.stegosuite.model.payload.block.Block;
import org.stegosuite.model.payload.block.FileBlock;
import org.stegosuite.model.payload.block.MessageBlock;
import org.stegosuite.application.embedding.Embedding;
import org.stegosuite.application.embedding.EmbeddingFactory;
import org.stegosuite.util.FileUtils;
public class Cli {
public class Cli implements StegosuiteUI {
private static final Logger LOG = LoggerFactory.getLogger(Cli.class);
private ImageFormat image;
private StegosuitePresenter presenter;
public void embed(CommandLine cmd) {
String steganogramPath = cmd.getOptionValue("e");
String message = cmd.getOptionValue("m");
String key = cmd.getOptionValue("k");
Payload payload = new Payload();
payload.addBlock(new MessageBlock(message));
payload.setPassword(key);
File steganogramFile = new File(steganogramPath);
String extension = FileUtils.getFileExtension(steganogramPath);
ImageFormat image = ImageFormat.newInstance(extension);
if (image == null) {
String supportedFormats = String.join(", ", ImageFormat.getSupportedFormats());
LOG.error("Error: Currently only these file types are supported: {}", supportedFormats);
return;
}
embed(steganogramPath, message, key);
}
Embedding embedding = EmbeddingFactory.getEmbedding(image);
embedding.setPointFilter(1);
private void embed(String steganogramPath, String message, String key) {
if (!validImageFormat(steganogramPath)) return;
try {
image.load(steganogramFile);
embedding.embed(payload, (embeddingMethod, embeddedImage) -> {
try {
String outputPath = FileUtils.addFileNameSuffix(steganogramFile.getAbsolutePath(), "_embed");
embeddedImage.save(new File(outputPath));
} catch (SteganoImageException e) {
e.printStackTrace();
}
});
} catch (SteganoEmbedException | SteganoImageException e) {
e.printStackTrace();
}
presenter.embed(message, key);
}
public void extract(CommandLine cmd) {
String steganogramPath = cmd.getOptionValue("x");
String key = cmd.getOptionValue("k");
Payload payload = new Payload();
payload.setPassword(key);
extract(steganogramPath, key);
}
private void extract(String steganogramPath, String key) {
if (!validImageFormat(steganogramPath)) return;
String extension = FileUtils.getFileExtension(steganogramPath);
ImageFormat image = ImageFormat.newInstance(extension);
presenter.extractUsing(key);
}
private boolean validImageFormat(String steganogramPath) {
image = getImageFormat(steganogramPath);
if (image == null) {
String supportedFormats = String.join(", ", ImageFormat.getSupportedFormats());
LOG.error("Error: Currently only these file types are supported: {}", supportedFormats);
return;
showFormatNotSupportedError();
return false;
}
presenter = new StegosuitePresenter(image, this);
return true;
}
File file = new File(steganogramPath);
private ImageFormat getImageFormat(String steganogramPath) {
try {
image.load(file);
Embedding embedding = EmbeddingFactory.getEmbedding(image);
embedding.setPointFilter(1);
embedding.extract(payload, (embeddingMethod) -> {
for (Block block : payload.getBlocks()) {
if (block.hasIdentifier(FileBlock.IDENTIFIER)) {
String outPath = FileUtils.changeFileName(steganogramPath, ((FileBlock) block).getFileName());
LOG.debug("Outputpath: {}", outPath);
try {
Files.write(Paths.get(outPath), ((FileBlock) block).getFileContent(),
StandardOpenOption.CREATE);
} catch (IOException e) {
e.printStackTrace();
}
} else if (block.hasIdentifier(MessageBlock.IDENTIFIER)) {
LOG.info("Extracted message:{}", ((MessageBlock) block).getMessage());
}
}
});
} catch (SteganoExtractException | SteganoImageException e) {
return ImageFormat.getImageFormat(steganogramPath);
} catch (SteganoImageException e) {
e.printStackTrace();
return null;
}
}
private void showFormatNotSupportedError() {
LOG.error("Error: Currently only these file types are supported: {}",
supportedFormats());
}
private String supportedFormats() {
return String.join(", ", ImageFormat.getSupportedFormats());
}
@Override
public void showEmbeddingError(SteganoEmbedException e) {
e.printStackTrace();
}
@Override
public void showExtractingError(SteganoExtractException e) {
e.printStackTrace();
}
@Override
public void extractingCompleted(String extractedMessage, String statusMessage, Visualizer visualizer, ImageData imageData) {
LOG.info("Extracted message:{}", extractedMessage);
}
@Override
public void embeddingCompleted(ImageFormat embeddedImage, String outputPath, Visualizer visualizer) {
}
@Override
public void addPayloadFile(String filename, String extension, long fileSize) {
}
}
package org.stegosuite.ui.cli;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import ch.qos.logback.classic.Level;
import org.apache.commons.cli.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
public class CliParser {
private String[] args = null;
......@@ -19,10 +12,9 @@ public class CliParser {
public CliParser(String[] s) {
args = s;
parse();
}
private void parse() {
public void parse() {
CommandLineParser parser = new DefaultParser();
Options options = new Options();
......
package org.stegosuite;
import org.junit.After;
import org.junit.Test;
import org.stegosuite.ui.cli.CliParser;
import java.io.File;
import static org.junit.Assert.assertTrue;
import static org.stegosuite.Resources.pathOf;
public class CliTest {
@After
public void tearDown() throws Exception {
Resources.delete("snow_embed.bmp");
}
@Test
public void testEmbedAndExtract() throws Exception {
// TODO: Remove temporal coupling and separate in two tests
testEmbed();
testExtract();
}
private void testEmbed() {
String imagePath = pathOf("snow.bmp");
String key = "password";
String message = "message";
String[] args = getEmbedCommand(imagePath, key, message);
new CliParser(args).parse();
assertTrue(new File(pathOf("snow_embed.bmp")).exists());
}
private void testExtract() {
String imagePath = pathOf("snow_embed.bmp");
String key = "password";
String[] args = getExtractCommand(imagePath, key);
new CliParser(args).parse();
// TODO: Assert something (Until now, the only side-effect is the logging)
}
private String[] getEmbedCommand(String imagePath, String key, String message) {
String command = String.format("-e %s -k %s -m %s",
imagePath, key, message);
return command.split(" ");
}
private String[] getExtractCommand(String imagePath, String key) {
String command = String.format("-x %s -k %s",
imagePath, key);
return command.split(" ");
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!