comparison software/mpdl-services/mpiwg-mpdl-cms/src/de/mpg/mpiwg/berlin/mpdl/cms/document/PdfHandlerUserAgent.java @ 23:e845310098ba

diverse Korrekturen
author Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de>
date Tue, 27 Nov 2012 12:35:19 +0100
parents
children
comparison
equal deleted inserted replaced
22:6a45a982c333 23:e845310098ba
1 package de.mpg.mpiwg.berlin.mpdl.cms.document;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.logging.Logger;
7
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 PdfHandlerUserAgent 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(PdfHandlerUserAgent.class.getName());
23 private SharedContext sharedContext;
24 private ITextOutputDevice outputDevice;
25
26 public PdfHandlerUserAgent() {
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.severe("Can't get image file: unexpected problem for URI: '" + uri + "': " + e.getMessage());
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.severe("first retry to get image for URL '" + url.toString() + "': " + e.getMessage());
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.severe("second retry to get image for URL '" + url.toString() + "': " + e.getMessage());
107 try {
108 image = Image.getInstance(url);
109 } catch (Exception e3) {
110 LOGGER.severe("third retry to get image for URL '" + url.toString() + "': " + e.getMessage());
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.severe("first retry to open stream for URL '" + uri + "': " + e.getMessage());
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.severe("second retry to open stream for URL '" + uri + "': " + e.getMessage());
139 try {
140 is = new URL(uri).openStream();
141 } catch (Exception e3) {
142 LOGGER.severe("third retry to open stream for URL '" + uri + "': " + e.getMessage());
143 return null;
144 }
145 }
146 }
147 return is;
148 }
149 }