diff --git a/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/FileConversionServiceImpl.java b/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/FileConversionServiceImpl.java index ad2a05bcb195..054f2fb7118f 100644 --- a/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/FileConversionServiceImpl.java +++ b/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/FileConversionServiceImpl.java @@ -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); diff --git a/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/converters/LoolFileConverter.java b/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/converters/LoolFileConverter.java index 0be85b881f2b..8c55540ca690 100644 --- a/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/converters/LoolFileConverter.java +++ b/kernel/kernel-impl/src/main/java/org/sakaiproject/content/impl/converters/LoolFileConverter.java @@ -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; + } } }