-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: urls in css now replaced by the base64 content (#215)
* feat: urls in css now replaced by the base64 content Refs: SchweizerischeBundesbahnen/weasyprint-service#61
- Loading branch information
Showing
26 changed files
with
537 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 0 additions & 45 deletions
45
src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/CustomImageUrlResolver.java
This file was deleted.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/CustomResourceUrlResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package ch.sbb.polarion.extension.pdf_exporter.util; | ||
|
||
import com.polarion.alm.tracker.internal.url.IUrlResolver; | ||
import com.polarion.core.util.logging.Logger; | ||
import lombok.SneakyThrows; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.VisibleForTesting; | ||
|
||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
import static java.net.HttpURLConnection.*; | ||
|
||
/** | ||
* Custom version of {@link com.polarion.alm.tracker.internal.url.GenericUrlResolver} with the redirect support. | ||
*/ | ||
public class CustomResourceUrlResolver implements IUrlResolver { | ||
|
||
private static final int CONNECTION_TIMEOUT_MS = 3_000; | ||
private static final int READ_TIMEOUT_MS = 3_000; | ||
|
||
public boolean canResolve(@NotNull String url) { | ||
return url.startsWith("http://") || url.startsWith("https://"); | ||
} | ||
|
||
public InputStream resolve(@NotNull String urlStr) { | ||
try { | ||
URL url = new URL(normalizeUrl(urlStr)); | ||
return resolveImpl(url); | ||
} catch (Exception e) { | ||
Logger.getLogger(this).warn("Failed to load resource: " + urlStr, e); | ||
} | ||
return null; | ||
} | ||
|
||
@SneakyThrows | ||
@VisibleForTesting | ||
public InputStream resolveImpl(@NotNull URL url) { | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setConnectTimeout(CONNECTION_TIMEOUT_MS); | ||
connection.setReadTimeout(READ_TIMEOUT_MS); | ||
connection.connect(); | ||
int responseCode = connection.getResponseCode(); | ||
if (responseCode == HTTP_OK) { | ||
return connection.getInputStream(); | ||
} else if (responseCode == HTTP_MOVED_PERM || responseCode == HTTP_MOVED_TEMP) { | ||
String location = connection.getHeaderField("Location"); | ||
if (location != null && canResolve(location)) { | ||
return resolve(location); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private String normalizeUrl(String urlStr) { | ||
return urlStr.replace(" ", "%20").replace("%5F", "_"); | ||
} | ||
} |
8 changes: 7 additions & 1 deletion
8
src/main/java/ch/sbb/polarion/extension/pdf_exporter/util/FileResourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
package ch.sbb.polarion.extension.pdf_exporter.util; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public interface FileResourceProvider { | ||
|
||
byte[] getResourceAsBytes(String resource); | ||
@Nullable | ||
String getResourceAsBase64String(@NotNull String resource); | ||
|
||
byte[] getResourceAsBytes(@NotNull String resource); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.