Commit fa007d0d by Eli Ben Baruch

mde: so far-load adapter by config, each adapter is configured to create http…

mde: so far-load adapter by config, each adapter is configured to create http request to the configured domain and apis. also for the responds include 2 kinds of normalizers- xml2json and jsonConverter
parent 057dd961
Showing with 1868 additions and 112 deletions
FROM 172.16.1.132:5000/ipgallery/base-microservice-java
FROM 172.16.1.212:5050/ipgallery.common.java/microservice
COPY build/libs/mde-*.jar /usr/
COPY cfg/config.properties /opt/mcx/config/
COPY cfg/*.json /opt/mcx/config/
RUN chmod +x /usr/run.sh
RUN mv /usr/mde-*.jar /usr/mde.jar
......
......@@ -13,11 +13,16 @@ repositories {
}
dependencies {
compile group:'com.ipgallery.common', name:'microservice', version:'1.0.0'
compile 'com.flipkart.zjsonpatch:zjsonpatch:0.2.1'
compile group:'com.ipgallery.common', name:'microservice', version:'1.2.0'
compile group:'com.ipgallery.common', name:'itc', version:'1.0.0'
// compile 'com.ipgallery.common:utils:1.1.0'
compile 'io.swagger:swagger-annotations:1.5.7'
compile 'org.slf4j:slf4j-simple:1.7.19'
compile 'com.javadocmd:simplelatlng:1.3.1'
compile group: 'org.json', name: 'json', version: '20160212'
testCompile 'junit:junit:4.12'
}
......@@ -28,11 +33,20 @@ jar {
exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
}
}
}
task buildDockerImage(type:Exec) {
dependsOn jar
mustRunAfter jar
println 'building docker image'
def commandWirhArgs = [ "docker" ,"build","-t", "172.16.1.132:5000/ipgallery/mde", "."]
def commandWirhArgs = [ "docker" ,"build","-t", "172.16.1.212:5050/ipgallery/mde", "."]
commandLine commandWirhArgs
}
......
{
"serviceId": "transportation",
"BaseURL": "http://www.ctabustracker.com/bustime/api/v1",
"baseUrl": "www.ctabustracker.com:80",
"basePath": "bustime/api/v1",
"apiList": [{
"id": "getroutes",
"method": "GET",
"headers": null,
"content": null,
"path": "getroutes",
"QueryParams": [{
"key": "$query.key"
"queryParams": [{
"name": "key",
"value": "$query.key"
}],
"headers": [],
"content": null,
"responseActions": [{
"action": "normalize",
"type": "xml2json",
"data": null
}, {
"action": "normalize",
"type": "jsonConverter",
"data": {
"jsonPatchfile": "jsonPatchBusTracker.json"
}
}]
}]
}
\ No newline at end of file
{
"serviceId":"chicago.transportation",
"baseUrl":"http://www.ctabustracker.com/bustime/api/v1",
"apiList":[
{
"id":"getroutes",
"method":"GET",
"path": "getroutes",
"QueryParams":[
{
"key":"$query.key"
}
],
"headers":null,
"content":null
}
]
}
{
"adapters": [{
"id": "adapter.chicago.transportation",
"instance": "HttpAdapter:myAdapter.jar",
"configFile": "adapter.chicago.transportation.json"
"type": "HttpAdapter"
}]
}
\ No newline at end of file
123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123123
{
"bustime-response": {
"route": [{
"rt": 1,
"rtnm": "Bronzeville/Union Station",
"rtclr": "#336633"
}, {
"rt": 2,
"rtnm": "Hyde Park Express",
"rtclr": "#993366"
}, {
"rt": 3,
"rtnm": "King Drive",
"rtclr": "#009900"
}, {
"rt": 4,
"rtnm": "Cottage Grove",
"rtclr": "#cc3300"
}, {
"rt": 5,
"rtnm": "South Shore Night Bus",
"rtclr": "#996633"
}, {
"rt": 6,
"rtnm": "Jackson Park Express",
"rtclr": "#ff0066"
}, {
"rt": 7,
"rtnm": "Harrison",
"rtclr": "#666600"
}, {
"rt": 8,
"rtnm": "Halsted",
"rtclr": "#ff00ff"
}, {
"rt": "8A",
"rtnm": "South Halsted",
"rtclr": "#66cc66"
}, {
"rt": 9,
"rtnm": "Ashland",
"rtclr": "#cc3366"
}, {
"rt": "X9",
"rtnm": "Ashland Express",
"rtclr": "#33ff00"
}, {
"rt": 10,
"rtnm": "Museum of S & I",
"rtclr": "#cc66ff"
}, {
"rt": 11,
"rtnm": "Lincoln",
"rtclr": "#ff6600"
}, {
"rt": 12,
"rtnm": "Roosevelt",
"rtclr": "#33ccff"
}, {
"rt": "J14",
"rtnm": "Jeffery Jump",
"rtclr": "#c8c8c8"
}, {
"rt": 15,
"rtnm": "Jeffery Local",
"rtclr": "#ff9999"
}, {
"rt": 18,
"rtnm": "16th/18th",
"rtclr": "#6699ff"
}, {
"rt": 19,
"rtnm": "United Center Express",
"rtclr": "#78aa50"
}, {
"rt": 20,
"rtnm": "Madison",
"rtclr": "#336633"
}, {
"rt": 21,
"rtnm": "Cermak",
"rtclr": "#009900"
}, {
"rt": 22,
"rtnm": "Clark",
"rtclr": "#cc3300"
}, {
"rt": 24,
"rtnm": "Wentworth",
"rtclr": "#9900cc"
}, {
"rt": 26,
"rtnm": "South Shore Express",
"rtclr": "#006666"
}, {
"rt": 28,
"rtnm": "Stony Island",
"rtclr": "#996633"
}, {
"rt": 29,
"rtnm": "State",
"rtclr": "#666600"
}, {
"rt": 30,
"rtnm": "South Chicago",
"rtclr": "#ff00ff"
}, {
"rt": 34,
"rtnm": "South Michigan",
"rtclr": "#cc3366"
}, {
"rt": 35,
"rtnm": "31st/35th",
"rtclr": "#33ff00"
}, {
"rt": 36,
"rtnm": "Broadway",
"rtclr": "#cc66ff"
}, {
"rt": 37,
"rtnm": "Sedgwick",
"rtclr": "#c8c8c8"
}, {
"rt": 39,
"rtnm": "Pershing",
"rtclr": "#33ccff"
}, {
"rt": 43,
"rtnm": "43rd",
"rtclr": "#cc9966"
}, {
"rt": 44,
"rtnm": "Wallace-Racine",
"rtclr": "#ff9999"
}, {
"rt": 47,
"rtnm": "47th",
"rtclr": "#99cc33"
}, {
"rt": 48,
"rtnm": "South Damen",
"rtclr": "#6699ff"
}, {
"rt": 49,
"rtnm": "Western",
"rtclr": "#336633"
}, {
"rt": "49B",
"rtnm": "North Western",
"rtclr": "#009900"
}, {
"rt": "X49",
"rtnm": "Western Express",
"rtclr": "#9900cc"
}, {
"rt": 50,
"rtnm": "Damen",
"rtclr": "#cc3300"
}, {
"rt": 51,
"rtnm": "51st",
"rtclr": "#006666"
}, {
"rt": 52,
"rtnm": "Kedzie/California",
"rtclr": "#996633"
}, {
"rt": "52A",
"rtnm": "South Kedzie",
"rtclr": "#ff0066"
}, {
"rt": 53,
"rtnm": "Pulaski",
"rtclr": "#666600"
}, {
"rt": "53A",
"rtnm": "South Pulaski",
"rtclr": "#ff00ff"
}, {
"rt": 54,
"rtnm": "Cicero",
"rtclr": "#cc3366"
}, {
"rt": "54A",
"rtnm": "North Cicero/Skokie Blvd.",
"rtclr": "#33ff00"
}, {
"rt": "54B",
"rtnm": "South Cicero",
"rtclr": "#cc66ff"
}, {
"rt": 55,
"rtnm": "Garfield",
"rtclr": "#33ccff"
}, {
"rt": "55A",
"rtnm": "55th/Austin",
"rtclr": "#cc9966"
}, {
"rt": "55N",
"rtnm": "55th/Narragansett",
"rtclr": "#ff9999"
}, {
"rt": 56,
"rtnm": "Milwaukee",
"rtclr": "#6699ff"
}, {
"rt": 57,
"rtnm": "Laramie",
"rtclr": "#993366"
}, {
"rt": 59,
"rtnm": "59th/61st",
"rtclr": "#009900"
}, {
"rt": 60,
"rtnm": "Blue Island/26th",
"rtclr": "#9900cc"
}, {
"rt": 62,
"rtnm": "Archer",
"rtclr": "#cc3300"
}, {
"rt": "62H",
"rtnm": "Archer/Harlem",
"rtclr": "#006666"
}, {
"rt": 63,
"rtnm": "63rd",
"rtclr": "#996633"
}, {
"rt": "63W",
"rtnm": "West 63rd",
"rtclr": "#ff0066"
}, {
"rt": 65,
"rtnm": "Grand",
"rtclr": "#ff00ff"
}, {
"rt": 66,
"rtnm": "Chicago",
"rtclr": "#66cc66"
}, {
"rt": 67,
"rtnm": "67th-69th-71st",
"rtclr": "#cc3366"
}, {
"rt": 68,
"rtnm": "Northwest Highway",
"rtclr": "#33ff00"
}, {
"rt": 70,
"rtnm": "Division",
"rtclr": "#ff6600"
}, {
"rt": 71,
"rtnm": "71st/South Shore",
"rtclr": "#33ccff"
}, {
"rt": 72,
"rtnm": "North",
"rtclr": "#cc9966"
}, {
"rt": 73,
"rtnm": "Armitage",
"rtclr": "#ff9999"
}, {
"rt": 74,
"rtnm": "Fullerton",
"rtclr": "#99cc33"
}, {
"rt": 75,
"rtnm": "74th-75th",
"rtclr": "#6699ff"
}, {
"rt": 76,
"rtnm": "Diversey",
"rtclr": "#336633"
}, {
"rt": 77,
"rtnm": "Belmont",
"rtclr": "#993366"
}, {
"rt": 78,
"rtnm": "Montrose",
"rtclr": "#009900"
}, {
"rt": 79,
"rtnm": "79th",
"rtclr": "#9900cc"
}, {
"rt": 80,
"rtnm": "Irving Park",
"rtclr": "#cc3300"
}, {
"rt": 81,
"rtnm": "Lawrence",
"rtclr": "#996633"
}, {
"rt": "81W",
"rtnm": "West Lawrence",
"rtclr": "#ff0066"
}, {
"rt": 82,
"rtnm": "Kimball-Homan",
"rtclr": "#666600"
}, {
"rt": 84,
"rtnm": "Peterson",
"rtclr": "#ff00ff"
}, {
"rt": 85,
"rtnm": "Central",
"rtclr": "#66cc66"
}, {
"rt": "85A",
"rtnm": "North Central",
"rtclr": "#cc3366"
}, {
"rt": 86,
"rtnm": "Narragansett/Ridgeland",
"rtclr": "#33ff00"
}, {
"rt": 87,
"rtnm": "87th",
"rtclr": "#cc66ff"
}, {
"rt": 88,
"rtnm": "Higgins",
"rtclr": "#ff6600"
}, {
"rt": 90,
"rtnm": "Harlem",
"rtclr": "#33ccff"
}, {
"rt": 91,
"rtnm": "Austin",
"rtclr": "#ff9999"
}, {
"rt": 92,
"rtnm": "Foster",
"rtclr": "#99cc33"
}, {
"rt": 93,
"rtnm": "California/Dodge",
"rtclr": "#6699ff"
}, {
"rt": 94,
"rtnm": "South California",
"rtclr": "#336633"
}, {
"rt": "95E",
"rtnm": "93rd-95th",
"rtclr": "#993366"
}, {
"rt": "95W",
"rtnm": "West 95th",
"rtclr": "#009900"
}, {
"rt": 96,
"rtnm": "Lunt",
"rtclr": "#9900cc"
}, {
"rt": 97,
"rtnm": "Skokie",
"rtclr": "#cc3300"
}, {
"rt": "X98",
"rtnm": "Avon Express",
"rtclr": "#006666"
}, {
"rt": 100,
"rtnm": "Jeffery Manor Express",
"rtclr": "#996633"
}, {
"rt": 103,
"rtnm": "West 103rd",
"rtclr": "#ff0066"
}, {
"rt": 106,
"rtnm": "East 103rd",
"rtclr": "#666600"
}, {
"rt": 108,
"rtnm": "Halsted/95th",
"rtclr": "#ff00ff"
}, {
"rt": 111,
"rtnm": "111th/King Drive",
"rtclr": "#66cc66"
}, {
"rt": "111A",
"rtnm": "Pullman Shuttle",
"rtclr": "#c8c8c8"
}, {
"rt": 112,
"rtnm": "Vincennes/111th",
"rtclr": "#cc3366"
}, {
"rt": 115,
"rtnm": "Pullman/115th",
"rtclr": "#c8c8c8"
}, {
"rt": 119,
"rtnm": "Michigan/119th",
"rtclr": "#33ff00"
}, {
"rt": 120,
"rtnm": "Ogilvie/Streeterville Express",
"rtclr": "#cc66ff"
}, {
"rt": 121,
"rtnm": "Union/Streeterville Express",
"rtclr": "#ff6600"
}, {
"rt": 124,
"rtnm": "Navy Pier",
"rtclr": "#ff9999"
}, {
"rt": 125,
"rtnm": "Water Tower Express",
"rtclr": "#99cc33"
}, {
"rt": 126,
"rtnm": "Jackson",
"rtclr": "#6699ff"
}, {
"rt": 130,
"rtnm": "Museum Campus",
"rtclr": "#c8c8c8"
}, {
"rt": 132,
"rtnm": "Goose Island Express",
"rtclr": "#993366"
}, {
"rt": 134,
"rtnm": "Stockton/LaSalle Express",
"rtclr": "#009900"
}, {
"rt": 135,
"rtnm": "Clarendon/LaSalle Express",
"rtclr": "#9900cc"
}, {
"rt": 136,
"rtnm": "Sheridan/LaSalle Express",
"rtclr": "#cc3300"
}, {
"rt": 143,
"rtnm": "Stockton/Michigan Express",
"rtclr": "#006666"
}, {
"rt": 146,
"rtnm": "Inner Drive/Michigan Express",
"rtclr": "#666600"
}, {
"rt": 147,
"rtnm": "Outer Drive Express",
"rtclr": "#ff00ff"
}, {
"rt": 148,
"rtnm": "Clarendon/Michigan Express",
"rtclr": "#66cc66"
}, {
"rt": 151,
"rtnm": "Sheridan",
"rtclr": "#cc3366"
}, {
"rt": 152,
"rtnm": "Addison",
"rtclr": "#33ff00"
}, {
"rt": 155,
"rtnm": "Devon",
"rtclr": "#cc66ff"
}, {
"rt": 156,
"rtnm": "LaSalle",
"rtclr": "#ff6600"
}, {
"rt": 157,
"rtnm": "Streeterville/Taylor",
"rtclr": "#33ccff"
}, {
"rt": 165,
"rtnm": "West 65th",
"rtclr": "#cc9966"
}, {
"rt": 169,
"rtnm": "69th-UPS Express",
"rtclr": "#99cc33"
}, {
"rt": 171,
"rtnm": "U. of Chicago/Hyde Park",
"rtclr": "#336633"
}, {
"rt": 172,
"rtnm": "U. of Chicago/Kenwood",
"rtclr": "#993366"
}, {
"rt": 192,
"rtnm": "U. of Chicago Hospitals Express",
"rtclr": "#cc3300"
}, {
"rt": 201,
"rtnm": "Central/Ridge",
"rtclr": "#996633"
}, {
"rt": 205,
"rtnm": "Chicago/Golf",
"rtclr": "#ff0066"
}, {
"rt": 206,
"rtnm": "Evanston Circulator",
"rtclr": "#666600"
}]
}
}
[
{"op": "move", "from": "/bustime-response", "path": ""}
]
[
{"op": "move", "from": "/bustime-response", "path": ""}
]
{
"services": [{
"apiId": "routes",
"apis": [{
"apiIn": "routes",
"actions": [{
"id": "forwardRequest",
"type": "forwardRequest",
"adapterId": "adapter.chicago.transportation",
"apiId": "getroutes",
"apiOut": "getroutes",//id
"Responses": [{
"code": "*",
"actionId": "sendResponseToOriginator"
}]
}]
}, {
"apiId": "getNearestBus",
"apiIn": "getNearestBus",
"actions": [{
"id": "forwardRequest",
"adapterId": "Chicago.transportation",
"apiId": "getNearestBus",
"type": "forwardRequest",
"adapterId": "adapter.chicago.transportation",
"apiOut": "getNearestBus",
"Responses": [{
"code": "*",
"actionId": "sendResponseToOriginator"
......
{
"baseUrl":"http://www.ctabustracker.com/bustime/api/v1",
"apiList":[
{
"id":"getroutes",
"method":"GET",
"path": "getroutes",
"queryParams":[
{
"name": "key",
"value": "gT2nciTKwRv6Jy5njqm8fe7LW"
}
],
"headers":[],
"content":null
}
]
}
......@@ -62,6 +62,7 @@ GET http://www.ctabustracker.com/bustime/api/v1/getroutes?key=gT2nciTKwRv6Jy5njq
GET ../mde/{tenantId}/{serviceId}/{apiId}?key=gT2nciTKwRv6Jy5njqm8fe7LW
GET ../mde/{tenantId}/chicago.transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
mde.HandleRequest(reqCtx)
{
......
......@@ -2,8 +2,8 @@ mde:
environment:
IPG_ENV_PARAMS: "-Dds.IpAddress=172.16.1.97:8080#\
-Dredis.host=172.16.1.244"
USE_DEBUG: yes
image: 172.16.1.132:5000/ipgallery/mde
USE_DEBUG: "yes"
image: 172.16.1.212:5050/ipgallery/mde
ports:
- "8000:8000"
- "8200:8000"
- "50040:50040"
\ No newline at end of file
#!/bin/sh
docker run -e IPG_ENV_PARAMS=-Dds.IpAddress=172.16.1.97:8080#-Dredis.host=172.16.1.244 -e USE_DEBUG=yes -p 8000:8000 -p 50040:50040 172.16.1.132:5000/ipgallery/mde
docker run -e IPG_ENV_PARAMS=-Dds.IpAddress=172.16.1.97:8080#-Dredis.host=172.16.1.244 -e USE_DEBUG=yes -p 8000:8000 -p 50040:50040 172.16.1.212:5050/ipgallery/mde
package defs;
/**
* Created by eli on 6/26/16.
*/
public class Constants {
public static final String ADAPTERS_PATH = "adapters.json";
public static final String CONFIG_LOCATION = "/opt/mcx/config/";
}
package defs;
/**
* Created by eli on 7/5/16.
*/
public class Enums {
public enum eKeyWord{
QUERY("query"),
HEADER("header");
private String keyWord;
private eKeyWord(String keyWord) {
this.keyWord = keyWord;
}
public String getKeyWord() {
return keyWord;
}
public static eKeyWord resolveKeyWord(String key){
eKeyWord[] keys = eKeyWord.values();
for (eKeyWord eKey : keys)
{
if (eKey.getKeyWord().equals(key))
return eKey;
}
return null;
}
}
}
......@@ -2,13 +2,16 @@ package handlers;
import logic.MdeManager;
import logic.adapter.HttpAdapter.HttpAdapter;
import microservice.RequestContext;
import microservice.handlers.BaseHandler;
import microservice.io.iface.ILogger;
import utils.common.CacheClient;
import utils.common.RedisCacheClient;
import utils.http.simpleRestClient.SimpleRestResponse;
import microservice.types.BaseRestResponse;
import util.MDEResult;
import common.CacheClient;
import common.RedisCacheClient;
import http.simpleRestClient.SimpleRestResponse;
import static java.lang.Thread.sleep;
/**
......@@ -31,30 +34,41 @@ public class MdeHandler extends BaseHandler {
@Override
public void init() {
// try {
// sleep(10000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
this.mdeManager = new MdeManager(logger);
this.rcc = new RedisCacheClient(this.getConfigValueAsString("redis.host","localhost"));
}
@Override
public void doCreate(RequestContext requestContext) {
BaseRestResponse brr = mdeManager.doCreate(requestContext);
writeObjectToResponse(requestContext, brr);
}
@Override
public void doRead(RequestContext requestContext) {
// GET ../mde/{tenantId}/{serviceId}/{apiId}
mdeManager.doRead(requestContext);
// example: GET ../mde/chicago/transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
//change to!!!!!: // example: GET ../mde/chicago.transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
BaseRestResponse brr = mdeManager.doRead(requestContext);
writeObjectToResponse(requestContext, brr);
}
@Override
public void doUpdate(RequestContext requestContext) {
BaseRestResponse brr = mdeManager.doUpdate(requestContext);
writeObjectToResponse(requestContext, brr);
}
@Override
public void doDelete(RequestContext requestContext) {
BaseRestResponse brr = mdeManager.doDelete(requestContext);
writeObjectToResponse(requestContext, brr);
}
private SimpleRestResponse errorResponse(String error) {
......
package logic;
import logic.adapter.BaseAdapter;
//import logic.adapter.HttpAdapter.model.DataHttpAdapter;
import logic.adapter.HttpAdapter.HttpAdapter;
import logic.adapter.repository.AdaptersRepository;
import microservice.RequestContext;
import microservice.io.iface.ILogger;
import microservice.types.BaseRestResponse;
import util.MDEResult;
import java.util.Map;
import java.util.List;
/**
* Created by eli on 6/7/16.
......@@ -13,39 +17,84 @@ import java.util.Map;
public class MdeManager {
private static final String DS_IP_ADDRESS = System.getProperty("ds.IpAddress", "localhost:8080");
private ILogger logger = null;
private int currentNumber = 0;
private ILogger logger;
private AdaptersRepository adaptersRepository;
// // TODO: 7/5/16
// private ServicesRepository adaptersRepository;
Map<String, BaseAdapter> AdapterList = null;
private List<String> serviceList = null;
public MdeManager(ILogger logger) {
try{
this.logger = logger;
LoadServices();
loadAdapters(logger);
}
catch(Exception e){
}
}
public void LoadServices() {
private void loadAdapters(ILogger logger)
{
adaptersRepository = new AdaptersRepository(logger);
adaptersRepository.load();
}
public void LoadAdapters() {
HttpAdapter httpAdapter = new HttpAdapter();
AdapterList.put("aaa", httpAdapter);
//read the cfg file SERVICES_FILE_NAME, to load each of the supported services
public void LoadServices() {
// final static String file = BASE_FILE_LOCATION+RELETIVE_FILE_PATH;
// JsonNode jNode = readJsonFromFile(file);
}
public void doRead(RequestContext requestContext) {
public BaseRestResponse doRead(RequestContext requestContext) {
BaseRestResponse brr;
// String tenantId = requestContext.params[0];
String serviceId = requestContext.params[0];
String apiId = requestContext.params[1];
// example: GET ../mde/chicago.transportation/routes?key=gT2nciTKwRv6Jy5njqm8fe7LW
// Service service = servicesRepository.getService(serviceId)
// Actions actions = servicesRepository.getServiceActions(apiId);
// for(Action action : actions)
// {
// String actionType = action.getType();
// if (actionType.equals(("forwardRequest"))) {
// BaseAdapter adapter = adaptersRepository.getAdapter(action.getAdapterId() );
// adapter.activateCommand(action.apiOut);
// }
//
// }
// adapter.activateCommand(action.apiOut);
String name = Thread.currentThread().getStackTrace()[1].getMethodName();
HttpAdapter adapter = (HttpAdapter) adaptersRepository.getAdapter("adapter.chicago.transportation" );
if (adapter!=null) {
MDEResult mdeResult = adapter.activateCommand(requestContext,/*action.apiOut*/ "getroutes");
brr = new BaseRestResponse(mdeResult.getSuccess(), mdeResult.getError());
brr.objectNode = mdeResult.getJsonData().get("data");
}
else
{
brr=new BaseRestResponse(false, "failed to find adapter "/*+ action.getAdapterId()*/+" adapter.chicago.transportation" );
}
return brr;
String tenantId = requestContext.params[0];
String serviceId = requestContext.params[1];
String apiId = requestContext.params[2];
}
public BaseRestResponse doCreate(RequestContext requestContext) {
return new BaseRestResponse(false, "method " +Thread.currentThread().getStackTrace()[1].getMethodName()+ "not implemented yet");
}
HttpAdapter httpAdapter = new HttpAdapter();
httpAdapter.doRead(requestContext);
public BaseRestResponse doUpdate(RequestContext requestContext) {
return new BaseRestResponse(false, "method " +Thread.currentThread().getStackTrace()[1].getMethodName() + "not implemented yet");
}
public BaseRestResponse doDelete(RequestContext requestContext) {
return new BaseRestResponse(false, "method " +Thread.currentThread().getStackTrace()[1].getMethodName() + "not implemented yet");
}
}
package logic.adapter;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.loader.IAdapterLoader;
import logic.adapter.model.BaseApi;
import logic.adapter.model.DataModel;
import logic.adapter.model.ResponseAction;
import logic.adapter.normalizer.INormalizer;
import logic.adapter.normalizer.JsonConverterNormalizer;
import logic.adapter.normalizer.Xml2jsonNormalizer;
import microservice.MicroserviceClient;
import microservice.RequestContext;
import microservice.io.iface.ILogger;
import util.MDEResult;
import util.Utils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import static util.Utils.SORTED_MAPPER;
/**
* Created by adi on 13/06/16.
* Created by eli on 13/06/16.
*/
public abstract class BaseAdapter {
public abstract class BaseAdapter<T extends BaseApi> {
protected String adapterId = null;
protected DataModel<T> dataModel ;
protected ILogger logger = null;
protected IAdapterLoader loader = null;
//iteration - order of insertion
private LinkedHashMap<String, INormalizer> normalizerMap = new LinkedHashMap<>();
protected LinkedHashMap<String, INormalizer> getNormalizerMap() {
return normalizerMap;
}
//apiClass is "T.class"
protected BaseAdapter(ILogger logger, String id, IAdapterLoader loader, Class<T> apiClass) throws Exception {
this.logger =logger;
this.adapterId = id;
this.loader = loader;
this.loadDataModel(apiClass);
//foreach api go trough each configured "responseAction" and create one and only one
//instance of normalizer (for this adapter). for the combination "$action.$type"
//for example: for normalize.xml2json create one normalizer (Xml2jsonNormalizer)
for (BaseApi api : dataModel.getApiList()) {
for (ResponseAction action : api.getResponseActions()){
switch(action.getAction()){
case "normalize":
switch (action.getType()){
case "xml2json":
Xml2jsonNormalizer xml2jsonNormalizer = new Xml2jsonNormalizer();
normalizerMap.putIfAbsent("normalize.xml2json", xml2jsonNormalizer);
break;
case "jsonConverter":
JsonConverterNormalizer jsonConverterNormalizer= new JsonConverterNormalizer();
normalizerMap.putIfAbsent("normalize.jsonConverter", jsonConverterNormalizer);
}
break;
}
}
}
}
private void loadDataModel(Class<T> apiClass) {
JsonNode jsonAdapterData = loader.load(adapterId);
this.dataModel = (DataModel<T>) Utils.getNodeAsParametricTypeObject(jsonAdapterData, DataModel.class, apiClass);
}
public DataModel<T> getDataModel(){ return dataModel;}
public void doRead(RequestContext requestContext) {};
public void doCreate(RequestContext requestContext) {};
public void doUpdate(RequestContext requestContext) {};
public void doDelete(RequestContext requestContext) {};
abstract public MDEResult activateCommand(RequestContext requestContext, String apiId);
protected INormalizer getNormalizer(String actionType){
return normalizerMap.get(actionType);
}
}
package logic.adapter.HttpAdapter;
import com.fasterxml.jackson.databind.JsonNode;
import defs.Enums;
import logic.adapter.BaseAdapter;
import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.HttpAdapter.model.NameValueParam;
import logic.adapter.loader.FileAdapterLoader;
import logic.adapter.model.DataModel;
import logic.adapter.model.ResponseAction;
import logic.adapter.normalizer.INormalizer;
import microservice.MicroserviceClient;
import microservice.RequestContext;
import microservice.io.iface.ILogger;
import microservice.params.CommandParams;
import microservice.params.RestClientParams;
import microservice.types.BaseRestResponse;
import util.MDEResult;
import http.simpleHttpClient.SimpleHttpClient;
import http.simpleHttpClient.SimpleHttpRequest;
import http.simpleHttpClient.SimpleHttpResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by adi on 13/06/16.
* Created by eli on 13/06/16.
*/
public class HttpAdapter extends BaseAdapter {
public class HttpAdapter extends BaseAdapter<DataHttpApi> {
//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 SimpleHttpClient httpClient = null;
public HttpAdapter(ILogger logger, String id) throws Exception {
super(logger, id, new FileAdapterLoader(logger), DataHttpApi.class);
httpClient = new SimpleHttpClient();
httpClient.Initialize(100);
}
public MDEResult activateCommand(RequestContext requestContext, String apiId) {
JsonNode jsonNode;
DataHttpApi api = dataModel.getApi(apiId);
MDEResult result;
CommandParams params = this.convertToCommandParams(requestContext, api);
SimpleHttpRequest.Method method=null;
switch (api.geteCmd()) {
case CREATE:
method=SimpleHttpRequest.Method.POST;
break;
case READ:
method=SimpleHttpRequest.Method.GET;
break;
case UPDATE:
method=SimpleHttpRequest.Method.PUT;
break;
case DELETE:
method=SimpleHttpRequest.Method.DELETE;
break;
}
result = doHttp(method, dataModel.getBaseUrl(), params);
if (result.getSuccess()) {
jsonNode = result.getJsonData();
String strContent = jsonNode.get("data").asText();
boolean flag = false;
if (result.getSuccess()) {
for (ResponseAction action : api.getResponseActions()) {
String normalizerKey = action.getAction() + "." + action.getType();
INormalizer normalizer = this.getNormalizerMap().get(normalizerKey);
if (normalizer != null) {
strContent = normalizer.activate(strContent, action.getData());
flag = true;
}
}
if (flag) result = new MDEResult(true, null, strContent);
}
}
return result;
}
protected String id = null;
private CommandParams convertToCommandParams(RequestContext requestContext, DataHttpApi api) {
public HttpAdapter() {
String path = ResolvePathValue(requestContext, api.getPath());
String requestParams = this.resolveQueryParamsToString(requestContext, api.getQueryParams());
//todo support headers and content - last 2 parameters
return new CommandParams(dataModel.getBasePath(), path, requestParams, null, null);
}
private String ResolvePathValue(RequestContext requestContext, String path) {
Matcher matcher = patternPathParamValue.matcher(path);
StringBuffer stringBuffer = new StringBuffer();
String matchedString, replacement;
int start, end;
boolean flag = false;
while (matcher.find()) {
if (!flag) flag = true;
start = matcher.start();
end = matcher.end();
matchedString = path.substring(start, end);
replacement = resolveFullMatchValue(requestContext, matchedString);
matcher.appendReplacement(stringBuffer, replacement);
}
if (flag)
return stringBuffer.toString();
else
return path;
}
public Boolean Init(String id) {
this.id = id;
return LoadFromFile(id);
private String resolveQueryParamsToString(RequestContext requestContext, List<NameValueParam> queryParams) {
StringBuilder sbQueryParams = new StringBuilder();
int size;
NameValueParam nameVal;
if (queryParams == null || (size = queryParams.size()) == 0) return null;
for (int i = 0; i < size; i++) {
nameVal = queryParams.get(i);
sbQueryParams.append(nameVal.getName()).append("=");
String value = resolveFullMatchValue(requestContext, nameVal.getValue());
sbQueryParams.append(value);
//if not the last element
if (i < size - 1)
sbQueryParams.append("&");
}
return sbQueryParams.toString();
}
public void doRead(RequestContext requestContext) {
String tenantId = requestContext.params[0];
String serviceId = requestContext.params[1];
String apiId = requestContext.params[2];
private String resolveFullMatchValue(RequestContext reqCnt, String value) {
Matcher matcher = patternFullMatchValue.matcher(value);
String resolvedValue = value;
if (matcher.matches()) {
//skip the $ and split to 2 Strings by '.'
String[] keyVal = value.substring(1).split("\\.");
String key = keyVal[0];
String val = keyVal[1];
Enums.eKeyWord ekey = Enums.eKeyWord.resolveKeyWord(key);
if (ekey!=null) {
switch (ekey) {
case QUERY:
//value contains query parameter
resolvedValue = reqCnt.getParameter(val);
break;
case HEADER:
// todo
break;
}
}
}
// todo: build the url & send to site
return resolvedValue;
}
protected Boolean LoadFromFile(String id) {
protected MDEResult doHttp(SimpleHttpRequest.Method method,
String domain,
int port,
String path,
String queryParams,
String content)
{
SimpleHttpResponse httpResp = null;
SimpleHttpRequest request = new SimpleHttpRequest();
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());
}
}
protected MDEResult doHttp(SimpleHttpRequest.Method method, String domain, CommandParams params) {
int port = 80;
String path;
// port
String[] domainTokens = domain.split(":");
if (domainTokens.length == 2)
port = Integer.valueOf(domainTokens[1]);
// path
if (params.getEntity() == null || params.getEntity() == "")
path = params.getParamsString();
else
path = params.getEntity() + "/" + params.getParamsString();
return false;
return doHttp(method, domain, port, path, params.getRequestParams(), params.getContent());
}
}
package logic.adapter.HttpAdapter.model;
import java.util.ArrayList;
import java.util.List;
/**
* Created by adi on 13/06/16.
*/
public class DataHttpAdapter {
public static enum eParamSource {
EPath,
EContent,
EQueryParams
};
public String serviceId = null;
public String baseUrl = null;
public List<DataHttpAdapterApi> apiList= null;
public DataHttpAdapter() {
apiList = new ArrayList<DataHttpAdapterApi>();
}
}
package logic.adapter.HttpAdapter.model;
import java.util.List;
/**
* Created by adi on 13/06/16.
*/
public class DataHttpAdapterApi {
public String id = null;
public String method = null;
public String uri = null;
public String content = null;
public List<String> headerList = null;
public List<String> pathList = null;
public List<String> queryParamsList = null;
}
package logic.adapter.HttpAdapter.model;
import java.util.ArrayList;
import java.util.List;
/**
* Created by eli on 13/06/16.
*/
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.annotation.JsonCreator;
import logic.adapter.model.BaseApi;
import logic.adapter.model.ResponseAction;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"id",
"method",
"path",
"queryParams",
"headers",
"content",
"responseActions"})
public class DataHttpApi extends BaseApi{
@JsonProperty
private String method = null;
@JsonProperty
private String path = null;
@JsonProperty
private List<NameValueParam> queryParams = null;
@JsonProperty
private List<NameValueParam> headers = null;
@JsonProperty
private String content = null;
@JsonCreator
public DataHttpApi(@JsonProperty("id") String id,
@JsonProperty("responseActions") ArrayList<ResponseAction> responseActions) {
super(id, responseActions);
}
@Override
protected void setCmd() {
switch (method){
case "POST":
setCmd(eCommand.CREATE);
break;
case "GET":
setCmd(eCommand.READ);
break;
case "PUT":
setCmd(eCommand.UPDATE);
break;
case "DELETE":
setCmd(eCommand.DELETE);
break;
}
}
@JsonIgnore
public String toString() {
StringBuilder sb=new StringBuilder("id="+getId()+", eCmd="+geteCmd().toString()+", method=" + method );
if (path!=null) sb.append(", path=" +path);
if (queryParams!=null) sb.append(", queryParams=" + queryParams.toString());
if (headers!=null) sb.append(", headers=" + headers.toString());
if (content!=null) sb.append(", content=" +content);
return sb.toString();
}
public List<NameValueParam> getHeaders() {
return headers;
}
public void setHeaders(ArrayList<NameValueParam> headers) {
this.headers = headers;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
setCmd();
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public List<NameValueParam> getQueryParams() {
return queryParams;
}
public void setQueryParams(ArrayList<NameValueParam> queryParams) {
this.queryParams = queryParams;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
package logic.adapter.HttpAdapter.model;
/**
* Created by eli on 6/27/16.
*/
import com.fasterxml.jackson.annotation.JsonIgnore;
public class NameValueParam {
private String name;
private String value;
@JsonIgnore
public String toString(){
return new String(name+"="+value);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
package logic.adapter.loader;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.io.iface.ILogger;
import util.Utils;
import static defs.Constants.ADAPTERS_PATH;
import static defs.Constants.CONFIG_LOCATION;
//import static defs.Constants.CONFIG_LOCATION;
/**
* Created by eli on 6/23/16.
*/
public class FileAdapterLoader implements IAdapterLoader {
private ILogger logger = null;
public FileAdapterLoader(ILogger logger) {
this.logger = logger;
}
public JsonNode load(String adapterId) {
String adapterFile = CONFIG_LOCATION+adapterId + ".json";
return Utils.readJsonNodeFromFile(adapterFile);
}
}
package logic.adapter.loader;
import com.fasterxml.jackson.databind.JsonNode;
import microservice.io.iface.ILogger;
/**
* Created by eli on 6/23/16.
*/
public interface IAdapterLoader {
JsonNode load(String adapterId);
}
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.annotation.JsonTypeInfo;
import microservice.params.CommandParams;
import java.util.ArrayList;
/**
* Created by eli on 6/30/16.
*/
//@JsonTypeInfo(use= JsonTypeInfo.Id.CLASS, include= JsonTypeInfo.As.PROPERTY, property="class")
public abstract class BaseApi {
public enum eCommand { CREATE, READ, UPDATE, DELETE, BAD_COMMAND}
@JsonCreator
protected BaseApi(String id,
ArrayList<ResponseAction> responseActions)
{ this.id=id; this.responseActions = responseActions; eCmd = eCommand.BAD_COMMAND;}
// @JsonCreator
// protected BaseApi(@JsonProperty("id") String id)
// { this.id=id; eCmd = eCommand.BAD_COMMAND;}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@JsonIgnore
public eCommand geteCmd() {
return eCmd;
}
@JsonIgnore
public void setCmd(eCommand eCmd) {
this.eCmd = eCmd;
}
@JsonIgnore
protected abstract void setCmd();
// @JsonIgnore
// public abstract void setCommandParams();
//
// @JsonIgnore
// public CommandParams getCmdParams() {
// return cmdParams;
// }
@JsonProperty
public ArrayList<ResponseAction> getResponseActions() {
return responseActions;
}
@JsonProperty
public void setResponseActions(ArrayList<ResponseAction> responseActions) {
this.responseActions = responseActions;
}
@JsonProperty
private String id;
@JsonProperty
private ArrayList<ResponseAction> responseActions= new ArrayList<ResponseAction>();
@JsonIgnore
private eCommand eCmd;
// @JsonIgnore
// protected CommandParams cmdParams;
}
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.databind.JavaType;
import com.fasterxml.jackson.databind.type.TypeFactory;
import java.util.ArrayList;
import java.util.List;
import static com.fasterxml.jackson.databind.type.TypeFactory.*;
/**
* Created by eli on 6/30/16.
*/
public class DataModel<T extends BaseApi> {
//private String serviceId = null;
@JsonProperty
private String baseUrl = null;
@JsonProperty
private String basePath;
@JsonProperty
private ArrayList<T> apiList = new ArrayList<>();
@JsonCreator
public DataModel() {
}
@JsonIgnore
public String toString() {
// return new String("serviceId="+serviceId+", baseUri="+baseUrl+"apilist="+ apiList.toString());
return new StringBuilder().append("baseUri=").append(baseUrl).append(", apilist=").append(apiList.toString()).toString();
}
// public void setApisCommandParams() {
// for (T api : apiList){
// api.setCommandParams();
// }
// }
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public ArrayList<T> getApiList() {
return apiList;
}
public void setApiList(ArrayList<T> apiList) {
this.apiList = apiList;
}
public T getApi(String id) {
//todo change the list to map for better performance
for(T api : apiList)
{
if (api.getId().equals(id))
return api;
}
return null;
}
public String getBasePath() {
return basePath;
}
public void setBasePath(String basePath) {
this.basePath = basePath;
}
}
package logic.adapter.model;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Created by eli on 7/11/16.
*/
public class ResponseAction {
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public JsonNode getData() {
return data;
}
public void setData(JsonNode data) {
this.data = data;
}
@JsonProperty
private String action;
@JsonProperty
private String type;
@JsonProperty
private JsonNode data;
}
package logic.adapter.normalizer;
import com.fasterxml.jackson.databind.JsonNode;
/**
* Created by eli on 7/11/16.
*/
public interface INormalizer {
String activate(String strToNormalize, JsonNode format);
}
package logic.adapter.normalizer;
import com.fasterxml.jackson.databind.JsonNode;
import com.flipkart.zjsonpatch.JsonPatch;
//import com.google.api.client.json.Json;
import defs.Constants;
import org.apache.commons.lang.text.StrTokenizer;
import util.Utils;
import common.JsonHandler;
import java.io.File;
import static util.Utils.readJsonNodeFromFile;
/**
* Created by eli on 7/13/16.
*/
public class JsonConverterNormalizer implements INormalizer {
final private static String JSON_PATCH_LOCATION = Constants.CONFIG_LOCATION;
@Override
public String activate(String strToNormalize, JsonNode format) {
if (format != null && !format.isNull()) {
try {
JsonNode jsonNodeToNormalize = JsonHandler.getJsonNodeFromString(strToNormalize);
JsonNode fileJsonNode = format.get("jsonPatchfile");
if (fileJsonNode != null && !fileJsonNode.isNull()) {
JsonNode jsonPatch = Utils.readJsonNodeFromFile(JSON_PATCH_LOCATION + fileJsonNode.asText());
JsonNode jsonTarget = JsonPatch.apply(jsonPatch, jsonNodeToNormalize);
strToNormalize = JsonHandler.getObjectAsJsonString(jsonTarget);
}}
catch (Exception e){
e.printStackTrace();
}
}
return strToNormalize;
}
}
package logic.adapter.normalizer;
//import org.json.XML
import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
/**
* Created by eli on 7/11/16.
*/
public class Xml2jsonNormalizer implements INormalizer {
/**
* convert xml data string to Json string
* @param data - xml string to convert
* @return
*/
/**
* convert xml data string to Json string
* @param strToNormalize - xml string to convert
* @param format format may hold format data that needed for the normalizer for the activate
* currently, this normalizer doesn't need any data
* @return normalized String, null on failure
*/
@Override
public String activate(String strToNormalize, JsonNode format) {
JSONObject jsonObject;
String retString=null;
try {
jsonObject=XML.toJSONObject(strToNormalize);
retString=jsonObject.toString();
}
catch(JSONException | NullPointerException e){
e.printStackTrace();
}
return retString;
}
}
package logic.adapter.repository;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import defs.Constants;
import logic.adapter.BaseAdapter;
import logic.adapter.HttpAdapter.HttpAdapter;
import microservice.io.iface.ILogger;
import util.Utils;
import java.util.HashMap;
import java.util.Map;
/**
* Created by eli on 6/21/16.
*/
public class AdaptersRepository {
private ILogger logger;
private HashMap<String, BaseAdapter> adapterList = null;
public AdaptersRepository(ILogger logger) {
this.logger = logger;
adapterList = new HashMap<>();
}
public void load() {
ArrayNode adaptersArray;
JsonNode jsonNode;
jsonNode = Utils.readJsonNodeFromFile(Constants.CONFIG_LOCATION+Constants.ADAPTERS_PATH);
if (jsonNode!=null) {
adaptersArray = (ArrayNode) jsonNode.get("adapters");
if ( adaptersArray!= null && !adaptersArray.isNull() &&!adaptersArray.isArray()) {
// logger.error("syntax error in file: " + CONFIG_LOCATION + ADAPTERS_PATH + ". the value of \"adapters\" must be array node");
return;
}
else
// load adapters
{
for (int i = 0; i < adaptersArray.size(); i++) {
JsonNode jsonAdapter = adaptersArray.get(i);
String adapterId = jsonAdapter.get("id").asText();
String adapterType = jsonAdapter.get("type").asText();
switch (adapterType) {
case "HttpAdapter":
HttpAdapter httpAdapter = null;
try {
httpAdapter = new HttpAdapter(logger, adapterId);
adapterList.put(adapterId, httpAdapter);
} catch (Exception e) {
e.printStackTrace();
logger.error("failed to load adapter: " + adapterId+ ", Exception:" + e.toString());
}
break;
default:
//shouldnt get here
break;
}
}
}
}
}
public BaseAdapter getAdapter(String adapterId){
return adapterList.get(adapterId);
}
}
package util;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import common.JsonHandler;
public class MDEResult {
protected boolean success;
protected String error;
protected JsonNode jsonData;
// public MDEResult(boolean success, String error, JsonNode jsonNode) {
// this.success = success;
// this.error = error;
// this.jsonData = jsonNode;
// }
public MDEResult(boolean success, String error, String data) {
this.success = success;
this.error = error;
ObjectNode dataNode = Utils.SORTED_MAPPER.createObjectNode();
dataNode.put("data", data);
this.jsonData = dataNode;
}
public MDEResult(String error) {
this.success = false;
this.error = error;
this.jsonData = null;
}
public boolean getSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
public JsonNode getJsonData() {
return jsonData;
}
public void setJsonData(JsonNode jsonData) {
this.jsonData = jsonData;
}
}
package util;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import java.io.File;
import java.io.IOException;
/**
* Created by eli on 6/23/16.
*/
public class Utils {
public static final ObjectMapper SORTED_MAPPER = new ObjectMapper();
public static JsonNode readJsonNodeFromFile(String strFile) {
File file=new File(strFile);
JsonNode jsonNode = null;
try {
jsonNode = SORTED_MAPPER.readTree(file);
} catch (IOException e) {
e.printStackTrace();
}
return jsonNode;
}
public static Object getNodeAsObject(JsonNode jsonNode, TypeReference<?> type) {
ObjectReader objectReader = SORTED_MAPPER.reader(type);
if(jsonNode != null && !jsonNode.isNull() && !jsonNode.isMissingNode() && objectReader != null) {
try {
return objectReader.readValue(jsonNode);
} catch (Exception var4) {
var4.printStackTrace();
}
}
else
return null;
return null;
}
public static Object getNodeAsObject(JsonNode jsonNode, JavaType javaType) {
ObjectReader objectReader = SORTED_MAPPER.reader(javaType);
if(jsonNode != null && !jsonNode.isNull() && !jsonNode.isMissingNode() && objectReader != null) {
try {
return objectReader.readValue(jsonNode);
} catch (Exception var4) {
var4.printStackTrace();
}
}
else
return null;
return null;
}
public static Object getNodeAsParametricTypeObject(JsonNode jsonNode, Class<?> outer, Class<?> inner) {
JavaType javaType = SORTED_MAPPER.getTypeFactory().constructParametricType(outer, inner);
return getNodeAsObject(jsonNode, javaType);
}
static {
SORTED_MAPPER.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true);
//todo turn on back again
SORTED_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
}
......@@ -8,8 +8,8 @@ package web;
import microservice.io.impl.IPubSubMQTTImpl;
import microservice.io.impl.IRestServerUndertowImpl;
import microservice.params.RestServerParams;
import utils.common.configuration.ConfigProperties;
import utils.common.configuration.EnumPropertyType;
import common.configuration.ConfigProperties;
import common.configuration.EnumPropertyType;
import handlers.MdeHandler;
public class App {
......
import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.model.BaseApi;
import org.junit.Test;
import java.util.ArrayList;
/**
* Created by eli on 7/7/16.
*/
public class TestArrayList {
public class ArrayTest<T extends BaseApi>{
public ArrayList<T> getApiList() {
return apiList;
}
public void setApiList(ArrayList<T> apiList) {
this.apiList = apiList;
}
public T getApi(String id){
T t = apiList.get(0);
return t;
}
private ArrayList<T> apiList = new ArrayList<>();
}
public class ArrayExtends extends ArrayTest<DataHttpApi>
{
public ArrayExtends() {
}
}
@Test
public void testArrayList(){
// ArrayList<T> apiList = new ArrayList<>();
// ArrayExtends arr = new ArrayExtends();
// DataHttpApi api = new DataHttpApi("adapter.chicago.transportation");
// arr.getApiList().add(api);
// api = arr.getApi("adapter.chicago.transportation");
// System.out.println(api.toString());
}
}
import logic.adapter.HttpAdapter.HttpAdapter;
import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.model.DataModel;
import microservice.io.iface.ILogger;
import org.junit.Test;
/**
* Created by eli on 7/7/16.
*/
public class TestHttpAdapter {
static public ILogger logger;
@Test
public void testHttpAdapter() {
HttpAdapter httpAdapter;
try {
httpAdapter = new HttpAdapter(logger, "adapter.chicago.transportation");
DataModel<DataHttpApi> data=httpAdapter.getDataModel();
System.out.println(data.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import logic.adapter.HttpAdapter.model.DataHttpApi;
import logic.adapter.model.DataModel;
import org.junit.Test;
import util.Utils;
import utils.common.JsonHandler;
import java.lang.reflect.Type;
import java.util.ArrayList;
/**
* Created by eli on 6/27/16.
*/
public class TestJson {
@Test
public void testSerialize()
{
JsonNode jsonNode = Utils.readJsonNodeFromFile("/home/eli/git/ipgallery/java/mde/cfg/testJson.json");
// DataModel dataModel = new DataModel(new ArrayList<DataHttpApi>());
TypeReference<DataModel<DataHttpApi>> type = new TypeReference<DataModel<DataHttpApi>>() {
@Override
public Type getType() {
return super.getType();
}
@Override
public int compareTo(TypeReference<DataModel<DataHttpApi>> o) {
return super.compareTo(o);
}
};
DataModel<DataHttpApi> dataModel = (DataModel<DataHttpApi>)Utils.getNodeAsObject(jsonNode,type);
System.out.println(dataModel.toString());
}
}
import com.fasterxml.jackson.databind.JsonNode;
import com.flipkart.zjsonpatch.JsonPatch;
import org.junit.Test;
import util.Utils;
import common.JsonHandler;
/**
* Created by eli on 7/14/16.
*/
public class TestJsonPatch {
@Test
public void testJsonPatch()
{
JsonNode jsonContent = Utils.readJsonNodeFromFile("/home/eli/git/ipgallery/java/mde/cfg/jsonContent.json");
JsonNode jsonPatch = Utils.readJsonNodeFromFile("/home/eli/git/ipgallery/java/mde/cfg/jsonPatchBusTracker.json");
JsonNode jsonTarget = JsonPatch.apply(jsonPatch, jsonContent);
String strToNormalize = JsonHandler.getObjectAsJsonString(jsonTarget);
System.out.println(strToNormalize);
}
}
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Test;
import util.Utils;
/**
* Created by eli on 7/6/16.
*/
public class TestReadJsonFile {
@Test
public void testReadJsonFile()
{
JsonNode jsonNode = Utils.readJsonNodeFromFile("/opt/mcx/config/mde/adapters.json");
}
}
import org.junit.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Created by eli on 7/5/16.
*/
public class TestRegex {
@Test
public void testRegex1(){
String str="$query.key";
String regex = "^\\$[a-zA-Z]{3,10}\\.[a-zA-Z]{3,10}$";
// Pattern pattern = Pattern.compile("\\$[a-z]\\.[a-zA-Z]");
// Pattern pattern = Pattern.compile("^(\\w)+(\\.)+(\\w)$");
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(str);
boolean flag=matcher.matches();
if (flag) {
System.out.print("SUCCESS!! str=" + str + " matches REGEX " + regex);
String[] keyVal = str.substring(1).split("\\.");
System.out.println("key=" +keyVal[0]+ " Val= "+ keyVal[1] );
}
else
System.out.print("FAIL!! str="+str+" does not matches REGEX " + regex);
}
@Test
public void testRegex2(){
String str="/eli/benb/$query.key/baruch";
String regex = "(/[a-zA-Z]{1,15})*(\\$[a-zA-Z]{1,15}\\.[a-zA-Z]{1,15}){1}(/[a-zA-Z]{1,15})*";
String regex1 = "\\$[a-zA-Z]{3,10}\\.[a-zA-Z]{3,10}";
// Pattern pattern = Pattern.compile("\\$[a-z]\\.[a-zA-Z]");
// Pattern pattern = Pattern.compile("^(\\w)+(\\.)+(\\w)$");
Pattern pattern = Pattern.compile(regex1);
Matcher matcher = pattern.matcher(str);
boolean flag=matcher.find();
if (flag) {
System.out.print("SUCCESS!! str=" + str + " matches REGEX " + regex);
String[] keyVal = str.substring(1).split("\\.");
System.out.println("key=" +keyVal[0]+ " Val= "+ keyVal[1] );
}
else
System.out.print("FAIL!! str="+str+" does not matches REGEX " + regex);
}
}
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