Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
J
Java Client API
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
3
Issues
3
List
Boards
Labels
Service Desk
Milestones
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Genesys PGR
Java Client API
Commits
b6ef8c98
Commit
b6ef8c98
authored
Jul 14, 2014
by
Matija Obreza
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extracted CLI from GenesysClient
parent
1e19babb
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
455 additions
and
311 deletions
+455
-311
client.properties
client.properties
+9
-0
src/main/java/org/genesys2/client/oauth/CLI.java
src/main/java/org/genesys2/client/oauth/CLI.java
+322
-0
src/main/java/org/genesys2/client/oauth/GenesysApiException.java
...n/java/org/genesys2/client/oauth/GenesysApiException.java
+29
-0
src/main/java/org/genesys2/client/oauth/GenesysClient.java
src/main/java/org/genesys2/client/oauth/GenesysClient.java
+68
-310
src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java
...g/genesys2/client/oauth/OAuthAuthenticationException.java
+1
-1
src/main/resources/log4j.properties
src/main/resources/log4j.properties
+26
-0
No files found.
client.properties
0 → 100644
View file @
b6ef8c98
#OAuth client properties
#Mon Jul 14 14:57:45 CEST 2014
client.callback
=
oob
base.url
=
http
\:
//localhost
\:
8080
access.token
=
4690da94-d387-4b93-8377-699ac0ca8639
client.key
=
CropHub
client.secret
=
0xcafebabe
refresh.token
=
d5294375-3ac7-4134-9f28-98068f02d926
api.url
=
/api/v0
src/main/java/org/genesys2/client/oauth/CLI.java
0 → 100644
View file @
b6ef8c98
package
org.genesys2.client.oauth
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.net.SocketException
;
import
java.util.Properties
;
import
java.util.Scanner
;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.log4j.LogManager
;
import
org.apache.log4j.Logger
;
import
org.scribe.exceptions.OAuthConnectionException
;
import
org.scribe.model.Token
;
import
org.scribe.model.Verb
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.node.ObjectNode
;
/**
* Command line interface to Genesys
*
* @author matijaobreza
*
*/
public
class
CLI
{
private
static
final
Logger
_log
=
LogManager
.
getLogger
(
CLI
.
class
);
private
File
propertiesFile
;
private
Properties
properties
;
private
Scanner
in
=
new
Scanner
(
System
.
in
);
private
static
ObjectMapper
mapper
=
new
ObjectMapper
();
private
GenesysClient
genesysClient
=
new
GenesysClient
();
public
static
void
main
(
String
[]
args
)
{
_log
.
info
(
"Hello World!"
);
CLI
cli
=
new
CLI
();
cli
.
loadProperties
(
"client.properties"
);
cli
.
run
();
}
private
void
run
()
{
checkPreconditions
();
try
{
System
.
out
.
println
(
"/me: "
+
genesysClient
.
query
(
"/me"
));
}
catch
(
OAuthAuthenticationException
e
)
{
_log
.
error
(
"Failed to fetch /me"
,
e
);
authenticate
();
}
catch
(
OAuthConnectionException
e
)
{
if
(
e
.
getCause
()
!=
null
&&
e
.
getCause
()
instanceof
SocketException
)
{
_log
.
error
(
"Could not connect to host"
);
return
;
}
else
{
_log
.
error
(
e
.
getMessage
(),
e
);
}
}
catch
(
Throwable
e
)
{
_log
.
error
(
e
.
getMessage
(),
e
);
return
;
}
try
{
doWork
();
}
catch
(
Throwable
e
)
{
_log
.
error
(
e
.
getMessage
(),
e
);
}
}
private
void
doWork
()
throws
GenesysApiException
{
String
line
=
null
;
do
{
System
.
out
.
println
(
"1 Datasets"
);
System
.
out
.
println
(
"2 Traits"
);
System
.
out
.
println
(
"0 Custom"
);
System
.
out
.
println
(
"Q QUIT"
);
line
=
in
.
nextLine
();
if
(
"1"
.
equals
(
line
))
doDatasets
();
else
if
(
"2"
.
equals
(
line
))
doTraits
();
else
if
(
"0"
.
equals
(
line
))
doCustom
();
}
while
(!
"Q"
.
equalsIgnoreCase
(
line
));
}
private
void
doCustom
()
throws
GenesysApiException
{
System
.
out
.
print
(
"URL: "
);
String
url
=
in
.
nextLine
();
System
.
out
.
print
(
"Method [GET]: "
);
String
method
=
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
"GET"
);
System
.
out
.
println
(
"JSON: "
);
String
data
=
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
null
);
// Exec
System
.
out
.
println
(
genesysClient
.
query
(
Verb
.
valueOf
(
method
),
url
,
null
,
data
));
}
private
void
doTraits
()
throws
GenesysApiException
{
String
line
=
null
;
do
{
System
.
out
.
println
(
"1 List parameters"
);
System
.
out
.
println
(
"2 List my methods"
);
System
.
out
.
println
(
"9 List all methods"
);
System
.
out
.
println
(
"3 Add method"
);
System
.
out
.
println
(
"0 Back"
);
line
=
in
.
nextLine
();
if
(
"1"
.
equals
(
line
))
System
.
out
.
println
(
"/parameters: "
+
genesysClient
.
query
(
"/descriptors"
));
else
if
(
"2"
.
equals
(
line
))
System
.
out
.
println
(
"/mymethods: "
+
genesysClient
.
query
(
"/mymethods"
));
else
if
(
"3"
.
equals
(
line
))
addMethod
();
else
if
(
"9"
.
equals
(
line
))
System
.
out
.
println
(
"/methods: "
+
genesysClient
.
query
(
"/methods"
));
else
if
(
"0"
.
equalsIgnoreCase
(
line
))
return
;
}
while
(!(
"0"
.
equalsIgnoreCase
(
line
)));
}
private
void
doDatasets
()
throws
GenesysApiException
{
String
line
=
null
;
do
{
System
.
out
.
println
(
"1 List"
);
System
.
out
.
println
(
"2 Add"
);
System
.
out
.
println
(
"3 Add data"
);
System
.
out
.
println
(
"4 Add RAW"
);
System
.
out
.
println
(
"0 Back"
);
line
=
in
.
nextLine
();
if
(
"1"
.
equals
(
line
))
System
.
out
.
println
(
"/datasets: "
+
genesysClient
.
query
(
"/datasets"
));
else
if
(
"2"
.
equals
(
line
))
addDataset
();
else
if
(
"3"
.
equals
(
line
))
addDatasetData
();
else
if
(
"4"
.
equals
(
line
))
addDatasetRaw
();
else
if
(
"0"
.
equalsIgnoreCase
(
line
))
return
;
}
while
(!(
"0"
.
equalsIgnoreCase
(
line
)));
}
private
void
addMethod
()
throws
GenesysApiException
{
ObjectNode
datasetJson
=
mapper
.
createObjectNode
();
System
.
out
.
println
(
"Method description: "
);
datasetJson
.
put
(
"description"
,
in
.
nextLine
());
System
.
out
.
println
(
"UOM: "
);
datasetJson
.
put
(
"unit"
,
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
null
));
System
.
out
.
println
(
"Field name: "
);
datasetJson
.
put
(
"fieldName"
,
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
null
));
System
.
out
.
println
(
"Field type: (0=String, 1=Double, 2=Long)"
);
int
fieldType
=
Integer
.
parseInt
(
in
.
nextLine
());
datasetJson
.
put
(
"fieldType"
,
fieldType
);
if
(
fieldType
==
0
)
{
System
.
out
.
println
(
"Field size: "
);
datasetJson
.
put
(
"fieldSize"
,
Integer
.
parseInt
(
in
.
nextLine
()));
}
System
.
out
.
println
(
"Options: "
);
datasetJson
.
put
(
"options"
,
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
null
));
System
.
out
.
println
(
"Range: "
);
datasetJson
.
put
(
"range"
,
StringUtils
.
defaultIfBlank
(
in
.
nextLine
(),
null
));
System
.
err
.
println
(
datasetJson
.
toString
());
// make a dataset
System
.
out
.
println
(
"Put method: "
+
genesysClient
.
query
(
Verb
.
PUT
,
"/methods"
,
null
,
datasetJson
.
toString
()));
}
private
void
addDataset
()
throws
GenesysApiException
{
ObjectNode
datasetJson
=
mapper
.
createObjectNode
();
System
.
out
.
println
(
"WIEWS Code: "
);
datasetJson
.
put
(
"institute"
,
in
.
nextLine
());
System
.
out
.
println
(
"Dataset title: "
);
datasetJson
.
put
(
"title"
,
in
.
nextLine
());
System
.
out
.
println
(
"Dataset description: "
);
datasetJson
.
put
(
"description"
,
in
.
nextLine
());
System
.
err
.
println
(
datasetJson
.
toString
());
// make a dataset
System
.
out
.
println
(
"Put dataset: "
+
genesysClient
.
query
(
Verb
.
PUT
,
"/datasets"
,
null
,
datasetJson
.
toString
()));
}
private
void
addDatasetData
()
throws
GenesysApiException
{
ObjectNode
datasetJson
=
mapper
.
createObjectNode
();
System
.
out
.
println
(
"Dataset ID: "
);
long
datasetId
=
Long
.
parseLong
(
in
.
nextLine
());
System
.
out
.
println
(
"WIEWS Code: "
);
datasetJson
.
put
(
"instCode"
,
in
.
nextLine
());
System
.
out
.
println
(
"Data data: "
);
datasetJson
.
put
(
"data"
,
in
.
nextLine
());
System
.
err
.
println
(
datasetJson
.
toString
());
// make a dataset
System
.
out
.
println
(
"Put dataset: "
+
genesysClient
.
query
(
Verb
.
PUT
,
"/datasets/"
+
datasetId
+
"/data"
,
null
,
datasetJson
.
toString
()));
}
private
void
addDatasetRaw
()
throws
GenesysApiException
{
System
.
out
.
println
(
"Dataset ID: "
);
long
datasetId
=
Long
.
parseLong
(
in
.
nextLine
());
System
.
out
.
println
(
"JSON: "
);
String
json
=
in
.
nextLine
();
System
.
err
.
println
(
json
);
// make a dataset
System
.
out
.
println
(
"Put dataset: "
+
genesysClient
.
query
(
Verb
.
PUT
,
"/datasets/"
+
datasetId
+
"/data"
,
null
,
json
));
}
private
void
checkPreconditions
()
{
boolean
restart
=
false
;
if
(
StringUtils
.
isBlank
(
properties
.
getProperty
(
"client.key"
)))
{
System
.
out
.
print
(
"Provide client key: "
);
properties
.
put
(
"client.key"
,
in
.
nextLine
());
restart
=
true
;
}
if
(
StringUtils
.
isBlank
(
properties
.
getProperty
(
"client.secret"
)))
{
System
.
out
.
print
(
"Provide client secret: "
);
properties
.
put
(
"client.secret"
,
in
.
nextLine
());
restart
=
true
;
}
if
(
StringUtils
.
isBlank
(
properties
.
getProperty
(
"api.url"
)))
{
System
.
out
.
print
(
"Provide API url: "
);
properties
.
put
(
"api.url"
,
in
.
nextLine
());
restart
=
true
;
}
if
(
restart
)
{
saveProperties
();
_log
.
warn
(
"Properties udpated, please restart CLI application"
);
System
.
exit
(-
1
);
}
}
private
void
authenticate
()
{
String
authorizationUrl
=
genesysClient
.
getAuthorizationUrl
(
GenesysClient
.
EMPTY_TOKEN
);
System
.
out
.
println
(
"Authorization URL: \n"
+
authorizationUrl
);
System
.
out
.
print
(
"\nVerifier: "
);
String
verifierCode
=
in
.
nextLine
();
System
.
out
.
println
();
// Trade the Request Token and Verifier for the Access Token
genesysClient
.
authenticate
(
verifierCode
);
Token
accessToken
=
genesysClient
.
getAccessToken
();
this
.
properties
.
put
(
"access.token"
,
accessToken
.
getToken
());
// Get refresh token ()
Token
refreshToken
=
genesysClient
.
getRefreshToken
();
if
(
refreshToken
!=
null
)
{
this
.
properties
.
put
(
"refresh.token"
,
refreshToken
.
getToken
());
}
else
{
this
.
properties
.
remove
(
"refresh.token"
);
}
saveProperties
();
}
private
void
saveProperties
()
{
FileOutputStream
fis
=
null
;
try
{
fis
=
new
FileOutputStream
(
propertiesFile
);
this
.
properties
.
store
(
fis
,
"OAuth client properties"
);
}
catch
(
IOException
e
)
{
_log
.
error
(
e
);
}
finally
{
IOUtils
.
closeQuietly
(
fis
);
}
}
private
void
loadProperties
(
String
propertiesFileName
)
{
// .properties file location
propertiesFile
=
new
File
(
propertiesFileName
);
Properties
properties
=
new
Properties
();
FileInputStream
fis
=
null
;
try
{
_log
.
info
(
"Loading "
+
propertiesFile
.
getAbsolutePath
());
fis
=
new
FileInputStream
(
propertiesFile
);
properties
.
load
(
fis
);
// Keep properties
this
.
properties
=
properties
;
}
catch
(
FileNotFoundException
e
)
{
_log
.
warn
(
e
,
e
);
}
catch
(
IOException
e
)
{
_log
.
error
(
e
,
e
);
}
finally
{
IOUtils
.
closeQuietly
(
fis
);
}
genesysClient
.
loadProperties
(
properties
);
}
}
src/main/java/org/genesys2/client/oauth/GenesysApiException.java
0 → 100644
View file @
b6ef8c98
/**
* Copyright 2013 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package
org.genesys2.client.oauth
;
public
class
GenesysApiException
extends
Exception
{
private
static
final
long
serialVersionUID
=
-
6008425446284829953L
;
public
GenesysApiException
()
{
}
public
GenesysApiException
(
String
string
)
{
super
(
string
);
}
}
src/main/java/org/genesys2/client/oauth/GenesysClient.java
View file @
b6ef8c98
This diff is collapsed.
Click to expand it.
src/main/java/org/genesys2/client/oauth/OAuthAuthenticationException.java
View file @
b6ef8c98
...
...
@@ -16,7 +16,7 @@
package
org.genesys2.client.oauth
;
public
class
OAuthAuthenticationException
extends
Exception
{
public
class
OAuthAuthenticationException
extends
GenesysApi
Exception
{
private
static
final
long
serialVersionUID
=
-
6008425446284829953L
;
public
OAuthAuthenticationException
()
{
...
...
src/main/resources/log4j.properties
0 → 100644
View file @
b6ef8c98
#-------------------------------------------------------------------------------
# Copyright 2013 Global Crop Diversity Trust
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-------------------------------------------------------------------------------
### direct log messages to stdout ###
log4j.appender.stdout
=
org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target
=
System.out
log4j.appender.stdout.encoding
=
UTF-8
log4j.appender.stdout.layout
=
org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern
=
%d{ABSOLUTE} %t %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger
=
info, stdout
log4j.category.org.genesys2
=
debug
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