mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2024-11-11 02:10:11 +01:00
Bug fix UI crash when url is unrechable (#1642)
* feat: Add URL reachability check in ConvertWebsiteToPDF * Add tests for URL reachability in ConvertWebsiteToPdfTest * test: Update URL in ConvertWebsiteToPdfTest for testing
This commit is contained in:
parent
771b312ee8
commit
148feda83f
@ -39,6 +39,12 @@ public class ConvertWebsiteToPDF {
|
|||||||
if (!URL.matches("^https?://.*") || !GeneralUtils.isValidURL(URL)) {
|
if (!URL.matches("^https?://.*") || !GeneralUtils.isValidURL(URL)) {
|
||||||
throw new IllegalArgumentException("Invalid URL format provided.");
|
throw new IllegalArgumentException("Invalid URL format provided.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// validate the URL is reachable
|
||||||
|
if (!GeneralUtils.isURLReachable(URL)) {
|
||||||
|
throw new IllegalArgumentException("URL is not reachable, please provide a valid URL.");
|
||||||
|
}
|
||||||
|
|
||||||
Path tempOutputFile = null;
|
Path tempOutputFile = null;
|
||||||
byte[] pdfBytes;
|
byte[] pdfBytes;
|
||||||
try {
|
try {
|
||||||
|
@ -13,6 +13,8 @@ import java.nio.file.SimpleFileVisitor;
|
|||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -71,6 +73,21 @@ public class GeneralUtils {
|
|||||||
} catch (MalformedURLException e) {
|
} catch (MalformedURLException e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean isURLReachable(String urlStr) {
|
||||||
|
try {
|
||||||
|
URL url = new URL(urlStr);
|
||||||
|
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
|
||||||
|
connection.setRequestMethod("HEAD");
|
||||||
|
int responseCode = connection.getResponseCode();
|
||||||
|
return (200 <= responseCode && responseCode <= 399);
|
||||||
|
} catch (MalformedURLException e) {
|
||||||
|
return false;
|
||||||
|
} catch (IOException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File multipartToFile(MultipartFile multipart) throws IOException {
|
public static File multipartToFile(MultipartFile multipart) throws IOException {
|
||||||
@ -95,19 +112,16 @@ public class GeneralUtils {
|
|||||||
sizeStr = sizeStr.replace(",", ".").replace(" ", "");
|
sizeStr = sizeStr.replace(",", ".").replace(" ", "");
|
||||||
try {
|
try {
|
||||||
if (sizeStr.endsWith("KB")) {
|
if (sizeStr.endsWith("KB")) {
|
||||||
return (long)
|
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024);
|
||||||
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024);
|
|
||||||
} else if (sizeStr.endsWith("MB")) {
|
} else if (sizeStr.endsWith("MB")) {
|
||||||
return (long)
|
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
|
||||||
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
|
* 1024
|
||||||
* 1024
|
* 1024);
|
||||||
* 1024);
|
|
||||||
} else if (sizeStr.endsWith("GB")) {
|
} else if (sizeStr.endsWith("GB")) {
|
||||||
return (long)
|
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
|
||||||
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
|
* 1024
|
||||||
* 1024
|
* 1024
|
||||||
* 1024
|
* 1024);
|
||||||
* 1024);
|
|
||||||
} else if (sizeStr.endsWith("B")) {
|
} else if (sizeStr.endsWith("B")) {
|
||||||
return Long.parseLong(sizeStr.substring(0, sizeStr.length() - 1));
|
return Long.parseLong(sizeStr.substring(0, sizeStr.length() - 1));
|
||||||
} else {
|
} else {
|
||||||
@ -170,13 +184,15 @@ public class GeneralUtils {
|
|||||||
|
|
||||||
int n = 0;
|
int n = 0;
|
||||||
while (true) {
|
while (true) {
|
||||||
// Replace 'n' with the current value of n, correctly handling numbers before 'n'
|
// Replace 'n' with the current value of n, correctly handling numbers before
|
||||||
|
// 'n'
|
||||||
String sanitizedExpression = insertMultiplicationBeforeN(expression, n);
|
String sanitizedExpression = insertMultiplicationBeforeN(expression, n);
|
||||||
Double result = evaluator.evaluate(sanitizedExpression);
|
Double result = evaluator.evaluate(sanitizedExpression);
|
||||||
|
|
||||||
// Check if the result is null or not within bounds
|
// Check if the result is null or not within bounds
|
||||||
if (result == null || result <= 0 || result.intValue() > maxValue) {
|
if (result == null || result <= 0 || result.intValue() > maxValue) {
|
||||||
if (n != 0) break;
|
if (n != 0)
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
results.add(result.intValue());
|
results.add(result.intValue());
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,41 @@
|
|||||||
|
package stirling.software.SPDF.controller.api.converters;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
|
||||||
|
import stirling.software.SPDF.model.api.converters.UrlToPdfRequest;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class ConvertWebsiteToPdfTest {
|
||||||
|
@Test
|
||||||
|
public void test_exemption_is_thrown_when_invalid_url_format_provided() {
|
||||||
|
|
||||||
|
String invalid_format_Url = "invalid-url";
|
||||||
|
// Arrange
|
||||||
|
ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF();
|
||||||
|
UrlToPdfRequest request = new UrlToPdfRequest();
|
||||||
|
request.setUrlInput(invalid_format_Url);
|
||||||
|
// Act
|
||||||
|
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
convertWebsiteToPDF.urlToPdf(request);
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
assertEquals("Invalid URL format provided.", thrown.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test_exemption_is_thrown_when_url_is_not_reachable() {
|
||||||
|
|
||||||
|
String unreachable_Url = "https://www.googleeeexyz.com";
|
||||||
|
// Arrange
|
||||||
|
ConvertWebsiteToPDF convertWebsiteToPDF = new ConvertWebsiteToPDF();
|
||||||
|
UrlToPdfRequest request = new UrlToPdfRequest();
|
||||||
|
request.setUrlInput(unreachable_Url);
|
||||||
|
// Act
|
||||||
|
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
convertWebsiteToPDF.urlToPdf(request);
|
||||||
|
});
|
||||||
|
// Assert
|
||||||
|
assertEquals("URL is not reachable, please provide a valid URL.", thrown.getMessage());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user