Commit c9cb636d by amir

end of day commits

parent f8a6fbf5
package microservice.io.impl;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
import com.fasterxml.jackson.databind.deser.Deserializers;
import common.JsonHandler;
import http.StringResponse;
import microservice.defs.Enums;
import microservice.io.iface.IServiceDiscovery;
import microservice.utils.RestHttpClient;
import rx.Observable;
import rx.Subscriber;
import http.simpleRestClient.SimpleRestClient;
......@@ -55,14 +60,14 @@ public class IRestClientRestImpl implements ICommandClient
@Override
public void call(Subscriber<? super BaseRestResponse> observer)
{
SimpleRestResponse srr = null;
BaseRestResponse brr = null;
try {
if (!observer.isUnsubscribed()) {
if (reqCtx.getParams() != null)
srr = httpRestClient.get(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
else
srr = httpRestClient.get(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
observer.onNext(getBaseRestResponse(srr));
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
observer.onNext(brr);
observer.onCompleted();
}
} catch (Exception e) {
......@@ -102,12 +107,12 @@ public class IRestClientRestImpl implements ICommandClient
@Override
protected BaseRestResponse run() throws Exception
{
SimpleRestResponse srr = null;
BaseRestResponse brr = null;
if (reqCtx.getParams() != null)
srr = httpRestClient.get(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
else
srr = httpRestClient.get(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
return getBaseRestResponse(srr);
brr = httpRestClient._get(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
return brr;
}
}
......@@ -122,16 +127,19 @@ public class IRestClientRestImpl implements ICommandClient
@Override
protected BaseRestResponse run() throws Exception
{
SimpleRestResponse srr = null;
BaseRestResponse brr = null;
String resp = 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
srr = httpRestClient.post(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams(),reqCtx.getContent());
return getBaseRestResponse(srr);
brr = httpRestClient._post(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams(),reqCtx.getContent());
return brr;
}
}
private class PutCommand extends Command {
public PutCommand(CommandParams reqCtx)
......@@ -142,12 +150,13 @@ public class IRestClientRestImpl implements ICommandClient
@Override
protected BaseRestResponse run() throws Exception
{
SimpleRestResponse srr = null;
BaseRestResponse brr = 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
srr = httpRestClient.put(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams(),reqCtx.getContent());
return getBaseRestResponse(srr);
brr = httpRestClient._put(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams(),reqCtx.getContent());
return brr;
}
}
......@@ -162,13 +171,13 @@ public class IRestClientRestImpl implements ICommandClient
@Override
protected BaseRestResponse run() throws Exception
{
SimpleRestResponse srr = null;
BaseRestResponse brr = null;
if (reqCtx.getParams() != null)
srr = httpRestClient.delete(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
brr = httpRestClient._delete(reqCtx.getEntity(), reqCtx.getParams(), reqCtx.getRequestParams());
else
srr = httpRestClient.delete(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
BaseRestResponse brr = getBaseRestResponse(srr);
return getBaseRestResponse(srr);
brr = httpRestClient._delete(reqCtx.getEntity(), reqCtx.getParamsString(),reqCtx.getRequestParams());
return brr;
}
}
......@@ -212,7 +221,7 @@ public class IRestClientRestImpl implements ICommandClient
/*********************************************************************************************************************************************/
private static final String COMMAND_ERROR = "Command Error: ";
RestClientParams clientParams = null;
SimpleRestClient httpRestClient = null;
RestHttpClient httpRestClient = null;
// HystrixMetricsPoller poller = null;
// MetricJsonListener jsonListener = null;
Optional<IServiceDiscovery> serviceDiscovery = Optional.empty();
......@@ -226,7 +235,10 @@ public class IRestClientRestImpl implements ICommandClient
this.clientParams = (RestClientParams)params;
else
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());
if (clientParams.isMetricsEnabled())
{
......@@ -235,8 +247,10 @@ public class IRestClientRestImpl implements ICommandClient
// poller = new HystrixMetricsPoller(jsonListener, POLLING_DELAY);
// poller.start();
}
}
private synchronized void initMetricsPublisher() {
String publisherStr = System.getProperty(HYSTRIX_PLUGIN_HYSTRIX_METRICS_PUBLISHER_IMPLEMENTATION);
if(publisherStr == null) {
......@@ -270,18 +284,44 @@ public class IRestClientRestImpl implements ICommandClient
}
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(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;
// }
@Override
public BaseRestResponse create(CommandParams reqCtx)
......
......@@ -53,7 +53,6 @@ public class IRestServiceHttpImpl extends CommonServices.IRestService implements
private String appName;
public ObjectMapper objMapper = null;
protected Enums.EnumAuthenticationType authType = Enums.EnumAuthenticationType.DEFAULT;
MicroserviceClient restClient = null;
/**
* 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
// 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;
}
......
......@@ -4,7 +4,11 @@ import com.fasterxml.jackson.databind.JsonNode;
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)
{
super();
......
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;
}
}
......@@ -101,6 +101,7 @@ public class ServiceBuilderFactory {
break;
case E_CLIENT_SERVER:
if (this.restServerParams == null || this.restClientParams == null)
return false;
break;
}
return true;
......
......@@ -77,8 +77,9 @@ public class TestMicroserviceApp {
System.setProperty("configFile.location","/opt/mcx/config/config.properties");
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))
.hasRestClientParams(new RestClientParams(null,false,0,null,null))
.build();
microservice.MicroserviceApp msApp = new microservice.MicroserviceApp(appName);
msApp.withMetrics()
......@@ -89,17 +90,20 @@ public class TestMicroserviceApp {
BaseRestResponse brr = new BaseRestResponse(true,null);
RestContext restContext = (RestContext)msgCtx;
msApp.getLogger();
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/r2",(msgCtx,orgService) -> {
.addMethod(Enums.EnumServiceType.E_REST,CommonServices.EnumRestCommands.E_READ,"/registry/{query}",(msgCtx,orgService) -> {
CommonServices.IRestService inRestService = (CommonServices.IRestService)MicroserviceApp.getsInstance().getService(Enums.EnumServiceType.E_REST,"undertowRestService");
CommandParams cmdParams = new CommandParams();
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);
restContext.container.writeObjectToResponse(restContext.response,brr);
})
......
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