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
4002ac06
authored
May 09, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
first working service - server only
parent
7fd64090
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
174 additions
and
91 deletions
doc/todo.txt
src/main/java/microservice/MicroserviceApp.java
src/main/java/microservice/RequestContext.java → src/main/java/microservice/RestContext.java
src/main/java/microservice/defs/Enums.java
src/main/java/microservice/handlers/BaseHandler.java
src/main/java/microservice/handlers/MBIHandler.java
src/main/java/microservice/handlers/MonitorHandler.java
src/main/java/microservice/handlers/Reactor.java
src/main/java/microservice/handlers/RestHandler.java
src/main/java/microservice/io/iface/CommonServices.java
src/main/java/microservice/io/impl/service/IPubSubServiceMqttImpl.java
src/main/java/microservice/io/impl/service/IRestServiceHttpImpl.java
src/main/java/microservice/types/UserProfile.java
src/test/java/microservice/TestMicroserviceApp.java
doc/todo.txt
View file @
4002ac06
...
...
@@ -2,7 +2,7 @@
# new services design
- counters and metrics in the reactor
- validateRequest in RestService , the jwt issues
- Add monitoring apis
- All the validation ,pre/post handling that was were done by the handler will be done by the base service
- add Runtime Test:
addTest(const char *testName, nsMicroservice_Iface::TestFunction testFunction);
...
...
src/main/java/microservice/MicroserviceApp.java
View file @
4002ac06
...
...
@@ -24,6 +24,7 @@ import java.util.*;
import
java.util.Map.Entry
;
import
java.util.function.BiConsumer
;
import
java.util.function.Consumer
;
import
java.util.stream.Stream
;
import
static
io
.
undertow
.
Handlers
.
resource
;
...
...
@@ -102,6 +103,12 @@ public class MicroserviceApp
return
appName
;
}
public
String
getId
()
{
return
id
;
}
public
boolean
isEnableDefaultServiceAuthorization
()
{
return
enableDefaultServiceAuthorization
;
}
/*************************
* WITH SECTION
*************************/
...
...
@@ -517,17 +524,17 @@ public class MicroserviceApp
/*************************************************************************/
//
/// the services array
private
final
List
<
Map
<
String
,
CommonServices
.
IService
>>
servicesArray
=
new
ArrayList
<>(
Enums
.
EnumServiceType
.
values
().
length
);
//
private final List<Map<String,CommonServices.IService>> servicesArray = new ArrayList<>(Enums.EnumServiceType.values().length);
private
final
Map
<
String
,
CommonServices
.
IService
>[]
servicesArray
=
new
Map
[
Enums
.
EnumServiceType
.
values
().
length
];
public
MicroserviceApp
addService
(
Enums
.
EnumServiceType
serviceType
,
CommonServices
.
IService
iService
,
String
serviceKey
)
{
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
.
get
(
serviceType
.
ordinal
())
;
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
[
serviceType
.
ordinal
()]
;
if
(
serviceMap
==
null
){
serviceMap
=
new
HashMap
<>();
servicesArray
.
set
(
serviceType
.
ordinal
(),
serviceMap
)
;
servicesArray
[
serviceType
.
ordinal
()]
=
serviceMap
;
}
serviceMap
.
put
(
serviceKey
,
iService
);
iService
.
setReactor
(
reactor
);
...
...
@@ -535,14 +542,14 @@ public class MicroserviceApp
}
CommonServices
.
IService
getService
(
Enums
.
EnumServiceType
serviceType
,
String
serviceKey
){
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
.
get
(
serviceType
.
ordinal
())
;
Map
<
String
,
CommonServices
.
IService
>
serviceMap
=
servicesArray
[
serviceType
.
ordinal
()]
;
if
(
serviceMap
!=
null
)
return
serviceMap
.
get
(
serviceKey
);
return
null
;
}
CommonServices
.
IService
getService
(
String
serviceKey
){
Optional
<
CommonServices
.
IService
>
iServiceOptional
=
servicesArray
.
stream
(
)
Optional
<
CommonServices
.
IService
>
iServiceOptional
=
Arrays
.
stream
(
servicesArray
)
.
filter
(
serviceMap
->
serviceMap
!=
null
&&
serviceMap
.
containsKey
(
serviceKey
))
.
map
(
serviceMap
->
serviceMap
.
get
(
serviceKey
))
.
findFirst
();
...
...
@@ -593,38 +600,45 @@ public class MicroserviceApp
/**
* init services
* iterating only over non empty elements
*/
servicesArray
.
stream
().
forEach
(
servicesMap
->
{
servicesMap
.
forEach
(
(
serviceKey
,
service
)
->
{
Arrays
.
stream
(
servicesArray
)
.
filter
(
p
->
p
!=
null
)
.
forEach
(
servicesMap
->
servicesMap
.
forEach
((
serviceKey
,
service
)
->
{
service
.
init
();
});
});
service
.
register
(
serviceDiscovery
,
id
);
})
);
return
this
;
}
public
void
_run
()
{
if
(!
servicesArray
.
isEmpty
())
/**
* מי מתחכם זה מיקו!!!
*/
if
(
Arrays
.
stream
(
servicesArray
)
.
filter
(
p
->
p
!=
null
)
.
count
()
>
0
)
{
if
(
this
.
enableMetrics
)
{
IMetricsFactoryImpl
.
getInstance
().
startReporting
(
appName
);
}
servicesArray
.
stream
().
forEach
(
servicesMap
->
{
servicesMap
.
forEach
(
(
serviceKey
,
service
)
->
{
service
.
run
();
});
});
Arrays
.
stream
(
servicesArray
)
.
filter
(
p
->
p
!=
null
)
.
forEach
(
servicesMap
->
servicesMap
.
forEach
(
(
serviceKey
,
service
)
->
service
.
run
()
)
);
Runtime
.
getRuntime
().
addShutdownHook
(
new
Thread
(()
->
{
try
{
servicesArray
.
stream
().
forEach
(
servicesMap
->
{
servicesMap
.
forEach
(
(
serviceKey
,
service
)
->
{
service
.
shutdown
();
});
});
Arrays
.
stream
(
servicesArray
)
.
filter
(
p
->
p
!=
null
)
.
forEach
(
servicesMap
->
servicesMap
.
forEach
(
(
serviceKey
,
service
)
->
service
.
shutdown
()
)
);
// notify main
notifyAll
();
}
catch
(
Exception
e
)
{
...
...
@@ -645,7 +659,7 @@ public class MicroserviceApp
}
else
{
System
.
out
.
println
(
"no serv
er
/s...exit!"
);
System
.
out
.
println
(
"no serv
ice
/s...exit!"
);
}
}
...
...
src/main/java/microservice/Re
que
stContext.java
→
src/main/java/microservice/RestContext.java
View file @
4002ac06
...
...
@@ -6,16 +6,18 @@ import java.util.Map;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
microservice.defs.Enums
;
import
microservice.io.iface.CommonServices
;
import
microservice.io.iface.IContainer
;
import
microservice.io.iface.IRequest
;
import
microservice.io.iface.IResponse
;
import
microservice.types.UserProfile
;
public
class
Re
quest
Context
public
class
Re
stContext
implements
CommonServices
.
IMsg
Context
{
public
String
[]
params
=
null
;
public
Map
<
String
,
Deque
<
String
>>
queryParameters
=
null
;
public
Map
<
String
,
String
>
pathParameters
=
null
;
// request-interface
// response interface
...
...
@@ -25,7 +27,7 @@ public class RequestContext
public
ObjectMapper
objMapper
;
public
String
rcid
;
// request correlation id
public
UserProfile
userProfile
;
public
Enums
.
EnumCrudMethod
enumCrudMethod
;
public
CommonServices
.
EnumRestCommands
enumRestCommands
;
public
String
getParameter
(
String
paramName
)
{
...
...
@@ -34,4 +36,16 @@ public class RequestContext
else
return
null
;
}
public
String
getPathParameter
(
String
paramName
)
{
if
(
pathParameters
!=
null
)
return
pathParameters
.
get
(
paramName
);
else
return
null
;
}
@Override
public
void
setParameters
(
Map
<
String
,
String
>
parameters
)
{
pathParameters
=
parameters
;
}
}
src/main/java/microservice/defs/Enums.java
View file @
4002ac06
...
...
@@ -80,5 +80,10 @@ public class Enums
E_PUBSUB
}
public
enum
EnumAuthenticationType
{
DEFAULT
,
NONE
,
JWT
,
FORM
}
}
src/main/java/microservice/handlers/BaseHandler.java
View file @
4002ac06
...
...
@@ -6,7 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
microservice.MicroserviceApp
;
import
microservice.Re
que
stContext
;
import
microservice.RestContext
;
import
microservice.io.iface.IConfiguration
;
import
microservice.io.iface.IContainer
;
import
microservice.io.iface.ILogger
;
...
...
@@ -29,7 +29,6 @@ public abstract class BaseHandler
protected
EnumAuthenticationType
authType
=
EnumAuthenticationType
.
DEFAULT
;
/*
* SOME HELPERS
*/
...
...
@@ -74,27 +73,27 @@ public abstract class BaseHandler
}
public
void
sendErrorResp
(
Re
que
stContext
reqCtx
,
String
error
)
public
void
sendErrorResp
(
RestContext
reqCtx
,
String
error
)
{
reqCtx
.
container
.
sendErrorResp
(
reqCtx
.
response
,
error
);
}
public
void
writeObjectToResponse
(
Re
que
stContext
reqCtx
,
Object
value
)
public
void
writeObjectToResponse
(
RestContext
reqCtx
,
Object
value
)
{
reqCtx
.
container
.
writeObjectToResponse
(
reqCtx
.
response
,
value
);
}
public
Object
readObjectFromRequest
(
Re
questContext
reqCtx
,
Class
<?>
ObjClass
)
public
Object
readObjectFromRequest
(
Re
stContext
reqCtx
,
Class
<?>
ObjClass
)
{
return
reqCtx
.
container
.
readObjectFromRequest
(
reqCtx
.
request
,
ObjClass
);
}
public
void
subscribe
(
Re
questContext
reqCtx
,
String
topic
,
INotifyCallback
notifyHandler
)
public
void
subscribe
(
Re
stContext
reqCtx
,
String
topic
,
INotifyCallback
notifyHandler
)
{
reqCtx
.
container
.
subscribe
(
topic
,
notifyHandler
);
}
public
void
unsubscribe
(
Re
questContext
reqCtx
,
String
topic
)
public
void
unsubscribe
(
Re
stContext
reqCtx
,
String
topic
)
{
reqCtx
.
container
.
unsubscribe
(
topic
);
}
...
...
@@ -113,7 +112,7 @@ public abstract class BaseHandler
optContainer
.
ifPresent
(
container
->
container
.
unsubscribe
(
topic
));
}
public
void
publish
(
Re
questContext
reqCtx
,
String
topic
,
JsonNode
messageNode
)
public
void
publish
(
Re
stContext
reqCtx
,
String
topic
,
JsonNode
messageNode
)
{
reqCtx
.
container
.
publish
(
topic
,
messageNode
);
}
...
...
@@ -157,32 +156,32 @@ public abstract class BaseHandler
* the create/post of CRUD
* @param reqCtx
*/
public
abstract
void
doCreate
(
Re
que
stContext
reqCtx
);
public
abstract
void
doCreate
(
RestContext
reqCtx
);
/**
* the read/get of CRUD
* @param reqCtx
*/
public
abstract
void
doRead
(
Re
que
stContext
reqCtx
);
public
abstract
void
doRead
(
RestContext
reqCtx
);
/**
* the update/put of CRUD
* @param reqCtx
*/
public
abstract
void
doUpdate
(
Re
que
stContext
reqCtx
);
public
abstract
void
doUpdate
(
RestContext
reqCtx
);
/**
* the delete of CRUD
* @param reqCtx
*/
public
abstract
void
doDelete
(
Re
que
stContext
reqCtx
);
public
abstract
void
doDelete
(
RestContext
reqCtx
);
/**
* represents simple send message - not a rest command
* @param reqCtx
*/
public
void
doMessage
(
Re
que
stContext
reqCtx
)
{};
public
void
doMessage
(
RestContext
reqCtx
)
{};
/**
* reloading configuration or other data
...
...
src/main/java/microservice/handlers/MBIHandler.java
View file @
4002ac06
...
...
@@ -5,7 +5,7 @@ import java.util.Deque;
import
java.util.HashMap
;
import
java.util.Map
;
import
microservice.Re
que
stContext
;
import
microservice.RestContext
;
import
microservice.defs.Constants
;
import
microservice.defs.Enums.EnumHttpMethod
;
import
microservice.io.iface.*
;
...
...
@@ -40,7 +40,7 @@ public class MBIHandler extends RMQHandler implements IContainer
@Override
public
void
handleRequest
(
RMQMessage
request
)
throws
Exception
{
Re
que
stContext
reqContext
=
getRequestContext
(
request
);
RestContext
reqContext
=
getRequestContext
(
request
);
if
(
reqContext
!=
null
)
{
String
sMethod
=
request
.
getParameterByName
(
RMQRestRequest
.
METHOD
);
...
...
@@ -88,9 +88,9 @@ public class MBIHandler extends RMQHandler implements IContainer
* @param exchange
* @return
*/
private
Re
que
stContext
getRequestContext
(
RMQMessage
request
)
private
RestContext
getRequestContext
(
RMQMessage
request
)
{
Re
questContext
reqCtx
=
new
Requ
estContext
();
Re
stContext
reqCtx
=
new
R
estContext
();
reqCtx
.
container
=
this
;
reqCtx
.
request
=
new
IRequestMBIImpl
(
request
);
reqCtx
.
response
=
new
IResponseMBIImpl
(
request
,
listenerRmqId
,
logger
);
...
...
@@ -131,28 +131,28 @@ public class MBIHandler extends RMQHandler implements IContainer
return
paramsMap
;
}
public
void
doGet
(
Re
que
stContext
reqCtx
)
public
void
doGet
(
RestContext
reqCtx
)
{
msHandler
.
doRead
(
reqCtx
);
}
public
void
doPost
(
Re
que
stContext
reqCtx
)
public
void
doPost
(
RestContext
reqCtx
)
{
msHandler
.
doCreate
(
reqCtx
);
}
public
void
doPut
(
Re
que
stContext
reqCtx
)
public
void
doPut
(
RestContext
reqCtx
)
{
msHandler
.
doUpdate
(
reqCtx
);
}
public
void
doDelete
(
Re
que
stContext
reqCtx
)
public
void
doDelete
(
RestContext
reqCtx
)
{
msHandler
.
doDelete
(
reqCtx
);
}
public
void
doMessage
(
Re
que
stContext
reqCtx
)
public
void
doMessage
(
RestContext
reqCtx
)
{
msHandler
.
doMessage
(
reqCtx
);
}
...
...
src/main/java/microservice/handlers/MonitorHandler.java
View file @
4002ac06
...
...
@@ -6,9 +6,7 @@ import java.util.List;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
microservice.io.impl.IMetricsFactoryImpl
;
import
microservice.types.BaseRestResponse
;
import
microservice.MicroserviceApp
;
import
microservice.RequestContext
;
import
microservice.io.iface.IContainer
;
import
microservice.RestContext
;
/**
* this class is for monitoring the microservice
...
...
@@ -35,7 +33,7 @@ public class MonitorHandler extends BaseHandler
}
@Override
public
void
doCreate
(
Re
que
stContext
reqCtx
)
public
void
doCreate
(
RestContext
reqCtx
)
{
switch
(
reqCtx
.
params
[
0
])
{
...
...
@@ -49,7 +47,7 @@ public class MonitorHandler extends BaseHandler
}
private
void
printStats
(
Re
que
stContext
reqCtx
)
{
private
void
printStats
(
RestContext
reqCtx
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
IMetricsFactoryImpl
factoryImpl
=
(
IMetricsFactoryImpl
)
IMetricsFactoryImpl
.
getInstance
();
ObjectNode
objectNode
=
this
.
objMapper
.
createObjectNode
();
...
...
@@ -64,7 +62,7 @@ public class MonitorHandler extends BaseHandler
writeObjectToResponse
(
reqCtx
,
brr
);
}
private
void
reloadServiceApp
(
Re
que
stContext
reqCtx
)
{
private
void
reloadServiceApp
(
RestContext
reqCtx
)
{
this
.
optConfiguration
.
ifPresent
(
cfg
->
cfg
.
reload
());
containers
.
forEach
(
handler
->
handler
.
doReload
());
...
...
@@ -72,7 +70,7 @@ public class MonitorHandler extends BaseHandler
}
@Override
public
void
doRead
(
Re
que
stContext
reqCtx
)
public
void
doRead
(
RestContext
reqCtx
)
{
switch
(
reqCtx
.
params
[
0
])
{
...
...
@@ -84,14 +82,14 @@ public class MonitorHandler extends BaseHandler
}
@Override
public
void
doUpdate
(
Re
que
stContext
reqCtx
)
public
void
doUpdate
(
RestContext
reqCtx
)
{
// TODO Auto-generated method stub
}
@Override
public
void
doDelete
(
Re
que
stContext
reqCtx
)
public
void
doDelete
(
RestContext
reqCtx
)
{
// TODO Auto-generated method stub
...
...
src/main/java/microservice/handlers/Reactor.java
View file @
4002ac06
...
...
@@ -84,6 +84,14 @@ public class Reactor implements CommonServices.IServiceReactor {
methodParams
.
getResourceUri
();
}
public
static
String
buildServiceKey
(
Enums
.
EnumServiceType
enumServiceType
,
CommonServices
.
IServiceCommands
serviceCommands
,
String
resourceUri
)
{
return
new
StringBuilder
(
32
).
append
(
enumServiceType
.
name
()).
append
(
Constants
.
TYPE_PREFIX_SEPERATOR
)
.
append
(
serviceCommands
.
toString
()).
append
(
Constants
.
TYPE_PREFIX_SEPERATOR
)
.
append
(
resourceUri
).
toString
();
}
/**
* delegating the msg/request from the service to the method if any
* @param orgService
...
...
@@ -98,12 +106,15 @@ public class Reactor implements CommonServices.IServiceReactor {
final
PathTemplateMatcher
.
PathMatchResult
<
MethodMatch
>
match
=
methodsMap
.
match
(
key
);
try
{
if
(
match
!=
null
&&
match
.
getValue
().
getMethodCallback
()
!=
null
)
{
msgContext
.
setParameters
(
match
.
getParameters
());
final
BiConsumer
<
CommonServices
.
IMsgContext
,
CommonServices
.
IService
>
methodCallback
=
match
.
getValue
().
getMethodCallback
();
/**
* pre-handling
*/
if
(
metricsFactory
!=
null
)
{
methodMetrics
=
methodMetricsMap
.
get
(
key
);
methodMetrics
=
methodMetricsMap
.
get
(
match
.
getMatchedTemplate
());
if
(
methodMetrics
!=
null
)
methodMetrics
.
preHandle
();
}
methodCallback
.
accept
(
msgContext
,
orgService
);
}
else
{
...
...
@@ -140,8 +151,8 @@ public class Reactor implements CommonServices.IServiceReactor {
* create counters for every method
*/
for
(
String
key
:
methodKeyList
){
IMetricsFactory
.
IMeter
meter
=
metricsFactory
.
createMeter
(
key
);
IMetricsFactory
.
ITimer
timer
=
metricsFactory
.
createTimer
(
key
);
IMetricsFactory
.
IMeter
meter
=
metricsFactory
.
createMeter
(
"Meter:"
+
key
);
IMetricsFactory
.
ITimer
timer
=
metricsFactory
.
createTimer
(
"Timer:"
+
key
);
methodMetricsMap
.
put
(
key
,
new
MethodMetrics
(
meter
,
timer
));
}
}
...
...
src/main/java/microservice/handlers/RestHandler.java
View file @
4002ac06
...
...
@@ -11,7 +11,7 @@ import io.undertow.util.HeaderMap;
import
io.undertow.util.Headers
;
import
io.undertow.util.HttpString
;
import
microservice.MicroserviceApp
;
import
microservice.Re
que
stContext
;
import
microservice.RestContext
;
import
microservice.common.EncryptionUtils
;
import
microservice.defs.Constants
;
import
microservice.defs.Enums
;
...
...
@@ -86,11 +86,11 @@ public class RestHandler implements HttpHandler , IContainer
/*
* async part
*/
Re
que
stContext
reqContext
=
getRequestContext
(
exchange
);
RestContext
reqContext
=
getRequestContext
(
exchange
);
if
(
reqContext
!=
null
)
{
HttpString
requestMethod
=
exchange
.
getRequestMethod
();
EnumHttpMethod
eMethod
=
EnumHttpMethod
.
resolveMethod
(
requestMethod
.
toString
());
reqContext
.
enum
CrudMethod
=
Enums
.
EnumCrudMethod
.
resolveMethodFromHttp
(
eMethod
);
reqContext
.
enum
RestCommands
=
CommonServices
.
EnumRestCommands
.
resolveMethodFromHttp
(
eMethod
);
//exchange. request.setCharacterEncoding(Constants.C_ENCODING_UTF8);
exchange
.
getResponseHeaders
().
put
(
Headers
.
CONTENT_TYPE
,
Constants
.
CONTENT_TYPE_JSON
);
...
...
@@ -143,15 +143,15 @@ public class RestHandler implements HttpHandler , IContainer
/**
* validate the request: service authorization etc.
* @param re
que
stContext
* @param restContext
* @return
*/
private
boolean
validateRequest
(
Re
questContext
requ
estContext
)
private
boolean
validateRequest
(
Re
stContext
r
estContext
)
{
boolean
valid
=
true
;
if
(
msHandler
.
getAuthType
().
equals
(
BaseHandler
.
EnumAuthenticationType
.
JWT
)){
// get token
String
token
=
getJwtToken
(
re
que
stContext
);
String
token
=
getJwtToken
(
restContext
);
if
(
token
!=
null
)
{
try
{
...
...
@@ -160,41 +160,41 @@ public class RestHandler implements HttpHandler , IContainer
// resolve service authorization
UserProfile
up
=
new
UserProfile
(
claims
);
// resolve authorization
valid
=
up
.
isServiceAuthorized
(
appName
,
re
questContext
.
enumCrudMethod
);
valid
=
up
.
isServiceAuthorized
(
appName
,
re
stContext
.
enumRestCommands
);
if
(
valid
)
{
// set user profile
re
que
stContext
.
userProfile
=
up
;
restContext
.
userProfile
=
up
;
}
else
{
sendErrorResp
(
re
que
stContext
.
response
,
METHOD_NOT_AUTHORIZED
);
sendErrorResp
(
restContext
.
response
,
METHOD_NOT_AUTHORIZED
);
}
}
else
{
sendErrorResp
(
re
que
stContext
.
response
,
Constants
.
INVALID_REQUEST_TOKEN
);
sendErrorResp
(
restContext
.
response
,
Constants
.
INVALID_REQUEST_TOKEN
);
}
}
catch
(
ExpiredJwtException
exception
){
sendErrorResp
(
re
que
stContext
.
response
,
TOKEN_EXPIRED
);
sendErrorResp
(
restContext
.
response
,
TOKEN_EXPIRED
);
valid
=
false
;
}
}
else
{
MicroserviceApp
.
getsInstance
().
getLogger
().
error
(
NO_TOKEN_FOR_REQUEST
);
sendErrorResp
(
re
que
stContext
.
response
,
NO_TOKEN_FOR_REQUEST
);
sendErrorResp
(
restContext
.
response
,
NO_TOKEN_FOR_REQUEST
);
valid
=
false
;
}
}
return
valid
;
}
private
String
getJwtToken
(
Re
questContext
requ
estContext
)
{
private
String
getJwtToken
(
Re
stContext
r
estContext
)
{
String
jwt
=
null
;
/*
* jwt token
*/
if
(
jwtTokenInAuthorization
)
{
String
authValue
=
re
que
stContext
.
request
.
getFirstHeader
(
Constants
.
AUTHORIZATION_HEADER
);
String
authValue
=
restContext
.
request
.
getFirstHeader
(
Constants
.
AUTHORIZATION_HEADER
);
if
(
authValue
!=
null
)
{
// "Bearer <token>"
...
...
@@ -206,7 +206,7 @@ public class RestHandler implements HttpHandler , IContainer
else
{
// url - param
jwt
=
requ
estContext
.
getParameter
(
JWT_TOKEN
);
jwt
=
r
estContext
.
getParameter
(
JWT_TOKEN
);
}
return
jwt
;
}
...
...
@@ -260,9 +260,9 @@ public class RestHandler implements HttpHandler , IContainer
* @param exchange
* @return
*/
private
Re
que
stContext
getRequestContext
(
HttpServerExchange
exchange
)
private
RestContext
getRequestContext
(
HttpServerExchange
exchange
)
{
Re
questContext
reqCtx
=
new
Requ
estContext
();
Re
stContext
reqCtx
=
new
R
estContext
();
reqCtx
.
container
=
this
;
reqCtx
.
request
=
new
IRequestRestImpl
(
exchange
);
reqCtx
.
response
=
new
IResponseRestImpl
(
exchange
);
...
...
@@ -290,26 +290,26 @@ public class RestHandler implements HttpHandler , IContainer
}
public
void
doGet
(
Re
que
stContext
reqCtx
)
public
void
doGet
(
RestContext
reqCtx
)
{
msHandler
.
doRead
(
reqCtx
);
}
public
void
doPost
(
Re
que
stContext
reqCtx
)
public
void
doPost
(
RestContext
reqCtx
)
{
msHandler
.
doCreate
(
reqCtx
);
}
public
void
doPut
(
Re
que
stContext
reqCtx
)
public
void
doPut
(
RestContext
reqCtx
)
{
msHandler
.
doUpdate
(
reqCtx
);
}
public
void
doDelete
(
Re
que
stContext
reqCtx
)
public
void
doDelete
(
RestContext
reqCtx
)
{
msHandler
.
doDelete
(
reqCtx
);
...
...
src/main/java/microservice/io/iface/CommonServices.java
View file @
4002ac06
...
...
@@ -5,6 +5,7 @@ import microservice.params.CommandParams;
import
microservice.types.BaseRestResponse
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.function.BiConsumer
;
import
java.util.function.Consumer
;
...
...
@@ -18,6 +19,7 @@ public class CommonServices {
}
public
interface
IMsgContext
{
void
setParameters
(
Map
<
String
,
String
>
parameters
);
}
public
interface
IServiceReactor
{
...
...
@@ -31,6 +33,8 @@ public class CommonServices {
public
abstract
void
run
();
public
abstract
void
shutdown
();
public
abstract
void
handleNotImplmented
(
IMsgContext
msgContext
);
/// when relevant
public
abstract
void
register
(
IServiceDiscovery
serviceDiscovery
,
String
id
);
public
void
setReactor
(
IServiceReactor
reactor
)
{
this
.
reactor
=
reactor
;
...
...
@@ -84,6 +88,26 @@ public class CommonServices {
E_READ
,
E_UPDATE
,
E_DELETE
;
public
static
EnumRestCommands
resolveMethodFromHttp
(
Enums
.
EnumHttpMethod
enumHttpMethod
){
EnumRestCommands
enumRestCommands
=
null
;
switch
(
enumHttpMethod
){
case
E_GET:
enumRestCommands
=
EnumRestCommands
.
E_READ
;
break
;
case
E_POST:
enumRestCommands
=
EnumRestCommands
.
E_CREATE
;
break
;
case
E_PUT:
enumRestCommands
=
EnumRestCommands
.
E_UPDATE
;
break
;
case
E_DELETE:
enumRestCommands
=
EnumRestCommands
.
E_DELETE
;
break
;
}
return
enumRestCommands
;
}
}
public
static
abstract
class
IRestService
extends
IService
{
...
...
@@ -111,6 +135,11 @@ public class CommonServices {
this
.
topic
=
topic
;
this
.
msg
=
msg
;
}
@Override
public
void
setParameters
(
Map
<
String
,
String
>
parameters
)
{
}
}
public
abstract
void
subscribe
(
String
topic
,
Consumer
<
String
>
notifyFunc
);
...
...
src/main/java/microservice/io/impl/service/IPubSubServiceMqttImpl.java
View file @
4002ac06
...
...
@@ -3,6 +3,7 @@ package microservice.io.impl.service;
import
com.fasterxml.jackson.databind.JsonNode
;
import
common.JsonHandler
;
import
microservice.io.iface.CommonServices
;
import
microservice.io.iface.IServiceDiscovery
;
import
org.eclipse.paho.client.mqttv3.*
;
import
java.util.HashMap
;
...
...
@@ -137,4 +138,9 @@ public class IPubSubServiceMqttImpl extends CommonServices.IPubSubService {
public
void
handleNotImplmented
(
CommonServices
.
IMsgContext
msgContext
)
{
}
@Override
public
void
register
(
IServiceDiscovery
serviceDiscovery
,
String
id
)
{
}
}
src/main/java/microservice/io/impl/service/IRestServiceHttpImpl.java
View file @
4002ac06
This diff is collapsed.
Click to expand it.
src/main/java/microservice/types/UserProfile.java
View file @
4002ac06
...
...
@@ -3,6 +3,7 @@ package microservice.types;
import
io.jsonwebtoken.Claims
;
import
io.jsonwebtoken.impl.DefaultClaims
;
import
microservice.defs.Enums
;
import
microservice.io.iface.CommonServices
;
import
java.util.HashMap
;
import
java.util.Map
;
...
...
@@ -62,14 +63,14 @@ public class UserProfile extends DefaultClaims {
return
serviceAuthorizations
;
}
public
boolean
isServiceAuthorized
(
String
serviceName
,
Enums
.
EnumCrudMethod
enumCrudMethod
){
public
boolean
isServiceAuthorized
(
String
serviceName
,
CommonServices
.
EnumRestCommands
enumRestCommands
){
boolean
authorized
=
false
;
String
serviceAuth
=
serviceAuthorizations
.
get
(
serviceName
);
if
(
serviceAuth
==
null
)
// check for all services authorizations
serviceAuth
=
serviceAuthorizations
.
get
(
ALL_SERVICES
);
if
(
serviceAuth
!=
null
)
authorized
=
serviceAuth
.
getBytes
()[
enum
CrudMethod
.
ordinal
()]
==
'Y'
;
authorized
=
serviceAuth
.
getBytes
()[
enum
RestCommands
.
ordinal
()]
==
'Y'
;
return
authorized
;
}
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
4002ac06
package
microservice
;
import
com.fasterxml.jackson.databind.node.JsonNodeFactory
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
import
microservice.defs.Enums
;
import
microservice.io.iface.CommonServices
;
import
microservice.io.iface.ICommandClient
;
...
...
@@ -78,22 +80,26 @@ public class TestMicroserviceApp {
.
withMonitoring
()
//.withDefaultServiceAuthorization()
.
addService
(
Enums
.
EnumServiceType
.
E_REST
,
new
IRestServiceHttpImpl
(
new
RestServerParams
(
32000
,
"localhost"
,
2
)),
"undertowRestService"
)
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/resource/r1"
,(
msgCtx
,
orgService
)
->
{
BaseRestResponse
brr
;
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/resource/{rid}"
,(
msgCtx
,
orgService
)
->
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
RestContext
restContext
=
(
RestContext
)
msgCtx
;
msApp
.
getLogger
();
CommonServices
.
IRestService
restService
=
(
CommonServices
.
IRestService
)
orgService
;
//brr.setObject({ "a":"b" })
//restService.writeResponse(brr);
String
rid
=
restContext
.
getParameter
(
"rid"
);
ObjectNode
objectNode
=
JsonNodeFactory
.
instance
.
objectNode
();
if
(
restContext
.
pathParameters
!=
null
)
restContext
.
pathParameters
.
forEach
((
key
,
value
)
->
objectNode
.
put
(
key
,
value
));
brr
.
objectNode
=
objectNode
;
restContext
.
container
.
writeObjectToResponse
(
restContext
.
response
,
brr
);
})
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/resource/r
1
"
,(
msgCtx
,
orgService
)
->
{
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/resource/r
2
"
,(
msgCtx
,
orgService
)
->
{
CommonServices
.
IRestService
outRestService
=
(
CommonServices
.
IRestService
)
MicroserviceApp
.
getsInstance
().
getService
(
Enums
.
EnumServiceType
.
E_REST
,
"undertowRestService"
);
CommonServices
.
IRestService
restService
=
(
CommonServices
.
IRestService
)
orgService
;
CommandParams
cmdParams
=
new
CommandParams
();
BaseRestResponse
brr
=
restService
.
read
(
cmdParams
);
//restService.writeResponse(brr);
})
.
build
()
.
run
();
.
_
build
()
.
_
run
();
}
}
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