Skip to content

Commit c5a8bfd

Browse files
authored
Guestlinelabs - fix session date parse issue (#622)
* fix miliseconds in date long string * add testCookieParsedSuccessfullyWithMiliseconds test * fixed testCorruptedSessionRenewalTimeValueThrowsExceptionOnCookieParsing * remove unused sessionAcquisitionTime and sessionRenewalTime alltogether * use substring instead of split * Fix build break - missing curly braces * SessionCookie.java: remove unused constant + cleanup * Remove HttpFactory class as it's not used. * updated changelog
1 parent 696f247 commit c5a8bfd

File tree

5 files changed

+30
-352
lines changed

5 files changed

+30
-352
lines changed

CHANGELOG.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
# CHANGELOG
22

33
## Version 2.0.2
4-
- Fix #616, added a way to have real time SDK Logs when logging on File.
5-
- Fix #609, fixes the inaccurate timestamp recorded with JMX Metrics.
4+
- Fix [#577](https://github.com/Microsoft/ApplicationInsights-Java/issues/577), removed HttpFactory class as it was not being used.
5+
- Fixed issue with sessionId not being set in request telemetry due to date parsing issues.
6+
- Fix [#616](https://github.com/Microsoft/ApplicationInsights-Java/issues/616), added a way to have real time SDK Logs when logging on File.
7+
- Fix [#609](https://github.com/Microsoft/ApplicationInsights-Java/issues/609), fixes the inaccurate timestamp recorded with JMX Metrics.
68
- Added a way to configure MaxInstantRetries from XML.
79
- Added the ability to have cold SDK initialization (no logs except critical logAlways messages)
810
- Fix issue when dependency start time was not being recorded correctly.
9-
- Fixed #533 HTTP Dependency Telemetry now matches with .NET SDK.
11+
- Fixed [#533](https://github.com/Microsoft/ApplicationInsights-Java/issues/533) HTTP Dependency Telemetry now matches with .NET SDK.
1012
- Introduced public method `httpMethodFinishedWithPath(String identifier, String method, String path, String correlationId, String uri, String target, int result, long delta)`
1113
to support instrumentation of Path of URI in HTTP requests.
1214
- `httpMethodFinished(String identifier, String method, String correlationId, String uri, String target, int result, int delta)` is now marked as deprecated
1315
- Logger Messages now being pushed as custom dimension when reporting exceptions via Loggers. (#400)
1416
- Enhanced Log4j2 appender to support basic parameters including Filters, Layouts and includeException. (#348)
1517
- Fixed PageView telemetry data not being reported.
16-
- Fixed Issue #526 (NPE in MapUtil.copy())
17-
- Fixed Issue #513 (Memory leak in SDKShutdownActivity). This fix upgrades our Servlet version from 2.5 to 3.0. The SDK must now be run on an application server supporting Servlet 3.0.
18-
- Fixed Issue #504 (SDK initialization happens twice) to improve startup performance.
18+
- Fixed Issue [#526](https://github.com/Microsoft/ApplicationInsights-Java/issues/526) (NPE in MapUtil.copy())
19+
- Fixed Issue [#513](https://github.com/Microsoft/ApplicationInsights-Java/issues/513) (Memory leak in SDKShutdownActivity). This fix upgrades our Servlet version from 2.5 to 3.0. The SDK must now be run on an application server supporting Servlet 3.0.
20+
- Fixed Issue [#504](https://github.com/Microsoft/ApplicationInsights-Java/issues/504) (SDK initialization happens twice) to improve startup performance.
1921

2022
## Version 2.0.1
2123
- Fix Inconsistency in artifact names in POM files

web/src/main/java/com/microsoft/applicationinsights/web/internal/cookies/HttpCookieFactory.java

Lines changed: 0 additions & 95 deletions
This file was deleted.

web/src/main/java/com/microsoft/applicationinsights/web/internal/cookies/SessionCookie.java

Lines changed: 12 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -21,80 +21,32 @@
2121

2222
package com.microsoft.applicationinsights.web.internal.cookies;
2323

24-
import java.text.ParseException;
25-
import java.util.Calendar;
26-
import java.util.Date;
2724
import javax.servlet.http.Cookie;
28-
import com.microsoft.applicationinsights.internal.util.DateTimeUtils;
2925

3026
/**
3127
* Created by yonisha on 2/4/2015.
3228
*/
3329
public class SessionCookie extends com.microsoft.applicationinsights.web.internal.cookies.Cookie {
3430

35-
// region Consts
36-
37-
private static final int UPDATE_TIMEOUT_IN_MINUTES = 5;
3831
public static final String COOKIE_NAME = "ai_session";
39-
public static final int SESSION_DEFAULT_EXPIRATION_TIMEOUT_IN_MINUTES = 30;
40-
41-
// endregion Consts
42-
43-
// region Members
44-
4532
private String sessionId;
46-
private Date acquisitionDate;
47-
private Date renewalDate;
48-
49-
private enum CookieFields {
50-
SESSION_ID(0),
51-
SESSION_ACQUISITION_DATE(1),
52-
SESSION_LAST_UPDATE_DATE(2);
53-
54-
private final int value;
55-
56-
CookieFields(int value) {
57-
this.value = value;
58-
}
59-
60-
public int getValue() { return value; }
61-
}
62-
63-
// endregion Members
64-
65-
// region Ctor
6633

6734
/**
6835
* Constructs new SessionCookie object from the given cookie.
6936
* @param cookie The http servlet cookie.
70-
* @throws Exception Thrown when the cookie information cannot be parsed.
7137
*/
72-
public SessionCookie(Cookie cookie) throws Exception {
73-
parseCookie(cookie);
38+
public SessionCookie(Cookie cookie) {
39+
this(parseCookie(cookie));
7440
}
7541

7642
/**
7743
* Constructs new SessionCookie with the given session ID.
7844
* @param sessionId The session ID.
7945
*/
8046
public SessionCookie(String sessionId) {
81-
String now = String.valueOf(System.currentTimeMillis());
82-
String[] cookieRawValues = new String[] { sessionId, now, now };
83-
String formattedCookie = SessionCookie.formatCookie(cookieRawValues);
84-
85-
Cookie cookie = new Cookie(COOKIE_NAME, formattedCookie);
86-
87-
try {
88-
parseCookie(cookie);
89-
} catch (Exception e) {
90-
// This exception is not expected in any case.
91-
}
47+
this.sessionId = sessionId;
9248
}
9349

94-
// endregion Ctor
95-
96-
// region Public
97-
9850
/**
9951
* Gets the session id.
10052
* @return The session id.
@@ -103,99 +55,18 @@ public String getSessionId() {
10355
return sessionId;
10456
}
10557

106-
/**
107-
* Gets the session acquisition date.
108-
* @return The session acquisition date.
109-
*/
110-
public Date getSessionAcquisitionDate() {
111-
return acquisitionDate;
112-
}
113-
114-
/**
115-
* Gets the session renewal date.
116-
* @return The session renewal date.
117-
*/
118-
public Date getSessionRenewalDate() {
119-
return renewalDate;
120-
}
121-
122-
/**
123-
* Determines if the session has expired.
124-
* @param sessionTimeoutInMinutes The session timeout in minutes.
125-
* @return True if the session has expired, false otherwise.
126-
*/
127-
public boolean isSessionExpired(int sessionTimeoutInMinutes) {
128-
Date expirationDate = DateTimeUtils.addToDate(
129-
this.getSessionRenewalDate(),
130-
Calendar.MINUTE,
131-
sessionTimeoutInMinutes);
132-
Date now = new Date();
133-
134-
return now.after(expirationDate);
135-
}
136-
137-
/**
138-
* Returns a value indicating whether the session cookie is up-to-date.
139-
* Session cookie is considered up-to-date when the last renewal time is less than
140-
* {@literal UPDATE_TIMEOUT_IN_MINUTES} minutes.
141-
* @return True if the session cookie up-to-date.
142-
*/
143-
public boolean isSessionCookieUpToDate() {
144-
Date expectedRenewalTime = DateTimeUtils.addToDate(
145-
this.getSessionRenewalDate(),
146-
Calendar.MINUTE,
147-
UPDATE_TIMEOUT_IN_MINUTES);
148-
Date now = new Date();
149-
150-
return now.before(expectedRenewalTime);
151-
}
152-
153-
// endregion Public
154-
155-
// region Private
156-
15758
/**
15859
* Parses the given cookie.
15960
* @param cookie The cookie contains the session information.
160-
* @throws Exception Thrown when the cookie information cannot be parsed.
61+
* @return sessionId
16162
*/
162-
private void parseCookie(Cookie cookie) throws Exception {
163-
String[] split = cookie.getValue().split(RAW_COOKIE_SPLIT_DELIMITER);
164-
165-
if (split.length < CookieFields.values().length) {
166-
167-
// TODO: dedicated exception
168-
String errorMessage = String.format("Session cookie is not in the correct format: %s", cookie.getValue());
169-
170-
throw new Exception(errorMessage);
171-
}
172-
173-
try {
174-
sessionId = split[CookieFields.SESSION_ID.getValue()];
175-
acquisitionDate = parseDateWithBackwardCompatibility(split[CookieFields.SESSION_ACQUISITION_DATE.getValue()]);
176-
renewalDate = parseDateWithBackwardCompatibility(split[CookieFields.SESSION_LAST_UPDATE_DATE.getValue()]);
177-
} catch (Exception e) {
178-
String errorMessage = String.format("Failed to parse session cookie with exception: %s", e.toString());
179-
180-
// TODO: dedicated exception
181-
throw new Exception(errorMessage);
63+
private static String parseCookie(Cookie cookie) {
64+
String value = cookie.getValue();
65+
int idx = value.indexOf(RAW_COOKIE_DELIMITER);
66+
if (idx >= 0) {
67+
return value.substring(0, idx);
68+
} else {
69+
return value;
18270
}
18371
}
184-
185-
/**
186-
* JavaScript SDK was changed to store dates as long, rather than a readable date string.
187-
* For backward compatibility, we first try to parse with the new format (time represented by long) and then backward
188-
* compatibility for time represented by a string.
189-
* @param dateStr The date to parse.
190-
* @return The parsed date.
191-
*/
192-
private Date parseDateWithBackwardCompatibility(String dateStr) throws ParseException {
193-
try {
194-
return new Date(Long.parseLong(dateStr));
195-
} catch (NumberFormatException e) {
196-
return DateTimeUtils.parseRoundTripDateString(dateStr);
197-
}
198-
}
199-
200-
// endregion Private
201-
}
72+
}

0 commit comments

Comments
 (0)