Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] fix NullPointException in DecodeUtil #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,16 @@
<artifactId>protobuf-java-format</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.66</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/iservice/sdk/core/operators/BaseDataOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package iservice.sdk.core.operators;

import iservice.sdk.entity.ctx.DataChainContext;

/**
* @author : ori
* @date : 2020/9/28 4:51 下午
*/
public abstract class BaseDataOperator {

BaseDataOperator next;

BaseDataOperator(BaseDataOperator next) {
this.next = next;
}

/**
* operation to do when sending message
*
* @param context
*/
public abstract void doSend(DataChainContext context);

/**
* operation to do when receiving message
*
* @param context
* @return
*/
public abstract void doReceive(DataChainContext context);
}
66 changes: 66 additions & 0 deletions src/main/java/iservice/sdk/core/operators/EncodeOperator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package iservice.sdk.core.operators;

import iservice.sdk.core.operators.factory.EncodeStrategyFactory;
import iservice.sdk.core.operators.factory.strategy.encode.EncodeStrategy;
import iservice.sdk.entity.Header;
import iservice.sdk.entity.ctx.DataChainContext;

import java.io.IOException;
import java.util.Base64;

/**
* @author : ori
* @date : 2020/9/28 5:15 下午
*/
public class EncodeOperator extends BaseDataOperator{

EncodeOperator(BaseDataOperator next) {
super(next);
}

@Override
public void doSend(DataChainContext context) {
EncodeStrategy strategy = getEncodeStrategy(context);
try {
context.setObj(strategy.encode(context.getObj()));
} catch (IOException e) {
e.printStackTrace();
}
if(next!= null) {
next.doSend(context);
}
}

@Override
public void doReceive(DataChainContext context) {
// make next doReceive first
if (next!= null) {
next.doReceive(context);
}
EncodeStrategy strategy = getEncodeStrategy(context);
try {
context.setObj(strategy.decode(context.getObj()));
} catch (Exception e) {
e.printStackTrace();
}
}

private EncodeStrategy getEncodeStrategy(DataChainContext context) {
return EncodeStrategyFactory.getInstance().getEncodeStrategy(context.getHeader().getEncoding());
}

public static void main(String[] args) {
DataChainContext ctx = new DataChainContext();
Header header = new Header();
header.setEncoding("gzip");
ctx.setHeader(header);
ctx.setObj("asdfaf".getBytes());
EncodeOperator operator = new EncodeOperator(null);
operator.doSend(ctx);
String s = new String(Base64.getEncoder().encode(ctx.getObj()));
System.out.println(s);
ctx.setObj(Base64.getDecoder().decode(s));
operator.doReceive(ctx);
System.out.println(new String(ctx.getObj()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package iservice.sdk.core.operators;

/**
* @author : ori
* @date : 2020/9/28 5:09 下午
*/
public class HeaderProtocolOperator {
private BaseDataOperator[] baseDataOperators;

public void doSendOperation(){

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package iservice.sdk.core.operators.factory;

import iservice.sdk.core.operators.factory.strategy.encode.EncodeStrategy;
import iservice.sdk.core.operators.factory.strategy.encode.GzipEncodeStrategy;

import java.util.HashMap;
import java.util.Map;

/**
* @author : ori
* @date : 2020/9/28 5:39 下午
*/
public class EncodeStrategyFactory {

private EncodeStrategyFactory(){
strategyMap.put(EncodeType.GZIP.getName(), new GzipEncodeStrategy());
}

private static class InstanceHolder {
private static final EncodeStrategyFactory INSTANCE = new EncodeStrategyFactory();
}

public static EncodeStrategyFactory getInstance(){
return InstanceHolder.INSTANCE;
}

private final Map<String, EncodeStrategy> strategyMap = new HashMap<>();

public EncodeStrategy getEncodeStrategy(String encodeStrategyName){
return strategyMap.get(encodeStrategyName);
}

enum EncodeType {
GZIP("gzip"),
COMPRESS("compress")
;
private final String name;

EncodeType(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package iservice.sdk.core.operators.factory.strategy.encode;

import java.io.IOException;

/**
* @author : ori
* @date : 2020/9/28 5:45 下午
*/
public abstract class EncodeStrategy {

public abstract byte[] encode(byte[] bytes) throws IOException;

public abstract byte[] decode(byte[] b) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package iservice.sdk.core.operators.factory.strategy.encode;

import iservice.sdk.util.GzipUtils;

import java.io.IOException;

/**
* @author : ori
* @date : 2020/9/28 6:43 下午
*/
public class GzipEncodeStrategy extends EncodeStrategy {
@Override
public byte[] encode(byte[] bytes) throws IOException {
return GzipUtils.gzip(bytes);
}

@Override
public byte[] decode(byte[] bytes) throws Exception {
return GzipUtils.unGzip(bytes);
}
}
71 changes: 65 additions & 6 deletions src/main/java/iservice/sdk/entity/Header.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package iservice.sdk.entity;

import iservice.sdk.enums.EncryptionType;

/**
* @author Yelong
*/
public class Header {
private String version = "1.0";
private Location location = new Location();

private String encoding;
private Encryption encryption;

public Header() {
}

Expand All @@ -26,18 +31,72 @@ public void setLocation(Location location) {
this.location = location;
}

public class Location {
private String status = "off";
public String getEncoding() {
return encoding;
}

public void setEncoding(String encoding) {
this.encoding = encoding;
}

public Encryption getEncryption() {
return encryption;
}

public void setEncryption(Encryption encryption) {
this.encryption = encryption;
}

public static class Location {
private String url;
private String hash;

public Location() {
}

public String getStatus() {
return status;
public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getHash() {
return hash;
}

public void setHash(String hash) {
this.hash = hash;
}

@Override
public String toString() {
return "Location{" +
"url='" + url + '\'' +
", hash='" + hash + '\'' +
'}';
}
}

public static class Encryption {
private EncryptionType encryptionType;
private String publicKey;

public EncryptionType getEncryptionType() {
return encryptionType;
}

public void setEncryptionType(EncryptionType encryptionType) {
this.encryptionType = encryptionType;
}

public String getPublicKey() {
return publicKey;
}

public void setStatus(String status) {
this.status = status;
public void setPublicKey(String publicKey) {
this.publicKey = publicKey;
}
}
}
38 changes: 38 additions & 0 deletions src/main/java/iservice/sdk/entity/ctx/DataChainContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package iservice.sdk.entity.ctx;

import iservice.sdk.entity.Header;

import java.util.Arrays;

/**
* @author : ori
* @date : 2020/9/29 10:48 上午
*/
public class DataChainContext {
Header header;
byte[] obj;

public Header getHeader() {
return header;
}

public void setHeader(Header header) {
this.header = header;
}

public byte[] getObj() {
return obj;
}

public void setObj(byte[] obj) {
this.obj = obj;
}

@Override
public String toString() {
return "DataChainContext{" +
"header=" + header +
", obj=" + Arrays.toString(obj) +
'}';
}
}
9 changes: 9 additions & 0 deletions src/main/java/iservice/sdk/enums/EncryptionType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package iservice.sdk.enums;

/**
* @author : ori
* @date : 2020/9/28 5:26 下午
*/
public enum EncryptionType {

}
Loading