Skip to content

Commit 035842a

Browse files
author
test
committed
ok
1 parent a80aa5d commit 035842a

File tree

4 files changed

+123
-54
lines changed

4 files changed

+123
-54
lines changed

service/src/main/java/com/appirio/service/member/util/MyDateTypeAdapter.java

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,74 @@
33
import com.google.gson.TypeAdapter;
44
import com.google.gson.stream.JsonReader;
55
import com.google.gson.stream.JsonWriter;
6+
7+
import java.util.Calendar;
68
import java.util.Date;
9+
import java.util.TimeZone;
710
import java.io.IOException;
811

12+
/**
13+
* Represents the MyDateTypeAdapter model
14+
*
15+
* Version 1.1 - Topcoder Member Service - Fix date parsing issue version 1.0
16+
* - fix the read method to handle the epoch time and milliseconds time from elasticsearch
17+
*
18+
*
19+
* @author TCCoder
20+
* @version 1.1
21+
*
22+
*/
923
public class MyDateTypeAdapter extends TypeAdapter<Date> {
24+
/**
25+
* The MINI_YEAR field
26+
*/
27+
private static final long MINI_YEAR = 1900;
28+
29+
/**
30+
* The MAX_YEAR field
31+
*/
32+
private static final long MAX_YEAR = 2100;
33+
34+
/**
35+
* Write the date
36+
*
37+
* @param out the writer out to use
38+
* @param value the date value
39+
* @throws IOException if any error occurs
40+
*/
1041
@Override
1142
public void write(JsonWriter out, Date value) throws IOException {
12-
if (value == null)
43+
if (value == null) {
1344
out.nullValue();
14-
else
45+
}
46+
else {
1547
out.value(value.getTime() / 1000);
48+
}
1649
}
1750

51+
/**
52+
* Read the date time
53+
*
54+
* @param in the reader in to use
55+
* @throws IOException if any error occurs
56+
* @return the Date result
57+
*/
1858
@Override
1959
public Date read(JsonReader in) throws IOException {
20-
if (in != null)
21-
return new Date(in.nextLong() * 1000);
22-
else
60+
if (in != null) {
61+
long time = in.nextLong();
62+
Calendar calendar = Calendar.getInstance();
63+
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
64+
calendar.setTimeInMillis(time * 1000);
65+
int year = calendar.get(Calendar.YEAR);
66+
// if the multiply the time by 1000, the year is still between 1900 and 2100, it should be a epoch time
67+
if (year > MINI_YEAR && year < MAX_YEAR) {
68+
return calendar.getTime();
69+
} else {
70+
return new Date(time);
71+
}
72+
} else {
2373
return null;
74+
}
2475
}
2576
}

service/src/main/java/com/appirio/service/member/util/TimestampDeserializer.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,63 @@
33
import java.io.IOException;
44
import java.util.Date;
55
import java.util.TimeZone;
6-
import java.text.SimpleDateFormat;
76
import java.text.ParseException;
87

98
import com.fasterxml.jackson.core.JsonParser;
109
import com.fasterxml.jackson.core.JsonProcessingException;
1110
import com.fasterxml.jackson.databind.DeserializationContext;
1211
import com.fasterxml.jackson.databind.JsonDeserializer;
1312

13+
import org.apache.commons.lang3.time.FastDateFormat;
1414
import org.slf4j.Logger;
1515
import org.slf4j.LoggerFactory;
1616

17+
/**
18+
* TimestampDeserializer is used to deerialize the date
19+
*
20+
* Changes in the version 1.1 (Topcoder Member Service - Fix date parsing issue v1.0)
21+
* - SimpleDateFormat is not thread safe, use FastDateFormat
22+
*
23+
* @author TCCoder
24+
* @version 1.1
25+
*
26+
*/
1727
public class TimestampDeserializer extends JsonDeserializer<Date> {
1828

1929
/**
2030
* Logger for the class
2131
*/
2232
private Logger logger = LoggerFactory.getLogger(TimestampDeserializer.class);
23-
24-
private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
2533

34+
/**
35+
* The formatter field
36+
*/
37+
private FastDateFormat formatter = FastDateFormat.getInstance(TimestampSerializer.DATE_FORMAT, TimeZone.getTimeZone("UTC"));
38+
39+
/**
40+
* Deserialize the date
41+
*
42+
* @param jsonParser the jsonParser to use
43+
* @param ctxt the ctxt to use
44+
* @throws IOException if any error occurs
45+
* @throws JsonProcessingException if any error occurs
46+
* @return the Date result
47+
*/
2648
@Override
2749
public Date deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
2850
String value = jsonParser.getText();
51+
2952
try {
3053
Long longTime = Long.parseLong(value);
3154
return new Date(longTime);
3255
} catch (NumberFormatException nfe ) {
3356
try {
3457
return formatter.parse(value);
3558
} catch (ParseException e) {
36-
logger.info("Parse Exception");
37-
logger.info(e.getMessage());
59+
logger.error("Date parsing error", e);
3860
throw new RuntimeException(e);
3961
} catch (Exception e) {
40-
logger.info("General exception");
62+
logger.error("Failed to parse the date", e);
4163
throw new RuntimeException(e);
4264
}
4365
}
Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,61 @@
11
package com.appirio.service.member.util;
22

33
import java.io.IOException;
4+
import java.text.SimpleDateFormat;
45
import java.util.Date;
56
import java.util.TimeZone;
6-
import java.text.SimpleDateFormat;
7-
import java.text.ParseException;
8-
9-
import com.fasterxml.jackson.core.JsonParser;
107
import com.fasterxml.jackson.core.JsonProcessingException;
11-
import com.fasterxml.jackson.databind.DeserializationContext;
128
import com.fasterxml.jackson.databind.JsonSerializer;
139
import com.fasterxml.jackson.core.JsonGenerator;
1410
import com.fasterxml.jackson.databind.SerializerProvider;
1511

12+
import org.apache.commons.lang3.time.FastDateFormat;
1613
import org.slf4j.Logger;
1714
import org.slf4j.LoggerFactory;
1815

16+
/**
17+
* TimestampSerializer is used to serialize the date
18+
*
19+
* Changes in the version 1.1 (Topcoder Member Service - Fix date parsing issue v1.0)
20+
* - serialize all the date in the unified format where ever TimestampSerializer is applied.
21+
*
22+
* @author TCCoder
23+
* @version 1.1
24+
*
25+
*/
1926
public class TimestampSerializer extends JsonSerializer<Date> {
27+
/**
28+
* The DATE_FORMAT field
29+
*/
30+
public static final String DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
2031

2132
/**
2233
* Logger for the class
2334
*/
2435
private Logger logger = LoggerFactory.getLogger(TimestampSerializer.class);
25-
26-
private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
27-
private SimpleDateFormat validate = new SimpleDateFormat("yyyy");
2836

37+
/**
38+
* The formatter field
39+
*/
40+
private FastDateFormat formatter = FastDateFormat.getInstance(DATE_FORMAT, TimeZone.getTimeZone("UTC"));
41+
42+
/**
43+
* Serialize the date value
44+
*
45+
* @param value the date value
46+
* @param gen the gen to use
47+
* @param serializers the serializers to use
48+
* @throws IOException if any error occurs
49+
* @throws JsonProcessingException if any error occurs
50+
*/
2951
@Override
3052
public void serialize(Date value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException {
3153
try {
32-
String dateString = "";
33-
if(gen.getOutputContext().getCurrentName() == "birthDate" || gen.getOutputContext().getCurrentName() == "timePeriodFrom" || gen.getOutputContext().getCurrentName() == "timePeriodTo") {
34-
if(isValidDate(value)) {
35-
dateString = formatter.format(value);
36-
} else {
37-
Date myDate = formatter.parse(formatter.format(value));
38-
Date currentTime = new Date((myDate.getTime()/1000));
39-
dateString = formatter.format(currentTime);
40-
}
41-
} else {
42-
if(isValidDate(value)) {
43-
dateString = String.valueOf(value.getTime() * 1000);
44-
} else {
45-
Date myDate = formatter.parse(formatter.format(value));
46-
Date currentTime = new Date((myDate.getTime()/1000));
47-
dateString = String.valueOf(value.getTime() * 1000);
48-
}
49-
}
54+
String dateString = formatter.format(value);
5055
gen.writeString(dateString);
51-
}
52-
catch (Exception e) {
53-
logger.info(e.getMessage());
56+
} catch (Exception e) {
57+
logger.error("Failed to format the date", e);
5458
throw new RuntimeException(e);
5559
}
5660
}
57-
58-
private Boolean isValidDate(Date value) {
59-
if((validate.format(value)).length() > 4) {
60-
return false;
61-
} else {
62-
return true;
63-
}
64-
}
6561
}

service/src/main/resources/member-service.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,23 +201,23 @@ emailVerificationConfig:
201201

202202
jestClientConfiguration:
203203
# the elastic search URL, current, use the local for test purpose
204-
elasticSearchUrl: "${ELASTIC_SEARCH_URL:http://cockpit.cloud.topcoder.com:9200}"
204+
elasticSearchUrl: http://cockpit.cloud.topcoder.com:9200
205205
# the index name for members
206-
membersIndexName: "${MEMBER_ES_INDEX:members}"
206+
membersIndexName: members
207207
# the type name for members
208-
membersProfileTypeName: "${MEMBER_ES_TYPE_NAME:profile}"
208+
membersProfileTypeName: profile
209209
# the type name for profile trait
210-
membersProfileTraitTypeName: "${MEMBER_TRAIT_ES_TYPE_NAME:profiletrait}"
210+
membersProfileTraitTypeName: profiletrait
211211
# the max total connections
212212
maxTotalConnections: 10
213213
# connection timeout in milli-seconds
214214
connTimeout: 60000
215215
# read timeout in milli-seconds
216216
readTimeout: 60000
217217
# AWS signing enabled or not
218-
awsSigningEnabled: "${AWS_SIGNING_ENABLED:false}"
218+
awsSigningEnabled: false
219219
# AWS region
220-
awsRegion: "${AWS_REGION:us-east-1}"
220+
awsRegion: us-east-1
221221
# AWS service
222222
awsService: es
223223

0 commit comments

Comments
 (0)