Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAK-45711 Kernel improve error handling in LoolFileConverter #13225

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public void startIfEnabled() {
log.debug("Converted file id: {}, site id: {} ", convertedFileName, siteId);

byte[] convertedFileBytes = LoolFileConverter.convert(converterBaseUrl, source.streamContent());

if (convertedFileBytes == null) {
throw new RuntimeException("File conversion failed - no bytes returned from converter");
}

ResourcePropertiesEdit properties = contentHostingService.newResourceProperties();
properties.addProperty(ResourceProperties.PROP_DISPLAY_NAME, convertedFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,46 @@
import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class LoolFileConverter {

public static byte[] convert(String baseUrl, InputStream sourceInputStream) throws IOException {

int timeoutMillis = 5000;
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(timeoutMillis)
.setConnectionRequestTimeout(timeoutMillis)
.setSocketTimeout(timeoutMillis * 1000).build();
CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
import lombok.extern.slf4j.Slf4j;

HttpPost httpPost = new HttpPost(baseUrl + "/lool/convert-to/pdf");
@Slf4j
public class LoolFileConverter {

HttpEntity multipart = MultipartEntityBuilder.create()
.setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
.addBinaryBody("data", sourceInputStream, ContentType.MULTIPART_FORM_DATA, "anything")
.build();
public static byte[] convert(String baseUrl, InputStream sourceInputStream) {
final HttpPost httpPost = new HttpPost(baseUrl + "/lool/convert-to/pdf");

final MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("data", sourceInputStream, ContentType.MULTIPART_FORM_DATA, "anything");

final HttpEntity multipart = builder.build();
httpPost.setEntity(multipart);
CloseableHttpResponse response = client.execute(httpPost);
byte[] convertedFileBytes = EntityUtils.toByteArray(response.getEntity());
client.close();
return convertedFileBytes;

try (CloseableHttpClient client = HttpClients.createDefault()) {
HttpResponse response = client.execute(httpPost);
final int statusCode = response.getStatusLine().getStatusCode();

if (statusCode == HttpStatus.SC_OK) {
return EntityUtils.toByteArray(response.getEntity());
} else {
log.error("File conversion failed with HTTP status code: {}. URL: {}",
statusCode, baseUrl + "/lool/convert-to/pdf");
return null;
}
} catch (IOException e) {
log.warn("Error during file conversion: {}", e.getMessage(), e);
return null;
} catch (Exception e) {
log.warn("Unexpected error during file conversion: {}", e.getMessage(), e);
return null;
}
}
}
Loading