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
ba2123c7
authored
May 10, 2017
by
amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
end of day commits
parent
fd1f5d80
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
337 additions
and
51 deletions
src/main/java/microservice/io/impl/IRestClientRestImpl.java
src/main/java/microservice/io/impl/service/IRestServiceHttpImpl.java
src/main/java/microservice/types/BaseRestResponse.java
src/main/java/microservice/utils/RestHttpClient.java
src/main/java/microservice/utils/ServiceBuilderFactory.java
src/test/java/microservice/TestMicroserviceApp.java
src/main/java/microservice/io/impl/IRestClientRestImpl.java
View file @
ba2123c7
package
microservice
.
io
.
impl
;
package
microservice
.
io
.
impl
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.Optional
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
java.util.concurrent.LinkedBlockingQueue
;
import
com.fasterxml.jackson.databind.deser.Deserializers
;
import
common.JsonHandler
;
import
http.StringResponse
;
import
microservice.defs.Enums
;
import
microservice.defs.Enums
;
import
microservice.io.iface.IServiceDiscovery
;
import
microservice.io.iface.IServiceDiscovery
;
import
microservice.utils.RestHttpClient
;
import
rx.Observable
;
import
rx.Observable
;
import
rx.Subscriber
;
import
rx.Subscriber
;
import
http.simpleRestClient.SimpleRestClient
;
import
http.simpleRestClient.SimpleRestClient
;
...
@@ -55,14 +60,14 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -55,14 +60,14 @@ public class IRestClientRestImpl implements ICommandClient
@Override
@Override
public
void
call
(
Subscriber
<?
super
BaseRestResponse
>
observer
)
public
void
call
(
Subscriber
<?
super
BaseRestResponse
>
observer
)
{
{
SimpleRestResponse
s
rr
=
null
;
BaseRestResponse
b
rr
=
null
;
try
{
try
{
if
(!
observer
.
isUnsubscribed
())
{
if
(!
observer
.
isUnsubscribed
())
{
if
(
reqCtx
.
getParams
()
!=
null
)
if
(
reqCtx
.
getParams
()
!=
null
)
srr
=
httpRestClient
.
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
else
else
srr
=
httpRestClient
.
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
observer
.
onNext
(
getBaseRestResponse
(
srr
)
);
observer
.
onNext
(
brr
);
observer
.
onCompleted
();
observer
.
onCompleted
();
}
}
}
catch
(
Exception
e
)
{
}
catch
(
Exception
e
)
{
...
@@ -102,12 +107,12 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -102,12 +107,12 @@ public class IRestClientRestImpl implements ICommandClient
@Override
@Override
protected
BaseRestResponse
run
()
throws
Exception
protected
BaseRestResponse
run
()
throws
Exception
{
{
SimpleRestResponse
s
rr
=
null
;
BaseRestResponse
b
rr
=
null
;
if
(
reqCtx
.
getParams
()
!=
null
)
if
(
reqCtx
.
getParams
()
!=
null
)
srr
=
httpRestClient
.
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
else
else
srr
=
httpRestClient
.
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
get
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
return
getBaseRestResponse
(
srr
)
;
return
brr
;
}
}
}
}
...
@@ -122,16 +127,19 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -122,16 +127,19 @@ public class IRestClientRestImpl implements ICommandClient
@Override
@Override
protected
BaseRestResponse
run
()
throws
Exception
protected
BaseRestResponse
run
()
throws
Exception
{
{
SimpleRestResponse
srr
=
null
;
BaseRestResponse
brr
=
null
;
String
resp
=
null
;
if
(
reqCtx
.
getParams
()
!=
null
)
if
(
reqCtx
.
getParams
()
!=
null
)
srr
=
httpRestClient
.
post
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
brr
=
httpRestClient
.
_
post
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
else
else
srr
=
httpRestClient
.
post
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
brr
=
httpRestClient
.
_post
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
return
getBaseRestResponse
(
srr
);
return
brr
;
}
}
}
}
private
class
PutCommand
extends
Command
{
private
class
PutCommand
extends
Command
{
public
PutCommand
(
CommandParams
reqCtx
)
public
PutCommand
(
CommandParams
reqCtx
)
...
@@ -142,12 +150,13 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -142,12 +150,13 @@ public class IRestClientRestImpl implements ICommandClient
@Override
@Override
protected
BaseRestResponse
run
()
throws
Exception
protected
BaseRestResponse
run
()
throws
Exception
{
{
SimpleRestResponse
s
rr
=
null
;
BaseRestResponse
b
rr
=
null
;
if
(
reqCtx
.
getParams
()
!=
null
)
if
(
reqCtx
.
getParams
()
!=
null
)
srr
=
httpRestClient
.
put
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
brr
=
httpRestClient
.
_
put
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
else
else
srr
=
httpRestClient
.
put
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
brr
=
httpRestClient
.
_put
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
(),
reqCtx
.
getContent
());
return
getBaseRestResponse
(
srr
);
return
brr
;
}
}
}
}
...
@@ -162,13 +171,13 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -162,13 +171,13 @@ public class IRestClientRestImpl implements ICommandClient
@Override
@Override
protected
BaseRestResponse
run
()
throws
Exception
protected
BaseRestResponse
run
()
throws
Exception
{
{
SimpleRestResponse
s
rr
=
null
;
BaseRestResponse
b
rr
=
null
;
if
(
reqCtx
.
getParams
()
!=
null
)
if
(
reqCtx
.
getParams
()
!=
null
)
srr
=
httpRestClient
.
delete
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
delete
(
reqCtx
.
getEntity
(),
reqCtx
.
getParams
(),
reqCtx
.
getRequestParams
());
else
else
srr
=
httpRestClient
.
delete
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
brr
=
httpRestClient
.
_
delete
(
reqCtx
.
getEntity
(),
reqCtx
.
getParamsString
(),
reqCtx
.
getRequestParams
());
BaseRestResponse
brr
=
getBaseRestResponse
(
srr
);
return
getBaseRestResponse
(
srr
)
;
return
brr
;
}
}
}
}
...
@@ -212,7 +221,7 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -212,7 +221,7 @@ public class IRestClientRestImpl implements ICommandClient
/*********************************************************************************************************************************************/
/*********************************************************************************************************************************************/
private
static
final
String
COMMAND_ERROR
=
"Command Error: "
;
private
static
final
String
COMMAND_ERROR
=
"Command Error: "
;
RestClientParams
clientParams
=
null
;
RestClientParams
clientParams
=
null
;
SimpleRest
Client
httpRestClient
=
null
;
RestHttp
Client
httpRestClient
=
null
;
// HystrixMetricsPoller poller = null;
// HystrixMetricsPoller poller = null;
// MetricJsonListener jsonListener = null;
// MetricJsonListener jsonListener = null;
Optional
<
IServiceDiscovery
>
serviceDiscovery
=
Optional
.
empty
();
Optional
<
IServiceDiscovery
>
serviceDiscovery
=
Optional
.
empty
();
...
@@ -226,7 +235,10 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -226,7 +235,10 @@ public class IRestClientRestImpl implements ICommandClient
this
.
clientParams
=
(
RestClientParams
)
params
;
this
.
clientParams
=
(
RestClientParams
)
params
;
else
else
throw
new
Exception
(
"wrong initialization params"
+
params
.
getClass
().
getName
());
throw
new
Exception
(
"wrong initialization params"
+
params
.
getClass
().
getName
());
httpRestClient
=
new
SimpleRestClient
(
clientParams
.
getServiceName
(),
clientParams
.
getAddress
());
if
(
clientParams
.
getAddress
()
==
null
||
clientParams
.
getAddress
().
isEmpty
())
httpRestClient
=
new
RestHttpClient
();
//SimpleRestClient(clientParams.getServiceName(),null,0);
else
httpRestClient
=
new
RestHttpClient
(
clientParams
.
getServiceName
(),
clientParams
.
getAddress
());
httpRestClient
.
Initialize
(
clientParams
.
getMaxConnection
());
httpRestClient
.
Initialize
(
clientParams
.
getMaxConnection
());
if
(
clientParams
.
isMetricsEnabled
())
if
(
clientParams
.
isMetricsEnabled
())
{
{
...
@@ -235,8 +247,10 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -235,8 +247,10 @@ public class IRestClientRestImpl implements ICommandClient
// poller = new HystrixMetricsPoller(jsonListener, POLLING_DELAY);
// poller = new HystrixMetricsPoller(jsonListener, POLLING_DELAY);
// poller.start();
// poller.start();
}
}
}
}
private
synchronized
void
initMetricsPublisher
()
{
private
synchronized
void
initMetricsPublisher
()
{
String
publisherStr
=
System
.
getProperty
(
HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION
);
String
publisherStr
=
System
.
getProperty
(
HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION
);
if
(
publisherStr
==
null
)
{
if
(
publisherStr
==
null
)
{
...
@@ -270,18 +284,44 @@ public class IRestClientRestImpl implements ICommandClient
...
@@ -270,18 +284,44 @@ public class IRestClientRestImpl implements ICommandClient
}
}
private
BaseRestResponse
getBaseRestResponse
(
SimpleRestResponse
srr
)
// private BaseRestResponse getBaseRestResponse(SimpleRestResponse srr)
{
// {
BaseRestResponse
brr
;
// BaseRestResponse brr;
if
(
srr
!=
null
)
// if (srr != null)
{
// {
brr
=
new
BaseRestResponse
(
srr
.
isSuccess
(),
srr
.
getError
());
// brr = new BaseRestResponse(srr.isSuccess(), srr.getError());
brr
.
objectNode
=
srr
.
objectNode
;
// brr.objectNode = srr.objectNode;
}
// }
else
// else
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"null response"
);
// brr = new BaseRestResponse(false, COMMAND_ERROR + "null response");
return
brr
;
// return brr;
}
// }
//
// private BaseRestResponse getBaseRestResponse(StringResponse srr)
// {
// BaseRestResponse brr = null;
// if (srr != null)
// {
// if (srr.getStatusCode() == 200){
// final JsonNode jsonNode = JsonHandler.getJsonNodeFromString(srr.getContent());
// /**
// * check for BaseRestResponse format of regular json
// */
// final boolean allMatch = brrFieldNames.stream().allMatch(name -> jsonNode.has(name));
// if (allMatch) {
// brr = (BaseRestResponse)JsonHandler.getNodeAsObject(jsonNode,BaseRestResponse.class);
// } else {
// brr = new BaseRestResponse(true,null);
// brr.objectNode = jsonNode;
// }
// } else {
// brr = new BaseRestResponse(false,COMMAND_ERROR + "Error response: " + String.valueOf(srr.getStatusCode()));
// }
// }
// else
// brr = new BaseRestResponse(false,COMMAND_ERROR + "null response");
// return brr;
// }
@Override
@Override
public
BaseRestResponse
create
(
CommandParams
reqCtx
)
public
BaseRestResponse
create
(
CommandParams
reqCtx
)
...
...
src/main/java/microservice/io/impl/service/IRestServiceHttpImpl.java
View file @
ba2123c7
...
@@ -53,7 +53,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
...
@@ -53,7 +53,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
private
String
appName
;
private
String
appName
;
public
ObjectMapper
objMapper
=
null
;
public
ObjectMapper
objMapper
=
null
;
protected
Enums
.
EnumAuthenticationType
authType
=
Enums
.
EnumAuthenticationType
.
DEFAULT
;
protected
Enums
.
EnumAuthenticationType
authType
=
Enums
.
EnumAuthenticationType
.
DEFAULT
;
MicroserviceClient
restClient
=
null
;
/**
/**
* is the jwt token in the Authorization header or in the request query param
* is the jwt token in the Authorization header or in the request query param
...
@@ -143,18 +142,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
...
@@ -143,18 +142,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
// build
// build
this
.
restServer
=
serverBuilder
.
build
();
this
.
restServer
=
serverBuilder
.
build
();
/**
* create client
*/
try
{
BaseClientParams
clientParams
=
new
RestClientParams
(
""
,
true
,
10
,
null
,
null
);
IRestClientRestImpl
cmdClient
=
new
IRestClientRestImpl
(
clientParams
)
.
withServiceDiscovery
(
MicroserviceApp
.
getsInstance
().
getServiceDiscovery
());
restClient
=
new
MicroserviceClient
(
cmdClient
,
clientParams
);
}
catch
(
Exception
exp
)
{
logger
.
error
(
exp
.
toString
());
return
false
;
}
return
true
;
return
true
;
}
}
...
...
src/main/java/microservice/types/BaseRestResponse.java
View file @
ba2123c7
...
@@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.JsonNode;
...
@@ -5,6 +5,10 @@ import com.fasterxml.jackson.databind.JsonNode;
public
class
BaseRestResponse
public
class
BaseRestResponse
{
{
public
static
final
String
SUCCESS
=
"success"
;
public
static
final
String
ERROR
=
"error"
;
public
static
final
String
OBJECT_NODE
=
"objectNode"
;
public
BaseRestResponse
(
boolean
success
,
String
error
)
public
BaseRestResponse
(
boolean
success
,
String
error
)
{
{
super
();
super
();
...
...
src/main/java/microservice/utils/RestHttpClient.java
0 → 100644
View file @
ba2123c7
package
microservice
.
utils
;
import
com.fasterxml.jackson.databind.JsonNode
;
import
common.JsonHandler
;
import
http.StringResponse
;
import
http.simpleHttpClient.SimpleHttpRequest
;
import
http.simpleHttpClient.SimpleHttpResponse
;
import
http.simpleRestClient.SimpleRestClient
;
import
http.simpleRestClient.SimpleRestResponse
;
import
microservice.types.BaseRestResponse
;
import
java.lang.reflect.Field
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.function.Supplier
;
import
static
microservice
.
io
.
iface
.
ICommandClient
.
COMMAND_ERROR
;
/**
* Created by amir on 10/05/17.
* for tehe open/close principle
*/
public
class
RestHttpClient
extends
SimpleRestClient
{
List
<
String
>
brrFieldNames
=
null
;
public
RestHttpClient
(
String
app
,
String
ip
,
int
port
)
{
super
(
app
,
ip
,
port
);
}
public
RestHttpClient
(
String
app
,
String
IpPort
)
{
super
(
app
,
IpPort
);
initBaseRestValidation
();
}
public
RestHttpClient
()
{
super
(
null
,
null
,
0
);
initBaseRestValidation
();
}
private
void
initBaseRestValidation
()
{
final
Field
[]
brrFields
=
BaseRestResponse
.
class
.
getDeclaredFields
();
brrFieldNames
=
new
ArrayList
<>();
for
(
Field
field
:
brrFields
){
brrFieldNames
.
add
(
field
.
getName
());
}
}
private
BaseRestResponse
executeRequest
(
String
entity
,
Supplier
<
BaseRestResponse
>
executeFunc
){
BaseRestResponse
ret
;
if
(
domain
==
null
)
{
domain
=
entity
;
entity
=
null
;
ret
=
executeFunc
.
get
();
domain
=
null
;
}
else
ret
=
executeFunc
.
get
();
return
ret
;
}
public
BaseRestResponse
_get
(
String
entity
,
String
[]
params
,
String
requestParams
)
{
String
paramPath
=
""
;
if
(
params
==
null
)
{
return
this
.
_get
(
entity
,
(
String
)
null
,
requestParams
);
}
else
{
for
(
int
i
=
0
;
i
<
params
.
length
;
++
i
)
{
paramPath
=
paramPath
+
"/"
+
params
[
i
];
}
return
this
.
_get
(
entity
,
paramPath
,
requestParams
);
}
}
public
BaseRestResponse
_get
(
String
entity
,
String
params
,
String
requestParams
)
{
StringResponse
srr
;
if
(
domain
==
null
)
{
domain
=
entity
;
entity
=
null
;
srr
=
super
.
getAsString
(
entity
,
params
,
requestParams
);
domain
=
null
;
}
else
srr
=
super
.
getAsString
(
entity
,
params
,
requestParams
);
return
getBaseRestResponse
(
srr
);
}
public
BaseRestResponse
_post
(
String
entity
,
String
[]
params
,
String
requestParams
,
String
content
)
{
String
paramPath
=
""
;
if
(
params
==
null
)
{
return
this
.
_post
(
entity
,
(
String
[])
null
,
requestParams
,
content
);
}
else
{
for
(
int
i
=
0
;
i
<
params
.
length
;
++
i
)
{
paramPath
=
paramPath
+
"/"
+
params
[
i
];
}
return
this
.
_post
(
entity
,
paramPath
,
requestParams
,
content
);
}
}
public
BaseRestResponse
_post
(
String
entity
,
String
params
,
String
requestParams
,
String
content
)
{
String
resp
;
if
(
domain
==
null
)
{
domain
=
entity
;
entity
=
null
;
resp
=
super
.
postAsString
(
entity
,
params
,
requestParams
,
content
);
domain
=
null
;
}
else
resp
=
super
.
postAsString
(
entity
,
params
,
requestParams
,
content
);
return
getBaseRestResponse
(
resp
);
}
public
BaseRestResponse
_put
(
String
entity
,
String
[]
params
,
String
requestParams
,
String
content
)
{
String
paramPath
=
""
;
if
(
params
==
null
)
{
return
this
.
_put
(
entity
,
(
String
[])
null
,
requestParams
,
content
);
}
else
{
for
(
int
i
=
0
;
i
<
params
.
length
;
++
i
)
{
paramPath
=
paramPath
+
"/"
+
params
[
i
];
}
return
this
.
_put
(
entity
,
paramPath
,
requestParams
,
content
);
}
}
public
BaseRestResponse
_put
(
final
String
entity
,
String
params
,
String
requestParams
,
String
content
)
{
BaseRestResponse
brr
=
executeRequest
(
entity
,()
->
executePut
(
entity
,
params
,
requestParams
,
content
));
// if (domain == null) {
// domain = entity;
// entity = null;
// brr = executePut(entity, params, requestParams, content);
// domain = null;
// } else
// brr = executePut(entity, params, requestParams, content);
return
brr
;
}
public
BaseRestResponse
executePut
(
String
entity
,
String
params
,
String
requestParams
,
String
content
)
{
BaseRestResponse
brr
;
SimpleHttpRequest
request
=
this
.
buildPutRequest
(
entity
,
params
,
requestParams
,
content
);
try
{
SimpleHttpResponse
response
=
this
.
processRequest
(
request
);
brr
=
getBaseRestResponse
(
response
);
}
catch
(
Exception
exp
)
{
brr
=
new
BaseRestResponse
(
false
,
exp
.
toString
());
}
return
brr
;
}
public
BaseRestResponse
_delete
(
String
entity
,
String
[]
params
,
String
requestParams
)
{
String
paramPath
=
""
;
if
(
params
==
null
)
{
return
this
.
_delete
(
entity
,
(
String
)
null
,
requestParams
);
}
else
{
for
(
int
i
=
0
;
i
<
params
.
length
;
++
i
)
{
paramPath
=
paramPath
+
"/"
+
params
[
i
];
}
return
this
.
_delete
(
entity
,
paramPath
,
requestParams
);
}
}
public
BaseRestResponse
_delete
(
final
String
entity
,
String
params
,
String
requestParams
)
{
BaseRestResponse
brr
=
executeRequest
(
entity
,()
->
executeDelete
(
entity
,
params
,
requestParams
));
// if (domain == null) {
// domain = entity;
// entity = null;
// brr = executeDelete(entity, params, requestParams);
// domain = null;
// } else
// brr = executeDelete(entity, params, requestParams);
//
return
brr
;
}
private
BaseRestResponse
executeDelete
(
String
entity
,
String
params
,
String
requestParams
)
{
BaseRestResponse
brr
;
SimpleHttpRequest
request
=
this
.
buildDeleteRequest
(
entity
,
params
,
requestParams
);
try
{
SimpleHttpResponse
response
=
this
.
processRequest
(
request
);
brr
=
getBaseRestResponse
(
response
);
}
catch
(
Exception
exp
)
{
brr
=
new
BaseRestResponse
(
false
,
exp
.
toString
());
}
return
brr
;
}
private
BaseRestResponse
getBaseRestResponse
(
String
resp
)
{
StringResponse
srr
;
if
(
resp
!=
null
){
srr
=
new
StringResponse
(
200
,
resp
);
}
else
{
srr
=
new
StringResponse
(
400
,
null
);
}
return
getBaseRestResponse
(
srr
);
}
private
BaseRestResponse
getBaseRestResponse
(
SimpleRestResponse
srr
)
{
BaseRestResponse
brr
;
if
(
srr
!=
null
)
{
brr
=
new
BaseRestResponse
(
srr
.
isSuccess
(),
srr
.
getError
());
brr
.
objectNode
=
srr
.
objectNode
;
}
else
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"null response"
);
return
brr
;
}
private
BaseRestResponse
getBaseRestResponse
(
StringResponse
srr
)
{
BaseRestResponse
brr
=
null
;
if
(
srr
!=
null
)
{
if
(
srr
.
getStatusCode
()
==
200
){
final
JsonNode
jsonNode
=
JsonHandler
.
getJsonNodeFromString
(
srr
.
getContent
());
/**
* check for BaseRestResponse format of regular json
*/
final
boolean
allMatch
=
brrFieldNames
.
stream
().
allMatch
(
name
->
jsonNode
.
has
(
name
));
if
(
allMatch
)
{
brr
=
(
BaseRestResponse
)
JsonHandler
.
getNodeAsObject
(
jsonNode
,
BaseRestResponse
.
class
);
}
else
{
brr
=
new
BaseRestResponse
(
true
,
null
);
brr
.
objectNode
=
jsonNode
;
}
}
else
{
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"Error response: "
+
String
.
valueOf
(
srr
.
getStatusCode
()));
}
}
else
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"null response"
);
return
brr
;
}
private
BaseRestResponse
getBaseRestResponse
(
SimpleHttpResponse
response
)
{
BaseRestResponse
brr
=
null
;
if
(
response
!=
null
){
if
(
response
.
getStatusCode
()
==
200
){
brr
=
getBaseRestResponse
(
new
StringResponse
(
200
,
response
.
getContent
()));
}
else
{
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"Error response: "
+
String
.
valueOf
(
response
.
getStatusCode
()));
}
}
else
{
brr
=
new
BaseRestResponse
(
false
,
COMMAND_ERROR
+
"null response"
);
}
return
brr
;
}
}
src/main/java/microservice/utils/ServiceBuilderFactory.java
View file @
ba2123c7
...
@@ -101,6 +101,7 @@ public class ServiceBuilderFactory {
...
@@ -101,6 +101,7 @@ public class ServiceBuilderFactory {
break
;
break
;
case
E_CLIENT_SERVER:
case
E_CLIENT_SERVER:
if
(
this
.
restServerParams
==
null
||
this
.
restClientParams
==
null
)
if
(
this
.
restServerParams
==
null
||
this
.
restClientParams
==
null
)
return
false
;
break
;
break
;
}
}
return
true
;
return
true
;
...
...
src/test/java/microservice/TestMicroserviceApp.java
View file @
ba2123c7
...
@@ -77,8 +77,9 @@ public class TestMicroserviceApp {
...
@@ -77,8 +77,9 @@ public class TestMicroserviceApp {
System
.
setProperty
(
"configFile.location"
,
"/opt/mcx/config/config.properties"
);
System
.
setProperty
(
"configFile.location"
,
"/opt/mcx/config/config.properties"
);
String
appName
=
"testApp"
;
String
appName
=
"testApp"
;
CommonServices
.
IService
restService
=
ServiceBuilderFactory
.
createRestServiceHttpBuilder
(
CommonServices
.
EnumRestServiceMode
.
E_SERVER
)
CommonServices
.
IService
restService
=
ServiceBuilderFactory
.
createRestServiceHttpBuilder
(
CommonServices
.
EnumRestServiceMode
.
E_
CLIENT_
SERVER
)
.
hasRestServerParams
(
new
RestServerParams
(
32000
,
"localhost"
,
2
))
.
hasRestServerParams
(
new
RestServerParams
(
32000
,
"localhost"
,
2
))
.
hasRestClientParams
(
new
RestClientParams
(
null
,
false
,
0
,
null
,
null
))
.
build
();
.
build
();
microservice
.
MicroserviceApp
msApp
=
new
microservice
.
MicroserviceApp
(
appName
);
microservice
.
MicroserviceApp
msApp
=
new
microservice
.
MicroserviceApp
(
appName
);
msApp
.
withMetrics
()
msApp
.
withMetrics
()
...
@@ -89,17 +90,20 @@ public class TestMicroserviceApp {
...
@@ -89,17 +90,20 @@ public class TestMicroserviceApp {
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
RestContext
restContext
=
(
RestContext
)
msgCtx
;
RestContext
restContext
=
(
RestContext
)
msgCtx
;
msApp
.
getLogger
();
msApp
.
getLogger
();
String
rid
=
restContext
.
getParameter
(
"rid"
);
ObjectNode
objectNode
=
JsonNodeFactory
.
instance
.
objectNode
();
ObjectNode
objectNode
=
JsonNodeFactory
.
instance
.
objectNode
();
if
(
restContext
.
pathParameters
!=
null
)
if
(
restContext
.
pathParameters
!=
null
)
restContext
.
pathParameters
.
forEach
((
key
,
value
)
->
objectNode
.
put
(
key
,
value
));
restContext
.
pathParameters
.
forEach
((
key
,
value
)
->
objectNode
.
put
(
key
,
value
));
brr
.
objectNode
=
objectNode
;
brr
.
objectNode
=
objectNode
;
restContext
.
container
.
writeObjectToResponse
(
restContext
.
response
,
brr
);
restContext
.
container
.
writeObjectToResponse
(
restContext
.
response
,
brr
);
})
})
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/re
source/r2
"
,(
msgCtx
,
orgService
)
->
{
.
addMethod
(
Enums
.
EnumServiceType
.
E_REST
,
CommonServices
.
EnumRestCommands
.
E_READ
,
"/re
gistry/{query}
"
,(
msgCtx
,
orgService
)
->
{
CommonServices
.
IRestService
inRestService
=
(
CommonServices
.
IRestService
)
MicroserviceApp
.
getsInstance
().
getService
(
Enums
.
EnumServiceType
.
E_REST
,
"undertowRestService"
);
CommonServices
.
IRestService
inRestService
=
(
CommonServices
.
IRestService
)
MicroserviceApp
.
getsInstance
().
getService
(
Enums
.
EnumServiceType
.
E_REST
,
"undertowRestService"
);
CommandParams
cmdParams
=
new
CommandParams
();
CommandParams
cmdParams
=
new
CommandParams
();
RestContext
restContext
=
(
RestContext
)
msgCtx
;
RestContext
restContext
=
(
RestContext
)
msgCtx
;
String
query
=
restContext
.
getPathParameter
(
"query"
);
cmdParams
.
setEntity
(
"172.16.1.132:5000"
);
cmdParams
.
setParamsString
(
"/v1/search"
);
cmdParams
.
setRequestParams
(
"q="
+
query
);
BaseRestResponse
brr
=
inRestService
.
read
(
cmdParams
);
BaseRestResponse
brr
=
inRestService
.
read
(
cmdParams
);
restContext
.
container
.
writeObjectToResponse
(
restContext
.
response
,
brr
);
restContext
.
container
.
writeObjectToResponse
(
restContext
.
response
,
brr
);
})
})
...
...
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