-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4793a1d
commit e2bdf58
Showing
10 changed files
with
533 additions
and
268 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<layout class="ch.qos.logback.classic.PatternLayout"> | ||
<Pattern>%-5level %logger{36} - %msg%n</Pattern> | ||
</layout> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT" /> | ||
</root> | ||
|
||
</configuration> |
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
355 changes: 90 additions & 265 deletions
355
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/Flipbook2PdfMain.java
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/BookConfig.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,14 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
/** info loaded from book config.js */ | ||
@Builder // @AllArgsConstructor | ||
@Getter | ||
public class BookConfig { | ||
long createdTime; | ||
int totalPageCount; | ||
int largePageWidth; | ||
int largePageHeight; | ||
} |
15 changes: 15 additions & 0 deletions
15
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/DownloadBookCtx.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,15 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import java.io.File; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class DownloadBookCtx { | ||
|
||
public File bookOutputDir; | ||
public final String bookTitle; | ||
public final String orderDetail; | ||
public BookConfig bookConfig; | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/Flipbook2PdfParams.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,45 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import java.io.File; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class Flipbook2PdfParams { | ||
|
||
private File baseOutputDir; | ||
|
||
private String orderDetail; | ||
private String bookTitle; | ||
|
||
|
||
private boolean trustAllCerts = true; | ||
private String httpProxyHost = null; // "localhost"; | ||
private int httpProxyPort = 8080; | ||
|
||
private boolean debugHttpCalls = false; | ||
|
||
|
||
public void parseArgs(String[] args) { | ||
for(int i = 0; i < args.length; i++) { | ||
switch(args[i]) { | ||
case "-o": case "--outputDir": | ||
this.baseOutputDir = new File(args[++i]); | ||
break; | ||
case "--orderDetail": | ||
this.orderDetail = args[++i]; | ||
break; | ||
default: | ||
throw new IllegalArgumentException("Unrecognized argument '" + args[i] + "'"); | ||
} | ||
} | ||
|
||
if (baseOutputDir == null) { | ||
baseOutputDir = new File("out"); | ||
} | ||
if (! baseOutputDir.exists()) { | ||
baseOutputDir.mkdirs(); | ||
} | ||
} | ||
|
||
} |
209 changes: 209 additions & 0 deletions
209
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/HttpClientHelper.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,209 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import java.net.CookieManager; | ||
import java.net.InetSocketAddress; | ||
import java.net.Proxy; | ||
import java.net.Proxy.Type; | ||
|
||
import javax.net.ssl.HostnameVerifier; | ||
import javax.net.ssl.SSLContext; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.TrustManager; | ||
|
||
import fr.an.tools.flipbook2pdf.utils.TrustAllX509TrustManager; | ||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.FormBody; | ||
import okhttp3.JavaNetCookieJar; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okhttp3.logging.HttpLoggingInterceptor; | ||
|
||
@Slf4j | ||
public class HttpClientHelper { | ||
|
||
private static final String PATH_DMDPDFSTAMPER_PDFS = "/module/dmdpdfstamper/pdfs"; | ||
private static final String PATH_FLIPBBOOKS = "/modules/dmdpdfstamper/flipbooks/"; | ||
private static final String PATH_FILES_MOBILE = "/files/mobile/"; | ||
private static final String PATH_MOBILE_JAVASCRIPT_CONFIGJS = "/mobile/javascript/config.js"; | ||
|
||
private String baseUrl = "https://boutique.ed-diamond.com"; | ||
|
||
private OkHttpClient httpClient; | ||
|
||
public HttpClientHelper() { | ||
} | ||
|
||
public void initHttpClient(Flipbook2PdfParams params) throws Exception { | ||
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); | ||
|
||
// cookie manager | ||
httpClientBuilder.cookieJar(new JavaNetCookieJar(new CookieManager())); | ||
|
||
if (params.isDebugHttpCalls()) { | ||
// init okhttp 3 logger | ||
HttpLoggingInterceptor logging = new HttpLoggingInterceptor(); | ||
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS); | ||
httpClientBuilder.addInterceptor(logging); | ||
} | ||
|
||
if (params.getHttpProxyHost() != null) { | ||
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(params.getHttpProxyHost(), params.getHttpProxyPort())); | ||
httpClientBuilder.proxy(proxy); | ||
} | ||
|
||
if (params.isTrustAllCerts()) { | ||
TrustAllX509TrustManager trustAllX509TrustManager = new TrustAllX509TrustManager(); | ||
SSLContext sslContext = SSLContext.getInstance("TLS"); | ||
TrustManager[] trustManagers = new TrustManager[]{trustAllX509TrustManager}; | ||
sslContext.init(null, trustManagers, new java.security.SecureRandom()); | ||
httpClientBuilder.sslSocketFactory(sslContext.getSocketFactory(), trustAllX509TrustManager); | ||
|
||
httpClientBuilder.hostnameVerifier(new HostnameVerifier() { | ||
@Override | ||
public boolean verify(String hostname, SSLSession session) { | ||
return true; | ||
} | ||
}); | ||
} | ||
|
||
this.httpClient = httpClientBuilder.build(); | ||
} | ||
|
||
public void httpAuthenticate(String authEmail, String authPasswd) { | ||
String authUrl = baseUrl + "/authentification"; | ||
|
||
FormBody authReqBody = new FormBody.Builder() | ||
.addEncoded("email", authEmail) | ||
.addEncoded("passwd", authPasswd) | ||
.addEncoded("back", "my-account") | ||
.addEncoded("SubmitLogin", "") | ||
.build(); | ||
|
||
Request authRequest = new Request.Builder() | ||
.url(authUrl) | ||
.header("Content-Type", "application/x-www-form-urlencoded") | ||
.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") | ||
.header("Cache-Control", "max-age=0") | ||
.header("Sec-Fetch-Dest", "document") | ||
.header("Sec-Fetch-Mode", "navigate") | ||
.header("Sec-Fetch-Site", "same-origin") | ||
.header("Sec-Fetch-User", "?1") | ||
.header("Upgrade-Insecure-Requests", "1") | ||
.header("Origin", baseUrl) | ||
.header("Referer", baseUrl + "/authentification?back=my-account") | ||
.post(authReqBody) | ||
.build(); | ||
try (Response response = httpClient.newCall(authRequest).execute()) { | ||
if (response.isRedirect() || response.isSuccessful()) { | ||
log.info("authenticated.."); | ||
} else { | ||
throw new RuntimeException("Failed to authenticate"); | ||
} | ||
} catch(Exception ex) { | ||
throw new RuntimeException("Failed", ex); | ||
} | ||
} | ||
|
||
public String downloadBooksHtmlPage() { | ||
String url = baseUrl + PATH_DMDPDFSTAMPER_PDFS; | ||
log.info("scanBooksPage: http query " + url); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.header("Accept", "*/*") | ||
// .header("Sec-Fetch-Mode", "no-cors") | ||
// .header("Sec-Fetch-Site", "same-origin") | ||
// .header("Referer", baseUrl + "/module/dmdpdfstamper/read?id_order_detail=" + orderDetail) | ||
.build(); | ||
String respContent; | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
ResponseBody respBody = response.body(); | ||
respContent = respBody.string(); | ||
} else { | ||
throw new RuntimeException("Failed to get book config: " + url); | ||
} | ||
} catch(Exception ex) { | ||
throw new RuntimeException("Failed", ex); | ||
} | ||
return respContent; | ||
} | ||
|
||
public String downloadBookConfig(String bookTitle, String orderDetail) { | ||
String url = baseUrl + PATH_FLIPBBOOKS + bookTitle + PATH_MOBILE_JAVASCRIPT_CONFIGJS; | ||
log.info("http query " + url); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.header("Accept", "*/*") | ||
.header("Sec-Fetch-Mode", "no-cors") | ||
.header("Sec-Fetch-Site", "same-origin") | ||
.header("Referer", baseUrl + "/module/dmdpdfstamper/read?id_order_detail=" + orderDetail) | ||
.build(); | ||
String respContent; | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
ResponseBody respBody = response.body(); | ||
respContent = respBody.string(); | ||
} else { | ||
throw new RuntimeException("Failed to get book config: " + url); | ||
} | ||
} catch(Exception ex) { | ||
throw new RuntimeException("Failed", ex); | ||
} | ||
return respContent; | ||
} | ||
|
||
public byte[] httpDownloadPageImage(DownloadBookCtx ctx, String imageName) { | ||
String url = baseUrl + PATH_FLIPBBOOKS + ctx.bookTitle + PATH_FILES_MOBILE + imageName +"?" + ctx.bookConfig.getCreatedTime(); | ||
log.info("download image " + imageName + ": http query " + url); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.header("Accept", "image/*") | ||
.header("Sec-Fetch-Dest", "image") | ||
.header("Sec-Fetch-Mode", "no-cors") | ||
.header("Sec-Fetch-Site", "same-origin") | ||
.header("Referer", baseUrl + "/module/dmdpdfstamper/read?id_order_detail=" + ctx.orderDetail) | ||
.build(); | ||
byte[] pageImgContent; | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
ResponseBody respBody = response.body(); | ||
pageImgContent = respBody.bytes(); | ||
if (pageImgContent.length == 0) { | ||
throw new RuntimeException("Failed to download image " + imageName + " - empty image!"); | ||
} | ||
} else { | ||
throw new RuntimeException("Failed to download image " + imageName + " " + response.code()); | ||
} | ||
} catch(Exception ex) { | ||
throw new RuntimeException("Failed", ex); | ||
} | ||
return pageImgContent; | ||
} | ||
|
||
public String downloadHtml(String url, String orderDetailId) { | ||
log.info("http query " + url); | ||
Request request = new Request.Builder() | ||
.url(url) | ||
.header("Accept", "*/*") | ||
.header("Sec-Fetch-Mode", "no-cors") | ||
.header("Sec-Fetch-Site", "same-origin") | ||
.header("Referer", baseUrl + "/module/dmdpdfstamper/read?id_order_detail=" + orderDetailId) | ||
.build(); | ||
String respContent; | ||
try (Response response = httpClient.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
ResponseBody respBody = response.body(); | ||
respContent = respBody.string(); | ||
} else { | ||
throw new RuntimeException("Failed to get url: " + url); | ||
} | ||
} catch(Exception ex) { | ||
throw new RuntimeException("Failed", ex); | ||
} | ||
return respContent; | ||
|
||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/OrderBookInfo.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,12 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import lombok.AllArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
public class OrderBookInfo { | ||
|
||
public final String orderDetailId; | ||
public final String label; | ||
public final String bookTitle; | ||
|
||
} |
43 changes: 43 additions & 0 deletions
43
http-flipbook-to-pdf/src/main/java/fr/an/tools/flipbook2pdf/impl/PdfBookWriter.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,43 @@ | ||
package fr.an.tools.flipbook2pdf.impl; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
|
||
import org.apache.commons.io.FileUtils; | ||
|
||
import com.itextpdf.text.Document; | ||
import com.itextpdf.text.Image; | ||
import com.itextpdf.text.Rectangle; | ||
import com.itextpdf.text.pdf.PdfWriter; | ||
|
||
public class PdfBookWriter { | ||
|
||
|
||
public void writePdf(BookConfig bookConfig, File imgDir, | ||
File outputFile) throws Exception { | ||
Document document = new Document(); | ||
try { | ||
PdfWriter.getInstance(document, new BufferedOutputStream(new FileOutputStream(outputFile))); | ||
document.open(); | ||
document.setPageCount(bookConfig.totalPageCount); | ||
final int width = bookConfig.largePageWidth; | ||
final int height = bookConfig.largePageHeight; | ||
document.setPageSize(new Rectangle(width, height)); | ||
|
||
for(int page = 1; page <= bookConfig.totalPageCount; page++) { | ||
String imageName = page + ".jpg"; | ||
File imageFile = new File(imgDir, imageName); | ||
byte[] imgBytes = FileUtils.readFileToByteArray(imageFile); | ||
|
||
Image img = Image.getInstance(imgBytes); | ||
document.add(img); | ||
|
||
document.newPage(); | ||
} | ||
|
||
} finally { | ||
document.close(); | ||
} | ||
} | ||
} |
Oops, something went wrong.