Commit 382727dc by Gil Sade

added camera

parent 88156cb1
Showing with 146 additions and 33 deletions
......@@ -23,11 +23,12 @@ public class GEManager {
public static String AUTH_URL = "8553482c-1d32-4d38-8597-2e56ab642dd3.predix-uaa.run.asv-pr.ice.predix.io";
public static String AUTH_PATH = "oauth/token";
public static String AUTH_PARAMS = "grant_type=client_credentials&client_id=ipgallery";
public static String BASE_URL = "ie-cities-metadata.run.asv-pr-pub.ice.predix.io";
public static String METADATA_URL = "ie-cities-metadata.run.asv-pr-pub.ice.predix.io";
public static String MEDIA_URL = "ie-cities-media.run.asv-pr-pub.ice.predix.io";
public static String ASSET_LIST_URL = "v2/assets/search";
// public static String TOKEN = "eyJhbGciOiJSUzI1NiIsImtpZCI6ImxlZ2FjeS10b2tlbi1rZXkiLCJ0eXAiOiJKV1QifQ.eyJqdGkiOiJlOTc5MTUyNjE5MjE0NDBhOGE3NWQ5YzQ3NGM0ODNjYSIsInN1YiI6ImlwZ2FsbGVyeSIsInNjb3BlIjpbImllLWN1cnJlbnQuaWNzLUlFLVBVQkxJQy1TQUZFVFkuSUUtUFVCTElDLVNBRkVUWS5MSU1JVEVELkRFVkVMT1AiLCJ1YWEucmVzb3VyY2UiLCJpZS1jdXJyZW50Lmljcy1JRS1FTlZJUk9OTUVOVEFMLklFLUVOVklST05NRU5UQUwuRlJFRS5ERVZFTE9QIiwiaWUtY3VycmVudC5pY3MtSUUtUEFSS0lORy5JRS1QQVJLSU5HLkxJTUlURUQuREVWRUxPUCIsImllLWN1cnJlbnQuaWNzLUlFLVRSQUZGSUMuSUUtVFJBRkZJQy5MSU1JVEVELkRFVkVMT1AiLCJpZS1jdXJyZW50Lmljcy1JRS1QRURFU1RSSUFOLklFLVBFREVTVFJJQU4uTElNSVRFRC5ERVZFTE9QIl0sImNsaWVudF9pZCI6ImlwZ2FsbGVyeSIsImNpZCI6ImlwZ2FsbGVyeSIsImF6cCI6ImlwZ2FsbGVyeSIsImdyYW50X3R5cGUiOiJjbGllbnRfY3JlZ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String PS_ZONE_ID = "ics-IE-PUBLIC-SAFETY";
public static String TOKEN = "NOT_VALID";
private SimpleHttpClient httpClient = null;
......@@ -37,46 +38,109 @@ public class GEManager {
}
public BaseRestResponse executeRequest(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content, boolean isMaintenanceRequest) {
SimpleHttpResponse resp = null;
SimpleHttpRequest req = null;
String error = null;
String error = null;
BaseRestResponse brr = new BaseRestResponse(false,null);
switch(apiIdAsParams[0]) {
case "cameras":
req = buildCameraHttpRequest(requestContext,content);
try {
resp = httpClient.processRequest(req);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
brr = getCamerasList(serviceId,apiIdAsParams,requestContext,content,isMaintenanceRequest);
break;
case "camera":
brr = getCamera(serviceId,apiIdAsParams,requestContext,content,isMaintenanceRequest);
break;
default:
}
return brr;
}
private BaseRestResponse getCamera(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content, boolean isMaintenanceRequest) {
SimpleHttpRequest req = null;
SimpleHttpResponse resp = null;
BaseRestResponse brr = new BaseRestResponse(false,null);
req = buildCameraHttpRequest(requestContext,content,apiIdAsParams);
try {
resp = httpClient.processRequest(req);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(resp.getStatusCode() == 200)
return buildResp(resp);
return buildCameraResp(resp);
else if (resp.getStatusCode() == 403 || resp.getStatusCode() == 401) {
req = buildGetTokenRequest();
try {
resp = httpClient.processRequest(req);
if(getNewToken())
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content,isMaintenanceRequest);
else
brr.setError("failed to retrieve a valid token");
}
return brr;
}
private BaseRestResponse getCamerasList(String serviceId, String[] apiIdAsParams, RequestContext requestContext, JsonNode content, boolean isMaintenanceRequest) {
SimpleHttpRequest req = null;
SimpleHttpResponse resp = null;
BaseRestResponse brr = new BaseRestResponse(false,null);
req = buildCamerasHttpRequest(requestContext,content);
try {
resp = httpClient.processRequest(req);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(resp.getStatusCode() == 200)
return buildCamerasResp(resp);
else if (resp.getStatusCode() == 403 || resp.getStatusCode() == 401) {
if(getNewToken())
brr = executeRequest(serviceId,apiIdAsParams,requestContext,content,isMaintenanceRequest);
else
brr.setError("failed to retrieve a valid token");
}
return brr;
}
private boolean getNewToken() {
SimpleHttpRequest req = null;
SimpleHttpResponse resp = null;
req = buildGetTokenRequest();
try {
resp = httpClient.processRequest(req);
if (resp.getStatusCode() == 200) {
String cont = resp.getContent();
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode contObj = mapper.readTree(cont);
TOKEN = contObj.get("access_token").asText();
return true;
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
}
} catch(UnsupportedEncodingException e){
e.printStackTrace();
}
return false;
}
private BaseRestResponse buildCameraResp(SimpleHttpResponse resp) {
BaseRestResponse brr = new BaseRestResponse(true,null);
SimpleHttpRequest req;
String content = resp.getContent();
ObjectMapper mapper = new ObjectMapper();
try {
ObjectNode objectNode = mapper.createObjectNode();
JsonNode contentObj = mapper.readTree(content);
JsonNode arrNode = contentObj.get("content");
if (arrNode.isArray()) {
req = buildOpenCameraLinkRequest(arrNode.get(0).get("url").asText());
resp = httpClient.processRequest(req);
}
// brr.objectNode = resp.getContent();
} catch (IOException e) {
e.printStackTrace();
}
return new BaseRestResponse(false,String.valueOf(resp.getStatusCode()));
return brr;
}
private BaseRestResponse buildResp(SimpleHttpResponse resp) {
private BaseRestResponse buildCamerasResp(SimpleHttpResponse resp) {
BaseRestResponse brr = new BaseRestResponse(true,null);
int size;
String content = resp.getContent();
......@@ -96,14 +160,14 @@ public class GEManager {
double lon = Double.parseDouble(latLon[1]);
objectNode.put("lat",lat);
objectNode.put("lon",lon);
ArrayNode linksArray = mapper.createArrayNode();
ObjectNode linksNode = mapper.createObjectNode();
linksNode.put("latestImage",(JsonNode)null);
linksArray.add(linksNode);
linksNode = mapper.createObjectNode();
linksNode.put("latestVideo",(JsonNode)null);
linksArray.add(linksNode);
objectNode.put("links",linksArray);
// ArrayNode linksArray = mapper.createArrayNode();
// ObjectNode linksNode = mapper.createObjectNode();
// linksNode.put("latestImage",(JsonNode)null);
// linksArray.add(linksNode);
// linksNode = mapper.createObjectNode();
// linksNode.put("latestVideo",(JsonNode)null);
// linksArray.add(linksNode);
// objectNode.put("links",linksArray);
node.add(objectNode);
}
brr.objectNode = node;
......@@ -116,10 +180,48 @@ public class GEManager {
return brr;
}
private SimpleHttpRequest buildCameraHttpRequest(RequestContext requestContext,JsonNode content) {
private SimpleHttpRequest buildOpenCameraLinkRequest(String link) {
SimpleHttpRequest request = new SimpleHttpRequest();
String[] domainFileUrl = extractDomainFile(link);
if(domainFileUrl != null) {
request.setProtocol("https");
request.setDomain(domainFileUrl[0]);
request.setPort(443);
request.setMethod(SimpleHttpRequest.Method.GET);
String path = "/v2/file/" + domainFileUrl[1];
request.setPath(path);
request.addHeader("Authorization", "Bearer " + TOKEN);
request.addHeader("Predix-Zone-Id", PS_ZONE_ID);
}
return request;
}
private SimpleHttpRequest buildCameraHttpRequest(RequestContext requestContext, JsonNode content, String[] apiIdAsParams) {
switch(apiIdAsParams[2]) {
case "latestImage":
SimpleHttpRequest request = new SimpleHttpRequest();
request.setProtocol("https");
request.setDomain(MEDIA_URL);
request.setPort(443);
request.setMethod(SimpleHttpRequest.Method.GET);
String path = "/v2/assets/" + apiIdAsParams[1] + "/media/latest";
request.setQueryString("mediaType=IMAGE");
request.setPath(path);
request.addHeader("Authorization", "Bearer " + TOKEN);
request.addHeader("Predix-Zone-Id", PS_ZONE_ID);
if(content != null)
request.setContent(content.toString());
return request;
default:
}
return null;
}
private SimpleHttpRequest buildCamerasHttpRequest(RequestContext requestContext,JsonNode content) {
SimpleHttpRequest request = new SimpleHttpRequest();
request.setProtocol("https");
request.setDomain(BASE_URL);
request.setDomain(METADATA_URL);
request.setPort(443);
request.setMethod(SimpleHttpRequest.Method.GET);
request.setPath(ASSET_LIST_URL);
......@@ -127,7 +229,7 @@ public class GEManager {
request.addHeader("Predix-Zone-Id", PARKING_ZONE_ID);
Map<String, Deque<String>> queryParams = requestContext.queryParameters;
if(queryParams != null && !queryParams.isEmpty()) {
String bbox = "bbox=" + queryParams.get("bbox").pop() + "&page=0&size=500&q=assetType:CAMERA";
String bbox = "bbox=" + queryParams.get("bbox").peek() + "&page=0&size=500&q=assetType:CAMERA";
request.setQueryString(bbox);
}
if(content != null)
......@@ -146,4 +248,15 @@ public class GEManager {
request.addHeader("Authorization", AUTH);
return request;
}
private String[] extractDomainFile(String link) {
String[] array = null;
String[] newArray = new String[2];
array = link.split("/");
if(array != null) {
newArray[0] = array[2];
newArray[1] = array[5];
}
return newArray;
}
}
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