Commit 5b97ddfd by Eli Ben Baruch

mde: add support for threadSafe microservice.

parent f43e562a
Showing with 674 additions and 723 deletions
This diff is collapsed. Click to expand it.
......@@ -50,7 +50,7 @@
"valueType": "ARRAY-NODE"
}
}, {
"metaDataActionClass": "logic.adapter.action.JsonConvertAction",
"metaDataActionClass": "logic.adapter.action.ArrayNodeAction",
"params": {
"intermediateOperations": [{
"metaDataOperationClass": "logic.adapter.HttpAdapter.model.MapStringToJsonOperation",
......
......@@ -50,7 +50,7 @@
"valueType": "ARRAY-NODE"
}
}, {
"metaDataActionClass": "logic.adapter.action.JsonConvertAction",
"metaDataActionClass": "logic.adapter.action.ArrayNodeAction",
"params": {
"intermediateOperations": [{
"metaDataOperationClass": "logic.adapter.HttpAdapter.model.MapStringToJsonOperation",
......
......@@ -50,7 +50,7 @@
"valueType": "ARRAY-NODE"
}
}, {
"metaDataActionClass": "logic.adapter.action.JsonConvertAction",
"metaDataActionClass": "logic.adapter.action.ArrayNodeAction",
"params": {
"intermediateOperations": [{
"metaDataOperationClass": "logic.adapter.HttpAdapter.model.MapStringToJsonOperation",
......
......@@ -6,5 +6,5 @@ log4j.prop.file.path=/logs/conf/log4j.xml
### microservice/rest server
server.port=50040
server.host=0.0.0.0
server.worker.threads=1
server.worker.threads=6
......@@ -10,5 +10,6 @@ mde:
extra_hosts:
- "transportation:172.16.1.56"
- "parking:172.16.1.56"
- "public-safety:172.16.1.151"
# volumes:
# - "/opt/mcz/user_images:/opt/mcz/user_images"
\ No newline at end of file
......@@ -35,11 +35,6 @@ public class MdeHandler extends BaseHandler {
@Override
public void init() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
this.mdeManager = new MdeManager(logger);
} catch (Exception e) {
e.printStackTrace();
......@@ -50,6 +45,7 @@ public class MdeHandler extends BaseHandler {
@Override
public void doCreate(RequestContext requestContext) {
JsonNode content = (JsonNode)readObjectFromRequest(requestContext,JsonNode.class);
BaseRestResponse brr;
brr = validityCheck(requestContext);
......
......@@ -17,10 +17,14 @@ import microservice.io.iface.ILogger;
import microservice.types.BaseRestResponse;
import util.Utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static util.Utils.currentDate;
import static util.Utils.paramsToString;
/**
* Created by eli on 6/7/16.
*/
......@@ -35,6 +39,9 @@ public class MdeManager {
public MdeManager(ILogger logger) throws Exception {
Thread.sleep(10000);
try{
jsonPatchAddLocationUID = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+"jsonPatchAddLocationUid.json");
jsonPatchAddAppKey = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+"jsonPatchAddAppKey.json");
......@@ -59,14 +66,20 @@ public class MdeManager {
// example: GET ../mde/api/v1/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
public BaseRestResponse doRead(RequestContext requestContext) {
BaseRestResponse brr=null;
String[] params = requestContext.params;
String serviceId = params[0] +"." +params[1];
String[] apiIdAsParams = getApiIdAsParams(requestContext);
String error = null;
SimpleHttpResponse resp=null;
BaseRestResponse brr;
String serviceId;// = params[0] +"." +params[1];
boolean isMaintenanceRequest=false;
int offset=0;
System.out.print(Thread.currentThread().getName()+ " " + currentDate() +" MdeManager.doRead: ");
if (requestContext.params[0].equals("maintenance")) {
isMaintenanceRequest=true;
//to skip first param("maintenance")
offset=1;
}
serviceId=getServiceId(requestContext, offset);
String[] apiIdAsParams = getApiIdAsParams(requestContext,offset);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,null);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,null,isMaintenanceRequest);
return brr;
}
......@@ -75,20 +88,30 @@ public class MdeManager {
public BaseRestResponse doCreate(RequestContext requestContext, JsonNode content) {
BaseRestResponse brr=null;
String serviceId ;
int offset=0;
boolean isMaintenanceRequest=false;
System.out.print(Thread.currentThread().getName()+ " " + currentDate() +" MdeManager.doCreate: ["+paramsToString(requestContext.params)+"] ");
if (requestContext.params[0].equals("maintenance")) {
System.out.print(" MAINTENANCE");
isMaintenanceRequest=true;
//to skip first param("maintenance")
offset=1;
}
System.out.print("\n");
serviceId = getServiceId(requestContext,offset);
String[] apiIdAsParams = getApiIdAsParams(requestContext, offset);
int i;
serviceId = getServiceId(requestContext);
String[] apiIdAsParams = getApiIdAsParams(requestContext);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content);
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content, isMaintenanceRequest);
return brr;
}
private BaseRestResponse executeRequest(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content) {
private BaseRestResponse executeRequest(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content, boolean isMaintenanceRequest) {
SimpleHttpResponse resp=null;
String error=null;
if (apiIdAsParams!=null) {
Api api = servicesRepository.getApi(serviceId, apiIdAsParams);
if (api != null) {
......@@ -98,7 +121,7 @@ public class MdeManager {
for (Action action : actions) {
BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId());
if (adapter != null) {// && adapter.getClass().isInstance(HttpAdapter.class)) {
RequestParams requestParams = convertToRequestParams(requestContext, content, api.getMapKeyToParamIndex());
RequestParams requestParams = convertToRequestParams(requestContext, apiIdAsParams, content, api.getMapKeyToParamIndex(),isMaintenanceRequest);
resp = ((HttpAdapter) adapter).executeFlow(action.getApiOut(), requestParams);
break;//currently only one action
}
......@@ -114,17 +137,17 @@ public class MdeManager {
return convertToBaseRestResponse(resp,error);
}
private String getServiceId(RequestContext requestContext) {
return requestContext.params[0]+"."+requestContext.params[1];
private String getServiceId(RequestContext requestContext, int offset) {
return requestContext.params[offset]+"."+requestContext.params[offset+1];
}
private String[] getApiIdAsParams(RequestContext requestContext) {
private String[] getApiIdAsParams(RequestContext requestContext, int offset) {
String[] params = null;
int size = requestContext.params.length;
if (size>2){
params = new String[size-2];
System.arraycopy(requestContext.params, 2, params,0, size-2);
if (size-offset>2){
params = new String[size-offset-2];
System.arraycopy(requestContext.params, 2+offset, params,0, size-offset-2);
}
return params;
}
......@@ -181,37 +204,38 @@ public class MdeManager {
private RequestParams convertToRequestParams(RequestContext requestContext, JsonNode content, Map<String, Integer> mapVariableToParamIndex, boolean isMaintenanceRequest) {
RequestParams requestParams = new RequestParams();
if (isMaintenanceRequest){
String[] params = new String[requestContext.params.length-1];
for (int i = 1 ; i< requestContext.params.length; i++){
params[i-1]=requestContext.params[i];
}
requestParams.setParams(params);
requestParams.setIsMaintenance(true);
}
else
requestParams.setParams(requestContext.params);
requestParams.setQueryParameters(requestContext.queryParameters);
requestParams.setMethod(requestContext.enumCrudMethod);
if (content != null )
requestParams.setContent(content.toString());
private RequestParams convertToRequestParams(RequestContext requestContext,
String[] apiIdAsParams,
JsonNode content,
Map<String, Integer> mapVariableToParamIndex,
boolean isMaintenanceRequest) {
RequestParams requestParams;
Map<String,String> keyValueVariables=null;
//create the variables with its values as derived from the defined keys in the exposed apis (prefixed with '$')
//i.e for $mdeKey, at run time this key will include "mdekey"->"100000025"
if (mapVariableToParamIndex!=null && mapVariableToParamIndex.size()>0) {
Map<String,String> keyValueVariables = mapVariableToParamIndex.entrySet().stream()
keyValueVariables = mapVariableToParamIndex.entrySet().stream()
.collect(Collectors.toMap(
e -> e.getKey(),
e -> requestContext.params[e.getValue()]));
requestParams.setVariablesValues(keyValueVariables);
//if maintenance request, set offset of 1 to skip "maintenance"
e -> requestContext.params[((isMaintenanceRequest)?(e.getValue()+1):e.getValue())]));
}
requestParams= new RequestParams(apiIdAsParams,
keyValueVariables,
requestContext.queryParameters,
requestContext.enumCrudMethod,
(content!=null)? content.toString():null,
isMaintenanceRequest);
return requestParams;
}
private RequestParams convertToRequestParams(RequestContext requestContext, JsonNode content,Map<String, Integer> mapVariableToParamIndex) {
return convertToRequestParams(requestContext, content, mapVariableToParamIndex, false);
private RequestParams convertToRequestParams(RequestContext requestContext,
String[] apiIdAsParams,
JsonNode content,
Map<String, Integer> mapVariableToParamIndex) {
return convertToRequestParams(requestContext, apiIdAsParams, content, mapVariableToParamIndex, false);
}
}
package logic.adapter.HttpAdapter;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Map;
/**
* Created by eli on 1/23/17.
*/
public class ComputeActionVariablesParams {
public ComputeActionVariablesParams(JsonNode jsonNode) {
this.jsonNode = jsonNode;
}
public Map<String, String> getVariablesValues() {
return variablesValues;
}
public void setVariablesValues(Map<String, String> variablesValues) {
this.variablesValues = variablesValues;
}
public JsonNode getJsonNode() {
return jsonNode;
}
private Map<String, String> variablesValues =null;
private JsonNode jsonNode;
}
......@@ -6,8 +6,8 @@ import logic.adapter.HttpAdapter.action.OnHttpResponse;
import logic.adapter.HttpAdapter.action.SimpleHttpAction;
import logic.adapter.HttpAdapter.action.WebSocketAction;
import logic.adapter.HttpAdapter.model.*;
import logic.adapter.action.ArrayNodeAction;
import logic.adapter.action.BaseAction;
import logic.adapter.action.JsonConvertAction;
import logic.adapter.loader.FileAdapterLoader;
import logic.adapter.model.*;
import logic.webSocket.WebSocketManager;
......@@ -17,6 +17,8 @@ import http.simpleHttpClient.SimpleHttpResponse;
import java.util.*;
import static util.Utils.myTypeOf;
/**
* Created by eli on 13/06/16.
......@@ -26,13 +28,13 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
private SimpleHttpClient httpClient = null;
private WebSocketManager webSocketManager = null;
public static String myTypeOf(Object obj){
int end_index = obj.toString().indexOf('@');
String type=obj.toString().substring(0,end_index);
int startIndex = type.lastIndexOf(".");
startIndex++;
return type.substring(startIndex,end_index);
}
// public static String myTypeOf(Object obj){
// int end_index = obj.toString().indexOf('@');
// String type=obj.toString().substring(0,end_index);
// int startIndex = type.lastIndexOf(".");
// startIndex++;
// return type.substring(startIndex,end_index);
// }
public HttpAdapter(ILogger logger, String id) throws Exception {
......@@ -78,7 +80,7 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
for (HttpFlow httpFlow :
getModel().getFlows()) {
httpAction = (SimpleHttpAction) httpFlow.getAction();
httpFlow.addHttpAction(httpAction);
// httpFlow.addHttpAction(httpAction);
httpAction.setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
......@@ -90,21 +92,21 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
for (BaseAction action :
actions) {
if (myTypeOf(action).equals("WebSocketAction")) {
httpFlow.addWebSocketAction((WebSocketAction) action);
((WebSocketAction) action).setWebSocetManager(webSocketManager);
// httpFlow.addWebSocketAction((WebSocketAction) action);
((WebSocketAction) action).setWebSocketManager(webSocketManager);
} else if (myTypeOf(action).equals("SimpleHttpAction")) {
action.setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
httpFlow.addHttpAction((SimpleHttpAction) action);
} else if (myTypeOf(action).equals("JsonConvertAction")) {
List<IntermediateOperation> interOperations = ((JsonConvertAction) action).getParams().getIntermediateOperations();
// httpFlow.addHttpAction((SimpleHttpAction) action);
} else if (myTypeOf(action).equals("ArrayNodeAction")) {
List<IntermediateOperation> interOperations = ((ArrayNodeAction) action).getParams().getIntermediateOperations();
for (IntermediateOperation operation :
interOperations) {
if (myTypeOf(operation).contains("Map")) {
BaseAction action1 = ((MapOperationParams) operation.getOperationParams()).getAction();
if (myTypeOf(action1).equals("SimpleHttpAction")) {
httpFlow.addHttpAction((SimpleHttpAction) action1);
// httpFlow.addHttpAction((SimpleHttpAction) action1);
action1.setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
......@@ -120,22 +122,22 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
actions = actionsListOnError.getActions();
for (BaseAction act : actions) {
if (myTypeOf(act).equals("WebSocketAction")) {
httpFlow.addWebSocketAction((WebSocketAction) act);
((WebSocketAction) act).setWebSocetManager(webSocketManager);
// httpFlow.addWebSocketAction((WebSocketAction) act);
((WebSocketAction) act).setWebSocketManager(webSocketManager);
}
if (myTypeOf(act).equals("SimpleHttpAction")) {
httpFlow.addHttpAction((SimpleHttpAction) act);
// httpFlow.addHttpAction((SimpleHttpAction) act);
act.setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
} else if (myTypeOf(act).equals("JsonConvertAction")) {
List<IntermediateOperation> interOperations = ((JsonConvertAction) act).getParams().getIntermediateOperations();
} else if (myTypeOf(act).equals("ArrayNodeAction")) {
List<IntermediateOperation> interOperations = ((ArrayNodeAction) act).getParams().getIntermediateOperations();
for (IntermediateOperation operation :
interOperations) {
if (myTypeOf(operation).contains("Map")) {
BaseAction action1 = ((MapOperationParams) operation.getOperationParams()).getAction();
if (myTypeOf(action1).equals("SimpleHttpAction")) {
httpFlow.addHttpAction((SimpleHttpAction) action1);
// httpFlow.addHttpAction((SimpleHttpAction) action1);
action1.setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
......@@ -151,31 +153,6 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
}
private void setHttpActionsReferences(List<BaseAction> listBaseActions) {
HttpAdapterConfig config = (HttpAdapterConfig) getModel().getConfig();
if (listBaseActions== null) {getLogger().error("setHttpActionsReferences failed"); return;}
// List<BaseAction> listBaseActions = actionsList.getActions();
for (BaseAction action : listBaseActions) {
if (action.getType().equals(SimpleHttpAction.TYPE)){
((SimpleHttpAction)action).setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
}
// else if (action.getClass().isInstance(JsonConvertAction.class)){
else if (action.getType().equals(JsonConvertAction.TYPE)){
JsonConvertActionParams params = ((JsonConvertAction)action).getParams();
List<IntermediateOperation> operationList = params.getIntermediateOperations();
for (IntermediateOperation operation : operationList) {
operation.getOperationParams().setAdapterReferences(config.getConfigHttpRequestParams(),
httpClient,
authClient);
}
}
}
}
@Override
......@@ -189,8 +166,6 @@ public class HttpAdapter extends BaseAdapter<HttpFlow, RequestParams, SimpleHttp
HttpFlow flow = this.getModel().getFlow(flowId);
if (flow!=null){
//set run time RequestParams to all action of types SimpleHttpAction and WebSocketAction
flow.setRunTimeHttpRequest(input);
try {
resp = flow.execute(input);
} catch (Exception e) {
......
package logic.adapter.HttpAdapter;
import logic.adapter.HttpAdapter.model.NameValueParam;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.defs.Enums;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Map;
......@@ -10,70 +9,80 @@ import java.util.Map;
* Created by eli on 11/29/16.
*/
public class RequestParams {
private String[] params = null;
private Map<String, String> variablesValues =null;
private Map<String, Deque<String>> queryParameters = null;
private Enums.EnumCrudMethod enumCrudMethod;
private Map<String, String> headersMap = null;
private String content;
private boolean isMaintenance=false;
private final String[] params;
private final Map<String, String> variablesValues;
private final Map<String, Deque<String>> queryParameters;
private final Enums.EnumCrudMethod enumCrudMethod;
private final String content;
private final boolean isMaintenance;
private Map<String, String> headersMap = null;
private ComputeActionVariablesParams computeActionVariablesParams;
public RequestParams(String[] params,
Map<String, String> keyValueVariables,
Map<String, Deque<String>> queryParameters,
Enums.EnumCrudMethod enumCrudMethod,
String content,
boolean isMaintenanceRequest) {
this(params, keyValueVariables, queryParameters, enumCrudMethod, content, isMaintenanceRequest, null);
}
public RequestParams(String[] params,
Map<String, String> keyValueVariables,
Map<String, Deque<String>> queryParameters,
Enums.EnumCrudMethod enumCrudMethod,
String content,
boolean isMaintenanceRequest,
Map<String, String> headersMap/*default null*/) {
this.params=params;
this.variablesValues=keyValueVariables;
this.queryParameters=queryParameters;
this.enumCrudMethod=enumCrudMethod;
this.content=content;
this.isMaintenance=isMaintenanceRequest;
this.headersMap=headersMap;
}
public ComputeActionVariablesParams getComputeActionVariablesParams() {
return computeActionVariablesParams;
}
public void setComputeActionVariablesParams(ComputeActionVariablesParams computeActionVariablesParams) {
this.computeActionVariablesParams = computeActionVariablesParams;
}
public boolean isMaintenance() {
return isMaintenance;
}
public String[] getParams() {
public String[] getParams() {
return params;
}
public void setParams(String[] params) {
this.params = params;
}
public Map<String, Deque<String>> getQueryParameters() {
return queryParameters;
}
public void setQueryParameters(Map<String, Deque<String>> queryParameters) {
this.queryParameters = queryParameters;
}
public Enums.EnumCrudMethod getEnumCrudMethod() {
public Enums.EnumCrudMethod getEnumCrudMethod()
{
return enumCrudMethod;
}
public void setMethod(Enums.EnumCrudMethod enumCrudMethod) {
this.enumCrudMethod = enumCrudMethod;
}
public Map<String, String> getHeadersMap() {
public Map<String, String> getHeadersMap()
{
return headersMap;
}
public void setHeadersMap(Map<String, String> headersMap) {
public void setHeadersMap(Map<String, String> headersMap) {
this.headersMap = headersMap;
}
public String getContent() {
public String getContent()
{
return content;
}
public void setContent(String content) {
this.content = content;
}
public Map<String, String> getVariablesValues() {
return variablesValues;
}
public void setVariablesValues(Map<String, String> variablesValues) {
this.variablesValues = variablesValues;
public Map<String, String> getVariablesValues()
{
return this.variablesValues;
}
public RequestParams() {}
public String getQueryParameter(String paramName) {
public String getQueryParameter(String paramName) {
return (((this.queryParameters != null) && (this.queryParameters.containsKey(paramName)))?
(String)((Deque)this.queryParameters.get(paramName)).getFirst():null);
}
public void setIsMaintenance(boolean isMaintenance) {
this.isMaintenance = isMaintenance;
}
}
......@@ -4,15 +4,16 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.HttpAdapter;
import logic.adapter.HttpAdapter.ComputeActionVariablesParams;
import logic.adapter.HttpAdapter.RequestParams;
import logic.adapter.HttpAdapter.model.ConvertActionRunTimeParams;
import logic.adapter.action.ArrayNodeAction;
import logic.adapter.action.BaseAction;
import logic.adapter.model.HttpFlowParams;
import util.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by eli on 11/27/16.
......@@ -81,7 +82,7 @@ public class HttpContentActions {//extends ActionsList<SimpleHttpResponse> {
}
// @Override
// @Override
public boolean isValid() {
if (actionsInput.equals("CONTENT")) {
if (actions.size() != 0)
......@@ -95,24 +96,23 @@ public class HttpContentActions {//extends ActionsList<SimpleHttpResponse> {
}
// public void setRunTimeHttpRequest(RequestParams inRequestParams) {
// this.runTimeHttpRequest = inRequestParams;
// }
//
// private RequestParams runTimeHttpRequest;
// @Override
public SimpleHttpResponse executeActions(SimpleHttpResponse input) throws Exception {
String content = input.getContent();
JsonNode node = (JsonNode)Utils.readObjectFromString1(content, JsonNode.class);
List<BaseAction> actions=getActions();
SimpleHttpResponse resp;
// public SimpleHttpResponse executeActions(SimpleHttpResponse input) throws Exception {
public SimpleHttpResponse executeActions(HttpFlowParams flowParams) throws Exception {
// HttpFlowParams flowParams;
SimpleHttpResponse input = flowParams.getHttpResp();
RequestParams requestParams = flowParams.getRequestParams();
String content = input.getContent();
JsonNode node = (JsonNode)Utils.readObjectFromString1(content, JsonNode.class);
List<BaseAction> actions = getActions();
SimpleHttpResponse resp;
for (BaseAction action : actions)
{
if (action.getType().equals(SimpleHttpAction.TYPE)) {
//SimpleHttpResponse resp = (SimpleHttpResponse) action.apply(runTimeHttpRequest);
((SimpleHttpAction)action).computeVariables(node);
resp = (SimpleHttpResponse) action.apply(null);
//prepare the run time input required to calculate variables inside action
requestParams.setComputeActionVariablesParams(new ComputeActionVariablesParams(node));
if (action.getObjectType().equals(SimpleHttpAction.TYPE)) {
resp = (SimpleHttpResponse) action.apply(requestParams);
if (resp.getStatusCode()!= 200)
{
return new SimpleHttpResponse(500,"failed onSuccess.executeActions.httpAction with error:" +resp.getStatusCode() );
......@@ -120,16 +120,20 @@ public class HttpContentActions {//extends ActionsList<SimpleHttpResponse> {
content = resp.getContent();
node = (JsonNode)Utils.readObjectFromString1(content, JsonNode.class);
}
//assumed that after extractValueAction
else if (HttpAdapter.myTypeOf(action).equals("WebSocketAction")){
// Map<String,String > runTimeParams = new HashMap<>();
// if (node!=null && node.isTextual()) runTimeParams.put("webSocket", node.asText());
// action.setRunTimeVariables(runTimeParams);
// SimpleHttpResponse resp = (SimpleHttpResponse) action.apply(runTimeHttpRequest);
((WebSocketAction)action).computeVariables(node);
resp = (SimpleHttpResponse) action.apply(null);
//assumed that this is the last response action.
else if (action.getObjectType().equals(WebSocketAction.TYPE)){
resp = (SimpleHttpResponse) action.apply(requestParams);
// content = resp.getContent();
// node = (JsonNode)Utils.readObjectFromString1(content, JsonNode.class);
}
else if (action.getObjectType().equals("ArrayNodeAction"))
{
ConvertActionRunTimeParams runTimeParams=new ConvertActionRunTimeParams(node,requestParams);
node = (((ArrayNodeAction)action).apply(runTimeParams));
if (node==null)
return new SimpleHttpResponse(500,"failed onSuccess.executeActions.JsonAction " );
}
else //JsonNodeAction.class
{
node = (JsonNode)action.apply(node);
......@@ -139,7 +143,6 @@ public class HttpContentActions {//extends ActionsList<SimpleHttpResponse> {
}
return new SimpleHttpResponse(200,node.toString());
// return input
}
}
......@@ -3,12 +3,8 @@ package logic.adapter.HttpAdapter.action;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.model.OnResponse;
import logic.adapter.action.ActionsList;
import logic.adapter.action.BaseAction;
import logic.adapter.model.HttpFlowParams;
import java.util.ArrayList;
import java.util.List;
/**
* Created by eli on 11/27/16.
......@@ -35,17 +31,17 @@ public class OnHttpResponse{ //extends OnResponse<SimpleHttpResponse> {
}
public SimpleHttpResponse execute(SimpleHttpResponse actionResp) {
if (actionResp.getStatusCode() == 200) {
public SimpleHttpResponse execute(HttpFlowParams flowParams) {
if (flowParams.getHttpResp().getStatusCode() == 200) {
try {
return onSuccess.executeActions(actionResp);
return onSuccess.executeActions(flowParams);
} catch (Exception e) {
return new SimpleHttpResponse(500, "OnHttpResponse.execute failed with error: " + e);
}
} else {
try {
return onError.executeActions(actionResp);
return onError.executeActions(flowParams);
} catch (Exception e) {
return new SimpleHttpResponse(500, "OnHttpResponse.execute failed with error: " + e);
}
......
......@@ -40,6 +40,7 @@ public class SimpleHttpAction extends BaseAction<HttpRequestActionParams ,Reques
*/
@JsonCreator
public SimpleHttpAction(@JsonProperty("params") HttpRequestActionParams params) {
super(params);
}
......@@ -63,12 +64,14 @@ public class SimpleHttpAction extends BaseAction<HttpRequestActionParams ,Reques
public SimpleHttpResponse apply(RequestParams requestParams) {
//if not null authentication required
SimpleHttpResponse authResp,resp;
SimpleHttpRequest httpRequest;
RequestParams inRequestParams = (requestParams!=null)?requestParams:getRunTimeInput();
SimpleHttpRequest httpRequest=null;
// RequestParams inRequestParams = (requestParams!=null)?requestParams:getRunTimeInput();
RequestParams inRequestParams = requestParams;
//if authentication not needed or token already exist
httpRequest = buildHttpRequest(inRequestParams);
try {
httpRequest = buildHttpRequest(inRequestParams);
resp = httpClient.processRequest(httpRequest);
if (resp.getStatusCode()==401 || resp.getStatusCode() == 403){
{
......@@ -83,9 +86,11 @@ public class SimpleHttpAction extends BaseAction<HttpRequestActionParams ,Reques
}
}
} catch (UnsupportedEncodingException e) {
resp = new SimpleHttpResponse(500, "failed to proccess http request with error: "+e);
resp = new SimpleHttpResponse(500, "failed to process http request with error: "+e);
} catch (ErrorLoginException e) {
resp = new SimpleHttpResponse(500, "authentication failed with error "+e);
} catch (Exception e) {
resp = new SimpleHttpResponse(500, "SimpleHttpRequest.apply failed with error: "+e);
}
return resp;
}
......@@ -94,20 +99,17 @@ public class SimpleHttpAction extends BaseAction<HttpRequestActionParams ,Reques
protected SimpleHttpRequest buildHttpRequest(RequestParams inRequestParams) {
protected SimpleHttpRequest buildHttpRequest(RequestParams inRequestParams) throws Exception {
SimpleHttpRequest request = new HttpRequestResolver().createResolvedRequest(inRequestParams,
SimpleHttpRequest request = HttpRequestResolver.createResolvedRequest(inRequestParams,
adapterHttpParams,
getParams(),
((oauth2Client != null)?oauth2Client.getToken():null),
getRunTimeVariables());
((oauth2Client != null)?oauth2Client.getToken():null));
return request;
}
public void computeVariables(JsonNode node) throws Exception {
setRunTimeVariables(getParams().computeVariables(node));
}
}
......@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.neovisionaries.ws.client.WebSocketException;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.ComputeActionVariablesParams;
import logic.adapter.HttpAdapter.ErrorLoginException;
import logic.adapter.HttpAdapter.RequestParams;
import logic.adapter.action.BaseAction;
......@@ -61,30 +62,45 @@ public class WebSocketAction extends BaseAction<WebSocketActionParams ,RequestPa
@Override
public SimpleHttpResponse apply(RequestParams requestParams) {
OnEventDetails details=null;
RequestParams inRequestParams = (requestParams!=null)?requestParams:getRunTimeInput();
String mdeKey=null;
String uid=null;
String httpPayload = inRequestParams.getContent();
// RequestParams inRequestParams = (requestParams!=null)?requestParams:getRunTimeInput();
RequestParams inRequestParams = requestParams;
Map<String,String> dynamicVariables=null;
try {
details = (OnEventDetails) Utils.readObjectFromString1(httpPayload, OnEventDetails.class);
} catch (IOException e) {
dynamicVariables = this.computeVariables(inRequestParams.getComputeActionVariablesParams());
} catch (Exception e) {
e.printStackTrace();
return new SimpleHttpResponse(500, "Failed to read Request payload OnEventDetails");
return new SimpleHttpResponse(500, "WebSocketAction Failed to compute Dynamic Variables with error: "+e);
}
String mdeKey=null;
String uid=null;
Map<String, String> runTimeParameters = getRunTimeVariables();
String webSocket=null;
if (runTimeParameters.containsKey("webSocket")){
webSocket = runTimeParameters.get("webSocket");
if (!inRequestParams.isMaintenance()){
String httpPayload = inRequestParams.getContent();
try {
details = (OnEventDetails) Utils.readObjectFromString1(httpPayload, OnEventDetails.class);
} catch (IOException e) {
e.printStackTrace();
return new SimpleHttpResponse(500, "Failed to read Request payload OnEventDetails with error:" +e);
}
}
if (webSocket == null)
return new SimpleHttpResponse(500, "Failed to extract web socket url");
String webSocket=null;
try {
mdeKey=getParams().getResolvedMdeKey(inRequestParams.getVariablesValues());
uid=getParams().getResolvedUid(inRequestParams.getVariablesValues());
webSocket=getParams().getResolvedWebSocket(getRunTimeVariables());
webSocketManager.connect(mdeKey,uid,webSocket,details);
WebSocketActionParams params;
if (dynamicVariables !=null) {
params = getParams();
mdeKey = params.getResolvedMdeKey(inRequestParams.getVariablesValues(), dynamicVariables);
uid = getParams().getResolvedUid(inRequestParams.getVariablesValues(), dynamicVariables);
webSocket = getParams().getResolvedWebSocket(dynamicVariables, inRequestParams.getVariablesValues());
}
else
{
mdeKey = getParams().getResolvedMdeKey(inRequestParams.getVariablesValues());
uid = getParams().getResolvedUid(inRequestParams.getVariablesValues());
webSocket = getParams().getResolvedWebSocket(inRequestParams.getVariablesValues());
}
webSocketManager.connect(mdeKey,uid,webSocket,details, inRequestParams.isMaintenance());
} catch (UnsupportedEncodingException e) {
return new SimpleHttpResponse(500, "failed to connect to webSocket with error: "+e);
} catch (ErrorLoginException e) {
......@@ -106,12 +122,12 @@ public class WebSocketAction extends BaseAction<WebSocketActionParams ,RequestPa
}
public void setWebSocetManager(WebSocketManager webSocetManager) {
this.webSocketManager = webSocetManager;
public void setWebSocketManager(WebSocketManager webSocketManager) {
this.webSocketManager = webSocketManager;
}
public void computeVariables(JsonNode node) throws Exception {
setRunTimeVariables(getParams().computeVariables(node));
public Map<String, String> computeVariables(ComputeActionVariablesParams computeActionVariablesParams) throws Exception {
return getParams().computeVariables(computeActionVariablesParams.getJsonNode());
}
}
......
......@@ -7,6 +7,7 @@ import logic.adapter.model.BaseActionParams;
import logic.adapter.model.DynamicVariablesParams;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -17,17 +18,14 @@ public class WebSocketActionParams extends DynamicVariablesParams{
@JsonProperty("headers")
private List<NameValueParam> headers;
final private List<NameValueParam> headers;
@JsonProperty("webSocket")
private String webSocket;
final private String webSocket;
@JsonProperty("mdeKey")
private String mdeKey;
final private String mdeKey;
@JsonProperty("uid")
private String uid;
final private String uid;
// "webSocket": "$webSocket"
// "mdeKey": "$mdeKey",
// "uid": "$uid"
public WebSocketActionParams(@JsonProperty("variables") Map<String, ComputeVariable> mapVariableToValueComputation,
@JsonProperty("headers") List<NameValueParam> headers,
......@@ -52,27 +50,35 @@ public class WebSocketActionParams extends DynamicVariablesParams{
return true;
}
public String getResolvedMdeKey(Map<String, String> variablesValues) {
return resolve(this.mdeKey,variablesValues);
public String getResolvedMdeKey(Map<String, String>... listOfMapsOfVariables) {
return resolve(this.mdeKey,listOfMapsOfVariables);
}
private String resolve(String key, Map<String, String> variablesValues) {
private String resolve(String key, Map<String, String>... listOfMapsOfVariables) {
String resolved=null;
String realKey=null;
if (key!=null && key.startsWith("$")){
if (key!=null && key.startsWith("$")) {
realKey = key.substring(1);
resolved = variablesValues.get(realKey);
for (Map<String, String> variables :
listOfMapsOfVariables) {
if (variables.containsKey(realKey)) {
resolved = variables.get(realKey);
break;
}
}
}
else if (key!=null)
resolved=key;
return resolved;
}
public String getResolvedUid(Map<String, String> variablesValues) {
return resolve(this.uid,variablesValues);
public String getResolvedUid(Map<String, String>... listOfMapsOfVariables) {
return resolve(this.uid,listOfMapsOfVariables);
}
public String getResolvedWebSocket(Map<String, String> variablesValues) {
return resolve(this.webSocket,variablesValues);
public String getResolvedWebSocket(Map<String, String>... listOfMapsOfVariables) {
return resolve(this.webSocket,listOfMapsOfVariables);
}
}
package logic.adapter.HttpAdapter.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.HttpAdapter.action.ExtractSubStringAction;
import logic.adapter.action.BaseAction;
import logic.adapter.action.ExtractValueAction;
import logic.adapter.action.SubStringAction;
import logic.adapter.model.BaseActionParams;
import java.util.List;
import static util.Utils.myTypeOf;
/**
* Created by eli on 12/11/16.
......@@ -31,9 +22,6 @@ public class ComputeVariable{
this.action = action;
}
@JsonIgnore
private String actionRunTimeResult;
public String execute(JsonNode node) throws Exception {
return action.apply(node);
......
package logic.adapter.HttpAdapter.model;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.HttpAdapter.RequestParams;
/**
* Created by eli on 1/25/17.
*/
public class ConvertActionRunTimeParams {
public ConvertActionRunTimeParams(JsonNode jsonNode, RequestParams requestParams) {
this.jsonNode = jsonNode;
this.requestParams = requestParams;
}
public JsonNode getJsonNode() {
return jsonNode;
}
public RequestParams getRequestParams() {
return requestParams;
}
private JsonNode jsonNode;
private RequestParams requestParams;
}
......@@ -9,7 +9,7 @@ import java.util.function.Predicate;
/**
* Created by eli on 11/22/16.
*/
public class FilterJsonOperation extends FilterIntermediateOperation<JsonNode> {
public class FilterJsonOperation extends FilterIntermediateOperation<ConvertActionRunTimeParams> {
public final static String TYPE="FilterJsonOperation";
@JsonCreator
public FilterJsonOperation(@JsonProperty("operationParams") FilterOperationParams operationParams) {
......@@ -17,7 +17,7 @@ public class FilterJsonOperation extends FilterIntermediateOperation<JsonNode> {
}
@Override
protected Predicate<JsonNode> buildFunc() {
protected Predicate<ConvertActionRunTimeParams> buildFunc() {
return null;
}
......
......@@ -11,6 +11,7 @@ import logic.adapter.HttpAdapter.action.SimpleHttpAction;
import logic.adapter.HttpAdapter.action.WebSocketAction;
import logic.adapter.action.BaseAction;
import logic.adapter.model.BaseFlow;
import logic.adapter.model.HttpFlowParams;
import java.util.ArrayList;
import java.util.List;
......@@ -19,30 +20,55 @@ import java.util.List;
* Created by eli on 11/17/16.
*/
public class HttpFlow extends BaseFlow{
@JsonIgnore
private List<SimpleHttpAction> httpActions = new ArrayList<>();
@JsonProperty("action")
protected SimpleHttpAction action;
@JsonProperty("onResponse")
protected OnHttpResponse onResponse;
@JsonIgnore
private List<WebSocketAction> webSocketActions = new ArrayList<>();
@JsonIgnore
public boolean addHttpAction(SimpleHttpAction action) { return httpActions.add(action);}
public void build(){
action.build();
// onResponse.build();
}
public SimpleHttpAction getAction() {
return action;
}
public void setAction(SimpleHttpAction action) {
this.action = action;
}
public OnHttpResponse getOnResponse() {
return onResponse;
}
public void setOnResponse(OnHttpResponse onResponse) {
this.onResponse = onResponse;
}
public SimpleHttpResponse execute(RequestParams input) throws Exception {
HttpFlowParams flowParams;
SimpleHttpResponse actionResp = action.apply(input);
flowParams = new HttpFlowParams();
flowParams.setRequestParams(input);
flowParams.setHttpResp(actionResp);
try {
return onResponse.execute(flowParams);
} catch (Exception e) {
e.printStackTrace();
return actionResp;
}
}
@Override
public boolean isValid() {
return (action.isValid() && onResponse.isValid());
}
@JsonIgnore
public boolean addWebSocketAction(WebSocketAction action) { return webSocketActions.add(action);}
@JsonCreator
public HttpFlow(@JsonProperty("action") SimpleHttpAction action,
@JsonProperty("onResponse") OnHttpResponse onResponse){
super(action, onResponse);
super(action.getParams().getId());
this.action = action;
this.onResponse = onResponse;
}
public void setRunTimeHttpRequest(RequestParams input) {
for (SimpleHttpAction action : httpActions){
action.setRunTimeInput(input);
}
for (WebSocketAction action : webSocketActions){
action.setRunTimeInput(input);
}
}
}
......@@ -6,10 +6,8 @@ import com.fasterxml.jackson.databind.JsonNode;
import common.JsonHandler;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.action.SimpleHttpAction;
import logic.adapter.action.BaseAction;
import logic.adapter.action.JsonNodeAction;
import java.util.Map;
import java.util.function.Function;
import static util.Utils.myTypeOf;
......@@ -17,7 +15,7 @@ import static util.Utils.myTypeOf;
/**
* Created by eli on 11/22/16.
*/
public class MapJsonToJsonOperation extends MapIntermediateOperation<JsonNode, JsonNode> {
public class MapJsonToJsonOperation extends MapIntermediateOperation<ConvertActionRunTimeParams, JsonNode> {
@JsonCreator
public MapJsonToJsonOperation(@JsonProperty("operationParams") MapOperationParams operationParams) {
......@@ -25,17 +23,18 @@ public class MapJsonToJsonOperation extends MapIntermediateOperation<JsonNode, J
}
@Override
protected Function<JsonNode, JsonNode> buildFunc() {
Function<JsonNode,JsonNode> ret = (node) -> {
protected Function<ConvertActionRunTimeParams, JsonNode> buildFunc() {
Function<ConvertActionRunTimeParams,JsonNode> ret = (convertActionRunTimeParams) -> {
SimpleHttpResponse resp;
String content;
JsonNode node=convertActionRunTimeParams.getJsonNode();
try {
String type=myTypeOf(getOperationParams().getAction());
if (type.equals("SimpleHttpAction")) {
SimpleHttpAction httpAction = (SimpleHttpAction)getOperationParams().getAction();
//assumed that the action holds its input already, and use it
httpAction.computeVariables(node);
resp = ((SimpleHttpAction)httpAction).apply(null);
// httpAction.computeVariables(convertActionRunTimeParams.get);
resp = ((SimpleHttpAction)httpAction).apply(convertActionRunTimeParams.getRequestParams());
if (resp.getStatusCode()!=200)
throw new Exception("MapJsonToJsonOperation: http action failed with status code: "+resp.getStatusCode()+" errorMsg: "+ resp.getContent() );
else{
......
......@@ -8,14 +8,13 @@ import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.action.SimpleHttpAction;
import logic.adapter.action.JsonNodeAction;
import java.util.Map;
import java.util.function.Function;
/**
* Created by eli on 11/22/16.
*/
public class MapJsonToStringOperation extends MapIntermediateOperation<JsonNode, JsonNode> {
public class MapJsonToStringOperation extends MapIntermediateOperation<ConvertActionRunTimeParams, JsonNode> {
@JsonCreator
public MapJsonToStringOperation(@JsonProperty("operationParams") MapOperationParams operationParams){
......@@ -23,16 +22,16 @@ public class MapJsonToStringOperation extends MapIntermediateOperation<JsonNode
}
@Override
protected Function<JsonNode, JsonNode> buildFunc() {
Function<JsonNode,JsonNode> ret = (node) -> {
protected Function<ConvertActionRunTimeParams, JsonNode> buildFunc() {
Function<ConvertActionRunTimeParams,JsonNode> ret = (convertActionRunTimeParams) -> {
SimpleHttpResponse resp;
JsonNode retNode;
try {
//in this case assumed that the action get and return JsonNode!!!!! no else section
if(!getOperationParams().getAction().getClass().isInstance(SimpleHttpAction.class)) {
JsonNodeAction JsonAction = (JsonNodeAction)getOperationParams().getAction();
retNode = (JsonNode)JsonAction.apply(node);
System.out.println("MapJsonToStringOperation ,params="+ getOperationParams().toString()+"convert from node= [" +node.toString()+"] to convertedNode= ["+ retNode.toString()+"]");
retNode = (JsonNode)JsonAction.apply(convertActionRunTimeParams.getJsonNode());
System.out.println("MapJsonToStringOperation ,params="+ getOperationParams().toString()+"convert from node= [" +convertActionRunTimeParams.getJsonNode().toString()+"] to convertedNode= ["+ retNode.toString()+"]");
return retNode;
}
......
......@@ -30,24 +30,6 @@ public class MapOperationParams extends BaseOperationParams{
return str;
}
// @JsonIgnore
// private Map<String, String> additionalProperties = new HashMap<String, String>();
// @JsonAnyGetter
// public Map<String, String> getAdditionalProperties() {
// return this.additionalProperties;
// }
//
// @JsonAnySetter
// public void setAdditionalProperty(String name, String value) {
// this.additionalProperties.put(name, value);
// }
@Override
public void setAdapterReferences(ConfigHttpRequestParams adapterHttpParams,
......
......@@ -5,6 +5,8 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import common.JsonHandler;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.ComputeActionVariablesParams;
import logic.adapter.HttpAdapter.RequestParams;
import logic.adapter.HttpAdapter.action.SimpleHttpAction;
import java.util.Map;
......@@ -13,7 +15,7 @@ import java.util.function.Function;
/**
* Created by eli on 11/22/16.
*/
public class MapStringToJsonOperation extends MapIntermediateOperation<JsonNode, JsonNode> {
public class MapStringToJsonOperation extends MapIntermediateOperation<ConvertActionRunTimeParams, JsonNode> {
@JsonCreator
public MapStringToJsonOperation(@JsonProperty("operationParams") MapOperationParams operationParams){
......@@ -21,24 +23,24 @@ public class MapStringToJsonOperation extends MapIntermediateOperation<JsonNode,
}
@Override
protected Function<JsonNode, JsonNode> buildFunc() {
protected Function<ConvertActionRunTimeParams, JsonNode> buildFunc() {
Function<JsonNode,JsonNode> ret = (node) -> {
Function<ConvertActionRunTimeParams,JsonNode> ret = (convertActionRunTimeParams) -> {
SimpleHttpResponse resp;
String content;
try {
//currently support only httpAction
if (getOperationParams().getAction().getType().equals(SimpleHttpAction.TYPE)) {
SimpleHttpAction httpAction = (SimpleHttpAction)getOperationParams().getAction();
//assumed that the action holds its input already, and use it
httpAction.computeVariables(node);
resp = httpAction.apply(null);
RequestParams requestParams = convertActionRunTimeParams.getRequestParams();
requestParams.setComputeActionVariablesParams(new ComputeActionVariablesParams(convertActionRunTimeParams.getJsonNode()));
resp = httpAction.apply(requestParams);
if (resp.getStatusCode()!=200)
throw new Exception("MapJsonToJsonOperation: http action failed with status code: "+resp.getStatusCode()+" errorMsg: "+ resp.getContent() );
content=resp.getContent();
System.out.println("MapStringToJsonOperation params="+ getOperationParams().toString()+" convert node= "+node.toString()+" to convertedNode= "+content.toString());
System.out.println("MapStringToJsonOperation params="+ getOperationParams().toString()+" convert node= "
+convertActionRunTimeParams.getJsonNode().toString()+" to convertedNode= "+content);
return JsonHandler.getJsonNodeFromString(resp.getContent());
}
......
......@@ -9,6 +9,7 @@ import logic.adapter.model.ModelValidator;
/**
* Created by eli on 11/17/16.
*/
@Deprecated
public abstract class OnResponse<TypeInOut > implements ModelValidator{
@JsonProperty("onSuccess")
......
......@@ -24,4 +24,10 @@ public abstract class Operation<T extends BaseOperationParams> {
private T operationParams;
@Override
public final String toString(){
return super.toString();
}
}
......@@ -12,6 +12,7 @@ import java.util.List;
/**
* Created by eli on 11/17/16.
*/
@Deprecated
public abstract class ActionsList<TypeExecuteInOut> implements ModelValidator{
@JsonProperty("actionsInput")
private String actionsInput;
......
package logic.adapter.action;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.HttpAdapter.RequestParams;
import logic.adapter.HttpAdapter.model.*;
import logic.adapter.model.ArrayNodeActionParams;
import logic.adapter.model.TerminateOperation;
import util.ArrayNodeCollector;
import java.util.List;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static util.Utils.myTypeOf;
/**
* Created by eli on 1/25/17.
*/
public class ArrayNodeAction extends BaseAction<ArrayNodeActionParams, ConvertActionRunTimeParams, JsonNode>{
@JsonCreator
public ArrayNodeAction(@JsonProperty("params") ArrayNodeActionParams params) {
super(params);
}
@Override
public String getType() {
return null;
}
@Override
public void build() throws Exception {
}
@Override
public JsonNode apply(ConvertActionRunTimeParams convertActionRunTimeParams) throws Exception {
final JsonNode arrayNode = convertActionRunTimeParams.getJsonNode();
final RequestParams requestParams= convertActionRunTimeParams.getRequestParams();
if (arrayNode == null || !arrayNode.isArray())
throw new Exception("ArrayNodeAction failed. expected ARRAY-NODE input ");
else{
Stream<JsonNode> streamOfJsonNodes;
streamOfJsonNodes = StreamSupport.stream(arrayNode.spliterator(),false);
ArrayNodeActionParams params=getParams();
List<IntermediateOperation> operations = params.getIntermediateOperations();
for (IntermediateOperation operation : operations) {
String type=myTypeOf(operation);
switch (type) {
case "MapJsonToJsonOperation":
streamOfJsonNodes = streamOfJsonNodes.map( (node) -> {
ConvertActionRunTimeParams runTimeParams = new ConvertActionRunTimeParams(node, requestParams);
return ((MapJsonToJsonOperation) operation).getFunc().apply(runTimeParams);
});
break;
case "MapJsonToStringOperation":
streamOfJsonNodes = streamOfJsonNodes.map((node -> {
ConvertActionRunTimeParams runTimeParams = new ConvertActionRunTimeParams(node, requestParams);
return ((MapJsonToStringOperation) operation).getFunc().apply(runTimeParams);
}));
break;
case "MapStringToJsonOperation":
streamOfJsonNodes = streamOfJsonNodes.map((node -> {
ConvertActionRunTimeParams runTimeParams = new ConvertActionRunTimeParams(node, requestParams);
return ((MapStringToJsonOperation) operation).getFunc().apply(runTimeParams);
}));
break;
case "FilterJsonOperation":
streamOfJsonNodes = streamOfJsonNodes.filter((node -> {
ConvertActionRunTimeParams runTimeParams = new ConvertActionRunTimeParams(node, requestParams);
return ((FilterJsonOperation) operation).getFunc().test(runTimeParams);
}));
break;
default:
throw new Exception("ArrayNodeAction failed. unsupported intermediate operation");
}
}
TerminateOperation termOperation = params.getTerminateOperation();
//HandleTerminateOperation
ArrayNodeCollector collector = new ArrayNodeCollector();
return streamOfJsonNodes.collect(collector);
}
}
}
......@@ -2,7 +2,6 @@ package logic.adapter.action;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.JsonNode;
import http.simpleHttpClient.SimpleHttpClient;
import logic.adapter.HttpAdapter.OAuth2Client;
import logic.adapter.HttpAdapter.model.ConfigHttpRequestParams;
......@@ -11,50 +10,40 @@ import logic.adapter.model.ModelValidator;
import java.util.Map;
import static util.Utils.myTypeOf;
/**
* Created by eli on 11/17/16.
*/
@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS, include= JsonTypeInfo.As.PROPERTY, property="metaDataActionClass")
public abstract class BaseAction<T extends BaseActionParams, TypeIn, TypeOut> implements ModelValidator, ThrowableFunction<TypeIn,TypeOut> {
private final String objectType;
@JsonProperty
T params;
public Map<String, String> getRunTimeVariables() {
return runTimeVariables;
}
private Map<String, String> runTimeVariables;
public BaseAction(T params) {
this.params = params;
this.objectType =myTypeOf(this);
}
public T getParams() {return params;}
public TypeIn getRunTimeInput() {
return runTimeInput;
}
public String getObjectType() { return objectType;}
public T getParams() {return params;}
public void setRunTimeInput(TypeIn runTimeInput) {
this.runTimeInput = runTimeInput;
@Override
public boolean isValid() {
return params.isValid();
}
TypeIn runTimeInput;
//must not override this method. used in myTypeOf to determine the type instead of using 'isInstanceOf'
@Override
public boolean isValid() {
return params.isValid();
public final String toString(){
return super.toString();
}
abstract public String getType();
public abstract void build() throws Exception;
public void setRunTimeVariables(Map<String, String> convertedParameters) {
this.runTimeVariables = convertedParameters;
}
public ConfigHttpRequestParams getAdapterHttpParams() {
return adapterHttpParams;
}
......
package logic.adapter.action;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.HttpAdapter.model.*;
import logic.adapter.model.JsonConvertActionParams;
import logic.adapter.model.TerminateOperation;
import util.ArrayNodeCollector;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static util.Utils.myTypeOf;
/**
* Created by eli on 11/21/16.
*/
/**
* This action used to perform all pre-configured set of supported operations on a given JsonNode.
* in order to change
* @param
*/
public class JsonConvertAction extends JsonNodeAction<JsonConvertActionParams>
{
public static final String TYPE = "JsonConvertAction";
@JsonCreator
public JsonConvertAction(@JsonProperty("params") JsonConvertActionParams params) {
super(params);
}
@Override
public String getType() {
return JsonConvertAction.TYPE;
}
@Override
public void build() {
}
/**
* Applies this function to the given argument.
*
* @param jsonNode the function argument
* @return the function result
*/
@Override
public JsonNode apply(JsonNode jsonNode) throws Exception {
if (jsonNode == null || !jsonNode.isArray())
throw new Exception("JsonConvertAction failed. expected ARRAY-NODE input ");
else{
Stream<JsonNode> streamOfJsonNodes;
streamOfJsonNodes = StreamSupport.stream(jsonNode.spliterator(),false);
JsonConvertActionParams params=getParams();
List<IntermediateOperation> operations = params.getIntermediateOperations();
for (IntermediateOperation operation : operations) {
String type=myTypeOf(operation);
if (type.equals("MapJsonToJsonOperation")){
Function<JsonNode,JsonNode> func = ((MapJsonToJsonOperation)operation).getFunc();
streamOfJsonNodes=streamOfJsonNodes.map(func);
}
else if (type.equals("MapJsonToStringOperation")) {
Function<JsonNode, JsonNode> func = ((MapJsonToStringOperation) operation).getFunc();
streamOfJsonNodes=streamOfJsonNodes.map(func);
}
else if (type.equals("MapStringToJsonOperation")) {
Function<JsonNode,JsonNode> func = ((MapStringToJsonOperation) operation).getFunc();
streamOfJsonNodes=streamOfJsonNodes.map(func);
}
else if (type.equals("FilterJsonOperation")) {
Predicate<JsonNode> func = (Predicate<JsonNode> )operation.getFunc();
streamOfJsonNodes=streamOfJsonNodes.filter(func);
}
else throw new Exception("JsonConvertAction failed. unsupported intermediate operation");
}
TerminateOperation termOperation = params.getTerminateOperation();
//HandleTerminateOperation
ArrayNodeCollector collector = new ArrayNodeCollector();
return streamOfJsonNodes.collect(collector);
}
}
}
......@@ -38,14 +38,6 @@ public class AdapterModel<T extends BaseFlow> implements ModelValidator{
return null;
}
public void buildFlows() {
for (T flow:
flows) {
flow.build();
}
}
@Override
public boolean isValid() {
if ( config == null || !config.isValid() || flows == null )
......
......@@ -11,7 +11,7 @@ import java.util.List;
/**
* Created by eli on 11/21/16.
*/
public class JsonConvertActionParams extends BaseActionParams {
public class ArrayNodeActionParams extends BaseActionParams {
@JsonProperty("intermediateOperations")
private List<IntermediateOperation> intermediateOperations= new ArrayList<>();
......@@ -29,8 +29,8 @@ public class JsonConvertActionParams extends BaseActionParams {
@JsonCreator
public JsonConvertActionParams(@JsonProperty("intermediateOperations") List<IntermediateOperation> intermediateOperations,
@JsonProperty("terminateOperation") TerminateOperation terminateOperation) {
public ArrayNodeActionParams(@JsonProperty("intermediateOperations") List<IntermediateOperation> intermediateOperations,
@JsonProperty("terminateOperation") TerminateOperation terminateOperation) {
this.intermediateOperations = intermediateOperations;
this.terminateOperation = terminateOperation;
}
......
......@@ -12,55 +12,14 @@ import logic.adapter.HttpAdapter.action.SimpleHttpAction;
//HttpRequestActionParams, RequestParams,SimpleHttpResponse
public abstract class BaseFlow implements ModelValidator{
@JsonProperty("action")
protected SimpleHttpAction action;
@JsonProperty("onResponse")
protected OnHttpResponse onResponse;
private String flowId;
public BaseFlow(SimpleHttpAction action,
OnHttpResponse onResponse)
{
this.action = action;
this.onResponse = onResponse;
public BaseFlow(String flowId) {
this.flowId = flowId;
}
public void build(){
action.build();
// onResponse.build();
}
public SimpleHttpAction getAction() {
return action;
}
public void setAction(SimpleHttpAction action) {
this.action = action;
}
public OnHttpResponse getOnResponse() {
return onResponse;
}
public void setOnResponse(OnHttpResponse onResponse) {
this.onResponse = onResponse;
}
public String getFlowId(){
if (this.action!=null)
return action.getParams().getId();
else
return null;
}
public SimpleHttpResponse execute(RequestParams input) throws Exception {
// action.getRunTimeVariables();
SimpleHttpResponse actionResp = action.apply(input);
try {
return onResponse.execute(actionResp);
} catch (Exception e) {
e.printStackTrace();
return actionResp;
}
}
@Override
public boolean isValid() {
return (action.isValid() && onResponse.isValid());
public String getFlowId() {
return flowId;
}
}
......
......@@ -11,7 +11,7 @@ import java.util.stream.Collectors;
* Created by eli on 12/15/16.
*/
public abstract class DynamicVariablesParams extends BaseActionParams{
private Map<String, ComputeVariable> mapVariableToValueComputation;
final private Map<String, ComputeVariable> mapVariableToValueComputation;
public DynamicVariablesParams(Map<String, ComputeVariable> mapVariableToValueComputation) {
this.mapVariableToValueComputation = mapVariableToValueComputation;
......
package logic.adapter.model;
import http.simpleHttpClient.SimpleHttpResponse;
import logic.adapter.HttpAdapter.RequestParams;
/**
* Created by eli on 1/19/17.
*/
public class HttpFlowParams {
public RequestParams getRequestParams() {
return requestParams;
}
public void setRequestParams(RequestParams requestParams) {
this.requestParams = requestParams;
}
public SimpleHttpResponse getHttpResp() {
return httpResp;
}
public void setHttpResp(SimpleHttpResponse httpResp) {
this.httpResp = httpResp;
}
private RequestParams requestParams;
private SimpleHttpResponse httpResp;
}
//package logic.adapter.model;
//
//
//import com.fasterxml.jackson.annotation.JsonCreator;
//import com.fasterxml.jackson.annotation.JsonIgnore;
//import com.fasterxml.jackson.annotation.JsonProperty;
//import com.fasterxml.jackson.core.JsonPointer;
//import com.fasterxml.jackson.databind.JsonNode;
//import com.google.api.client.zjsonpatch.Json;
//
///**
// * Created by eli on 11/15/16.
// */
//public class JsonExtractFieldValue extends BaseActionParams {
//
// @JsonProperty
// private String path;
// @JsonProperty
// private EnumJsonFieldValueType enumFieldValueType;
//
//
// @JsonIgnore
// private JsonPointer jsonPointer;
//
// @JsonCreator
// public JsonExtractValueParams(String path, String strFieldValueType) throws IllegalArgumentException{
// this.path = path;
// this.jsonPointer= JsonPointer.compile(path);
// //if the string holds non valid string, enumFieldValueType is null
// this.enumFieldValueType = Enum.valueOf(EnumJsonFieldValueType.class, strFieldValueType);
// }
//
// JsonNode activate(JsonNode)
//
//
//
//
// private enum EnumJsonFieldValueType {
// ARRAY_VALUE("ARRAY"),
// STRING_VALUE("STRING");
//
// public String getName() {
// return name;
// }
//
// private final String name;
//
//
// EnumJsonFieldValueType(String name) {
// this.name = name;
// }
//
//// public static EnumJsonFieldValueType resolveEnumField(String name){
//// EnumJsonFieldValueType[] eValueTypes = EnumJsonFieldValueType.values();
//// for (EnumJsonFieldValueType eType : eValueTypes)
//// {
//// if (eType.getName().equals(name))
//// return eType;
//// }
//// return null;
////
//// }
// }
//
//}
......@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode;
/**
* Created by eli on 7/11/16.
*/
@Deprecated
public class ResponseAction {
public String getType() {
return type;
......
......@@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode;
/**
* Created by eli on 7/11/16.
*/
@Deprecated
public interface INormalizer {
String activate(String strToNormalize, JsonNode format);
}
......@@ -11,6 +11,7 @@ import util.zjsonpatch.ZJsonPatch;
/**
* Created by eli on 7/13/16.
*/
@Deprecated
public class JsonConverterNormalizer implements INormalizer {
final private static String JSON_PATCH_LOCATION = Constants.CONFIG_LOCATION;
@Override
......
......@@ -10,6 +10,7 @@
///**
// * Created by eli on 7/11/16.
// */
//public class Xml2jsonNormalizer implements INormalizer {
//
// /**
......
......@@ -22,6 +22,8 @@ import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import static util.Utils.currentDate;
/**
* Created by eli on 12/4/16.
*/
......@@ -68,6 +70,7 @@ public class WebSocketEventListener implements WebSocketListener {
mapUidToDetails.putIfAbsent(clientId,details);
return true;
}
System.out.println("registerForEvents(key already exist): clientId="+clientId+", Details="+ details.toString());
}
}
......@@ -80,11 +83,7 @@ public class WebSocketEventListener implements WebSocketListener {
enum eTraceLevel{ eFATAL, eERROR, eWARNING, eINFO, eDEBUG, eTRACE};
private static String currentDate() {
Date date = new Date(System.currentTimeMillis());
DateTime dateTime = new DateTime(date);
return dateTime.toStringRfc3339();
}
public WebSocketEventListener(WebSocketConnection webSocketConnection, String adapterId, String connectionId, String uid, OnEventDetails details, ILogger logFile, Map<String, OnEventDetails> mapUidToDetails) {
......@@ -106,7 +105,7 @@ public class WebSocketEventListener implements WebSocketListener {
}
private void logging(String str, eTraceLevel level, boolean writeToLog ) {
System.out.println(adapterId+" "+str);
System.out.println(Thread.currentThread().getName()+" "+adapterId+" "+str);
if (writeToLog){
switch (level){
case eFATAL:
......@@ -211,7 +210,7 @@ public class WebSocketEventListener implements WebSocketListener {
@Override
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
String str = connectionId +" "+ currentDate()+" "+"onDisconnected: the WebSocket connection was closed by"+ ((closedByServer)?" server": " client");
this.webSocketConnection.webSocketClosed();
this.webSocketConnection.setDisconnected();
setState(websocket.getState());
logging(str, eTraceLevel.eDEBUG,false);
}
......@@ -395,7 +394,7 @@ public class WebSocketEventListener implements WebSocketListener {
logging(connectionId+ " sendHttpPost failed to send event with error: "+e, eTraceLevel.eERROR, false);
return;
}
logging(connectionId+ " sendHttpPost event was send successfuly to"+callBackUrl, eTraceLevel.eDEBUG, false);
logging(connectionId+ " sendHttpPost event was send successfully to: "+callBackUrl, eTraceLevel.eDEBUG, false);
}
/**
......
package logic.webSocket;
import com.neovisionaries.ws.client.WebSocketException;
import io.undertow.websockets.WebSocketConnectionCallback;
import logic.adapter.HttpAdapter.ErrorLoginException;
import logic.adapter.HttpAdapter.OAuth2Client;
import logic.adapter.HttpAdapter.model.NameValueParam;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static util.Utils.currentDate;
/**
* Created by eli on 12/4/16.
*/
public class WebSocketManager {
private String adapterId;
private Map<String, WebSocketConnection> webSocketConnections;
private List<NameValueParam> defaultHeaders;
private OAuth2Client auth2Client;
private final String adapterId;
private final Map<String, WebSocketConnection> webSocketConnections;
private final List<NameValueParam> defaultHeaders;
private final OAuth2Client auth2Client;
public WebSocketManager(OAuth2Client oAuth2Client, List<NameValueParam> defaultHeaders, String adapterId)
{
this.adapterId=adapterId;
this.webSocketConnections = new HashMap<>();
public WebSocketManager(OAuth2Client oAuth2Client, List<NameValueParam> defaultHeaders, String adapterId) {
this.adapterId = adapterId;
this.webSocketConnections = new ConcurrentHashMap<>();
this.defaultHeaders = defaultHeaders;
this.auth2Client = oAuth2Client;
}
public boolean isConnected( String connectionId, String uid){
public boolean isConnected(String connectionId, String uid) {
if (webSocketConnections.containsKey(connectionId)){
if (webSocketConnections.containsKey(connectionId)) {
WebSocketConnection connection = webSocketConnections.get(connectionId);
if (connection!=null ){
if (connection != null) {
if (connection.isRegisteredListener(uid))
return true;
}
......@@ -43,32 +41,58 @@ public class WebSocketManager {
return false;
}
public void connect(String connectionId, String uid, String webSocketUrl, OnEventDetails details) throws Exception {
public void connect(String connectionId,
String uid,
String webSocketUrl,
OnEventDetails details,
boolean isReconnect) throws Exception {
StringBuilder sb = new StringBuilder();
sb.append(connectionId).append(":").append(uid);
String key = sb.toString();
WebSocketConnection connection,reConnection;
WebSocketConnection connection, reConnection;
if (!webSocketConnections.containsKey(connectionId)) {
connection = new WebSocketConnection(adapterId,connectionId, uid, webSocketUrl, auth2Client, defaultHeaders, details,null);
this.startConnection(connection,details);
}
else{
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " websocketManager.connect[connectionId = " + connectionId + "uid="+(uid!=null?uid:"")+"]. create new connection");
connection = new WebSocketConnection(adapterId, connectionId, uid, webSocketUrl, auth2Client, defaultHeaders, details, null, true);
synchronized (connection) {
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ "LOCKING connection: "+ connection.toString()+ " websocketManager.connect[connectionId = " + connectionId + "uid="+(uid!=null?uid:"")+"]. create new connection");
this.startConnection(connection, details, false);
webSocketConnections.put(connection.getConnectionId(), connection);
System.out.println(Thread.currentThread().getName()+" "+ currentDate() + "UN-LOCKING connection: "+ connection.toString()+ " "+((isReconnect) ? "RECONNECTED " : "") + "websocket created successfully: "
+ connection.getConnectionId() + " uid=" + connection.getUid() + " " + ((details != null) ? details.toString() : ""));
}
} else {
connection = webSocketConnections.get(connectionId);
synchronized (connection) {
if (connection.isClosedWebSocketWaitingForMaintenance()) {
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " LOCKING connection: " + connection.toString()+
" websocketManager.connect[connectionId = " + connectionId + " ,uid="+(uid!=null?uid:"")+", adapterId=" + connection.getAdapterId() + "]. isReconnect=" + isReconnect + ", isConnectionWaitingForMaintenance" + connection.isStopRequested());
if (isReconnect && connection.isDisconnected()) {
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+"websocketManager.RECONNECT[connectionId = " + connectionId + ", adapterId=" + connection.getAdapterId() + "]. RECONNECTING starts ");
reConnection = createReconnection(webSocketUrl, connection);
this.startConnection(reConnection, details);
connection.setIsClosedWebSocketWaitForMaintenance(false);
} else {
this.startConnection(reConnection, details, true);
webSocketConnections.put(connection.getConnectionId(), reConnection);
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+"RECONNECTED websocket created successfully: "
+ connection.getConnectionId() + " " +connection.toString()+" uid=" + connection.getUid());
connection.setStopRequested();
// connection.notify();
} else { //if (!isReconnect){
connection.addEventListener(uid, details);
System.out.println("websocket updated successfully: " + connectionId + " " + "uid="
+ uid + " was added to websocket. " + ((details != null) ? details.toString() : "Details=NULL"));
System.out.print(Thread.currentThread().getName()+" "+ currentDate()+"websocket updated successfully: " + connectionId + " " + "uid="
+ uid + " , " + ((details != null) ? details.toString() : "Details=NULL")+"uidList: [" );
connection.getEventListener().getMapUidToDetails().entrySet().forEach(entry -> System.out.print(entry.getKey()+ " "));
System.out.print("]\n");
}
}
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " UN-LOCKING connection: " + connection.toString()+
" websocketManager.connect[connectionId = " + connectionId + " ,uid="+(uid!=null?uid:"")+", adapterId=" + connection.getAdapterId() + "]. isReconnect=" + isReconnect + ", isConnectionWaitingForMaintenance" + connection.isStopRequested());
}
}
private WebSocketConnection createReconnection(String reConnectUrl, WebSocketConnection connection) throws ErrorLoginException, IOException, WebSocketException {
private WebSocketConnection createReconnection(String reConnectUrl,
WebSocketConnection connection) throws ErrorLoginException, IOException, WebSocketException {
WebSocketConnection reConnection = new WebSocketConnection((connection.getAdapterId()),
connection.getConnectionId(),
......@@ -77,23 +101,22 @@ public class WebSocketManager {
connection.getAuth2Client(),
connection.getHttpHeaders(),
connection.getDetails(),
connection.getEventListener());
connection.getEventListener(),
false);
return reConnection;
}
private void startConnection(WebSocketConnection connection, OnEventDetails details) throws Exception {
synchronized (connection){
private void startConnection(WebSocketConnection connection, OnEventDetails details, boolean isReconnect) throws Exception {
synchronized (connection) {
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " LOCKING connection: "+ connection.toString()+ " waiting for startConnection[connectionId= " + connection.getConnectionId() +"]" );
connection.start();
connection.wait();
if (connection.getException()!=null)
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " notified startConnection[connectionId= " + connection.getConnectionId() +"] locking connection: " + connection.toString());
if (connection.getException() != null)
throw connection.getException();
else {
webSocketConnections.put(connection.getConnectionId(), connection);
System.out.println(((details==null)?"RECONNECTED ":"")+"websocket created successfully: "
+ connection.getConnectionId()+ " uid="+connection.getUid()+" "+ ((details!=null)?details.toString():""));
}
}
System.out.println(Thread.currentThread().getName()+" "+ currentDate()+ " UN-LOCKED connection: "+ connection.toString()+ " waiting for startConnection[connectionId= " + connection.getConnectionId() +"]" );
}
}
package util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import http.simpleHttpClient.SimpleHttpRequest;
import logic.adapter.HttpAdapter.RequestParams;
......@@ -26,66 +27,30 @@ import static defs.Enums.eKeyWord;
* Created by eli on 11/29/16.
*/
public class HttpRequestResolver {
//this regexQueryParamValue means string starting with '$' followed with word (1-15 chars length) followed by period, ending with
//another word (1-15 chars)
// private final static String regexQueryParamValue = "^\\$[a-zA-Z]{1,15}\\.[a-zA-Z]{1,15}$";
// private final static Pattern patternFullMatchValue = Pattern.compile(regexQueryParamValue);
// private final static String regexPathParamValue = "^\\$[a-zA-Z]{1,15}\\.[a-zA-Z]{1,15}$";
// private final static Pattern patternPathParamValue = Pattern.compile(regexPathParamValue);
// private final static String regexPathKeyWord= "^\\$[a-zA-Z0-9]{1,15}\\.[a-zA-Z]{1,15}$";
// private final static Pattern patternPathKeyWord = Pattern.compile(regexPathKeyWord);
// private final String tokenAccess;
//
// private RequestContext srcRequest;
// //field hold the dst request parameters that added to all request of the given adapter.
// //some of the parameters has to be resolved
// private ConfigHttpRequestParams dstConfigRequestParamsPattern;
// //field hold the dst request parameters that relevant for specific httpRequest
// //some of the parameters has to be resolved
// private HttpRequestActionParams dstRequestParamsPattern;
//GENERAL
// "baseUrl": "ie-parking.run.aws-usw02-pr.ice.predix.io/",
// "basePath": "v1/",
// "headers": [{
// "name": "predix-zone-id",
// "value": "c54e3e63-8dc6-425e-a533-64e061f64023"
// }, {
// "name": "Authorization",
// "value": "bearer $token"
// }]
//GetAllParkingSpots
// "metaDataActionClass": "logic.adapter.HttpAdapter.action.SimpleHttpAction",
// "params": {
// "id": "GetAllParkingSpots",
// "httpMethod": "GET",
// "path": "locations/search",
// "queryParams": [{
// "name": "bbox",
// "value": "32.123:-117,32.723179:-117.172655"
// }],
// "headers": [],
// "content": null
// }
public SimpleHttpRequest createResolvedRequest(RequestParams srcRequest,
ConfigHttpRequestParams dstConfigRequestParamsPattern,
HttpRequestActionParams dstRequestParamsPattern,
String tokenAccess,
Map<String,String> runTimeVariables){
public static SimpleHttpRequest createResolvedRequest(RequestParams srcRequest,
ConfigHttpRequestParams commonRequestParams,
HttpRequestActionParams requestActionParams,
String tokenAccess) throws Exception {
// use it to create JsonNode JsonNodeFactory.instance.arrayNode();
String domain,path;
String name,value;
Map<String,String> runTimeVariables=null;
JsonNode node=null;
try {
if (srcRequest.getComputeActionVariablesParams()!=null) {
node = srcRequest.getComputeActionVariablesParams().getJsonNode();
runTimeVariables = requestActionParams.computeVariables(node);
}
} catch (Exception e) {
throw new Exception("createResolvedRequest failed on computeVariables. error:"+e);
}
SimpleHttpRequest request = new SimpleHttpRequest();
if (tokenAccess!=null)
request.setProtocol("https");
else
request.setProtocol("https");
domain = dstConfigRequestParamsPattern.getBaseUrl()+dstConfigRequestParamsPattern.getBasePath();
domain = commonRequestParams.getBaseUrl()+commonRequestParams.getBasePath();
request.setDomain(domain);
String method = dstRequestParamsPattern.getHttpMethod();
String method = requestActionParams.getHttpMethod();
SimpleHttpRequest.Method eMethod;
switch (method){
case "GET":
......@@ -105,7 +70,7 @@ public class HttpRequestResolver {
break;
}
request.setMethod(eMethod);
List<NameValueParam> headers= dstConfigRequestParamsPattern.getHeaders();
List<NameValueParam> headers= commonRequestParams.getHeaders();
for (NameValueParam header: headers)
{
name = header.getName();
......@@ -114,16 +79,23 @@ public class HttpRequestResolver {
}
if (tokenAccess!=null) {
request.addHeader("Authorization", "Bearer " + tokenAccess);
request.setProtocol("https");
}
else
request.setProtocol("http");
path = dstRequestParamsPattern.getPath();
/*Handle Dynamic variables assignment, currently support only "path".
implement the same logic if needed for variables on query params, headers etc*/
//start
path = requestActionParams.getPath();
if (srcRequest.getVariablesValues()!=null && srcRequest.getVariablesValues().size()>0)
path = replaceMatchedKeysByItsValues(path, srcRequest.getVariablesValues());
if (runTimeVariables!=null && runTimeVariables.size()>0)
path = replaceMatchedKeysByItsValues(path, runTimeVariables);
request.setPath(path);
//end
List<NameValueParam> queryParams = dstRequestParamsPattern.getQueryParams();
List<NameValueParam> queryParams = requestActionParams.getQueryParams();
if (queryParams!=null) {
request.InitQueryStringBuilder();
for (NameValueParam param : queryParams) {
......
......@@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.JsonHandler;
@Deprecated
public class MDEResult {
protected boolean success;
......
//package util;
//
//import http.simpleHttpClient.SimpleHttpClient;
//import http.simpleHttpClient.SimpleHttpRequest;
//import http.simpleHttpClient.SimpleHttpResponse;
////import io.undertow.websockets.core.protocol.version07.Base64;
//import logic.adapter.HttpAdapter.model.DataHttpApi;
//import logic.adapter.loader.FileAdapterLoader;
//import microservice.io.iface.ILogger;
//
//import java.util.Base64;
//
///**
// * Created by eli on 10/31/16.
// */
//public class RestClientAccessToken {
// private static final String UAA_URL = "9deacc64-7c53-4790-9a6c-c9de0fdebcdf.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token?grant_type=client_credentials";
//
//
// private SimpleHttpClient httpClient = null;
// private SimpleHttpRequest request = null;
//
// public RestClientAccessToken(String clientId, String secret) throws Exception {
//
// String clientCredentials = clientId+ ":" +secret;
// String encodedClientCredential = Base64.getEncoder().encodeToString(clientCredentials.getBytes("utf-8"));
// // String base64ClientCredentials = new String(Base64.encodeBase64(plainClientCredentials.getBytes()));
//
//
// httpClient = new SimpleHttpClient();
// httpClient.Initialize(100);
// request = new SimpleHttpRequest();
//
// request.setProtocol("https");
// request.setMethod(SimpleHttpRequest.Method.GET);
// request.setDomain(UAA_URL);
// request.setPort(443);
//// request.setPath(path);
// // optional
// request.setQueryString("grant_type=client_credentials");
// request.addHeader("Authorization", "Basic "+encodedClientCredential);
//
//
// }
//
// SimpleHttpResponse httpResp = null;
//
//
// request.setProtocol("http");
// request.setMethod(method);
// request.setDomain(domain);
// request.setPort(port);
// request.setPath(path);
// // optional
// request.setQueryString(queryParams);
// request.setContent(content);
//
// try
// {
// httpResp = httpClient.processRequest(request);
// if (httpResp.getStatusCode() == 200) {
// return new MDEResult(true, null, httpResp.getContent());
// }
// else
// return new MDEResult(httpResp.getContent());
// }
// catch(Exception e)
// {
// e.printStackTrace();
// return new MDEResult("doHttp failed with exception. e: " + e.toString());
// }
//
//
//}
package util;
/**
* Created by eli on 10/31/16.
*/
public class TokenResponse {
}
......@@ -2,8 +2,11 @@ package util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.google.api.client.util.DateTime;
import java.io.File;
import java.io.IOException;
import java.util.Date;
/**
* Created by eli on 6/23/16.
......@@ -11,7 +14,26 @@ import java.io.IOException;
public class Utils {
public static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
public static String paramsToString(String[] params){
StringBuilder sb = new StringBuilder();
if (params == null || params.length==0 || params[0].equals(""))
return "";
else{
sb.append("[");
for (int i=0; i< params.length; i++){
sb.append(params[i]);
if (i<params.length-1)
sb.append(",");
}
sb.append("]");
return sb.toString();
}
}
public static String currentDate() {
Date date = new Date(System.currentTimeMillis());
DateTime dateTime = new DateTime(date);
return dateTime.toStringRfc3339();
}
public static String myTypeOf(Object obj){
int end_index = obj.toString().indexOf('@');
......
import org.junit.Test;
import java.util.stream.Stream;
/**
* Created by eli on 1/24/17.
*/
public class TestStream {
@Test
public void testStream(){
Stream.of("d2", "a2", "b1", "b3", "c")
.map(s -> {
System.out.println("map: " + s);
return s.toUpperCase();
})
.anyMatch(s -> {
System.out.println("anyMatch: " + s);
return s.startsWith("A");
});
}
}
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