Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cookies for internal requests #32

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions flying-saucer-pdf/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,30 @@
<version>${project.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}-${project.version}-shaded</finalName>
<artifactSet>
<includes>
<include>org.xhtmlrenderer:flying-saucer-core</include>
<include>org.xhtmlrenderer:flying-saucer-log4j</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,24 @@ 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 cookie, String baseURL) {
_dotsPerPoint = dotsPerPoint;

_outputDevice = new ITextOutputDevice(_dotsPerPoint);

ITextUserAgent userAgent = new ITextUserAgent(_outputDevice);
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);

Expand Down
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,29 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<configuration>
<finalName>${project.artifactId}-shaded</finalName>
<artifactSet>
<includes>
<include>flying-saucer-core</include>
<include>flying-saucer-pdf</include>
<include>flying-saucer-log4j</include>
</includes>
</artifactSet>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down