2.59. 平台 HTTP
此扩展允许创建 HTTP 端点来消耗 HTTP 请求。
它基于由 quarkus-vertx-http 扩展提供的 Eclipse Vert.x HTTP 服务器构建。
2.59.1. 内部内容 复制链接链接已复制到粘贴板!
-
平台 HTTP 组件, URI 语法:
platform-http:path
有关使用和配置详情,请参阅上述链接。
2.59.2. Maven 协调 复制链接链接已复制到粘贴板!
在 code.quarkus.redhat.com 上创建一个具有此扩展名的新项目
或者在现有项目中添加协调:
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-platform-http</artifactId>
</dependency>
2.59.3. 使用方法 复制链接链接已复制到粘贴板!
2.59.3.1. 基本用法 复制链接链接已复制到粘贴板!
在 /hello 端点上提供所有 HTTP 方法:
from("platform-http:/hello").setBody(simple("Hello ${header.name}"));
仅在 /hello 端点上服务 GET 请求:
from("platform-http:/hello?httpMethodRestrict=GET").setBody(simple("Hello ${header.name}"));
2.59.3.2. 通过 Camel REST DSL 使用 platform-http 复制链接链接已复制到粘贴板!
要将 Camel REST DSL 与 platform-http 组件搭配使用,请将 camel-quarkus-rest 添加到 pom.xml 中:
<dependency>
<groupId>org.apache.camel.quarkus</groupId>
<artifactId>camel-quarkus-rest</artifactId>
</dependency>
然后,您可以使用 Camel REST DSL:
rest()
.get("/my-get-endpoint")
.to("direct:handleGetRequest");
.post("/my-post-endpoint")
.to("direct:handlePostRequest");
2.59.3.3. 处理 多部分/数据 文件上传 复制链接链接已复制到粘贴板!
您可以通过列出它们,将上传限制到特定的文件扩展:
from("platform-http:/upload/multipart?fileNameExtWhitelist=html,txt&httpMethodRestrict=POST")
.to("log:multipart")
.process(e -> {
final AttachmentMessage am = e.getMessage(AttachmentMessage.class);
if (am.hasAttachments()) {
am.getAttachments().forEach((fileName, dataHandler) -> {
try (InputStream in = dataHandler.getInputStream()) {
// do something with the input stream
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
});
}
});
2.59.3.4. 保护 platform-http 端点 复制链接链接已复制到粘贴板!
Quarkus 提供了各种安全性和身份验证机制,可用于保护 平台-http 端点的安全。如需了解更多详细信息,请参阅 Quarkus 安全文档。
在路由中,可以获取经过身份验证的用户及其关联的 SecurityIdentity 和 Principal :
from("platform-http:/secure")
.process(e -> {
Message message = e.getMessage();
QuarkusHttpUser user = message.getHeader(VertxPlatformHttpConstants.AUTHENTICATED_USER, QuarkusHttpUser.class);
SecurityIdentity securityIdentity = user.getSecurityIdentity();
Principal principal = securityIdentity.getPrincipal();
// Do something useful with SecurityIdentity / Principal. E.g check user roles etc.
});
另外,检查 Quarkus 文档中的 quarkus.http.body.* 配置选项,esp。Quarkus.http.body.handle-file-uploads,quarkus.http.body.uploads-directory 和 quarkus.http.body.delete-uploaded-files-on-end.
2.59.3.5. 实施反向代理 复制链接链接已复制到粘贴板!
平台 HTTP 组件可以充当反向代理,本例中为 Exchange.HTTP_URI,Exchange.HTTP_HOST 标头由 HTTP 请求行中收到的绝对 URL 填充。
以下是 HTTP 代理的示例,它只是将 Exchange 重定向到原始服务器。
from("platform-http:proxy")
.toD("http://"
+ "${headers." + Exchange.HTTP_HOST + "}");
2.59.4. 其他 Camel Quarkus 配置 复制链接链接已复制到粘贴板!
2.59.4.1. 平台 HTTP 服务器配置 复制链接链接已复制到粘贴板!
平台 HTTP 服务器配置由 Quarkus 管理。有关配置选项的完整列表,请参阅 Quarkus HTTP 配置指南。
要为平台 HTTP 服务器配置 SSL,请遵循 与 SSL 的安全连接,指南 为。请注意,目前不支持使用 SSLContextParameters 为 SSL 配置服务器。
2.59.4.2. 字符编码 复制链接链接已复制到粘贴板!
如果您期望应用程序使用非默认 编码来发送或接收请求,请检查 Native 模式指南的 Character encodings 部分。