From 549996ff7e737081017d6c793ec5ee833b8e10b2 Mon Sep 17 00:00:00 2001 From: Federico Soldani Date: Wed, 22 Feb 2012 11:10:46 +0100 Subject: [PATCH 1/3] Add cookies for all requests --- .../pdf/ITextCookieUserAgent.java | 58 +++++++++++++++++++ .../org/xhtmlrenderer/pdf/ITextRenderer.java | 15 +++-- 2 files changed, 69 insertions(+), 4 deletions(-) create mode 100644 src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java diff --git a/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java b/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java new file mode 100644 index 000000000..7e7b01633 --- /dev/null +++ b/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java @@ -0,0 +1,58 @@ +package org.xhtmlrenderer.pdf; + +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import org.xhtmlrenderer.util.XRLog; + +public class ITextCookieUserAgent extends ITextUserAgent { + + private String cookie = ""; + private String fs_baseURL = null; + + public ITextCookieUserAgent(ITextOutputDevice outputDevice, String cookies, String baseURL) { + super(outputDevice); + this.cookie = cookies; + this.fs_baseURL = baseURL; + this.setBaseURL(baseURL); + } + + //TOdO:implement this with nio. + protected InputStream resolveAndOpenStream(String uri) { + java.io.InputStream is = null; + uri = resolveURI(uri); + try { + URLConnection connection = new URL(uri).openConnection(); + connection.setRequestProperty("Cookie", this.cookie); + is = connection.getInputStream(); + } catch (java.net.MalformedURLException e) { + XRLog.exception("bad URL given: " + uri, e); + } catch (java.io.FileNotFoundException e) { + XRLog.exception("item at URI " + uri + " not found"); + } catch (java.io.IOException e) { + XRLog.exception("IO problem for " + uri, e); + } + return is; + } + + /** + * Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for + * the agent. + * + * @param uri A URI, possibly relative. + * + * @return A URI as String, resolved, or null if there was an exception (for example if the URI is malformed). + */ + public String resolveURI(String uri) { + if (uri == null) return null; + + if(this.getBaseURL() == null && fs_baseURL != null) { + this.setBaseURL(fs_baseURL); + } + + // Call Super + return super.resolveURI(uri); + } + +} \ No newline at end of file diff --git a/src/java/org/xhtmlrenderer/pdf/ITextRenderer.java b/src/java/org/xhtmlrenderer/pdf/ITextRenderer.java index d60be3ba0..faa3e86ee 100644 --- a/src/java/org/xhtmlrenderer/pdf/ITextRenderer.java +++ b/src/java/org/xhtmlrenderer/pdf/ITextRenderer.java @@ -103,15 +103,22 @@ public ITextRenderer() { this(DEFAULT_DOTS_PER_POINT, DEFAULT_DOTS_PER_PIXEL); } + public ITextRenderer(String cookie, String baseURL) { + this(DEFAULT_DOTS_PER_POINT, DEFAULT_DOTS_PER_PIXEL, cookie, baseURL); + } + public ITextRenderer(float dotsPerPoint, int dotsPerPixel) { + this(dotsPerPoint, dotsPerPixel, "", null); + } + + public ITextRenderer(float dotsPerPoint, int dotsPerPixel, String cookies, String baseURL) { _dotsPerPoint = dotsPerPoint; _outputDevice = new ITextOutputDevice(_dotsPerPoint); - ITextUserAgent userAgent = new ITextUserAgent(_outputDevice); - _sharedContext = new SharedContext(); - _sharedContext.setUserAgentCallback(userAgent); - _sharedContext.setCss(new StyleReference(userAgent)); + ITextCookieUserAgent userAgent = new ITextCookieUserAgent(_outputDevice, cookies, baseURL); + _sharedContext = new SharedContext(userAgent); + _sharedContext.setBaseURL(baseURL); userAgent.setSharedContext(_sharedContext); _outputDevice.setSharedContext(_sharedContext); From ed4c371f0b8471d47e9fa0f7833f7b550e54a4d0 Mon Sep 17 00:00:00 2001 From: Federico Soldani Date: Fri, 22 Mar 2013 12:00:50 +0100 Subject: [PATCH 2/3] merge from main --- flying-saucer-pdf/pom.xml | 26 +++++++++ .../org/xhtmlrenderer/pdf/ITextRenderer.java | 8 ++- pom.xml | 23 ++++++++ .../pdf/ITextCookieUserAgent.java | 58 ------------------- 4 files changed, 54 insertions(+), 61 deletions(-) delete mode 100644 src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java diff --git a/flying-saucer-pdf/pom.xml b/flying-saucer-pdf/pom.xml index 977661607..8a17a414f 100644 --- a/flying-saucer-pdf/pom.xml +++ b/flying-saucer-pdf/pom.xml @@ -28,4 +28,30 @@ ${project.version} + + + + + org.apache.maven.plugins + maven-shade-plugin + + ${project.artifactId}-${project.version}-shaded + + + org.xhtmlrenderer:flying-saucer-core + org.xhtmlrenderer:flying-saucer-log4j + + + + + + package + + shade + + + + + + diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java index 99b206c67..a46f22fc9 100644 --- a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextRenderer.java @@ -106,13 +106,15 @@ public ITextRenderer(float dotsPerPoint, int dotsPerPixel) { this(dotsPerPoint, dotsPerPixel, "", null); } - public ITextRenderer(float dotsPerPoint, int dotsPerPixel, String cookies, String baseURL) { + public ITextRenderer(float dotsPerPoint, int dotsPerPixel, String cookie, String baseURL) { _dotsPerPoint = dotsPerPoint; _outputDevice = new ITextOutputDevice(_dotsPerPoint); - ITextCookieUserAgent userAgent = new ITextCookieUserAgent(_outputDevice, cookies, baseURL); - _sharedContext = new SharedContext(userAgent); + ITextUserAgent userAgent = (null != cookie && cookie.trim().length() > 0) ? new ITextCookieUserAgent(_outputDevice, cookie, baseURL) : new ITextUserAgent(_outputDevice); + _sharedContext = new SharedContext(); + _sharedContext.setUserAgentCallback(userAgent); + _sharedContext.setCss(new StyleReference(userAgent)); _sharedContext.setBaseURL(baseURL); userAgent.setSharedContext(_sharedContext); _outputDevice.setSharedContext(_sharedContext); diff --git a/pom.xml b/pom.xml index d408d8f61..95d679cc3 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,29 @@ + + + org.apache.maven.plugins + maven-shade-plugin + + ${project.artifactId}-shaded + + + flying-saucer-core + flying-saucer-pdf + flying-saucer-log4j + + + + + + package + + shade + + + + diff --git a/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java b/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java deleted file mode 100644 index 7e7b01633..000000000 --- a/src/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.xhtmlrenderer.pdf; - -import java.io.InputStream; -import java.net.URL; -import java.net.URLConnection; - -import org.xhtmlrenderer.util.XRLog; - -public class ITextCookieUserAgent extends ITextUserAgent { - - private String cookie = ""; - private String fs_baseURL = null; - - public ITextCookieUserAgent(ITextOutputDevice outputDevice, String cookies, String baseURL) { - super(outputDevice); - this.cookie = cookies; - this.fs_baseURL = baseURL; - this.setBaseURL(baseURL); - } - - //TOdO:implement this with nio. - protected InputStream resolveAndOpenStream(String uri) { - java.io.InputStream is = null; - uri = resolveURI(uri); - try { - URLConnection connection = new URL(uri).openConnection(); - connection.setRequestProperty("Cookie", this.cookie); - is = connection.getInputStream(); - } catch (java.net.MalformedURLException e) { - XRLog.exception("bad URL given: " + uri, e); - } catch (java.io.FileNotFoundException e) { - XRLog.exception("item at URI " + uri + " not found"); - } catch (java.io.IOException e) { - XRLog.exception("IO problem for " + uri, e); - } - return is; - } - - /** - * Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for - * the agent. - * - * @param uri A URI, possibly relative. - * - * @return A URI as String, resolved, or null if there was an exception (for example if the URI is malformed). - */ - public String resolveURI(String uri) { - if (uri == null) return null; - - if(this.getBaseURL() == null && fs_baseURL != null) { - this.setBaseURL(fs_baseURL); - } - - // Call Super - return super.resolveURI(uri); - } - -} \ No newline at end of file From 65c642b93347e679e452becf27473478aadea741 Mon Sep 17 00:00:00 2001 From: Federico Soldani Date: Fri, 22 Mar 2013 12:03:04 +0100 Subject: [PATCH 3/3] Merge from main repository --- .../pdf/ITextCookieUserAgent.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java diff --git a/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java new file mode 100644 index 000000000..7e7b01633 --- /dev/null +++ b/flying-saucer-pdf/src/main/java/org/xhtmlrenderer/pdf/ITextCookieUserAgent.java @@ -0,0 +1,58 @@ +package org.xhtmlrenderer.pdf; + +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import org.xhtmlrenderer.util.XRLog; + +public class ITextCookieUserAgent extends ITextUserAgent { + + private String cookie = ""; + private String fs_baseURL = null; + + public ITextCookieUserAgent(ITextOutputDevice outputDevice, String cookies, String baseURL) { + super(outputDevice); + this.cookie = cookies; + this.fs_baseURL = baseURL; + this.setBaseURL(baseURL); + } + + //TOdO:implement this with nio. + protected InputStream resolveAndOpenStream(String uri) { + java.io.InputStream is = null; + uri = resolveURI(uri); + try { + URLConnection connection = new URL(uri).openConnection(); + connection.setRequestProperty("Cookie", this.cookie); + is = connection.getInputStream(); + } catch (java.net.MalformedURLException e) { + XRLog.exception("bad URL given: " + uri, e); + } catch (java.io.FileNotFoundException e) { + XRLog.exception("item at URI " + uri + " not found"); + } catch (java.io.IOException e) { + XRLog.exception("IO problem for " + uri, e); + } + return is; + } + + /** + * Resolves the URI; if absolute, leaves as is, if relative, returns an absolute URI based on the baseUrl for + * the agent. + * + * @param uri A URI, possibly relative. + * + * @return A URI as String, resolved, or null if there was an exception (for example if the URI is malformed). + */ + public String resolveURI(String uri) { + if (uri == null) return null; + + if(this.getBaseURL() == null && fs_baseURL != null) { + this.setBaseURL(fs_baseURL); + } + + // Call Super + return super.resolveURI(uri); + } + +} \ No newline at end of file