Commit 7c497249 by amir

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

parent e2e6fdd9
### Microservice Framework in JAVA
## 1.3.2:
- add getMD5Hash, catch unhandled exceptions, add command-params builder
## 1.3.1:
- add rabbotmq command client + change version to 1.3.1
## 1.3.0:
......
group 'com.ipgallery.common'
version '1.3.1'
version '1.3.2'
apply plugin: 'java'
apply plugin: 'maven-publish'
......@@ -28,9 +28,7 @@ dependencies {
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
compile 'com.ipgallery.common:utils:1.1.4'
compile ('com.ipgallery.common:rabbitmq:1.0.3')
//compile 'com.rabbitmq:amqp-client:3.6.3'
//compile files ('resources/rabbitmq-1.0.x.jar')
compile 'com.ecwid.consul:consul-api:1.1.9'
compile 'com.ecwid.consul:consul-api:1.1.9'
compile 'com.github.davidb:metrics-influxdb:0.8.2'
compile 'io.dropwizard.metrics:metrics-graphite:3.1.2'
compile 'io.jsonwebtoken:jjwt:0.6.0'
......
......@@ -17,11 +17,21 @@ public class EncryptionUtils {
public final static String JWT_SALT = System.getProperty("jwt.salt","12345678901234567890123456789012");
private static int workload = 5;
public static String getMD5Hash(String data){
String encrypted = getDigestedString(data,"MD5");
return encrypted;
}
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;
MessageDigest md = null;
try {
md = MessageDigest.getInstance("SHA-256");
md = MessageDigest.getInstance(algoritm);
md.update(data.getBytes());
byte byteData[] = md.digest();
......
......@@ -81,59 +81,60 @@ public class RestHandler implements HttpHandler , IContainer
return;
}
try {
/*
* async part
*/
RequestContext reqContext = getRequestContext(exchange);
if (reqContext != null)
{
HttpString requestMethod = exchange.getRequestMethod();
EnumHttpMethod eMethod = EnumHttpMethod.resolveMethod(requestMethod.toString());
reqContext.enumCrudMethod = Enums.EnumCrudMethod.resolveMethodFromHttp(eMethod);
RequestContext reqContext = getRequestContext(exchange);
if (reqContext != null) {
HttpString requestMethod = exchange.getRequestMethod();
EnumHttpMethod eMethod = EnumHttpMethod.resolveMethod(requestMethod.toString());
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);
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;
// post
if (enableMetrics)
postHandleMetrics(eMethod);
}
// post
if (enableMetrics)
postHandleMetrics(eMethod);
} else {
sendErrorResp(reqContext.response, Constants.METHOD_NOT_IMPLEMENTED);
}
}
else
{
sendErrorResp(reqContext.response,Constants.METHOD_NOT_IMPLEMENTED);
} else {
IResponse iResp = new IResponseRestImpl(exchange);
sendErrorResp(iResp, Constants.FAILED_TO_GET_PARAMS);
}
}
else
catch (Exception exp)
{
IResponse iResp = new IResponseRestImpl(exchange);
sendErrorResp(iResp,Constants.FAILED_TO_GET_PARAMS);
sendErrorResp(iResp, exp.toString());
}
/*
* flush
*/
......
......@@ -10,10 +10,12 @@ public class CommandParams
String requestParams;
String content;
Map<String,String> headersMap = null;
public CommandParams() {
}
public CommandParams(String entity, String paramsString,
String requestParams, String content,Map<String,String> headersMap)
String requestParams, String content, Map<String,String> headersMap)
{
super();
this.entity = entity;
......@@ -60,7 +62,32 @@ public class CommandParams
{
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;
import com.fasterxml.jackson.databind.ObjectMapper;
import microservice.io.iface.ICommandClient;
import microservice.io.impl.IRMQClientRestImpl;
import microservice.params.BaseClientParams;
import microservice.params.CommandParams;
import microservice.params.CommandParamsBuilder;
import microservice.params.RMQClientParams;
import microservice.types.BaseRestResponse;
import org.junit.Test;
......@@ -128,11 +130,26 @@ public class TestCommandClient {
}
}
@Test
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 {
RMQClientParams clientParams = new RMQClientParams("test", "respQ@localhost", "myFirstQ@localhost", 1, 100, "/logs");
......@@ -148,4 +165,13 @@ public class TestCommandClient {
}
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