Add pipeline stages
diff --git a/src/main/java/com/intel/jndn/utils/server/PipelineStage.java b/src/main/java/com/intel/jndn/utils/server/PipelineStage.java
new file mode 100644
index 0000000..15d23f0
--- /dev/null
+++ b/src/main/java/com/intel/jndn/utils/server/PipelineStage.java
@@ -0,0 +1,24 @@
+/*
+ * jndn-utils
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.utils.server;
+
+/**
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public interface PipelineStage<T, Y> {
+ public Y process(T context) throws Exception;
+// public void setNextStage(PipelineStage<Y, ?> nextStage);
+// public PipelineStage<Y, ?> getNextStage();
+}
diff --git a/src/main/java/com/intel/jndn/utils/server/pipeline/CompressionStage.java b/src/main/java/com/intel/jndn/utils/server/pipeline/CompressionStage.java
new file mode 100644
index 0000000..687bde0
--- /dev/null
+++ b/src/main/java/com/intel/jndn/utils/server/pipeline/CompressionStage.java
@@ -0,0 +1,49 @@
+/*
+ * jndn-utils
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.utils.server.pipeline;
+
+import com.intel.jndn.utils.server.PipelineStage;
+import java.io.ByteArrayOutputStream;
+import java.util.zip.GZIPOutputStream;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.util.Blob;
+
+/**
+ * Sample stage for compressing {@link Data} content using GZIP
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class CompressionStage implements PipelineStage<Data, Data> {
+
+ /**
+ * Compress and replace the {@link Data} content. Note: this stage will return
+ * the same {@link Data} instance and will modify only its content.
+ *
+ * @param context the {@link Data} packet
+ * @return the same packet but with GZIP-compressed content
+ * @throws Exception if compression fails
+ */
+ @Override
+ public Data process(Data context) throws Exception {
+ ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+ try (GZIPOutputStream stream = new GZIPOutputStream(buffer)) {
+ stream.write(context.getContent().getImmutableArray(), 0, context.getContent().size());
+ stream.close();
+ }
+
+ context.setContent(new Blob(buffer.toByteArray()));
+ return context;
+ }
+
+}
diff --git a/src/main/java/com/intel/jndn/utils/server/pipeline/SigningStage.java b/src/main/java/com/intel/jndn/utils/server/pipeline/SigningStage.java
new file mode 100644
index 0000000..5296d89
--- /dev/null
+++ b/src/main/java/com/intel/jndn/utils/server/pipeline/SigningStage.java
@@ -0,0 +1,68 @@
+/*
+ * jndn-utils
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.utils.server.pipeline;
+
+import com.intel.jndn.utils.server.PipelineStage;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.security.KeyChain;
+import net.named_data.jndn.security.SecurityException;
+
+/**
+ * As a part of a {@link com.intel.jndn.utils.ServerIn} pipeline, this stage
+ * will sign a {@link Data} packet.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class SigningStage implements PipelineStage<Data, Data> {
+
+ private final KeyChain keyChain;
+ private final Name certificateName;
+
+ /**
+ * Default constructor.
+ *
+ * @param keyChain the {@link KeyChain} to use for signing
+ * @param certificateName the certificate to sign with
+ */
+ public SigningStage(KeyChain keyChain, Name certificateName) {
+ this.keyChain = keyChain;
+ this.certificateName = certificateName;
+ }
+
+ /**
+ * Build the stage using the default certificate name defined on the
+ * {@link KeyChain}.
+ *
+ * @param keyChain the {@link KeyChain} to use for signing
+ * @throws SecurityException if no default certificate is found
+ */
+ public SigningStage(KeyChain keyChain) throws SecurityException {
+ this.keyChain = keyChain;
+ this.certificateName = keyChain.getDefaultCertificateName();
+ }
+
+ /**
+ * Sign a {@link Data} packet.
+ *
+ * @param context the data packet to sign
+ * @return the signed data packet
+ * @throws Exception if signing fails
+ */
+ @Override
+ public Data process(Data context) throws Exception {
+ keyChain.sign(context, certificateName);
+ return context;
+ }
+}
diff --git a/src/test/java/com/intel/jndn/utils/server/pipeline/CompressionStageTest.java b/src/test/java/com/intel/jndn/utils/server/pipeline/CompressionStageTest.java
new file mode 100644
index 0000000..12fcd28
--- /dev/null
+++ b/src/test/java/com/intel/jndn/utils/server/pipeline/CompressionStageTest.java
@@ -0,0 +1,51 @@
+/*
+ * jndn-utils
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.utils.server.pipeline;
+
+import com.intel.jndn.utils.SegmentedClient;
+import java.util.logging.Logger;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.util.Blob;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Test {@link CompressionStage}.
+ *
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class CompressionStageTest {
+
+ private static final Logger logger = Logger.getLogger(SegmentedClient.class.getName());
+
+ /**
+ * Test of process method, of class CompressionStage.
+ */
+ @Test
+ public void testProcess() throws Exception {
+ Data data = new Data(new Name("/test/packet"));
+ data.setContent(new Blob(".............................................."));
+ int originalSize = data.getContent().size();
+ logger.info("Uncompressed size: " + originalSize);
+ logger.info("Uncompressed: " + data.getContent().toString());
+
+ CompressionStage instance = new CompressionStage();
+ Data result = instance.process(data);
+ logger.info("Compressed size: " + result.getContent().size());
+ logger.info("Compressed: " + result.getContent().toString());
+
+ assertTrue(result.getContent().size() < originalSize);
+ }
+}
diff --git a/src/test/java/com/intel/jndn/utils/server/pipeline/SigningStageTest.java b/src/test/java/com/intel/jndn/utils/server/pipeline/SigningStageTest.java
new file mode 100644
index 0000000..4f10ab4
--- /dev/null
+++ b/src/test/java/com/intel/jndn/utils/server/pipeline/SigningStageTest.java
@@ -0,0 +1,61 @@
+/*
+ * jndn-utils
+ * Copyright (c) 2015, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU Lesser General Public License,
+ * version 3, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ */
+package com.intel.jndn.utils.server.pipeline;
+
+import com.intel.jndn.mock.MockKeyChain;
+import net.named_data.jndn.Data;
+import net.named_data.jndn.Name;
+import net.named_data.jndn.security.KeyChain;
+import net.named_data.jndn.security.SecurityException;
+import net.named_data.jndn.util.Blob;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ * Test {@link SigningStage}.
+ * @author Andrew Brown <andrew.brown@intel.com>
+ */
+public class SigningStageTest {
+
+ KeyChain keyChain;
+ SigningStage instance;
+
+ public SigningStageTest() throws SecurityException {
+ keyChain = MockKeyChain.configure(new Name("/test/signer"));
+ instance = new SigningStage(keyChain);
+ }
+
+ /**
+ * Test of process method, of class SigningStage.
+ */
+ @Test
+ public void testProcess() throws Exception {
+ Data data = new Data(new Name("/test/packet"));
+ data.setContent(new Blob("....."));
+ Data result = instance.process(data);
+ assertTrue(result.getSignature().getSignature().size() > 0);
+ }
+
+ /**
+ * Test of process method, of class SigningStage.
+ */
+ @Test(expected = Exception.class)
+ public void testProcessFailure() throws Exception {
+ SigningStage stage = new SigningStage(keyChain, null); // no default certificate set
+ Data data = new Data(new Name("/test/packet"));
+ data.setContent(new Blob("....."));
+ stage.process(data);
+ }
+
+}