Skip to content

Commit

Permalink
The JVM core updated to include more common components.
Browse files Browse the repository at this point in the history
  • Loading branch information
radekg committed Apr 9, 2015
1 parent 7454cff commit 787c463
Show file tree
Hide file tree
Showing 14 changed files with 580 additions and 1 deletion.
30 changes: 29 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

<groupId>com.gossiperl</groupId>
<artifactId>gossiperl-core</artifactId>
<version>1.0.0</version>
<version>2.0.0</version>

<packaging>jar</packaging>

<properties>
<thrift.version>0.9.2</thrift.version>
<maven.compiler.version>3.0</maven.compiler.version>
<maven.surefire.version>2.7</maven.surefire.version>
<junit.version>4.12</junit.version>
<slf4j-api.version>1.7.7</slf4j-api.version>
<log4j.version>1.2.16</log4j.version>
</properties>

<build>
Expand Down Expand Up @@ -48,6 +51,31 @@
<artifactId>libthrift</artifactId>
<version>${thrift.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j-api.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>

</project>
7 changes: 7 additions & 0 deletions src/main/java/com/gossiperl/client/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gossiperl.client;

public class Util {
public static long getTimestamp() {
return (long)System.currentTimeMillis()/1000;
}
}
58 changes: 58 additions & 0 deletions src/main/java/com/gossiperl/client/encryption/Aes256.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.gossiperl.client.encryption;

import org.apache.log4j.Logger;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.security.*;

public class Aes256 {

private SecretKeySpec key;
private static Logger LOG = Logger.getLogger(Aes256.class);

public Aes256(String key) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] digestBytes = md.digest(key.getBytes("utf-8"));
this.key = new SecretKeySpec(digestBytes, "AES");
}

public byte[] encrypt(byte[] data) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
byte[] ivBytes = generateIv();
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, this.key, new IvParameterSpec(ivBytes));
try {
byte[] encrypted = cipher.doFinal(data);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(ivBytes);
outputStream.write(encrypted);
return outputStream.toByteArray();
} catch (Exception ex) {
return null;
}
}

public byte[] decrypt(byte[] data) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
byte[] ivBytes = new byte[16];
byte[] message = new byte[ data.length - 16 ];
System.arraycopy(data, 0, ivBytes, 0, ivBytes.length);
System.arraycopy(data, ivBytes.length, message, 0, message.length);
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, this.key, new IvParameterSpec(ivBytes));
return cipher.doFinal(message);
}

private byte[] generateIv() {
SecureRandom random = new SecureRandom();
byte[] ivBytes = new byte[16];
random.nextBytes(ivBytes);
return ivBytes;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gossiperl.client.exceptions;

public class GossiperlClientException extends Exception {
public GossiperlClientException() {}
public GossiperlClientException(String message) { super(message); }
public GossiperlClientException(Throwable cause) { super(cause); }
public GossiperlClientException(String message, Throwable cause) { super(message, cause); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.gossiperl.client.exceptions;

public class GossiperlUnsupportedSerializableTypeException extends Exception {
public GossiperlUnsupportedSerializableTypeException(String message) { super(message); }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.gossiperl.client.serialization;

import com.gossiperl.client.exceptions.GossiperlClientException;
import org.apache.thrift.protocol.TType;

public class CustomDigestField {

private String fieldName;
private Object value;
private byte type;
private short fieldOrder;

public CustomDigestField(String fieldName, String value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = value;
this.type = TType.STRING;
}

public CustomDigestField(String fieldName, boolean value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Boolean(value);
this.type = TType.BOOL;
}

public CustomDigestField(String fieldName, byte value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Byte(value);
this.type = TType.BYTE;
}

public CustomDigestField(String fieldName, double value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Double(value);
this.type = TType.DOUBLE;
}

public CustomDigestField(String fieldName, short value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Short(value);
this.type = TType.I16;
}

public CustomDigestField(String fieldName, int value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Integer(value);
this.type = TType.I32;
}

public CustomDigestField(String fieldName, long value, int fieldOrder)
throws GossiperlClientException {
if ( fieldOrder < 0 || fieldOrder > Short.MAX_VALUE ) {
throw new GossiperlClientException("Field ID must be at least 0 and no greater than " + Short.MAX_VALUE + ".");
}
this.fieldName = fieldName;
this.fieldOrder = (short)fieldOrder;
this.value = new Long(value);
this.type = TType.I64;
}

public String getFieldName() {
return fieldName;
}

public Object getValue() {
return value;
}

public byte getType() {
return type;
}

public short getFieldOrder() {
return fieldOrder;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gossiperl.client.serialization;

/**
* Created by rad on 18/12/14.
*/
public class DeserializeKillPill extends DeserializeResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.gossiperl.client.serialization;

public abstract class DeserializeResult {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gossiperl.client.serialization;

import java.util.Map;

public class DeserializeResultCustomOK extends DeserializeResult {
private String digestType;
private Map<String, Object> resultData;
public DeserializeResultCustomOK(String digestType, Map<String, Object> resultData) {
this.digestType = digestType;
this.resultData = resultData;
}
public String getDigestType() {
return this.digestType;
}
public Map<String, Object> getResultData() {
return this.resultData;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.gossiperl.client.serialization;

import com.gossiperl.client.exceptions.GossiperlClientException;

public class DeserializeResultError extends DeserializeResult {
private GossiperlClientException cause;
public DeserializeResultError(GossiperlClientException ex) {
this.cause = ex;
}
public GossiperlClientException getCause() {
return this.cause;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.gossiperl.client.serialization;

import com.gossiperl.client.thrift.DigestEnvelope;
import org.apache.thrift.TBase;

public class DeserializeResultForward extends DeserializeResult {

private String digestType;
private byte[] binaryEnvelope;
private String envelopeId;

public DeserializeResultForward(String digestType, byte[] binaryEnvelope, String envelopeId) {
this.digestType = digestType;
this.binaryEnvelope = binaryEnvelope;
this.envelopeId = envelopeId;
}

public String getDigestType() {
return digestType;
}

public byte[] getBinaryEnvelope() {
return binaryEnvelope;
}

public String getEnvelopeId() {
return envelopeId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gossiperl.client.serialization;

import org.apache.thrift.TBase;

public class DeserializeResultOK extends DeserializeResult {
private String digestType;
private TBase digest;
public DeserializeResultOK(String digestType, TBase digest) {
this.digestType = digestType;
this.digest = digest;
}
public String getDigestType() {
return this.digestType;
}
public TBase getDigest() {
return this.digest;
}
}
Loading

0 comments on commit 787c463

Please sign in to comment.