2424import static oracle .nosql .driver .util .HttpConstants .CONNECTION ;
2525import static oracle .nosql .driver .util .HttpConstants .CONTENT_LENGTH ;
2626import static oracle .nosql .driver .util .HttpConstants .CONTENT_TYPE ;
27+ import static oracle .nosql .driver .util .HttpConstants .COOKIE ;
2728import static oracle .nosql .driver .util .HttpConstants .NOSQL_DATA_PATH ;
2829import static oracle .nosql .driver .util .HttpConstants .REQUEST_ID_HEADER ;
2930import static oracle .nosql .driver .util .HttpConstants .USER_AGENT ;
@@ -172,6 +173,14 @@ public class Client {
172173 */
173174 private StatsControlImpl statsControl ;
174175
176+ /*
177+ * for session persistence, if used. This has the
178+ * full "session=xxxxx" key/value pair.
179+ */
180+ private volatile String sessionCookie ;
181+ /* note this must end with '=' */
182+ private final String SESSION_COOKIE_FIELD = "session=" ;
183+
175184 public Client (Logger logger ,
176185 NoSQLHandleConfig httpConfig ) {
177186
@@ -546,6 +555,9 @@ public Result execute(Request kvRequest) {
546555 headers .add (HttpHeaderNames .HOST , host )
547556 .add (REQUEST_ID_HEADER , requestId )
548557 .setInt (CONTENT_LENGTH , contentLength );
558+ if (sessionCookie != null ) {
559+ headers .add (COOKIE , sessionCookie );
560+ }
549561
550562 authProvider .setRequiredHeaders (authString , kvRequest , headers );
551563
@@ -569,6 +581,7 @@ public Result execute(Request kvRequest) {
569581
570582 ByteBuf wireContent = responseHandler .getContent ();
571583 Result res = processResponse (responseHandler .getStatus (),
584+ responseHandler .getHeaders (),
572585 wireContent ,
573586 kvRequest );
574587 int resSize = wireContent .readerIndex ();
@@ -955,6 +968,7 @@ private short writeContent(ByteBuf content, Request kvRequest)
955968 * @return the programmatic response object
956969 */
957970 final Result processResponse (HttpResponseStatus status ,
971+ HttpHeaders headers ,
958972 ByteBuf content ,
959973 Request kvRequest ) {
960974
@@ -966,6 +980,8 @@ final Result processResponse(HttpResponseStatus status,
966980 status );
967981 }
968982
983+ setSessionCookie (headers );
984+
969985 try (ByteInputStream bis = new NettyByteInputStream (content )) {
970986 return processOKResponse (bis , kvRequest );
971987 }
@@ -1037,6 +1053,38 @@ private void processNotOKResponse(HttpResponseStatus status,
10371053 ", reason = " + status .reasonPhrase ());
10381054 }
10391055
1056+ /* set session cookie, if set in response headers */
1057+ private void setSessionCookie (HttpHeaders headers ) {
1058+ if (headers == null ) {
1059+ return ;
1060+ }
1061+ /*
1062+ * NOTE: this code assumes there will always be at most
1063+ * one Set-Cookie header in the response. If the load balancer
1064+ * settings change, or the proxy changes to add Set-Cookie
1065+ * headers, this code may need to be changed to look for
1066+ * multiple Set-Cookie headers.
1067+ */
1068+ String v = headers .get ("Set-Cookie" );
1069+ /* note SESSION_COOKIE_FIELD has appended '=' */
1070+ if (v == null || v .startsWith (SESSION_COOKIE_FIELD ) == false ) {
1071+ return ;
1072+ }
1073+ int semi = v .indexOf (";" );
1074+ if (semi < 0 ) {
1075+ setSessionCookieValue (v );
1076+ } else {
1077+ setSessionCookieValue (v .substring (0 , semi ));
1078+ }
1079+ if (isLoggable (logger , Level .FINE )) {
1080+ logTrace (logger , "Set session cookie to \" " + sessionCookie + "\" " );
1081+ }
1082+ }
1083+
1084+ private synchronized void setSessionCookieValue (String pVal ) {
1085+ sessionCookie = pVal ;
1086+ }
1087+
10401088 /**
10411089 * Return true if table needs limits refresh.
10421090 */
0 commit comments