vttranlina commented on code in PR #1088: URL: https://github.com/apache/james-project/pull/1088#discussion_r930555982
########## third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RSpamDHttpClient.java: ########## @@ -0,0 +1,133 @@ +/**************************************************************** + * 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.james.rspamd.client; + +import static org.apache.james.rspamd.client.CombinedHeaderAndContentInputStreamHelper.getInputStreamOfMessageHeaders; +import static org.apache.james.rspamd.client.CombinedHeaderAndContentInputStreamHelper.mergeHeaderAndContentInputStream; +import static org.apache.james.rspamd.client.RSpamDClientConfiguration.DEFAULT_TIMEOUT_IN_SECONDS; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.Duration; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.apache.james.rspamd.exception.RSpamDUnexpectedException; +import org.apache.james.rspamd.exception.UnauthorizedException; +import org.apache.james.rspamd.model.AnalysisResult; +import org.apache.james.util.ReactorUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.github.fge.lambdas.Throwing; + +import io.netty.buffer.Unpooled; +import reactor.core.publisher.Mono; +import reactor.netty.ByteBufMono; +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.client.HttpClientResponse; + +public class RSpamDHttpClient { + public static final String CHECK_V2_ENDPOINT = "/checkV2"; + public static final String LEARN_SPAM_ENDPOINT = "/learnspam"; + public static final String LEARN_HAM_ENDPOINT = "/learnham"; + private static final int OK = 200; + private static final int FORBIDDEN = 403; + + private final HttpClient httpClient; + private final ObjectMapper objectMapper; + + public RSpamDHttpClient(RSpamDClientConfiguration configuration) { + httpClient = buildReactorNettyHttpClient(configuration); + this.objectMapper = new ObjectMapper().registerModule(new Jdk8Module()); + } + + public Mono<AnalysisResult> checkV2(MimeMessage mimeMessage) throws MessagingException, IOException { + InputStream headerInputStream = getInputStreamOfMessageHeaders(mimeMessage); + InputStream contentInputStream = mimeMessage.getInputStream(); + + return httpClient.post() + .uri(CHECK_V2_ENDPOINT) + .send(ReactorUtils.toChunks(mergeHeaderAndContentInputStream(headerInputStream, contentInputStream), 16 * 1024) Review Comment: int static CHUNK = 16 * 1024 = 16348 ########## third-party/rspamd/src/main/java/org/apache/james/rspamd/client/RSpamDHttpClient.java: ########## @@ -0,0 +1,133 @@ +/**************************************************************** + * 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.james.rspamd.client; + +import static org.apache.james.rspamd.client.CombinedHeaderAndContentInputStreamHelper.getInputStreamOfMessageHeaders; +import static org.apache.james.rspamd.client.CombinedHeaderAndContentInputStreamHelper.mergeHeaderAndContentInputStream; +import static org.apache.james.rspamd.client.RSpamDClientConfiguration.DEFAULT_TIMEOUT_IN_SECONDS; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.time.Duration; + +import javax.mail.MessagingException; +import javax.mail.internet.MimeMessage; + +import org.apache.james.rspamd.exception.RSpamDUnexpectedException; +import org.apache.james.rspamd.exception.UnauthorizedException; +import org.apache.james.rspamd.model.AnalysisResult; +import org.apache.james.util.ReactorUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import com.github.fge.lambdas.Throwing; + +import io.netty.buffer.Unpooled; +import reactor.core.publisher.Mono; +import reactor.netty.ByteBufMono; +import reactor.netty.http.client.HttpClient; +import reactor.netty.http.client.HttpClientResponse; + +public class RSpamDHttpClient { + public static final String CHECK_V2_ENDPOINT = "/checkV2"; + public static final String LEARN_SPAM_ENDPOINT = "/learnspam"; + public static final String LEARN_HAM_ENDPOINT = "/learnham"; + private static final int OK = 200; + private static final int FORBIDDEN = 403; + + private final HttpClient httpClient; + private final ObjectMapper objectMapper; + + public RSpamDHttpClient(RSpamDClientConfiguration configuration) { + httpClient = buildReactorNettyHttpClient(configuration); + this.objectMapper = new ObjectMapper().registerModule(new Jdk8Module()); + } + + public Mono<AnalysisResult> checkV2(MimeMessage mimeMessage) throws MessagingException, IOException { + InputStream headerInputStream = getInputStreamOfMessageHeaders(mimeMessage); + InputStream contentInputStream = mimeMessage.getInputStream(); Review Comment: It should be supplied in Mono will avoid blocking. ``` Mono.fromSupplier(() -> mimeMessage.getInputStream()) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
