22
22
23
23
import java .io .IOException ;
24
24
import java .time .Instant ;
25
+ import java .util .HashMap ;
25
26
import java .util .LinkedList ;
26
27
import java .util .List ;
27
28
import java .util .Map ;
@@ -139,24 +140,24 @@ public void createConversation(
139
140
) {
140
141
initConversationMetaIndexIfAbsent (ActionListener .wrap (indexExists -> {
141
142
if (indexExists ) {
142
- String userstr = getUserStrFromThreadContext ();
143
+ String userStr = getUserStrFromThreadContext ();
143
144
Instant now = Instant .now ();
144
- IndexRequest request = Requests
145
- . indexRequest ( META_INDEX_NAME )
146
- . source (
147
- ConversationalIndexConstants . META_CREATED_TIME_FIELD ,
148
- now ,
149
- ConversationalIndexConstants . META_UPDATED_TIME_FIELD ,
150
- now ,
151
- ConversationalIndexConstants .META_NAME_FIELD ,
152
- name ,
153
- ConversationalIndexConstants . USER_FIELD ,
154
- userstr == null ? null : User . parse ( userstr ). getName (),
155
- ConversationalIndexConstants . APPLICATION_TYPE_FIELD ,
156
- applicationType ,
157
- ConversationalIndexConstants .META_ADDITIONAL_INFO_FIELD ,
158
- additionalInfos == null ? Map . of () : additionalInfos
159
- );
145
+ Map < String , Object > sourceMap = new HashMap <>();
146
+ sourceMap . put ( ConversationalIndexConstants . META_CREATED_TIME_FIELD , now );
147
+ sourceMap . put ( ConversationalIndexConstants . META_UPDATED_TIME_FIELD , now );
148
+ if ( name != null && ! name . trim (). isEmpty ()) {
149
+ sourceMap . put ( ConversationalIndexConstants . META_NAME_FIELD , name );
150
+ }
151
+ if ( userStr != null && ! userStr . trim (). isEmpty ()) {
152
+ sourceMap . put ( ConversationalIndexConstants .USER_FIELD , User . parse ( userStr ). getName ());
153
+ }
154
+ if ( applicationType != null && ! applicationType . trim (). isEmpty ()) {
155
+ sourceMap . put ( ConversationalIndexConstants . APPLICATION_TYPE_FIELD , applicationType );
156
+ }
157
+ if ( additionalInfos != null && ! additionalInfos . isEmpty ()) {
158
+ sourceMap . put ( ConversationalIndexConstants .META_ADDITIONAL_INFO_FIELD , additionalInfos );
159
+ }
160
+ IndexRequest request = Requests . indexRequest ( META_INDEX_NAME ). source ( sourceMap );
160
161
try (ThreadContext .StoredContext threadContext = client .threadPool ().getThreadContext ().stashContext ()) {
161
162
ActionListener <String > internalListener = ActionListener .runBefore (listener , () -> threadContext .restore ());
162
163
ActionListener <IndexResponse > al = ActionListener .wrap (resp -> {
@@ -210,12 +211,12 @@ public void getConversations(int from, int maxResults, ActionListener<List<Conve
210
211
return ;
211
212
}
212
213
SearchRequest request = Requests .searchRequest (META_INDEX_NAME );
213
- String userstr = getUserStrFromThreadContext ();
214
+ String userStr = getUserStrFromThreadContext ();
214
215
QueryBuilder queryBuilder ;
215
- if (userstr == null )
216
+ if (userStr == null )
216
217
queryBuilder = new MatchAllQueryBuilder ();
217
218
else
218
- queryBuilder = new TermQueryBuilder (ConversationalIndexConstants .USER_FIELD , User .parse (userstr ).getName ());
219
+ queryBuilder = new TermQueryBuilder (ConversationalIndexConstants .USER_FIELD , User .parse (userStr ).getName ());
219
220
request .source ().query (queryBuilder );
220
221
request .source ().from (from ).size (maxResults );
221
222
request .source ().sort (ConversationalIndexConstants .META_UPDATED_TIME_FIELD , SortOrder .DESC );
@@ -264,8 +265,8 @@ public void deleteConversation(String conversationId, ActionListener<Boolean> li
264
265
return ;
265
266
}
266
267
DeleteRequest delRequest = Requests .deleteRequest (META_INDEX_NAME ).id (conversationId );
267
- String userstr = getUserStrFromThreadContext ();
268
- String user = User .parse (userstr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userstr ).getName ();
268
+ String userStr = getUserStrFromThreadContext ();
269
+ String user = User .parse (userStr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userStr ).getName ();
269
270
this .checkAccess (conversationId , ActionListener .wrap (access -> {
270
271
if (access ) {
271
272
try (ThreadContext .StoredContext threadContext = client .threadPool ().getThreadContext ().stashContext ()) {
@@ -308,7 +309,7 @@ public void checkAccess(String conversationId, ActionListener<Boolean> listener)
308
309
listener .onResponse (true );
309
310
return ;
310
311
}
311
- String userstr = getUserStrFromThreadContext ();
312
+ String userStr = getUserStrFromThreadContext ();
312
313
try (ThreadContext .StoredContext threadContext = client .threadPool ().getThreadContext ().stashContext ()) {
313
314
ActionListener <Boolean > internalListener = ActionListener .runBefore (listener , () -> threadContext .restore ());
314
315
GetRequest getRequest = Requests .getRequest (META_INDEX_NAME ).id (conversationId );
@@ -318,12 +319,12 @@ public void checkAccess(String conversationId, ActionListener<Boolean> listener)
318
319
throw new ResourceNotFoundException ("Memory [" + conversationId + "] not found" );
319
320
}
320
321
// If security is off - User doesn't exist - you have permission
321
- if (userstr == null || User .parse (userstr ) == null ) {
322
+ if (userStr == null || User .parse (userStr ) == null ) {
322
323
internalListener .onResponse (true );
323
324
return ;
324
325
}
325
326
ConversationMeta conversation = ConversationMeta .fromMap (conversationId , getResponse .getSourceAsMap ());
326
- String user = User .parse (userstr ).getName ();
327
+ String user = User .parse (userStr ).getName ();
327
328
// If you're not the owner of this conversation, you do not have permission
328
329
if (!user .equals (conversation .getUser ())) {
329
330
internalListener .onResponse (false );
@@ -353,9 +354,9 @@ public void searchConversations(SearchRequest request, ActionListener<SearchResp
353
354
QueryBuilder originalQuery = request .source ().query ();
354
355
BoolQueryBuilder newQuery = new BoolQueryBuilder ();
355
356
newQuery .must (originalQuery );
356
- String userstr = getUserStrFromThreadContext ();
357
- if (userstr != null ) {
358
- String user = User .parse (userstr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userstr ).getName ();
357
+ String userStr = getUserStrFromThreadContext ();
358
+ if (userStr != null ) {
359
+ String user = User .parse (userStr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userStr ).getName ();
359
360
newQuery .must (new TermQueryBuilder (ConversationalIndexConstants .USER_FIELD , user ));
360
361
}
361
362
request .source ().query (newQuery );
@@ -388,11 +389,11 @@ public void updateConversation(String conversationId, UpdateRequest updateReques
388
389
if (access ) {
389
390
innerUpdateConversation (updateRequest , listener );
390
391
} else {
391
- String userstr = client
392
+ String userStr = client
392
393
.threadPool ()
393
394
.getThreadContext ()
394
395
.getTransient (ConfigConstants .OPENSEARCH_SECURITY_USER_INFO_THREAD_CONTEXT );
395
- String user = User .parse (userstr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userstr ).getName ();
396
+ String user = User .parse (userStr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userStr ).getName ();
396
397
throw new OpenSearchStatusException (
397
398
"User [" + user + "] does not have access to memory " + conversationId ,
398
399
RestStatus .UNAUTHORIZED
@@ -421,7 +422,7 @@ public void getConversation(String conversationId, ActionListener<ConversationMe
421
422
listener .onFailure (new IndexNotFoundException ("cannot get memory since the memory index does not exist" , META_INDEX_NAME ));
422
423
return ;
423
424
}
424
- String userstr = getUserStrFromThreadContext ();
425
+ String userStr = getUserStrFromThreadContext ();
425
426
try (ThreadContext .StoredContext threadContext = client .threadPool ().getThreadContext ().stashContext ()) {
426
427
ActionListener <ConversationMeta > internalListener = ActionListener .runBefore (listener , () -> threadContext .restore ());
427
428
GetRequest request = Requests .getRequest (META_INDEX_NAME ).id (conversationId );
@@ -432,12 +433,12 @@ public void getConversation(String conversationId, ActionListener<ConversationMe
432
433
}
433
434
ConversationMeta conversation = ConversationMeta .fromMap (conversationId , getResponse .getSourceAsMap ());
434
435
// If no security, return conversation
435
- if (userstr == null || User .parse (userstr ) == null ) {
436
+ if (userStr == null || User .parse (userStr ) == null ) {
436
437
internalListener .onResponse (conversation );
437
438
return ;
438
439
}
439
440
// If security and correct user, return conversation
440
- String user = User .parse (userstr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userstr ).getName ();
441
+ String user = User .parse (userStr ) == null ? ActionConstants .DEFAULT_USERNAME_FOR_ERRORS : User .parse (userStr ).getName ();
441
442
if (user .equals (conversation .getUser ())) {
442
443
internalListener .onResponse (conversation );
443
444
log .info ("Successfully get the memory for {}" , conversationId );
0 commit comments