This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch brow in repository https://gitbox.apache.org/repos/asf/camel.git
commit fd820def960127901a0ee716fd3d41ea16d6d713 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Sep 7 18:02:33 2024 +0200 CAMEL-21183: BrowseEndpoint should have limit/filter to optimize returned data from component implementations --- .../camel/component/browse/BrowseEndpoint.java | 7 +- .../camel/component/browse/BrowseFilterTest.java | 77 ++++++++++++++++++++++ .../camel/component/browse/BrowseLimitTest.java | 64 ++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) diff --git a/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java b/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java index 09f6c0342b6..a0fda82ea59 100644 --- a/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java +++ b/components/camel-browse/src/main/java/org/apache/camel/component/browse/BrowseEndpoint.java @@ -19,7 +19,6 @@ package org.apache.camel.component.browse; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Predicate; -import java.util.stream.Collectors; import org.apache.camel.Category; import org.apache.camel.Component; @@ -139,8 +138,10 @@ public class BrowseEndpoint extends DefaultEndpoint implements BrowsableEndpoint protected void onExchange(Exchange exchange) throws Exception { if (limit > 0) { // make room if end of capacity - while (exchanges.size() >= (limit - 1)) { - exchanges.remove(0); + int max = exchanges.size(); + if (max >= limit) { + int space = Math.abs(max - (limit - 1)); + exchanges = exchanges.subList(space, max); } } exchanges.add(exchange); diff --git a/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseFilterTest.java b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseFilterTest.java new file mode 100644 index 00000000000..b15c2312c98 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseFilterTest.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.browse; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.Exchange; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.BrowsableEndpoint; +import org.junit.jupiter.api.Test; + +import java.util.Collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BrowseFilterTest extends ContextTestSupport { + + protected final Object body1 = "one"; + protected final Object body2 = "two"; + protected final Object body3 = "three"; + protected final Object body4 = "four"; + protected final Object body5 = "five"; + + @Test + public void testFilter() throws Exception { + template.sendBody("browse:foo", body1); + template.sendBody("browse:foo", body2); + template.sendBody("browse:foo", body3); + template.sendBody("browse:foo", body4); + template.sendBody("browse:foo", body5); + + Collection<Endpoint> list = context.getEndpoints(); + assertEquals(2, list.size(), "number of endpoints"); + + BrowsableEndpoint be1 = context.getEndpoint("browse:foo", BrowsableEndpoint.class); + assertEquals(5, be1.getExchanges().size()); + + BrowsableEndpoint be2 = context.getEndpoint("browse:bar?filter=#evenFilter", BrowsableEndpoint.class); + assertEquals(2, be2.getExchanges().size()); + assertEquals("two", be2.getExchanges().get(0).getMessage().getBody()); + assertEquals("four", be2.getExchanges().get(1).getMessage().getBody()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + context.getRegistry().bind("evenFilter", new EvenPredicate()); + + from("browse:foo").to("browse:bar?filter=#evenFilter"); + } + }; + } + + private static class EvenPredicate implements java.util.function.Predicate<Exchange> { + + @Override + public boolean test(Exchange exchange) { + String b = exchange.getMessage().getBody(String.class); + return "two".equals(b) || "four".equals(b); + } + } +} diff --git a/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseLimitTest.java b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseLimitTest.java new file mode 100644 index 00000000000..42299c47796 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/browse/BrowseLimitTest.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.browse; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Endpoint; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.BrowsableEndpoint; +import org.junit.jupiter.api.Test; + +import java.util.Collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class BrowseLimitTest extends ContextTestSupport { + + protected final Object body1 = "one"; + protected final Object body2 = "two"; + protected final Object body3 = "three"; + protected final Object body4 = "four"; + protected final Object body5 = "five"; + + @Test + public void testLimit() throws Exception { + template.sendBody("browse:foo?limit=1", body1); + template.sendBody("browse:foo?limit=1", body2); + template.sendBody("browse:foo?limit=1", body3); + template.sendBody("browse:foo?limit=1", body4); + template.sendBody("browse:foo?limit=1", body5); + + Collection<Endpoint> list = context.getEndpoints(); + assertEquals(2, list.size(), "number of endpoints"); + + BrowsableEndpoint be1 = context.getEndpoint("browse:foo?limit=1", BrowsableEndpoint.class); + assertEquals(1, be1.getExchanges().size()); + assertEquals("five", be1.getExchanges().get(0).getMessage().getBody()); + + BrowsableEndpoint be2 = context.getEndpoint("browse:bar?limit=5", BrowsableEndpoint.class); + assertEquals(5, be2.getExchanges().size()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from("browse:foo?limit=1").to("browse:bar?limit=5"); + } + }; + } +}