Commit f0d17541 by TongZuu

init csc-websocket-api

parents
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_75"/>
<classpathentry kind="lib" path="D:/EnterprisePT/Programmer/myHRAppServer/wildfly-10.0.0.tpbs/standalone/deployments/TPBS.ear/hrAppWeb.war/WEB-INF/lib/gson.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>csc-websocket-api</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
/FileFtpFactory.class
/PackageDataFactory.class
package com.csc.library.factory;
import java.io.File;
import com.csc.library.properties.JsonFile;
import com.csc.library.properties.JsonFileImpl;
public class FileFtpFactory {
private static FileFtpFactory instance;
private FileFtpFactory(){
}
public static FileFtpFactory getInstance(){
if(instance == null){
instance = new FileFtpFactory();
}
return instance;
}
public JsonFile createJsonFile(File file) throws Exception{
return new JsonFileImpl(file);
}
public JsonFile createJsonFile(String directory , String fileName , File file) throws Exception{
return new JsonFileImpl(file ,directory , fileName);
}
public JsonFile createJsonFile(String directory , String fileName , byte[] bytes) throws Exception{
return new JsonFileImpl(bytes ,directory , fileName);
}
}
package com.csc.library.factory;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Vector;
import com.csc.library.gson.ArrayDeserializer;
import com.csc.library.gson.ByteBufferDeserialize;
import com.csc.library.gson.SerializeAndDeserializeBase;
import com.csc.library.properties.HandlePackage;
import com.csc.library.properties.JsonFile;
import com.csc.library.properties.JsonFileImpl;
import com.csc.library.request.CommonModeParameter;
import com.csc.library.request.CommonParameterImpl;
import com.csc.library.request.ReqParameterImpl;
import com.csc.library.request.RequestParameter;
import com.csc.library.request.RequestPropertiesImpl;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResDataImpl;
import com.csc.library.response.ResponseData;
import com.csc.library.response.ResponsePropertiesImpl;
import com.csc.library.response.ResponseWebsocket;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PackageDataFactory<Session> {
private static PackageDataFactory instance;
private LinkedList<ArrayList> vList = new LinkedList<ArrayList>();
private Gson gson;
private PackageDataFactory(){
GsonBuilder gb = new GsonBuilder();
gb.registerTypeAdapter(String[].class, new ArrayDeserializer());
gb.registerTypeAdapter(RequestWebsocket.class, new SerializeAndDeserializeBase(RequestPropertiesImpl.class));
gb.registerTypeAdapter(RequestParameter.class, new SerializeAndDeserializeBase(ReqParameterImpl.class));
gb.registerTypeAdapter(CommonModeParameter.class, new SerializeAndDeserializeBase(CommonParameterImpl.class));
gb.registerTypeAdapter(ResponseWebsocket.class, new SerializeAndDeserializeBase(ResponsePropertiesImpl.class));
gb.registerTypeAdapter(ResponseData.class, new SerializeAndDeserializeBase(ResDataImpl.class));
gb.registerTypeAdapter(JsonFile.class, new SerializeAndDeserializeBase(JsonFileImpl.class));
gb.registerTypeAdapter(ByteBuffer.class, new ByteBufferDeserialize());
gson = gb.create();
}
public static PackageDataFactory getInstance(){
if(instance == null){
instance = new PackageDataFactory();
}
return instance;
}
public Gson getGson(){
return this.gson;
}
public void handlePackageData(Session session , HandlePackage handle , String json){
try{
String[] list = gson.fromJson(json , String[].class);
if(handle.isExistsRequest(session , list[0])){
ResponseWebsocket res = gson.fromJson(list[1], ResponseWebsocket.class);
//check complete or error ź͡ҡ list 㹡 tag
handle.onResponse(session , res);
}else{
RequestWebsocket req = gson.fromJson(list[1], RequestWebsocket.class);
//check host server ǹ зͧ server 蹷ӧҹ
handle.onRequest(session , req);
}
}catch(Exception e){
e.printStackTrace();
}
}
public void sendData(Session session , HandlePackage handle , RequestWebsocket req){
if(handle.validateSendRequest(session, req)){
handle.send(session , createJson(req.getRequestId() , req));
}
}
public void sendData(Session session , HandlePackage handle , ResponseWebsocket res){
if(handle.validateSendResponse(session, res)){
handle.send(session , createJson(res.getRequestId() , res));
}
}
private String createJson(String id , Object req){
ArrayList arr = this.getList();
arr.add(id);
arr.add(req);
String json = this.getGson().toJson(arr);
this.returnList(arr);
return json;
}
private synchronized ArrayList getList(){
synchronized (vList) {
if(vList.size() == 0){
return new ArrayList();
}else{
return vList.removeFirst();
}
}
}
private synchronized void returnList(ArrayList arr){
synchronized (vList) {
arr.clear();
vList.addLast(arr);
}
}
}
/SerializeAndDeserializeBase.class
/ArrayDeserializer.class
/ByteBufferDeserialize.class
/ByteStringDeserialize.class
package com.csc.library.gson;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
public class ArrayDeserializer implements JsonDeserializer<String[]>{
@Override
public String[] deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
int size = json.getAsJsonArray().size();
String[] result = new String[size];
for(int i = 0; i < size; i++){
result[i] = json.getAsJsonArray().get(i).toString();
}
return result;
}
}
package com.csc.library.gson;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import com.google.gson.JsonArray;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
public class ByteBufferDeserialize implements JsonDeserializer<ByteBuffer>{
@Override
public ByteBuffer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
/*Type type = new TypeToken<ByteStringDeserialize>() {}.getType();
ByteStringDeserialize bs = context.deserialize(jsonObj, type);
ByteBuffer bytebuf = ByteBuffer.allocate(bs.getCapcity());
bytebuf.wrap(bs.getBytes());*/
ByteBuffer bytebuf = ByteBuffer.allocate(jsonObj.get("capacity").getAsInt());
JsonArray arr = jsonObj.get("hb").getAsJsonArray();
int size = arr.size();
for(int i = 0; i < size; i ++){
bytebuf.put(arr.get(i).getAsByte());
}
/*Iterator<JsonElement> it = arr.iterator();
while(it.hasNext()){
bytebuf.put(it.next().getAsByte());
}*/
/*arr.forEach(new Consumer<JsonElement>(){
@Override
public void accept(JsonElement t) {
bytebuf.put(t.getAsByte());
}
});*/
return bytebuf;
}
}
package com.csc.library.gson;
public class ByteStringDeserialize {
private int capacity;
private byte[] hb;
public int getCapcity(){
return this.capacity;
}
public byte[] getBytes(){
return this.hb;
}
}
package com.csc.library.gson;
import java.lang.reflect.Type;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
public class SerializeAndDeserializeBase<T> implements JsonDeserializer<T>,JsonSerializer<T>{
private Class myClass = null;
public SerializeAndDeserializeBase(Class clss) {
this.myClass = clss;
}
public Class getMyClass(){
return this.myClass;
}
public JsonElement serialize(Object src, Type typeOfSrc,
JsonSerializationContext context) {
return context.serialize(src, this.myClass);
}
public T deserialize(JsonElement json, Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
return context.deserialize(json, this.myClass);
}
}
/HandlePackage.class
/JsonFile.class
/JsonFileImpl.class
package com.csc.library.properties;
import com.csc.library.request.RequestWebsocket;
import com.csc.library.response.ResponseWebsocket;
public interface HandlePackage<Session> {
public boolean isExistsRequest(Session session, String reqId);
public void onRequest(Session session , RequestWebsocket req);
public void onResponse(Session session , ResponseWebsocket res);
/* Ǩͺ Request ͡ */
public boolean validateSendRequest(Session session , RequestWebsocket req);
/* Ǩͺ Response ͡ */
public boolean validateSendResponse(Session session , ResponseWebsocket req);
public void send(Session session , String jsonSting);
}
package com.csc.library.properties;
import java.io.IOException;
public interface JsonFile {
public String getDirectory();
public String getPath();
public String getFileName();
public void writeFile(String path) throws IOException;
public byte[] getBytes();
}
package com.csc.library.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class JsonFileImpl implements JsonFile , Serializable{
private String dirName;
private String fileName;
private transient ByteBuffer buffer;
private byte[] bytes;
public JsonFileImpl(File file) throws Exception{
if(file == null){
throw new NullPointerException();
}else if(!file.exists()){
throw new FileNotFoundException();
}else if(!file.isFile()){
throw new Exception("UploadProperties accept File Only. cannot load Directory");
}
this.dirName = file.getPath();
this.fileName = file.getName();
this.readFile(file);
}
public JsonFileImpl(File file , String dirName , String fileName) throws Exception{
if(file == null){
throw new NullPointerException();
}else if(!file.exists()){
throw new FileNotFoundException();
}else if(!file.isFile()){
throw new Exception("UploadProperties accept File Only. cannot load Directory");
}
this.dirName = dirName;
this.fileName = fileName;
this.readFile(file);
}
public JsonFileImpl(byte[] bytes , String dirName , String fileName) throws Exception{
this.dirName = dirName;
this.fileName = fileName;
this.bytes = bytes;
}
protected void readBytes(byte[] bytes){
buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);
}
protected void readFile(File file) throws IOException{
FileInputStream fIn = new FileInputStream(file);
FileChannel fChan = fIn.getChannel();
buffer = ByteBuffer.allocate((int) fChan.size());
fChan.read(buffer);
fChan.close();
fIn.close();
this.bytes = buffer.array();
}
public void writeFile(String path) throws IOException{
if( bytes != null) {
FileOutputStream out = new FileOutputStream(path , false);
FileChannel channel = out.getChannel();
buffer = ByteBuffer.allocate(bytes.length);
buffer.put(bytes);
buffer.flip();
channel.write(buffer);
channel.close();
out.close();
}
}
public String getPath(){
return "";
}
public String getFileName(){
return this.fileName;
}
@Override
public String getDirectory() {
return this.dirName;
}
public byte[] getBytes(){
return this.bytes;
}
}
/CMDMode.class
/CommandState.class
/CommonModeParameter.class
/CommonParameterImpl.class
/ReqParameterImpl.class
/RequestParameter.class
/RequestPropertiesImpl.class
/RequestWebsocket.class
package com.csc.library.request;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
import com.google.gson.reflect.TypeToken;
public enum CMDMode{
/* ⌒축앝窪 CMDMode ⓓ들系餓宣逗졔饑 Controller 닻픈*/
common(0 , "Standard API") ,
special(1 , "Special API") ,
process(2 , "Process Entry") ,
ftp(3 , "FTP API");
int mode = 0;
String description = "";
private CMDMode(int mode, String desc){
this.mode = mode;
this.description = desc;
}
public int getModeCode(){
return this.mode;
}
public String getDescription(){
return this.description;
}
}
package com.csc.library.request;
public enum CommandState {
fail(-1 , "Command is Fail."),
noAction(0 , "No Action."),
success(2 , "Command is Success.");
int code = 0;
String description = "";
private CommandState(int code , String description){
this.code = code;
}
public int getCode(){
return this.code;
}
public String getDescription(){
return this.description;
}
}
package com.csc.library.request;
import java.util.Map;
public interface CommonModeParameter {
public String getTableName();/* EmployeeTimeTemp */
public Map<String,String> getKey(); /* {companyId:001,memberId:M001} */
public String getJsonField(); /* movement */
public Map<String,String> getJsonKey(); /* {swipeDate:2015-01-02,swipeTime:10.30} */
public Map<String,String> getJson(); /* {swipeDate:2015-01-02,timeStampType:OUT} */
public String getGroupField(); /* process */
}
package com.csc.library.request;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class CommonParameterImpl implements CommonModeParameter ,Serializable{
private String tableName = ""; /* EmployeeTimeTemp */
private Map<String,String> key = null;//new HashMap<String,String>(); /* {companyId:001,memberId:M001} */
private String jsonField = ""; /* movement */
private Map<String,String> jsonKey = null;//new HashMap<String,String>(); /* {swipeDate:2015-01-02,swipeTime:10.30} */
private Map<String,String> json = null;//new HashMap<String,String>(); /* {swipeDate:2015-01-02,timeStampType:OUT} */
private String groupField = ""; /* process */
public String getTableName() {
return this.tableName;
}
public void setTableName(String name){
this.tableName = name;
}
public Map<String, String> getKey() {
return this.key;
}
public void setKey(HashMap<String,String> key){
this.key = key;
}
public String getJsonField() {
return this.jsonField;
}
public void setJsonField(String jsonField){
this.jsonField = jsonField;
}
public Map<String, String> getJsonKey() {
return this.jsonKey;
}
public void setJsonKey(HashMap<String,String> jsonKey){
this.jsonKey = jsonKey;
}
public Map<String, String> getJson() {
return this.json;
}
public void setJson(HashMap<String,String> json){
this.json = json;
}
public String getGroupField() {
return this.groupField;
}
public void setGroupField(String groupField){
this.groupField = groupField;
}
}
package com.csc.library.request;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.csc.library.properties.JsonFile;
public class ReqParameterImpl implements RequestParameter , Serializable{
private CommonModeParameter common = null;//new CommonParameterImpl();
private List<String> special = null;//new ArrayList<String>();
private Map<String,String> process = null;//new HashMap<String,String>();
private JsonFile jsonFile = null;
public ReqParameterImpl(){}
public CommonModeParameter getCommonParameter(){
return this.common;
}
public void setCommonModeParameter(CommonModeParameter param){
this.common = param;
}
public List<String> getSpecialParameter(){
return this.special;
}
public void setSpecialParameter(ArrayList<String> list){
this.special = list;
}
public Map<String,String> getProcessParameter(){
return this.process;
}
public void setProcessParameter(HashMap<String,String> param){
this.process = param;
}
public JsonFile getJsonFile() {
return this.jsonFile;
}
public void setJsonFile(JsonFile file){
this.jsonFile = file;
}
}
package com.csc.library.request;
import java.util.List;
import java.util.Map;
import com.csc.library.properties.JsonFile;
public interface RequestParameter {
public CommonModeParameter getCommonParameter();
public List<String> getSpecialParameter();
public Map<String,String> getProcessParameter();
public JsonFile getJsonFile();
}
package com.csc.library.request;
import java.io.Serializable;
public class RequestPropertiesImpl implements RequestWebsocket , Serializable{
private String requestId;
private String deviceNo;
private String versionNo;
private String lang;
private CMDMode cmdMode;
private String cmdCode;
private String host;
private RequestParameter parameter = null;
public String getRequestId(){
return this.requestId;
}
public void setRequestId(String reqId){
this.requestId = reqId;
}
public String getDeviceNo(){
return this.deviceNo;
}
public void setDeviceNo(String device){
this.deviceNo = device;
}
public String getVersionNo(){
return this.versionNo;
}
public void setVersionNo(String version){
this.versionNo = version;
}
public String getLang(){
return this.lang;
}
public void setLang(String lang){
this.lang = lang;
}
public CMDMode getCommandMode(){
return this.cmdMode;
}
public void setCommandMode(CMDMode mode){
this.cmdMode = mode;
}
public String getCommandCode(){
return this.cmdCode;
}
public void setCommandCode(String code){
this.cmdCode = code;
}
public RequestParameter getRequestParameter(){
return this.parameter;
}
public void setRequestParameter(RequestParameter reqParam){
this.parameter = reqParam;
}
}
package com.csc.library.request;
public interface RequestWebsocket {
public String getRequestId();
public String getDeviceNo();
public String getVersionNo();
public String getLang();
public CMDMode getCommandMode();
public String getCommandCode();
public RequestParameter getRequestParameter();
}
/ResDataImpl.class
/ResponseData.class
/ResponsePropertiesImpl.class
/ResponseStatus.class
/ResponseWebsocket.class
package com.csc.library.response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.csc.library.properties.JsonFile;
public class ResDataImpl implements ResponseData{
private Map<String,String> dataRecord = null;//new HashMap<String,String>();
private List<Map<String,String>> dataList = null;//new ArrayList<Map<String,String>>();
private JsonFile jsonFile = null;
public void setData(Map<String,String> data){
this.dataRecord = data;
}
public void setData(List<Map<String,String>> data){
this.dataList = data;
}
public void setDataRecord(Map<String,String> data){
this.dataRecord = data;
}
public void setDataList(List<Map<String,String>> data){
this.dataList = data;
}
@Override
public Map<String, String> getDataRecord() {
return this.dataRecord;
}
@Override
public List<Map<String, String>> getDataList() {
return this.dataList;
}
public void setFile(JsonFile file){
this.jsonFile = file;
}
public JsonFile getFile(){
return this.jsonFile;
}
}
package com.csc.library.response;
import java.util.List;
import java.util.Map;
import com.csc.library.properties.JsonFile;
public interface ResponseData {
public Map<String,String> getDataRecord();
public List<Map<String,String>> getDataList();
public JsonFile getFile();
public void setData(Map<String,String> data);
public void setData(List<Map<String,String>> data);
public void setFile(JsonFile file);
}
package com.csc.library.response;
import com.csc.library.request.CommandState;
import com.csc.library.request.RequestWebsocket;
public class ResponsePropertiesImpl implements ResponseWebsocket{
private String requestid;
private String message;
private CommandState stateCommand;
private ResponseStatus statusCode;
private ResponseData data = null;//new ResDataImpl();
public void setRequestWebsocket(RequestWebsocket req){
this.requestid = req.getRequestId();
}
public void setResponseData(ResponseData resData){
this.data = resData;
}
public void setMessage(String message){
this.message = message;
}
public void setResponseStatus(ResponseStatus state){
this.statusCode = state;
}
public void setStateCommand(CommandState state){
this.stateCommand = state;
}
@Override
public String getRequestId() {
return this.requestid;
}
@Override
public String getMessage() {
return this.message;
}
@Override
public ResponseStatus getStatus() {
return this.statusCode;
}
@Override
public ResponseData getData() {
return this.data;
}
public CommandState getCommandState(){
return this.stateCommand;
}
public void clear(){
this.requestid="";
this.message="";
this.statusCode = ResponseStatus.NoAction;
this.data = null;
this.stateCommand = CommandState.noAction;
}
}
package com.csc.library.response;
public enum ResponseStatus {
NoAction(0 , "No Action"),
Receive(1 , "Server Receive Request"),
SendMessage(2 , "Server Response Message"),
Finish(3 , "Request Finish"),
Error(4 , "Request Error");
int code = 0;
String description = "";
private ResponseStatus(int code , String description){
this.code = code;
}
public int getCode(){
return this.code;
}
public String getDescription(){
return this.description;
}
}
package com.csc.library.response;
import com.csc.library.request.CommandState;
public interface ResponseWebsocket {
public String getRequestId();
public String getMessage();
public ResponseStatus getStatus();
public ResponseData getData();
public void clear();
public CommandState getCommandState();
}
package com.csc.library.security;
public class EncryptionUtil {
private static final String ALGORITHM = "RSA";
private static final int KEYSIZE = 128;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment