|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.apache.karaf.camel.test; |
| 17 | + |
| 18 | +import java.net.URI; |
| 19 | +import java.net.http.HttpClient; |
| 20 | +import java.net.http.HttpRequest; |
| 21 | +import java.net.http.HttpResponse; |
| 22 | +import java.util.function.Function; |
| 23 | + |
| 24 | +import org.apache.camel.Exchange; |
| 25 | +import org.apache.camel.Processor; |
| 26 | +import org.apache.camel.builder.RouteBuilder; |
| 27 | +import org.apache.camel.model.RouteDefinition; |
| 28 | +import org.apache.karaf.camel.itests.AbstractCamelComponentResultMockBased; |
| 29 | +import org.osgi.service.component.annotations.Component; |
| 30 | + |
| 31 | +@Component( |
| 32 | + name = "karaf-camel-jetty-test", |
| 33 | + immediate = true |
| 34 | +) |
| 35 | +public class CamelJettyComponent extends AbstractCamelComponentResultMockBased { |
| 36 | + |
| 37 | + private final int port = getNextAvailablePort(); |
| 38 | + |
| 39 | + @Override |
| 40 | + protected Function<RouteBuilder, RouteDefinition> consumerRoute() { |
| 41 | + return builder -> builder.from("jetty://http://localhost:%s/jettyTest".formatted(port)).transform(builder.constant("OK")); |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + protected void configureProducer(RouteBuilder builder, RouteDefinition producerRoute) { |
| 46 | + producerRoute.log("calling http endpoint") |
| 47 | + .process(new HttpClientProcessor()); |
| 48 | + } |
| 49 | + |
| 50 | + class HttpClientProcessor implements Processor { |
| 51 | + |
| 52 | + @Override |
| 53 | + public void process(Exchange exchange) throws Exception { |
| 54 | + |
| 55 | + HttpClient client = HttpClient.newHttpClient(); |
| 56 | + |
| 57 | + // Create a URI for the request |
| 58 | + URI uri = URI.create("http://localhost:%s/jettyTest".formatted(port)); |
| 59 | + |
| 60 | + // Create a HttpRequest |
| 61 | + HttpRequest request = HttpRequest.newBuilder() |
| 62 | + .uri(uri) |
| 63 | + .build(); |
| 64 | + |
| 65 | + client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
| 69 | + |
0 commit comments