Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
ipgallery
/
mde
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Registry
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
2e579c94
authored
Mar 20, 2017
by
Adi Amir
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
re-organize code
parent
8e36c353
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
197 additions
and
130 deletions
cfg/config.properties
src/main/java/logic/GEManager.java
cfg/config.properties
View file @
2e579c94
...
...
@@ -6,6 +6,6 @@ log4j.prop.file.path=/logs/conf/log4j.xml
### microservice/rest server
server.port
=
50040
server.host
=
0.0.0.0
server.worker.threads
=
1
0
server.worker.threads
=
1
6
src/main/java/logic/GEManager.java
View file @
2e579c94
...
...
@@ -7,8 +7,10 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
import
http.simpleHttpClient.SimpleHttpClient
;
import
http.simpleHttpClient.SimpleHttpRequest
;
import
http.simpleHttpClient.SimpleHttpResponse
;
import
microservice.MicroserviceClient
;
import
microservice.RequestContext
;
import
microservice.io.iface.ILogger
;
import
microservice.params.RestClientParams
;
import
microservice.types.BaseRestResponse
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.http.HttpResponse
;
...
...
@@ -20,6 +22,7 @@ import org.apache.http.conn.scheme.SchemeRegistry;
import
org.apache.http.conn.ssl.SSLSocketFactory
;
import
org.apache.http.impl.client.DefaultHttpClient
;
import
org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager
;
import
web.App
;
import
java.io.IOException
;
import
java.io.InputStream
;
...
...
@@ -46,11 +49,18 @@ public class GEManager {
public
static
String
TOKEN
=
"NOT_VALID"
;
private
SimpleHttpClient
httpClient
=
null
;
private
MicroserviceClient
geClient
=
null
;
private
RestClientParams
geClientParams
=
null
;
private
ILogger
logger
;
private
ObjectMapper
objMapper
=
new
ObjectMapper
();
public
GEManager
(
ILogger
logger
)
throws
Exception
{
this
.
logger
=
logger
;
initHttpClient
();
}
private
void
initHttpClient
()
{
httpClient
=
new
SimpleHttpClient
();
httpClient
.
Initialize
(
100
);
httpClient
.
addMimeType
(
"video/mp4"
);
...
...
@@ -76,6 +86,9 @@ public class GEManager {
else
if
(
apiIdAsParams
.
length
==
3
&&
apiIdAsParams
[
0
].
equals
(
"camera"
)
&&
apiIdAsParams
[
2
].
equals
(
"details"
))
{
brr
=
getCameraDetails
(
apiIdAsParams
[
1
]
/*camId*/
,
requestContext
);
}
else
if
(
apiIdAsParams
.
length
==
1
&&
apiIdAsParams
[
0
].
equals
(
"sensors"
))
{
brr
=
getSensorList
(
requestContext
);
}
else
{
String
err
=
"executeRequest() failed. Unknown api: "
+
getApiIdString
(
apiIdAsParams
);
brr
=
errorResponse
(
err
);
...
...
@@ -94,11 +107,14 @@ public class GEManager {
long
tEnd
=
System
.
currentTimeMillis
();
double
elapsedTime
=
((
double
)(
tEnd
-
tStart
))
/
1000.0
;
logger
.
info
(
"executeRequest finished
:
. time= "
+
elapsedTime
+
" success="
+
brr
.
success
+
" api="
+
getApiIdString
(
apiIdAsParams
));
logger
.
info
(
"executeRequest finished. time= "
+
elapsedTime
+
" success="
+
brr
.
success
+
" api="
+
getApiIdString
(
apiIdAsParams
));
return
brr
;
}
/*** A P I ***/
private
BaseRestResponse
getLatestMediaFile
(
String
camId
,
String
mediaType
,
RequestContext
requestContext
)
{
SimpleHttpResponse
httpResp
=
null
;
...
...
@@ -153,67 +169,6 @@ public class GEManager {
return
brr
;
}
private
SimpleHttpRequest
buildLatestMediaFileUrlRequest
(
String
camId
,
String
mediaType
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
MEDIA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
String
path
=
"/v2/assets/"
+
camId
+
"/media/latest"
;
request
.
setQueryString
(
"mediaType="
+
mediaType
);
request
.
setPath
(
path
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
request
.
addHeader
(
"Predix-Zone-Id"
,
PS_ZONE_ID
);
return
request
;
}
private
BaseRestResponse
buildLatestMediaFileResp
(
String
fileData
,
String
url
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
try
{
ObjectNode
dataObj
=
objMapper
.
createObjectNode
();
// parse file url
// String[0]: cameraId
// String[1]: timestamp
// String[2]: media type (IMAGE/VIDEO)
// String[3]: fileName
String
[]
urlParts
=
getMediaFileName
(
url
,
"latest"
);
if
(
urlParts
==
null
)
{
String
err
=
"buildLatestMediaFileResp() - failed to parse file url: "
+
url
;
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
dataObj
.
put
(
"cameraId"
,
urlParts
[
0
]);
dataObj
.
put
(
"timestamp"
,
Long
.
valueOf
(
urlParts
[
1
]));
dataObj
.
put
(
"mediaType"
,
urlParts
[
2
]);
dataObj
.
put
(
"fileName"
,
urlParts
[
3
]);
dataObj
.
put
(
"fileData"
,
fileData
);
brr
.
objectNode
=
dataObj
;
}
catch
(
Exception
e
)
{
String
err
=
"buildLatestMediaFileResp() failed with exception. e: "
+
e
.
toString
();
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
return
brr
;
}
private
SimpleHttpRequest
buildGetCameraDetailsRequest
(
String
camId
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
METADATA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
String
path
=
ASSETS_URL
+
"/"
+
camId
;
request
.
setPath
(
path
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
request
.
addHeader
(
"Predix-Zone-Id"
,
PARKING_ZONE_ID
);
return
request
;
}
private
BaseRestResponse
getCameraList
(
RequestContext
requestContext
)
{
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
...
...
@@ -222,7 +177,7 @@ public class GEManager {
logger
.
info
(
"getCamerasList() activated."
);
// execute the request ...
req
=
buildCameraList
Http
Request
(
requestContext
);
req
=
buildCameraListRequest
(
requestContext
);
try
{
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
...
...
@@ -248,7 +203,7 @@ public class GEManager {
logger
.
info
(
"getCameraDetails() activated. camId="
+
cameraId
);
// execute the request ...
req
=
buildCameraDetails
Http
Request
(
cameraId
);
req
=
buildCameraDetailsRequest
(
cameraId
);
try
{
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
...
...
@@ -268,39 +223,6 @@ public class GEManager {
return
brr
;
}
private
SimpleHttpRequest
buildCameraDetailsHttpRequest
(
String
camId
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
METADATA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
String
path
=
ASSETS_URL
+
"/"
+
camId
;
request
.
setPath
(
path
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
request
.
addHeader
(
"Predix-Zone-Id"
,
PARKING_ZONE_ID
);
return
request
;
}
private
BaseRestResponse
buildCameraDetailsResp
(
SimpleHttpResponse
resp
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
int
size
;
String
content
=
resp
.
getContent
();
ObjectMapper
mapper
=
new
ObjectMapper
();
try
{
JsonNode
contentObj
=
mapper
.
readTree
(
content
);
String
view
=
contentObj
.
at
(
"/properties/VIEW"
).
asText
();
ObjectNode
objectNode
=
mapper
.
createObjectNode
();
objectNode
.
put
(
"view"
,
view
);
brr
.
objectNode
=
objectNode
;
}
catch
(
IOException
e
)
{
return
new
BaseRestResponse
(
false
,
e
.
toString
());
}
return
brr
;
}
private
boolean
getNewAccessToken
()
{
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
...
...
@@ -328,6 +250,117 @@ public class GEManager {
}
private
BaseRestResponse
getSensorList
(
RequestContext
requestContext
)
{
SimpleHttpRequest
req
=
null
;
SimpleHttpResponse
httpResp
=
null
;
BaseRestResponse
brr
=
new
BaseRestResponse
(
false
,
null
);
logger
.
info
(
"getSensorList() activated."
);
// execute the request ...
req
=
buildSensorListRequest
(
requestContext
);
try
{
httpResp
=
httpClient
.
processRequest
(
req
);
if
(
httpResp
.
getStatusCode
()
==
200
)
brr
=
buildSensorListResp
(
httpResp
);
else
{
logHttpError
(
"getSensorList"
,
httpResp
);
return
errorHttpResponse
(
httpResp
);
}
}
catch
(
Exception
e
)
{
String
err
=
"getSensorList() failed with exception. e="
+
e
.
toString
();
logger
.
error
(
err
);
return
errorResponse
(
err
);
}
return
brr
;
}
/*** B U I L D E R S ***/
private
SimpleHttpRequest
buildLatestMediaFileUrlRequest
(
String
camId
,
String
mediaType
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
MEDIA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
String
path
=
"/v2/assets/"
+
camId
+
"/media/latest"
;
request
.
setQueryString
(
"mediaType="
+
mediaType
);
request
.
setPath
(
path
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
request
.
addHeader
(
"Predix-Zone-Id"
,
PS_ZONE_ID
);
return
request
;
}
private
BaseRestResponse
buildLatestMediaFileResp
(
String
fileData
,
String
url
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
try
{
ObjectNode
dataObj
=
objMapper
.
createObjectNode
();
// parse file url
// String[0]: cameraId
// String[1]: timestamp
// String[2]: media type (IMAGE/VIDEO)
// String[3]: fileName
String
[]
urlParts
=
getMediaFileName
(
url
,
"latest"
);
if
(
urlParts
==
null
)
{
String
err
=
"buildLatestMediaFileResp() - failed to parse file url: "
+
url
;
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
dataObj
.
put
(
"cameraId"
,
urlParts
[
0
]);
dataObj
.
put
(
"timestamp"
,
Long
.
valueOf
(
urlParts
[
1
]));
dataObj
.
put
(
"mediaType"
,
urlParts
[
2
]);
dataObj
.
put
(
"fileName"
,
urlParts
[
3
]);
dataObj
.
put
(
"fileData"
,
fileData
);
brr
.
objectNode
=
dataObj
;
}
catch
(
Exception
e
)
{
String
err
=
"buildLatestMediaFileResp() failed with exception. e: "
+
e
.
toString
();
logger
.
error
(
err
);
return
new
BaseRestResponse
(
false
,
err
);
}
return
brr
;
}
private
SimpleHttpRequest
buildDownloadLatestMediaFileRequest
(
String
url
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
String
[]
domainFileUrl
=
extractDomainFile
(
url
);
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
buildCameraListRequest
(
RequestContext
requestContext
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
METADATA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
request
.
setPath
(
ASSET_LIST_URL
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
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"
).
peek
()
+
"&page=0&size=500&q=assetType:CAMERA"
;
request
.
setQueryString
(
bbox
);
}
return
request
;
}
private
BaseRestResponse
buildCameraListResp
(
SimpleHttpResponse
resp
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
int
size
;
...
...
@@ -360,45 +393,54 @@ public class GEManager {
return
brr
;
}
private
SimpleHttpRequest
build
DownloadLatestMediaFileRequest
(
String
url
)
{
private
SimpleHttpRequest
build
CameraDetailsRequest
(
String
camId
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
String
[]
domainFileUrl
=
extractDomainFile
(
url
);
if
(
domainFileUrl
!=
null
)
{
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
domainFileUrl
[
0
]
);
request
.
setDomain
(
METADATA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
String
path
=
"/v2/file/"
+
domainFileUrl
[
1
]
;
String
path
=
ASSETS_URL
+
"/"
+
camId
;
request
.
setPath
(
path
);
request
.
addHeader
(
"Authorization"
,
"Bearer "
+
TOKEN
);
request
.
addHeader
(
"Predix-Zone-Id"
,
PS_ZONE_ID
);
}
request
.
addHeader
(
"Predix-Zone-Id"
,
PARKING_ZONE_ID
);
return
request
;
}
private
SimpleHttpRequest
buildCameraHttpRequest
(
RequestContext
requestContext
,
String
[]
apiIdAsParams
)
{
switch
(
apiIdAsParams
[
2
])
{
case
"latestImage"
:
private
BaseRestResponse
buildCameraDetailsResp
(
SimpleHttpResponse
resp
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
int
size
;
String
content
=
resp
.
getContent
();
ObjectMapper
mapper
=
new
ObjectMapper
();
try
{
JsonNode
contentObj
=
mapper
.
readTree
(
content
);
String
view
=
contentObj
.
at
(
"/properties/VIEW"
).
asText
();
ObjectNode
objectNode
=
mapper
.
createObjectNode
();
objectNode
.
put
(
"view"
,
view
);
brr
.
objectNode
=
objectNode
;
}
catch
(
IOException
e
)
{
return
new
BaseRestResponse
(
false
,
e
.
toString
());
}
return
brr
;
}
private
SimpleHttpRequest
buildGetTokenRequest
()
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
MEDIA
_URL
);
request
.
setDomain
(
AUTH
_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
);
request
.
setPath
(
AUTH_PATH
);
request
.
setQueryString
(
AUTH_PARAMS
);
request
.
addHeader
(
"Authorization"
,
AUTH
);
return
request
;
default
:
}
return
null
;
}
private
SimpleHttpRequest
build
CameraListHttp
Request
(
RequestContext
requestContext
)
{
private
SimpleHttpRequest
build
SensorList
Request
(
RequestContext
requestContext
)
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
METADATA_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
...
...
@@ -407,23 +449,48 @@ 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"
).
peek
()
+
"&page=0&size=500&q=assetType:
CAMERA
"
;
String
bbox
=
"bbox="
+
queryParams
.
get
(
"bbox"
).
peek
()
+
"&page=0&size=500&q=assetType:
ENV_SENSOR
"
;
request
.
setQueryString
(
bbox
);
}
return
request
;
}
private
SimpleHttpRequest
buildGetTokenRequest
()
{
SimpleHttpRequest
request
=
new
SimpleHttpRequest
();
request
.
setProtocol
(
"https"
);
request
.
setDomain
(
AUTH_URL
);
request
.
setPort
(
443
);
request
.
setMethod
(
SimpleHttpRequest
.
Method
.
GET
);
request
.
setPath
(
AUTH_PATH
);
request
.
setQueryString
(
AUTH_PARAMS
);
request
.
addHeader
(
"Authorization"
,
AUTH
);
return
request
;
private
BaseRestResponse
buildSensorListResp
(
SimpleHttpResponse
resp
)
{
BaseRestResponse
brr
=
new
BaseRestResponse
(
true
,
null
);
int
size
;
String
content
=
resp
.
getContent
();
ObjectMapper
mapper
=
new
ObjectMapper
();
try
{
JsonNode
contentObj
=
mapper
.
readTree
(
content
);
size
=
contentObj
.
get
(
"totalElements"
).
asInt
();
JsonNode
contentArray
=
contentObj
.
get
(
"content"
);
ArrayNode
sensorArray
=
mapper
.
createArrayNode
();
if
(
contentArray
.
isArray
())
{
for
(
int
i
=
0
;
i
<
contentArray
.
size
();
++
i
)
{
ObjectNode
objSensor
=
mapper
.
createObjectNode
();
JsonNode
envSensor
=
contentArray
.
get
(
i
);
objSensor
.
put
(
"id"
,
envSensor
.
get
(
"assetUid"
));
objSensor
.
put
(
"type"
,
envSensor
.
get
(
"assetType"
));
String
[]
latLon
=
envSensor
.
get
(
"coordinates"
).
asText
().
split
(
":"
);
double
lat
=
Double
.
parseDouble
(
latLon
[
0
]);
double
lon
=
Double
.
parseDouble
(
latLon
[
1
]);
objSensor
.
put
(
"lat"
,
lat
);
objSensor
.
put
(
"lon"
,
lon
);
objSensor
.
put
(
"eventTypes"
,
envSensor
.
get
(
"eventTypes"
));
sensorArray
.
add
(
objSensor
);
}
brr
.
objectNode
=
sensorArray
;
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
brr
;
}
/*** H E L P E R S ***/
private
String
[]
extractDomainFile
(
String
link
)
{
String
[]
array
=
null
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment