Skip to content

Commit 9ee3b91

Browse files
committed
MLE-24405 Making ClientCookie a real public class
This was already in the public API via the Transaction interface, but it was in the "impl" package. I also can't determine any reason why it was reconstructing an OkHttp Cookie and then holding onto it. The Cookie class is immutable - but ClientCookie was already being given the immutable values that it needed. So no idea why ClientCookie was then rebuilding an OkHttp Cookie. It no longer does that - it's just a simple bean now, holding onto the values of interest that were extracted from an OkHttp Cookie.
1 parent cc05b57 commit 9ee3b91

File tree

6 files changed

+109
-126
lines changed

6 files changed

+109
-126
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
/**
9+
* ClientCookie is a wrapper around the Cookie implementation so that the
10+
* underlying implementation can be changed.
11+
*
12+
*/
13+
public class ClientCookie {
14+
15+
private final String name;
16+
private final String value;
17+
private final long expiresAt;
18+
private final String domain;
19+
private final String path;
20+
private final boolean secure;
21+
22+
public ClientCookie(String name, String value, long expiresAt, String domain, String path, boolean secure) {
23+
this.name = name;
24+
this.value = value;
25+
this.expiresAt = expiresAt;
26+
this.domain = domain;
27+
this.path = path;
28+
this.secure = secure;
29+
}
30+
31+
public boolean isSecure() {
32+
return secure;
33+
}
34+
35+
public String getPath() {
36+
return path;
37+
}
38+
39+
public String getDomain() {
40+
return domain;
41+
}
42+
43+
public long expiresAt() {
44+
return expiresAt;
45+
}
46+
47+
public String getName() {
48+
return name;
49+
}
50+
51+
public int getMaxAge() {
52+
return (int) TimeUnit.MILLISECONDS.toSeconds(expiresAt - System.currentTimeMillis());
53+
}
54+
55+
public String getValue() {
56+
return value;
57+
}
58+
}

marklogic-client-api/src/main/java/com/marklogic/client/Transaction.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
*/
44
package com.marklogic.client;
55

6-
import com.marklogic.client.impl.ClientCookie;
76
import com.marklogic.client.io.marker.StructureReadHandle;
87

98
import java.util.List;

marklogic-client-api/src/main/java/com/marklogic/client/impl/ClientCookie.java

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

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public OkHttpServices(ConnectionConfig connectionConfig) {
131131
this.okHttpClient = connect(connectionConfig);
132132
}
133133

134+
private static ClientCookie parseClientCookie(HttpUrl url, String setCookieHeaderValue) {
135+
Cookie cookie = Cookie.parse(url, setCookieHeaderValue);
136+
if(cookie == null) throw new IllegalStateException(setCookieHeaderValue + " is not a well-formed cookie");
137+
return new ClientCookie(cookie.name(), cookie.value(), cookie.expiresAt(), cookie.domain(), cookie.path(),
138+
cookie.secure());
139+
}
140+
134141
private FailedRequest extractErrorFields(Response response) {
135142
if (response == null) return null;
136143
try {
@@ -1501,7 +1508,7 @@ public Response apply(Request.Builder funcBuilder) {
15011508
String location = response.headers().get("Location");
15021509
List<ClientCookie> cookies = new ArrayList<>();
15031510
for (String setCookie : response.headers(HEADER_SET_COOKIE)) {
1504-
ClientCookie cookie = ClientCookie.parse(requestBldr.build().url(), setCookie);
1511+
ClientCookie cookie = parseClientCookie(requestBldr.build().url(), setCookie);
15051512
cookies.add(cookie);
15061513
}
15071514
closeResponse(response);
@@ -5599,7 +5606,7 @@ private void executeRequest(CallResponseImpl responseImpl) {
55995606
if (session != null) {
56005607
List<ClientCookie> cookies = new ArrayList<>();
56015608
for (String setCookie : response.headers(HEADER_SET_COOKIE)) {
5602-
ClientCookie cookie = ClientCookie.parse(requestBldr.build().url(), setCookie);
5609+
ClientCookie cookie = parseClientCookie(requestBldr.build().url(), setCookie);
56035610
cookies.add(cookie);
56045611
}
56055612
((SessionStateImpl) session).setCookies(cookies);

marklogic-client-api/src/main/java/com/marklogic/client/impl/SessionStateImpl.java

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package com.marklogic.client.impl;
55

6+
import com.marklogic.client.ClientCookie;
67
import com.marklogic.client.SessionState;
78

89
import java.util.ArrayList;
@@ -12,43 +13,42 @@
1213
import java.util.concurrent.atomic.AtomicBoolean;
1314

1415
public class SessionStateImpl implements SessionState {
15-
private List<ClientCookie> cookies;
16-
private String sessionId;
17-
private AtomicBoolean setCreatedTimestamp;
18-
private Calendar created;
19-
20-
public SessionStateImpl() {
21-
sessionId = Long.toUnsignedString(ThreadLocalRandom.current().nextLong(), 16);
22-
cookies = new ArrayList<>();
23-
setCreatedTimestamp = new AtomicBoolean(false);
24-
}
25-
26-
@Override
27-
public String getSessionId() {
28-
return sessionId;
29-
}
30-
31-
List<ClientCookie> getCookies() {
32-
return cookies;
33-
}
34-
35-
void setCookies(List<ClientCookie> cookies) {
36-
if ( cookies != null ) {
37-
if(setCreatedTimestamp.compareAndSet(false, true)) {
38-
for (ClientCookie cookie : cookies) {
39-
// Drop the SessionId cookie received from the server. We add it every
40-
// time we make a request with a SessionState object passed
41-
if(cookie.getName().equalsIgnoreCase("SessionId")) continue;
42-
// make a clone to ensure we're not holding on to any resources
43-
// related to an HTTP connection that need to be released
44-
this.cookies.add(new ClientCookie(cookie));
45-
}
46-
created = Calendar.getInstance();
47-
}
48-
}
49-
}
50-
51-
Calendar getCreatedTimestamp() {
52-
return created;
53-
}
16+
17+
private List<ClientCookie> cookies;
18+
private String sessionId;
19+
private AtomicBoolean setCreatedTimestamp;
20+
private Calendar created;
21+
22+
public SessionStateImpl() {
23+
sessionId = Long.toUnsignedString(ThreadLocalRandom.current().nextLong(), 16);
24+
cookies = new ArrayList<>();
25+
setCreatedTimestamp = new AtomicBoolean(false);
26+
}
27+
28+
@Override
29+
public String getSessionId() {
30+
return sessionId;
31+
}
32+
33+
List<ClientCookie> getCookies() {
34+
return cookies;
35+
}
36+
37+
void setCookies(List<ClientCookie> cookies) {
38+
if (cookies != null) {
39+
if (setCreatedTimestamp.compareAndSet(false, true)) {
40+
for (ClientCookie cookie : cookies) {
41+
// Drop the SessionId cookie received from the server. We add it every
42+
// time we make a request with a SessionState object passed
43+
if (cookie.getName().equalsIgnoreCase("SessionId")) continue;
44+
this.cookies.add(cookie);
45+
}
46+
created = Calendar.getInstance();
47+
}
48+
}
49+
}
50+
51+
Calendar getCreatedTimestamp() {
52+
return created;
53+
}
5454
}

marklogic-client-api/src/main/java/com/marklogic/client/impl/TransactionImpl.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
package com.marklogic.client.impl;
55

6-
import com.marklogic.client.impl.ClientCookie;
6+
import com.marklogic.client.ClientCookie;
77
import com.marklogic.client.FailedRequestException;
88
import com.marklogic.client.ForbiddenUserException;
99
import com.marklogic.client.Transaction;
@@ -19,7 +19,7 @@ class TransactionImpl implements Transaction {
1919
private RESTServices services;
2020
private String transactionId;
2121
private String hostId;
22-
// we keep cookies scoped with each tranasaction to work with load balancers
22+
// we keep cookies scoped with each transaction to work with load balancers
2323
// that need to keep requests for one transaction on a specific MarkLogic Server host
2424
private List<ClientCookie> cookies = new ArrayList<>();
2525
private Calendar created;
@@ -29,9 +29,7 @@ class TransactionImpl implements Transaction {
2929
this.transactionId = transactionId;
3030
if ( cookies != null ) {
3131
for (ClientCookie cookie : cookies) {
32-
// make a clone to ensure we're not holding on to any resources
33-
// related to an HTTP connection that need to be released
34-
this.cookies.add(new ClientCookie(cookie));
32+
this.cookies.add(cookie);
3533
if ( "HostId".equalsIgnoreCase(cookie.getName()) ) {
3634
hostId = cookie.getValue();
3735
}
@@ -44,9 +42,6 @@ class TransactionImpl implements Transaction {
4442
public String getTransactionId() {
4543
return transactionId;
4644
}
47-
public void setTransactionId(String transactionId) {
48-
this.transactionId = transactionId;
49-
}
5045

5146
@Override
5247
public List<ClientCookie> getCookies() {
@@ -57,9 +52,6 @@ public List<ClientCookie> getCookies() {
5752
public String getHostId() {
5853
return hostId;
5954
}
60-
protected void setHostId(String hostId) {
61-
this.hostId = hostId;
62-
}
6355

6456
public Calendar getCreatedTimestamp() {
6557
return created;

0 commit comments

Comments
 (0)