forked from apache/camel-k-runtime
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
create platform http service apache#265
- Loading branch information
1 parent
c74965b
commit 0448459
Showing
29 changed files
with
1,157 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...k-main/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/PlatformHttpRouter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* 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.k.http; | ||
|
||
import io.vertx.ext.web.Router; | ||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.component.platform.http.PlatformHttpConstants; | ||
import org.apache.camel.support.CamelContextHelper; | ||
|
||
public class PlatformHttpRouter { | ||
public static final String PLATFORM_HTTP_ROUTER_NAME = PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME + "-router"; | ||
|
||
private final Router router; | ||
|
||
public PlatformHttpRouter(Router router) { | ||
this.router = router; | ||
} | ||
|
||
public Router get() { | ||
return router; | ||
} | ||
|
||
// ********************** | ||
// | ||
// Helpers | ||
// | ||
// ********************** | ||
|
||
public static PlatformHttpRouter lookup(CamelContext camelContext) { | ||
return CamelContextHelper.mandatoryLookup( | ||
camelContext, | ||
PlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME, | ||
PlatformHttpRouter.class | ||
); | ||
} | ||
} |
178 changes: 178 additions & 0 deletions
178
...k-main/camel-k-runtime-http/src/main/java/org/apache/camel/k/http/PlatformHttpServer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
* 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.k.http; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.ExecutorService; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpServer; | ||
import io.vertx.ext.web.Router; | ||
import io.vertx.ext.web.RoutingContext; | ||
import io.vertx.ext.web.handler.BodyHandler; | ||
import org.apache.camel.CamelContext; | ||
import org.apache.camel.component.platform.http.PlatformHttpConstants; | ||
import org.apache.camel.support.service.ServiceSupport; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
final class PlatformHttpServer extends ServiceSupport { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PlatformHttpServer.class); | ||
|
||
private final CamelContext context; | ||
private final PlatformHttpServiceConfiguration configuration; | ||
private final Vertx vertx; | ||
private final ExecutorService executor; | ||
|
||
private HttpServer server; | ||
|
||
public PlatformHttpServer(CamelContext context, PlatformHttpServiceConfiguration configuration, Vertx vertx, ExecutorService executor) { | ||
this.context = context; | ||
this.configuration = configuration; | ||
this.vertx = vertx; | ||
this.executor = executor; | ||
} | ||
|
||
@Override | ||
protected void doStart() throws Exception { | ||
startAsync().toCompletableFuture().join(); | ||
} | ||
|
||
@Override | ||
protected void doStop() throws Exception { | ||
try { | ||
if (server != null) { | ||
stopAsync().toCompletableFuture().join(); | ||
} | ||
} finally { | ||
this.server = null; | ||
} | ||
} | ||
|
||
private CompletionStage<Void> startAsync() { | ||
final Router router = Router.router(vertx); | ||
final Router subRouter = Router.router(vertx); | ||
|
||
router.route() | ||
.order(Integer.MIN_VALUE) | ||
.handler(ctx -> { | ||
ctx.request().resume(); | ||
createBodyHandler().handle(ctx); | ||
}); | ||
|
||
router.mountSubRouter(configuration.getPath(), subRouter); | ||
|
||
context.getRegistry().bind(PlatformHttpRouter.PLATFORM_HTTP_ROUTER_NAME, new PlatformHttpRouter(subRouter)); | ||
|
||
//HttpServerOptions options = new HttpServerOptions(); | ||
if (configuration.getSslContextParameters() != null) { | ||
// TODO: add ssl support | ||
throw new UnsupportedOperationException("Not yet implemented"); | ||
} | ||
|
||
server = vertx.createHttpServer(); | ||
|
||
return CompletableFuture.runAsync( | ||
() -> { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
server.requestHandler(router).listen(configuration.getBindPort(), configuration.getBindHost(), result -> { | ||
try { | ||
if (result.failed()) { | ||
LOGGER.warn("Failed to start Vert.x HttpServer on {}:{}, reason: {}", | ||
configuration.getBindHost(), | ||
configuration.getBindPort(), | ||
result.cause().getMessage() | ||
); | ||
|
||
throw new RuntimeException(result.cause()); | ||
} | ||
|
||
LOGGER.info("Vert.x HttpServer started on {}:{}", configuration.getBindHost(), configuration.getBindPort()); | ||
} finally { | ||
latch.countDown(); | ||
} | ||
}); | ||
|
||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}, | ||
executor | ||
); | ||
} | ||
|
||
protected CompletionStage<Void> stopAsync() { | ||
return CompletableFuture.runAsync( | ||
() -> { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
|
||
// remove the platform-http component | ||
context.removeComponent(PlatformHttpConstants.PLATFORM_HTTP_COMPONENT_NAME); | ||
|
||
server.close(result -> { | ||
try { | ||
if (result.failed()) { | ||
LOGGER.warn("Failed to close Vert.x HttpServer reason: {}", | ||
result.cause().getMessage() | ||
); | ||
|
||
throw new RuntimeException(result.cause()); | ||
} | ||
|
||
LOGGER.info("Vert.x HttpServer stopped"); | ||
} finally { | ||
latch.countDown(); | ||
} | ||
}); | ||
|
||
try { | ||
latch.await(); | ||
} catch (InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}, | ||
executor | ||
); | ||
} | ||
|
||
private Handler<RoutingContext> createBodyHandler() { | ||
BodyHandler bodyHandler = BodyHandler.create(); | ||
|
||
if (configuration.getMaxBodySize() != null) { | ||
bodyHandler.setBodyLimit(configuration.getMaxBodySize().longValueExact()); | ||
} | ||
|
||
bodyHandler.setHandleFileUploads(configuration.getBodyHandler().isHandleFileUploads()); | ||
bodyHandler.setUploadsDirectory(configuration.getBodyHandler().getUploadsDirectory()); | ||
bodyHandler.setDeleteUploadedFilesOnEnd(configuration.getBodyHandler().isDeleteUploadedFilesOnEnd()); | ||
bodyHandler.setMergeFormAttributes(configuration.getBodyHandler().isMergeFormAttributes()); | ||
bodyHandler.setPreallocateBodyBuffer(configuration.getBodyHandler().isPreallocateBodyBuffer()); | ||
|
||
return new Handler<RoutingContext>() { | ||
@Override | ||
public void handle(RoutingContext event) { | ||
event.request().resume(); | ||
bodyHandler.handle(event); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.