Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
佳 邓
Gitlab4j Api
Commits
f05942ff
Commit
f05942ff
authored
8 years ago
by
Greg Messner
Browse files
Options
Download
Email Patches
Plain Diff
Now handles all date strings received from GitLab.
parent
05678e63
main
5.0.x
5.0.x.jdk17
6.x
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/main/java/com/messners/gitlab/api/ISO8601.java
+18
-27
src/main/java/com/messners/gitlab/api/ISO8601.java
src/main/java/com/messners/gitlab/api/JacksonJson.java
+31
-1
src/main/java/com/messners/gitlab/api/JacksonJson.java
with
49 additions
and
28 deletions
+49
-28
src/main/java/com/messners/gitlab/api/ISO8601.java
+
18
-
27
View file @
f05942ff
...
@@ -6,6 +6,8 @@ import java.util.Calendar;
...
@@ -6,6 +6,8 @@ import java.util.Calendar;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.TimeZone
;
import
java.util.TimeZone
;
import
javax.xml.bind.DatatypeConverter
;
/**
/**
* This class provides utility methods for parsing and formatting ISO8601 formatted dates.
* This class provides utility methods for parsing and formatting ISO8601 formatted dates.
*/
*/
...
@@ -14,29 +16,29 @@ public class ISO8601 {
...
@@ -14,29 +16,29 @@ public class ISO8601 {
public
static
final
String
PATTERN_MSEC
=
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
;
public
static
final
String
PATTERN_MSEC
=
"yyyy-MM-dd'T'HH:mm:ss.SSSZ"
;
public
static
final
String
OUTPUT_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss'Z'"
;
public
static
final
String
OUTPUT_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss'Z'"
;
public
static
final
String
OUTPUT_MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
;
public
static
final
String
OUTPUT_MSEC_PATTERN
=
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
;
public
static
final
String
ALTERNATE
_PATTERN
=
"yyyy-MM-dd HH:mm:ss"
;
public
static
final
String
UTC
_PATTERN
=
"yyyy-MM-dd HH:mm:ss
'UTC'
"
;
private
static
final
SimpleDateFormat
iso8601Format
;
private
static
final
SimpleDateFormat
iso8601Format
;
private
static
final
SimpleDateFormat
iso8601MsecFormat
;
private
static
final
SimpleDateFormat
iso8601MsecFormat
;
private
static
final
SimpleDateFormat
iso8601OutputFormat
;
private
static
final
SimpleDateFormat
iso8601OutputFormat
;
private
static
final
SimpleDateFormat
iso8601OutputMsecFormat
;
private
static
final
SimpleDateFormat
iso8601OutputMsecFormat
;
private
static
final
SimpleDateFormat
iso8601
Alternate
Format
;
private
static
final
SimpleDateFormat
iso8601
Utc
Format
;
static
{
static
{
iso8601Format
=
new
SimpleDateFormat
(
PATTERN
);
iso8601Format
=
new
SimpleDateFormat
(
PATTERN
);
iso8601Format
.
setLenient
(
true
);
iso8601Format
.
setLenient
(
true
);
iso8601Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601MsecFormat
=
new
SimpleDateFormat
(
PATTERN_MSEC
);
iso8601MsecFormat
=
new
SimpleDateFormat
(
PATTERN_MSEC
);
iso8601MsecFormat
.
setLenient
(
true
);
iso8601MsecFormat
.
setLenient
(
true
);
iso8601MsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601MsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601OutputFormat
=
new
SimpleDateFormat
(
OUTPUT_PATTERN
);
iso8601OutputFormat
=
new
SimpleDateFormat
(
OUTPUT_PATTERN
);
iso8601OutputFormat
.
setLenient
(
true
);
iso8601OutputFormat
.
setLenient
(
true
);
iso8601OutputFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601OutputFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601OutputMsecFormat
=
new
SimpleDateFormat
(
OUTPUT_MSEC_PATTERN
);
iso8601OutputMsecFormat
=
new
SimpleDateFormat
(
OUTPUT_MSEC_PATTERN
);
iso8601OutputMsecFormat
.
setLenient
(
true
);
iso8601OutputMsecFormat
.
setLenient
(
true
);
iso8601OutputMsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601OutputMsecFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
iso8601
Alternate
Format
=
new
SimpleDateFormat
(
ALTERNATE
_PATTERN
);
iso8601
Utc
Format
=
new
SimpleDateFormat
(
UTC
_PATTERN
);
iso8601
Alternate
Format
.
setLenient
(
true
);
iso8601
Utc
Format
.
setLenient
(
true
);
iso8601
Alternate
Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
GMT
"
));
iso8601
Utc
Format
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"
UTC
"
));
}
}
/**
/**
...
@@ -47,7 +49,7 @@ public class ISO8601 {
...
@@ -47,7 +49,7 @@ public class ISO8601 {
public
static
String
getTimestamp
()
{
public
static
String
getTimestamp
()
{
return
(
iso8601Format
.
format
(
new
Date
()));
return
(
iso8601Format
.
format
(
new
Date
()));
}
}
/**
/**
* Get a ISO8601formatted string for the current date and time.
* Get a ISO8601formatted string for the current date and time.
*
*
...
@@ -85,9 +87,7 @@ public class ISO8601 {
...
@@ -85,9 +87,7 @@ public class ISO8601 {
}
}
long
time
=
date
.
getTime
();
long
time
=
date
.
getTime
();
return
(
time
%
1000
!=
0
?
return
(
time
%
1000
!=
0
?
iso8601OutputMsecFormat
.
format
(
date
)
:
iso8601OutputFormat
.
format
(
date
));
iso8601OutputMsecFormat
.
format
(
date
)
:
iso8601OutputFormat
.
format
(
date
));
}
}
/**
/**
...
@@ -104,22 +104,13 @@ public class ISO8601 {
...
@@ -104,22 +104,13 @@ public class ISO8601 {
}
}
dateTimeString
=
dateTimeString
.
trim
();
dateTimeString
=
dateTimeString
.
trim
();
SimpleDateFormat
fmt
;
if
(
dateTimeString
.
endsWith
(
"UTC"
))
{
if
(
dateTimeString
.
length
()
>
10
)
{
synchronized
(
iso8601UtcFormat
)
{
if
(
dateTimeString
.
charAt
(
10
)
==
'T'
)
{
return
(
iso8601UtcFormat
.
parse
(
dateTimeString
));
fmt
=
(
dateTimeString
.
endsWith
(
"Z"
)
?
(
dateTimeString
.
charAt
(
dateTimeString
.
length
()
-
4
)
==
'.'
?
iso8601OutputMsecFormat
:
iso8601OutputFormat
)
:
iso8601Format
);
}
else
{
fmt
=
iso8601AlternateFormat
;
}
}
}
else
{
}
else
{
fmt
=
iso8601Format
;
Calendar
cal
=
DatatypeConverter
.
parseDateTime
(
dateTimeString
);
}
return
(
cal
.
getTime
());
synchronized
(
fmt
)
{
return
(
fmt
.
parse
(
dateTimeString
));
}
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/main/java/com/messners/gitlab/api/JacksonJson.java
+
31
-
1
View file @
f05942ff
...
@@ -3,7 +3,10 @@ package com.messners.gitlab.api;
...
@@ -3,7 +3,10 @@ package com.messners.gitlab.api;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.Reader
;
import
java.io.Reader
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.Date
;
import
java.util.Date
;
import
java.util.TimeZone
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.Produces
;
import
javax.ws.rs.core.MediaType
;
import
javax.ws.rs.core.MediaType
;
...
@@ -14,8 +17,11 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
...
@@ -14,8 +17,11 @@ import com.fasterxml.jackson.annotation.JsonInclude.Include;
import
com.fasterxml.jackson.core.JsonGenerationException
;
import
com.fasterxml.jackson.core.JsonGenerationException
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.core.JsonGenerator
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonParseException
;
import
com.fasterxml.jackson.core.JsonParser
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.DeserializationContext
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.DeserializationFeature
;
import
com.fasterxml.jackson.databind.JsonDeserializer
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.JsonMappingException
;
import
com.fasterxml.jackson.databind.JsonSerializer
;
import
com.fasterxml.jackson.databind.JsonSerializer
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
...
@@ -34,6 +40,13 @@ import com.messners.gitlab.api.models.AccessLevel;
...
@@ -34,6 +40,13 @@ import com.messners.gitlab.api.models.AccessLevel;
@Produces
(
MediaType
.
APPLICATION_JSON
)
@Produces
(
MediaType
.
APPLICATION_JSON
)
public
class
JacksonJson
extends
JacksonJaxbJsonProvider
implements
ContextResolver
<
ObjectMapper
>
{
public
class
JacksonJson
extends
JacksonJaxbJsonProvider
implements
ContextResolver
<
ObjectMapper
>
{
private
static
final
SimpleDateFormat
iso8601UtcFormat
;
static
{
iso8601UtcFormat
=
new
SimpleDateFormat
(
ISO8601
.
UTC_PATTERN
);
iso8601UtcFormat
.
setLenient
(
true
);
iso8601UtcFormat
.
setTimeZone
(
TimeZone
.
getTimeZone
(
"UTC"
));
}
private
final
ObjectMapper
objectMapper
;
private
final
ObjectMapper
objectMapper
;
public
JacksonJson
()
{
public
JacksonJson
()
{
...
@@ -50,6 +63,7 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
...
@@ -50,6 +63,7 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
SimpleModule
module
=
new
SimpleModule
(
"GitLabApiJsonModule"
);
SimpleModule
module
=
new
SimpleModule
(
"GitLabApiJsonModule"
);
module
.
addSerializer
(
Date
.
class
,
new
JsonDateSerializer
());
module
.
addSerializer
(
Date
.
class
,
new
JsonDateSerializer
());
module
.
addDeserializer
(
Date
.
class
,
new
JsonDateDeserializer
());
module
.
addSerializer
(
AccessLevel
.
class
,
new
JsonAccessLevelSerializer
());
module
.
addSerializer
(
AccessLevel
.
class
,
new
JsonAccessLevelSerializer
());
objectMapper
.
registerModule
(
module
);
objectMapper
.
registerModule
(
module
);
}
}
...
@@ -137,7 +151,23 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
...
@@ -137,7 +151,23 @@ public class JacksonJson extends JacksonJaxbJsonProvider implements ContextResol
gen
.
writeString
(
iso8601String
);
gen
.
writeString
(
iso8601String
);
}
}
}
}
/**
* JsonDeserializer for deserializing ISO8601 formatted dates.
*/
public
static
class
JsonDateDeserializer
extends
JsonDeserializer
<
Date
>
{
@Override
public
Date
deserialize
(
JsonParser
jsonparser
,
DeserializationContext
context
)
throws
IOException
,
JsonProcessingException
{
try
{
return
(
ISO8601
.
toDate
(
jsonparser
.
getText
()));
}
catch
(
ParseException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
/**
/**
* JsonSerializer for serializing AccessLevel values.
* JsonSerializer for serializing AccessLevel values.
*/
*/
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new 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
Menu
Explore
Projects
Groups
Snippets