@@ -178,19 +178,9 @@ public HttpKafkaWithFetchResult resolveFetch(
178178 if (filter .key .isPresent ())
179179 {
180180 String key0 = filter .key .get ();
181- Matcher keyMatcher = paramsMatcher .reset (key0 );
182- if (keyMatcher .matches ())
183- {
184- key0 = keyMatcher .replaceAll (replacer );
185- }
186-
187- keyMatcher = identityMatcher .reset (key0 );
188- if (keyMatcher .matches ())
189- {
190- key0 = keyMatcher .replaceAll (r -> identityReplacer .apply (authorization , r ));
191- }
192-
193- key0 = resolveAttribute (authorization , keyMatcher , key0 );
181+ key0 = findAndReplace (key0 , paramsMatcher , replacer );
182+ key0 = findAndReplace (key0 , identityMatcher , r -> identityReplacer .apply (authorization , r ));
183+ key0 = findAndReplace (key0 , attributeMatcher , r -> attributeReplacer .apply (authorization , r ));
194184
195185 key = new String16FW (key0 ).value ();
196186 }
@@ -206,19 +196,9 @@ public HttpKafkaWithFetchResult resolveFetch(
206196 DirectBuffer name = new String16FW (name0 ).value ();
207197
208198 String value0 = header0 .value ;
209- Matcher valueMatcher = paramsMatcher .reset (value0 );
210- if (valueMatcher .matches ())
211- {
212- value0 = valueMatcher .replaceAll (replacer );
213- }
214-
215- valueMatcher = identityMatcher .reset (value0 );
216- if (valueMatcher .matches ())
217- {
218- value0 = valueMatcher .replaceAll (r -> identityReplacer .apply (authorization , r ));
219- }
220-
221- value0 = resolveAttribute (authorization , valueMatcher , value0 );
199+ value0 = findAndReplace (value0 , paramsMatcher , replacer );
200+ value0 = findAndReplace (value0 , identityMatcher , r -> identityReplacer .apply (authorization , r ));
201+ value0 = findAndReplace (value0 , attributeMatcher , r -> attributeReplacer .apply (authorization , r ));
222202
223203 DirectBuffer value = new String16FW (value0 ).value ();
224204
@@ -305,12 +285,7 @@ public HttpKafkaWithProduceResult resolveProduce(
305285 String name0 = header .name ;
306286 String8FW name = new String8FW (name0 );
307287
308- String value0 = header .value ;
309- Matcher valueMatcher = paramsMatcher .reset (value0 );
310- if (valueMatcher .find ())
311- {
312- value0 = valueMatcher .replaceAll (replacer );
313- }
288+ String value0 = findAndReplace (header .value , paramsMatcher , replacer );
314289
315290 String value = value0 ;
316291 Supplier <String16FW > valueRef = () -> new String16FW (value );
@@ -320,11 +295,7 @@ public HttpKafkaWithProduceResult resolveProduce(
320295 valueRef = () ->
321296 {
322297 String value1 = value ;
323- Matcher value1Matcher = correlationIdMatcher .reset (value1 );
324- if (value1Matcher .find ())
325- {
326- value1 = value1Matcher .replaceAll (hash .correlationId ().asString ());
327- }
298+ value1 = findAndReplace (value1 , correlationIdMatcher , r -> hash .correlationId ().asString ());
328299
329300 return new String16FW (value1 );
330301 };
@@ -343,19 +314,9 @@ public HttpKafkaWithProduceResult resolveProduce(
343314 if (produce .key .isPresent ())
344315 {
345316 String key0 = produce .key .get ();
346- Matcher keyMatcher = paramsMatcher .reset (key0 );
347- if (keyMatcher .matches ())
348- {
349- key0 = keyMatcher .replaceAll (replacer );
350- }
351-
352- keyMatcher = identityMatcher .reset (key0 );
353- if (keyMatcher .matches ())
354- {
355- key0 = keyMatcher .replaceAll (r -> identityReplacer .apply (authorization , r ));
356- }
357-
358- key0 = resolveAttribute (authorization , keyMatcher , key0 );
317+ key0 = findAndReplace (key0 , paramsMatcher , replacer );
318+ key0 = findAndReplace (key0 , identityMatcher , r -> identityReplacer .apply (authorization , r ));
319+ key0 = findAndReplace (key0 , attributeMatcher , r -> attributeReplacer .apply (authorization , r ));
359320
360321 String key = key0 ;
361322 keyRef = () -> new String16FW (key ).value ();
@@ -365,11 +326,7 @@ public HttpKafkaWithProduceResult resolveProduce(
365326 keyRef = () ->
366327 {
367328 String key1 = key ;
368- Matcher key1Matcher = idempotencyKeyMatcher .reset (key1 );
369- if (key1Matcher .find ())
370- {
371- key1 = key1Matcher .replaceAll (idempotencyKey .asString ());
372- }
329+ key1 = findAndReplace (key1 , idempotencyKeyMatcher , r -> idempotencyKey .asString ());
373330 return new String16FW (key1 ).value ();
374331 };
375332 }
@@ -386,19 +343,9 @@ public HttpKafkaWithProduceResult resolveProduce(
386343 DirectBuffer name = new String16FW (name0 ).value ();
387344
388345 String value0 = override .value ;
389- Matcher valueMatcher = paramsMatcher .reset (value0 );
390- if (valueMatcher .matches ())
391- {
392- value0 = valueMatcher .replaceAll (replacer );
393- }
394-
395- valueMatcher = identityMatcher .reset (value0 );
396- if (valueMatcher .matches ())
397- {
398- value0 = valueMatcher .replaceAll (r -> identityReplacer .apply (authorization , r ));
399- }
400-
401- value0 = resolveAttribute (authorization , valueMatcher , value0 );
346+ value0 = findAndReplace (value0 , paramsMatcher , replacer );
347+ value0 = findAndReplace (value0 , identityMatcher , r -> identityReplacer .apply (authorization , r ));
348+ value0 = findAndReplace (value0 , attributeMatcher , r -> attributeReplacer .apply (authorization , r ));
402349
403350 String value = value0 ;
404351 Supplier <DirectBuffer > valueRef = () -> new String16FW (value ).value ();
@@ -407,11 +354,7 @@ public HttpKafkaWithProduceResult resolveProduce(
407354 valueRef = () ->
408355 {
409356 String value1 = value ;
410- Matcher value1Matcher = correlationIdMatcher .reset (value1 );
411- if (value1Matcher .find ())
412- {
413- value1 = value1Matcher .replaceAll (hash .correlationId ().asString ());
414- }
357+ value1 = findAndReplace (value1 , idempotencyKeyMatcher , r -> idempotencyKey .asString ());
415358 return new String16FW (value1 ).value ();
416359 };
417360 }
@@ -424,11 +367,7 @@ public HttpKafkaWithProduceResult resolveProduce(
424367 if (produce .replyTo .isPresent ())
425368 {
426369 String replyTo0 = produce .replyTo .get ();
427- Matcher replyToMatcher = paramsMatcher .reset (replyTo0 );
428- if (replyToMatcher .matches ())
429- {
430- replyTo0 = replyToMatcher .replaceAll (replacer );
431- }
370+ replyTo0 = findAndReplace (replyTo0 , paramsMatcher , replacer );
432371 replyTo = new String16FW (replyTo0 );
433372 }
434373
@@ -449,41 +388,28 @@ public HttpKafkaWithProduceResult resolveProduce(
449388 produce .correlationId , idempotencyKey , async , hash , timeout );
450389 }
451390
452- private String resolveAttribute (
453- long authorization ,
454- Matcher matcher ,
455- String value )
456- {
457- matcher = attributeMatcher .reset (value );
458- if (matcher .matches ())
459- {
460- value = matcher .replaceAll (r -> attributeReplacer .apply (authorization , r ));
461- }
462- return value ;
463- }
464-
465391 private String16FW resolveTopic (
466392 long authorization ,
467393 String topic )
468394 {
469- Matcher topicMatcher = paramsMatcher .reset (topic );
470- if (topicMatcher .matches ())
471- {
472- topic = topicMatcher .replaceAll (replacer );
473- }
395+ topic = findAndReplace (topic , paramsMatcher , replacer );
396+ topic = findAndReplace (topic , identityMatcher , r -> identityReplacer .apply (authorization , r ));
397+ topic = findAndReplace (topic , attributeMatcher , r -> attributeReplacer .apply (authorization , r ));
474398
475- topicMatcher = identityMatcher .reset (topic );
476- if (topicMatcher .find ())
477- {
478- topic = topicMatcher .replaceAll (r -> identityReplacer .apply (authorization , r ));
479- }
399+ return new String16FW (topic );
400+ }
480401
481- topicMatcher = attributeMatcher .reset (topic );
482- if (topicMatcher .find ())
402+ private static String findAndReplace (
403+ String value ,
404+ Matcher matcher ,
405+ Function <MatchResult , String > replacer )
406+ {
407+ matcher .reset (value );
408+ while (matcher .find ())
483409 {
484- topic = topicMatcher .replaceAll (r -> attributeReplacer .apply (authorization , r ));
410+ value = matcher .replaceAll (replacer );
411+ matcher .reset (value );
485412 }
486-
487- return new String16FW (topic );
413+ return value ;
488414 }
489415}
0 commit comments