Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery.common.java
/
microservice
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
7c497249
authored
Aug 16, 2016
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
- add getMD5Hash, catch unhandled exceptions, add command-params builder
parent
e2e6fdd9
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
164 additions
and
54 deletions
README.md
build.gradle
src/main/java/microservice/common/EncryptionUtils.java
src/main/java/microservice/handlers/RestHandler.java
src/main/java/microservice/params/CommandParams.java
src/main/java/microservice/params/CommandParamsBuilder.java
src/test/java/microservice/TestCommandClient.java
README.md
View file @
7c497249
### 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:
...
...
build.gradle
View file @
7c497249
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'
...
...
src/main/java/microservice/common/EncryptionUtils.java
View file @
7c497249
...
...
@@ -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
();
...
...
src/main/java/microservice/handlers/RestHandler.java
View file @
7c497249
...
...
@@ -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
*/
...
...
src/main/java/microservice/params/CommandParams.java
View file @
7c497249
...
...
@@ -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
;
}
}
src/main/java/microservice/params/CommandParamsBuilder.java
0 → 100644
View file @
7c497249
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
;
}
}
src/test/java/microservice/TestCommandClient.java
View file @
7c497249
...
...
@@ -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
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment