comparison software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/MpdlITextUserAgent.java @ 0:408254cf2f1d

Erstellung
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Wed, 24 Nov 2010 17:24:23 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:408254cf2f1d
1 package de.mpg.mpiwg.berlin.mpdl.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6
7 import org.apache.log4j.Logger;
8 import org.xhtmlrenderer.layout.SharedContext;
9 import org.xhtmlrenderer.pdf.ITextFSImage;
10 import org.xhtmlrenderer.pdf.ITextOutputDevice;
11 import org.xhtmlrenderer.pdf.PDFAsImage;
12 import org.xhtmlrenderer.resource.ImageResource;
13 import org.xhtmlrenderer.swing.NaiveUserAgent;
14
15 import com.lowagie.text.Image;
16 import com.lowagie.text.Rectangle;
17 import com.lowagie.text.pdf.PdfReader;
18
19 public class MpdlITextUserAgent extends NaiveUserAgent {
20 private static final int IMAGE_CACHE_CAPACITY = 32;
21 private static final float DEFAULT_DOTS_PER_POINT = 20f * 4f / 3f;
22 private static Logger LOGGER = Logger.getLogger(MpdlITextUserAgent.class); // Logs to EXIST_HOME/webapp/WEB-INF/logs/exist.log
23 private SharedContext sharedContext;
24 private ITextOutputDevice outputDevice;
25
26 public MpdlITextUserAgent() {
27 super(IMAGE_CACHE_CAPACITY);
28 outputDevice = new ITextOutputDevice(DEFAULT_DOTS_PER_POINT);
29 }
30
31 @SuppressWarnings("unchecked")
32 public ImageResource getImageResource(String inputUri) {
33 ImageResource resource = null;
34 String uri = resolveURI(inputUri);
35 resource = (ImageResource) _imageCache.get(uri);
36 if (resource == null) {
37 InputStream is = resolveAndOpenStream(uri);
38 if (is != null) {
39 try {
40 URL url = new URL(uri);
41 if (url.getPath() != null && url.getPath().toLowerCase().endsWith(".pdf")) {
42 PdfReader reader = outputDevice.getReader(url);
43 PDFAsImage image = new PDFAsImage(url);
44 Rectangle rect = reader.getPageSizeWithRotation(1);
45 image.setInitialWidth(rect.getWidth()*outputDevice.getDotsPerPoint());
46 image.setInitialHeight(rect.getHeight()*outputDevice.getDotsPerPoint());
47 resource = new ImageResource(image);
48 } else {
49 Image image = getImage(url);
50 if (image == null)
51 return null;
52 scaleToOutputResolution(image);
53 resource = new ImageResource(new ITextFSImage(image));
54 }
55 _imageCache.put(uri, resource);
56 } catch (IOException e) {
57 LOGGER.error("Can't get image file: unexpected problem for URI: '" + uri + "': " + e.getMessage(), e);
58 } finally {
59 try {
60 if (is != null)
61 is.close();
62 } catch (IOException e) {
63 // ignore
64 }
65 }
66 }
67 }
68 if (resource == null) {
69 resource = new ImageResource(null);
70 }
71 return resource;
72 }
73
74 private void scaleToOutputResolution(Image image) {
75 float factor = sharedContext.getDotsPerPixel();
76 image.scaleAbsolute(image.getPlainWidth() * factor, image.getPlainHeight() * factor);
77 }
78
79 public SharedContext getSharedContext() {
80 return sharedContext;
81 }
82
83 public void setSharedContext(SharedContext sharedContext) {
84 this.sharedContext = sharedContext;
85 }
86
87 private Image getImage(URL url) {
88 Image image = null;
89 try {
90 image = Image.getInstance(url);
91 } catch (Exception e) {
92 try {
93 Thread.sleep(1000);
94 } catch (InterruptedException ee) {
95 // nothing
96 }
97 LOGGER.error("first retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e);
98 try {
99 image = Image.getInstance(url);
100 } catch (Exception e2) {
101 try {
102 Thread.sleep(1000);
103 } catch (InterruptedException ee) {
104 // nothing
105 }
106 LOGGER.error("second retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e);
107 try {
108 image = Image.getInstance(url);
109 } catch (Exception e3) {
110 LOGGER.error("third retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e);
111 return null;
112 }
113 }
114 }
115 return image;
116 }
117
118 protected InputStream resolveAndOpenStream(String inputUri) {
119 InputStream is = null;
120 String uri = resolveURI(inputUri);
121 try {
122 is = new URL(uri).openStream();
123 } catch (Exception e) {
124 try {
125 Thread.sleep(1000);
126 } catch (InterruptedException ee) {
127 // nothing
128 }
129 LOGGER.error("first retry to open stream for URL '" + uri + "': " + e.getMessage(), e);
130 try {
131 is = new URL(uri).openStream();
132 } catch (Exception e2) {
133 try {
134 Thread.sleep(1000);
135 } catch (InterruptedException ee) {
136 // nothing
137 }
138 LOGGER.error("second retry to open stream for URL '" + uri + "': " + e.getMessage(), e);
139 try {
140 is = new URL(uri).openStream();
141 } catch (Exception e3) {
142 LOGGER.error("third retry to open stream for URL '" + uri + "': " + e.getMessage(), e);
143 return null;
144 }
145 }
146 }
147 return is;
148 }
149 }