Commit 7c497249 by amir

- add getMD5Hash, catch unhandled exceptions, add command-params builder

parent e2e6fdd9
### Microservice Framework in JAVA ### Microservice Framework in JAVA
## 1.3.2:
- add getMD5Hash, catch unhandled exceptions, add command-params builder
## 1.3.1: ## 1.3.1:
- add rabbotmq command client + change version to 1.3.1 - add rabbotmq command client + change version to 1.3.1
## 1.3.0: ## 1.3.0:
......
group 'com.ipgallery.common' group 'com.ipgallery.common'
version '1.3.1' version '1.3.2'
apply plugin: 'java' apply plugin: 'java'
apply plugin: 'maven-publish' apply plugin: 'maven-publish'
...@@ -28,9 +28,7 @@ dependencies { ...@@ -28,9 +28,7 @@ dependencies {
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2' compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
compile 'com.ipgallery.common:utils:1.1.4' compile 'com.ipgallery.common:utils:1.1.4'
compile ('com.ipgallery.common:rabbitmq:1.0.3') compile ('com.ipgallery.common:rabbitmq:1.0.3')
//compile 'com.rabbitmq:amqp-client:3.6.3' compile 'com.ecwid.consul:consul-api:1.1.9'
//compile files ('resources/rabbitmq-1.0.x.jar')
compile 'com.ecwid.consul:consul-api:1.1.9'
compile 'com.github.davidb:metrics-influxdb:0.8.2' compile 'com.github.davidb:metrics-influxdb:0.8.2'
compile 'io.dropwizard.metrics:metrics-graphite:3.1.2' compile 'io.dropwizard.metrics:metrics-graphite:3.1.2'
compile 'io.jsonwebtoken:jjwt:0.6.0' compile 'io.jsonwebtoken:jjwt:0.6.0'
......
...@@ -17,11 +17,21 @@ public class EncryptionUtils { ...@@ -17,11 +17,21 @@ public class EncryptionUtils {
public final static String JWT_SALT = System.getProperty("jwt.salt","12345678901234567890123456789012"); public final static String JWT_SALT = System.getProperty("jwt.salt","12345678901234567890123456789012");
private static int workload = 5; private static int workload = 5;
public static String getMD5Hash(String data){
String encrypted = getDigestedString(data,"MD5");
return encrypted;
}
public static String getSHA256Hash(String data){ public static String getSHA256Hash(String data){
String encrypted = getDigestedString(data,"SHA-256");
return encrypted;
}
private static String getDigestedString(String data,String algoritm) {
String encrypted = null; String encrypted = null;
MessageDigest md = null; MessageDigest md = null;
try { try {
md = MessageDigest.getInstance("SHA-256"); md = MessageDigest.getInstance(algoritm);
md.update(data.getBytes()); md.update(data.getBytes());
byte byteData[] = md.digest(); byte byteData[] = md.digest();
......
...@@ -81,59 +81,60 @@ public class RestHandler implements HttpHandler , IContainer ...@@ -81,59 +81,60 @@ public class RestHandler implements HttpHandler , IContainer
return; return;
} }
try {
/* /*
* async part * async part
*/ */
RequestContext reqContext = getRequestContext(exchange); RequestContext reqContext = getRequestContext(exchange);
if (reqContext != null) if (reqContext != null) {
{ HttpString requestMethod = exchange.getRequestMethod();
HttpString requestMethod = exchange.getRequestMethod(); EnumHttpMethod eMethod = EnumHttpMethod.resolveMethod(requestMethod.toString());
EnumHttpMethod eMethod = EnumHttpMethod.resolveMethod(requestMethod.toString()); reqContext.enumCrudMethod = Enums.EnumCrudMethod.resolveMethodFromHttp(eMethod);
reqContext.enumCrudMethod = Enums.EnumCrudMethod.resolveMethodFromHttp(eMethod);
//exchange. request.setCharacterEncoding(Constants.C_ENCODING_UTF8);
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, Constants.CONTENT_TYPE_JSON);
if (eMethod != null && reqContext != null) {
if (validateRequest(reqContext)) {
// pre
if (enableMetrics)
preHandleMetrics(eMethod);
switch (eMethod) {
case E_DELETE:
doDelete(reqContext);
break;
case E_GET:
doGet(reqContext);
break;
case E_POST:
doPost(reqContext);
break;
case E_PUT:
doPut(reqContext);
break;
default:
break;
}
//exchange. request.setCharacterEncoding(Constants.C_ENCODING_UTF8); // post
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, Constants.CONTENT_TYPE_JSON); if (enableMetrics)
if (eMethod != null && reqContext != null) postHandleMetrics(eMethod);
{
if (validateRequest(reqContext)) {
// pre
if (enableMetrics)
preHandleMetrics(eMethod);
switch (eMethod) {
case E_DELETE:
doDelete(reqContext);
break;
case E_GET:
doGet(reqContext);
break;
case E_POST:
doPost(reqContext);
break;
case E_PUT:
doPut(reqContext);
break;
default:
break;
} }
} else {
// post sendErrorResp(reqContext.response, Constants.METHOD_NOT_IMPLEMENTED);
if (enableMetrics)
postHandleMetrics(eMethod);
} }
} } else {
else IResponse iResp = new IResponseRestImpl(exchange);
{ sendErrorResp(iResp, Constants.FAILED_TO_GET_PARAMS);
sendErrorResp(reqContext.response,Constants.METHOD_NOT_IMPLEMENTED);
} }
} }
else catch (Exception exp)
{ {
IResponse iResp = new IResponseRestImpl(exchange); IResponse iResp = new IResponseRestImpl(exchange);
sendErrorResp(iResp,Constants.FAILED_TO_GET_PARAMS); sendErrorResp(iResp, exp.toString());
} }
/* /*
* flush * flush
*/ */
......
...@@ -10,10 +10,12 @@ public class CommandParams ...@@ -10,10 +10,12 @@ public class CommandParams
String requestParams; String requestParams;
String content; String content;
Map<String,String> headersMap = null; Map<String,String> headersMap = null;
public CommandParams() {
}
public CommandParams(String entity, String paramsString, public CommandParams(String entity, String paramsString,
String requestParams, String content,Map<String,String> headersMap) String requestParams, String content, Map<String,String> headersMap)
{ {
super(); super();
this.entity = entity; this.entity = entity;
...@@ -60,7 +62,32 @@ public class CommandParams ...@@ -60,7 +62,32 @@ public class CommandParams
{ {
return content; return content;
} }
public Map<String, String> getHeadersMap() {
return headersMap;
}
public void setEntity(String entity) {
this.entity = entity;
}
public void setParams(String[] params) {
this.params = params;
}
public void setParamsString(String paramsString) {
this.paramsString = paramsString;
}
public void setRequestParams(String requestParams) {
this.requestParams = requestParams;
}
public void setContent(String content) {
this.content = content;
}
public void setHeadersMap(Map<String, String> headersMap) {
this.headersMap = headersMap;
}
} }
package microservice.params;
import java.util.Map;
/**
* Created by amir on 15/08/16.
*/
public class CommandParamsBuilder {
CommandParams commandParams = new CommandParams();
public CommandParamsBuilder setEntity(String entity) {
commandParams.setEntity(entity);
return this;
}
public CommandParamsBuilder setParams(String[] params) {
if (commandParams.getParamsString() == null)
commandParams.setParams(params);
return this;
}
public CommandParamsBuilder setParamsString(String paramsString) {
if (commandParams.getParams() == null)
commandParams.setParamsString(paramsString);
return this;
}
public CommandParamsBuilder setRequestParams(String requestParams) {
commandParams.setRequestParams(requestParams);
return this;
}
public CommandParamsBuilder setContent(String content) {
commandParams.setContent(content);
return this;
}
public CommandParamsBuilder setHeadersMap(Map<String, String> headersMap) {
commandParams.setHeadersMap(headersMap);
return this;
}
public CommandParams build(){
return commandParams;
}
}
...@@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode; ...@@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.io.iface.ICommandClient; import microservice.io.iface.ICommandClient;
import microservice.io.impl.IRMQClientRestImpl; import microservice.io.impl.IRMQClientRestImpl;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams; import microservice.params.CommandParams;
import microservice.params.CommandParamsBuilder;
import microservice.params.RMQClientParams; import microservice.params.RMQClientParams;
import microservice.types.BaseRestResponse; import microservice.types.BaseRestResponse;
import org.junit.Test; import org.junit.Test;
...@@ -128,11 +130,26 @@ public class TestCommandClient { ...@@ -128,11 +130,26 @@ public class TestCommandClient {
} }
} }
@Test @Test
public void testCommand() throws InterruptedException public void testCommand() throws InterruptedException
{ {
ASM asm = new ASM();
ASMClient asmClient = new ASMClient(asm);
MicroserviceClient msClient = new MicroserviceClient(asmClient, new BaseClientParams("", false, 0));
CommandParams reqCtx = new CommandParams("AE", "ae1", null, null, null);
msClient.create(reqCtx);
System.out.println("sleeping");
//Thread.sleep(5000);
reqCtx = new CommandParams("AE", "ae1", null, null, null);
BaseRestResponse readBrr = msClient.read(reqCtx);
System.out.println(readBrr.objectNode.toString());
System.out.println("end test");
}
@Test
public void testRabbitCommand() throws InterruptedException
{
try { try {
RMQClientParams clientParams = new RMQClientParams("test", "respQ@localhost", "myFirstQ@localhost", 1, 100, "/logs"); RMQClientParams clientParams = new RMQClientParams("test", "respQ@localhost", "myFirstQ@localhost", 1, 100, "/logs");
...@@ -148,4 +165,13 @@ public class TestCommandClient { ...@@ -148,4 +165,13 @@ public class TestCommandClient {
} }
System.out.println("end test"); System.out.println("end test");
} }
@Test
public void testCommandParams()
{
CommandParams commandParams = new CommandParamsBuilder()
.setEntity("test")
.setParamsString("stam")
.build();
}
} }
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 sign in to comment