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
2
Issues
2
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
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
21cde930
Commit
21cde930
authored
Oct 20, 2017
by
Matija Obreza
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch '5-logging' into 'master'
Logging Closes
#5
See merge request
!5
parents
e568b535
4aeb3013
Pipeline
#3005
passed with stage
in 21 seconds
Changes
3
Pipelines
2
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
52 additions
and
51 deletions
+52
-51
pom.xml
pom.xml
+6
-5
src/main/java/org/genesys2/client/oauth/CLI.java
src/main/java/org/genesys2/client/oauth/CLI.java
+14
-14
src/main/java/org/genesys2/client/oauth/GenesysClient.java
src/main/java/org/genesys2/client/oauth/GenesysClient.java
+32
-32
No files found.
pom.xml
View file @
21cde930
...
...
@@ -40,6 +40,7 @@
<maven-gpg-plugin.version>
1.5
</maven-gpg-plugin.version>
<nexus-staging-maven-plugin.version>
1.6.8
</nexus-staging-maven-plugin.version>
<slf4j.version>
1.7.21
</slf4j.version>
<show.deprecations>
true
</show.deprecations>
</properties>
...
...
@@ -92,11 +93,11 @@
<artifactId>
scribejava-core
</artifactId>
<version>
4.2.0
</version>
</dependency>
<dependency>
<groupId>
log
4j
</groupId>
<artifactId>
log
4j
</artifactId>
<version>
1.2.17
</version>
</dependency>
<dependency>
<groupId>
org.slf
4j
</groupId>
<artifactId>
jcl-over-slf
4j
</artifactId>
<version>
${slf4j.version}
</version>
</dependency>
<dependency>
<groupId>
commons-lang
</groupId>
<artifactId>
commons-lang
</artifactId>
...
...
src/main/java/org/genesys2/client/oauth/CLI.java
View file @
21cde930
...
...
@@ -41,8 +41,8 @@ import com.github.scribejava.core.model.Verb;
import
org.apache.commons.io.IOUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.
apache.log4j.LogMana
ger
;
import
org.
apache.log4j.Logger
;
import
org.
slf4j.Log
ger
;
import
org.
slf4j.LoggerFactory
;
/**
* Simple command line interface to Genesys.
...
...
@@ -51,8 +51,8 @@ import org.apache.log4j.Logger;
*/
public
class
CLI
{
/** The Constant
_log
. */
private
static
final
Logger
_log
=
LogManager
.
getLogger
(
CLI
.
class
);
/** The Constant
LOG
. */
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
CLI
.
class
);
/** The properties file. */
private
File
propertiesFile
;
...
...
@@ -88,7 +88,7 @@ public class CLI {
* @param args the arguments
*/
public
static
void
main
(
final
String
[]
args
)
{
_log
.
info
(
"Hello World!"
);
LOG
.
info
(
"Hello World!"
);
final
CLI
cli
=
new
CLI
();
cli
.
genesysClient
=
GenesysClient
.
build
(
cli
.
loadProperties
(
"client.properties"
));
...
...
@@ -108,22 +108,22 @@ public class CLI {
saveProperties
();
}
catch
(
final
OAuthAuthenticationException
e
)
{
_log
.
error
(
"Failed to fetch /me"
,
e
);
LOG
.
error
(
"Failed to fetch /me"
,
e
);
try
{
authenticate
();
}
catch
(
OAuthAuthenticationException
e1
)
{
_log
.
error
(
"Failed to authenticate"
,
e1
);
LOG
.
error
(
"Failed to authenticate"
,
e1
);
return
;
}
}
catch
(
final
Throwable
e
)
{
_log
.
error
(
e
.
getMessage
(),
e
);
LOG
.
error
(
e
.
getMessage
(),
e
);
return
;
}
try
{
doWork
();
}
catch
(
final
Throwable
e
)
{
_log
.
error
(
e
.
getMessage
(),
e
);
LOG
.
error
(
e
.
getMessage
(),
e
);
}
}
...
...
@@ -555,7 +555,7 @@ public class CLI {
if
(
restart
)
{
saveProperties
();
_log
.
warn
(
"Properties udpated, please restart CLI application"
);
LOG
.
warn
(
"Properties udpated, please restart CLI application"
);
System
.
exit
(-
1
);
}
}
...
...
@@ -592,7 +592,7 @@ public class CLI {
fis
=
new
FileOutputStream
(
propertiesFile
);
properties
.
store
(
fis
,
"OAuth client properties"
);
}
catch
(
final
IOException
e
)
{
_log
.
error
(
e
);
LOG
.
error
(
e
.
getMessage
(),
e
);
}
finally
{
IOUtils
.
closeQuietly
(
fis
);
}
...
...
@@ -612,15 +612,15 @@ public class CLI {
FileInputStream
fis
=
null
;
try
{
_log
.
info
(
"Loading "
+
propertiesFile
.
getAbsolutePath
());
LOG
.
info
(
"Loading {}"
,
propertiesFile
.
getAbsolutePath
());
fis
=
new
FileInputStream
(
propertiesFile
);
properties
.
load
(
fis
);
// Keep properties
this
.
properties
=
properties
;
}
catch
(
final
FileNotFoundException
e
)
{
_log
.
warn
(
e
,
e
);
LOG
.
warn
(
e
.
getMessage
()
,
e
);
}
catch
(
final
IOException
e
)
{
_log
.
error
(
e
,
e
);
LOG
.
error
(
e
.
getMessage
()
,
e
);
}
finally
{
IOUtils
.
closeQuietly
(
fis
);
}
...
...
src/main/java/org/genesys2/client/oauth/GenesysClient.java
View file @
21cde930
...
...
@@ -50,19 +50,19 @@ import com.github.scribejava.core.oauth.OAuth20Service;
import
org.apache.commons.io.FileUtils
;
import
org.apache.commons.lang.StringUtils
;
import
org.apache.log4j.LogManager
;
import
org.apache.log4j.Logger
;
import
org.genesys2.client.oauth.api.GenesysApi
;
import
org.genesys2.client.oauth.api.accession.AccessionJson
;
import
org.genesys2.client.oauth.api.images.RepositoryImage
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* Genesys API client using Scribe.
*/
public
class
GenesysClient
{
/** The Constant
_log
. */
private
static
final
Logger
_log
=
LogManager
.
getLogger
(
GenesysClient
.
class
);
/** The Constant
LOG
. */
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
GenesysClient
.
class
);
/** The mapper. */
private
static
ObjectMapper
objectMapper
;
...
...
@@ -215,8 +215,8 @@ public class GenesysClient {
}
}
if
(
_log
.
isDebugEnabled
())
{
_log
.
debug
(
method
+
" "
+
request
.
getCompleteUrl
());
if
(
LOG
.
isDebugEnabled
())
{
LOG
.
debug
(
"{} {}"
,
method
,
request
.
getCompleteUrl
());
}
if
(
StringUtils
.
isNotBlank
(
contentType
))
{
...
...
@@ -225,8 +225,8 @@ public class GenesysClient {
if
(
postBody
!=
null
)
{
// System.err.println("Adding data: " + data);
if
(
_log
.
isTraceEnabled
())
{
_log
.
trace
(
"Body: "
+
new
String
(
postBody
));
if
(
LOG
.
isTraceEnabled
())
{
LOG
.
trace
(
"Body: {}"
,
new
String
(
postBody
));
}
request
.
setPayload
(
postBody
);
}
...
...
@@ -240,18 +240,18 @@ public class GenesysClient {
final
String
responseBody
=
response
.
getBody
();
_log
.
debug
(
"HTTP status code "
+
response
.
getCode
());
LOG
.
debug
(
"HTTP status code {}"
,
response
.
getCode
());
if
(
response
.
getCode
()
>=
200
&&
response
.
getCode
()
<
300
)
{
_log
.
debug
(
"Returning response body"
);
LOG
.
debug
(
"Returning response body"
);
return
responseBody
;
}
else
{
if
(
response
.
getCode
()
==
301
||
response
.
getCode
()
==
302
)
{
_log
.
debug
(
"Redirect: "
+
response
.
getHeader
(
HttpConstants
.
LOCATION
));
LOG
.
debug
(
"Redirect: {}"
,
response
.
getHeader
(
HttpConstants
.
LOCATION
));
throw
new
HttpRedirectException
(
response
.
getHeader
(
HttpConstants
.
LOCATION
));
}
if
(
response
.
getCode
()
==
401
)
{
_log
.
warn
(
"Response error: "
+
response
.
getCode
());
LOG
.
warn
(
"Response error: {}"
,
response
.
getCode
());
System
.
err
.
println
(
responseBody
);
if
(
i
==
0
)
{
refreshAccessToken
();
...
...
@@ -259,10 +259,10 @@ public class GenesysClient {
throw
new
OAuthAuthenticationException
(
"Unauthorized"
);
}
}
else
{
_log
.
error
(
method
+
" "
+
request
.
getCompleteUrl
());
_log
.
error
(
new
String
(
postBody
));
_log
.
error
(
"HTTP response code: "
+
response
.
getCode
());
_log
.
error
(
"Response: "
+
responseBody
);
LOG
.
error
(
"{} {}"
,
method
,
request
.
getCompleteUrl
());
LOG
.
error
(
new
String
(
postBody
));
LOG
.
error
(
"HTTP response code: {}"
,
response
.
getCode
());
LOG
.
error
(
"Response: {}"
,
responseBody
);
if
(
responseBody
.
contains
(
"Deadlock found when trying to get lock; try restarting transaction"
)
||
responseBody
.
contains
(
"nested exception is org.hibernate.exception.LockAcquisitionException: could not execute statement"
)
||
responseBody
.
contains
(
"nested exception is org.hibernate.exception.LockTimeoutException: could not execute statement"
))
{
...
...
@@ -298,14 +298,14 @@ public class GenesysClient {
*/
public
void
refreshAccessToken
()
throws
GenesysApiException
{
if
(
tokens
.
hasRefreshToken
())
{
_log
.
info
(
"Using Refresh Token to get new access token"
);
LOG
.
info
(
"Using Refresh Token to get new access token"
);
try
{
OAuth2AccessToken
accessToken
=
service
.
refreshAccessToken
(
tokens
.
getRefreshToken
());
tokens
.
setAccessToken
(
accessToken
.
getAccessToken
());
_log
.
info
(
"Got new Access Token!"
);
LOG
.
info
(
"Got new Access Token!"
);
}
catch
(
Throwable
e
)
{
_log
.
info
(
"Refresh token didn't work: "
+
e
.
getMessage
());
LOG
.
info
(
"Refresh token didn't work: {}"
,
e
.
getMessage
());
throw
new
OAuthAuthenticationException
(
"Refresh token not valid, please re-authenticate"
);
}
}
else
{
...
...
@@ -370,7 +370,7 @@ public class GenesysClient {
return
null
;
}
_log
.
debug
(
"Sending: "
+
accns
);
LOG
.
debug
(
"Sending: {}"
,
accns
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/update"
,
null
,
objectMapper
.
writeValueAsString
(
accns
));
}
...
...
@@ -394,7 +394,7 @@ public class GenesysClient {
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/upsert"
,
null
,
jsonAccessionList
);
}
catch
(
final
PleaseRetryException
e
)
{
final
long
sleepTime
=
(
long
)
(
Math
.
pow
(
2
,
retry
)
*
100
+
Math
.
pow
(
2
,
retry
)
*
2500
*
Math
.
random
());
_log
.
warn
(
"Retrying PUT after "
+
sleepTime
+
" ms."
);
LOG
.
warn
(
"Retrying PUT after {} ms."
,
sleepTime
);
Thread
.
sleep
(
sleepTime
);
}
}
...
...
@@ -424,10 +424,10 @@ public class GenesysClient {
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/upsert"
,
null
,
data
);
}
catch
(
final
PleaseRetryException
e
)
{
final
long
sleepTime
=
(
long
)
(
Math
.
pow
(
2
,
retry
)
*
100
+
Math
.
pow
(
2
,
retry
)
*
2500
*
Math
.
random
());
_log
.
warn
(
"Retrying PUT after "
+
sleepTime
+
" ms."
);
LOG
.
warn
(
"Retrying PUT after {} ms."
,
sleepTime
);
Thread
.
sleep
(
sleepTime
);
}
catch
(
final
GenesysApiException
e
)
{
_log
.
error
(
"Failed to upload data: "
+
data
);
LOG
.
error
(
"Failed to upload data: {}"
,
data
);
throw
e
;
}
}
...
...
@@ -443,11 +443,11 @@ public class GenesysClient {
* @throws GenesysApiException the genesys api exception
*/
public
String
updateOrganizationMembers
(
final
String
organizationSlug
,
final
ArrayNode
institutes
)
throws
GenesysApiException
{
_log
.
debug
(
"Sending: "
+
institutes
);
LOG
.
debug
(
"Sending: {}"
,
institutes
);
try
{
return
query
(
Verb
.
PUT
,
"/org/"
+
organizationSlug
+
"/set-institutes"
,
null
,
institutes
.
toString
());
}
catch
(
final
PleaseRetryException
e
)
{
_log
.
warn
(
"Retrying PUT after some time..."
);
LOG
.
warn
(
"Retrying PUT after some time..."
);
try
{
Thread
.
sleep
((
long
)
(
1000
*
Math
.
random
()));
}
catch
(
final
InterruptedException
e1
)
{
...
...
@@ -466,17 +466,17 @@ public class GenesysClient {
* @throws GenesysApiException the genesys api exception
*/
public
String
updateAccessionNames
(
final
String
instCode
,
final
Collection
<
ObjectNode
>
batch
)
throws
GenesysApiException
{
_log
.
debug
(
"Sending: "
+
batch
);
LOG
.
debug
(
"Sending: {}"
,
batch
);
try
{
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/names"
,
null
,
batch
.
toString
());
}
catch
(
final
PleaseRetryException
e
)
{
_log
.
warn
(
"Retrying PUT after some time..."
);
LOG
.
warn
(
"Retrying PUT after some time..."
);
try
{
Thread
.
sleep
((
long
)
(
1000
*
Math
.
random
()));
}
catch
(
final
InterruptedException
e1
)
{
e1
.
printStackTrace
();
}
_log
.
warn
(
"Retrying PUT"
);
LOG
.
warn
(
"Retrying PUT"
);
return
query
(
Verb
.
PUT
,
"/acn/"
+
instCode
+
"/names"
,
null
,
batch
.
toString
());
}
}
...
...
@@ -550,16 +550,16 @@ public class GenesysClient {
try
{
OAuth2AccessToken
accessToken
=
service
.
getAccessToken
(
verifierCode
);
_log
.
info
(
"ACCESS TOKEN: "
+
accessToken
.
getAccessToken
()
+
" scope="
+
accessToken
.
getScope
()
+
" raw="
+
accessToken
.
getRawResponse
());
LOG
.
info
(
"ACCESS TOKEN: {} scope={} raw={}"
,
accessToken
.
getAccessToken
(),
accessToken
.
getScope
(),
accessToken
.
getRawResponse
());
final
String
refreshToken
=
accessToken
.
getRefreshToken
();
_log
.
info
(
"REFRESH TOKEN: "
+
refreshToken
);
LOG
.
info
(
"REFRESH TOKEN: {}"
,
refreshToken
);
tokens
.
setAccessToken
(
accessToken
.
getAccessToken
());
tokens
.
setRefreshToken
(
refreshToken
);
}
catch
(
IOException
|
InterruptedException
|
ExecutionException
e
)
{
_log
.
error
(
"Auth error"
,
e
);
LOG
.
error
(
"Auth error"
,
e
);
throw
new
OAuthAuthenticationException
(
e
.
getMessage
());
}
}
...
...
@@ -1046,7 +1046,7 @@ public class GenesysClient {
throw
new
GenesysApiException
(
"Content-Type must be provided for the file "
+
file
.
getAbsolutePath
());
}
_log
.
debug
(
"Image content type: "
+
contentType
);
LOG
.
debug
(
"Image content type: {}"
,
contentType
);
// PUT file on server
final
String
json
=
query
(
Verb
.
PUT
,
String
.
format
(
"/img/%1$s/acn/%2$s/"
,
instCode
,
acceNumb
),
Collections
.
singletonMap
(
"originalFilename"
,
file
.
getName
()),
contentType
,
...
...
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