FontConfigImpl.java
5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.jcr.api.SlingRepository
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.rewriter.xml.fop;
import com.day.cq.rewriter.xml.fop.FontConfig;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Component
@Service
public class FontConfigImpl
implements FontConfig {
private final Logger logger;
public static final String TMP_DIR = System.getProperty("java.io.tmpdir");
private static final String FONT_CONFIG_ROOT = "/libs/wcm/core/content/pdf";
private static final String FONT_CONFIG_FONTS = "/libs/wcm/core/content/pdf/fonts";
public static final String FONT_CONFIG = "/libs/wcm/core/content/pdf/foconfig.xml";
private static final String FONTCONFIG_SERVICE = "fontconfig-service";
@Reference
private SlingRepository repo;
private File fontsDir;
private File configFile;
public FontConfigImpl() {
this.logger = LoggerFactory.getLogger(this.getClass());
}
@Override
public File getFontConfigDirectory() {
return this.fontsDir;
}
@Override
public File getUserConfigFile() {
return this.configFile;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
protected void activate(ComponentContext ctx) {
if (TMP_DIR != null) {
File tmpDir = new File(TMP_DIR);
this.fontsDir = new File(tmpDir, "fonts." + UUID.randomUUID());
this.fontsDir.mkdir();
this.fontsDir.deleteOnExit();
Session session = null;
try {
session = this.repo.loginService("fontconfig-service", null);
if (session.nodeExists("/libs/wcm/core/content/pdf/foconfig.xml")) {
this.configFile = this.createFile(session.getNode("/libs/wcm/core/content/pdf/foconfig.xml"));
}
if (session.nodeExists("/libs/wcm/core/content/pdf/fonts")) {
Node fonts = session.getNode("/libs/wcm/core/content/pdf/fonts");
NodeIterator children = fonts.getNodes();
while (children.hasNext()) {
this.createFile((Node)children.next());
}
}
}
catch (RepositoryException e) {
this.logger.error("Failed exporting FOP font data.", (Throwable)e);
}
finally {
if (session != null) {
session.logout();
}
}
}
}
protected void deactivate(ComponentContext ctx) {
this.deleteFile(this.fontsDir);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private File createFile(Node node) throws RepositoryException {
if (node.hasProperty("jcr:content/jcr:data")) {
Property data = node.getProperty("jcr:content/jcr:data");
Binary binary = data.getBinary();
FileOutputStream out = null;
InputStream in = null;
try {
File result = new File(this.fontsDir, node.getName());
result.createNewFile();
result.deleteOnExit();
in = binary.getStream();
out = new FileOutputStream(result);
IOUtils.copy((InputStream)in, (OutputStream)out);
File file = result;
return file;
}
catch (IOException e) {
this.logger.error("Failed exporting FOP font data.", (Throwable)e);
}
finally {
IOUtils.closeQuietly((InputStream)in);
IOUtils.closeQuietly((OutputStream)out);
binary.dispose();
}
}
return null;
}
private void deleteFile(File f) {
if (f != null) {
File[] children = f.listFiles();
if (children != null) {
for (File child : children) {
this.deleteFile(child);
}
}
f.delete();
}
}
protected void bindRepo(SlingRepository slingRepository) {
this.repo = slingRepository;
}
protected void unbindRepo(SlingRepository slingRepository) {
if (this.repo == slingRepository) {
this.repo = null;
}
}
}