PersistingFOPNGSerializerFactory.java
6.04 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.commons.io.IOUtils
* org.apache.felix.scr.annotations.Component
* org.apache.felix.scr.annotations.Property
* org.apache.felix.scr.annotations.Reference
* org.apache.felix.scr.annotations.Service
* org.apache.sling.api.SlingHttpServletRequest
* org.apache.sling.api.resource.Resource
* org.apache.sling.api.resource.ResourceResolver
* org.apache.sling.commons.osgi.OsgiUtil
* org.apache.sling.rewriter.ProcessingComponentConfiguration
* org.apache.sling.rewriter.ProcessingContext
* org.apache.sling.rewriter.Serializer
* org.apache.sling.rewriter.SerializerFactory
* org.osgi.service.component.ComponentContext
* org.slf4j.Logger
*/
package com.day.cq.rewriter.xml.fop;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.rewriter.xml.fop.FOPNGSerializer;
import com.day.cq.rewriter.xml.fop.FontConfig;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.Dictionary;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.commons.osgi.OsgiUtil;
import org.apache.sling.rewriter.ProcessingComponentConfiguration;
import org.apache.sling.rewriter.ProcessingContext;
import org.apache.sling.rewriter.Serializer;
import org.apache.sling.rewriter.SerializerFactory;
import org.osgi.service.component.ComponentContext;
import org.slf4j.Logger;
import org.xml.sax.SAXException;
@Component
@Service(value={SerializerFactory.class})
@Property(name="pipeline.type", value={"persisting-fop"})
public class PersistingFOPNGSerializerFactory
implements SerializerFactory {
private static final String DEFAULT_STORE_PATH = "/var/dam/docs/documents";
@Property
private static final String PROP_STORE_PATH = "store.path";
private String storePath;
@Reference
private FontConfig fc;
protected void activate(ComponentContext ctx) {
this.storePath = OsgiUtil.toString(ctx.getProperties().get("store.path"), (String)"/var/dam/docs/documents");
if (!this.storePath.endsWith("/")) {
this.storePath = this.storePath + '/';
}
}
public Serializer createSerializer() {
PersistingFOPNGSerializer serializer = new PersistingFOPNGSerializer(this.storePath);
serializer.setFontConfig(this.fc);
return serializer;
}
protected void bindFc(FontConfig fontConfig) {
this.fc = fontConfig;
}
protected void unbindFc(FontConfig fontConfig) {
if (this.fc == fontConfig) {
this.fc = null;
}
}
public static final class PersistingFOPNGSerializer
extends FOPNGSerializer {
private final String storePath;
private SlingHttpServletRequest request;
private String pdfName;
private static final String PDF_NAME_PARAM = "filename";
public PersistingFOPNGSerializer(String path) {
this.storePath = path;
}
@Override
public void init(ProcessingContext context, ProcessingComponentConfiguration config) throws IOException {
super.init(context, config);
this.request = context.getRequest();
String param = this.request.getParameter("filename");
String name = param != null && !"".equals(param) ? param : this.request.getResource().getPath();
this.pdfName = name.startsWith("/") ? this.storePath + name.substring(1) : this.storePath + name;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
@Override
public void endDocument() throws SAXException {
super.endDocument();
this.logger.debug("Persisting pdf at {}", (Object)this.pdfName);
Session session = (Session)this.request.getResourceResolver().adaptTo(Session.class);
if (session != null) {
ByteArrayInputStream is = null;
try {
JcrUtil.createPath((String)this.pdfName, (boolean)false, (String)"{http://www.jcp.org/jcr/nt/1.0}folder", (String)"{http://www.jcp.org/jcr/nt/1.0}file", (Session)session, (boolean)false);
Node content = JcrUtil.createPath((String)(this.pdfName + "/jcr:content"), (boolean)false, (String)"{http://www.jcp.org/jcr/nt/1.0}folder", (String)"{http://www.jcp.org/jcr/nt/1.0}unstructured", (Session)session, (boolean)false);
is = new ByteArrayInputStream(this.cachingOS.toByteArray());
Binary binary = session.getValueFactory().createBinary((InputStream)is);
content.setProperty("jcr:data", binary);
content.setProperty("{http://www.jcp.org/jcr/1.0}mimeType", "application/pdf");
content.setProperty("{http://www.jcp.org/jcr/1.0}lastModified", Calendar.getInstance());
session.save();
this.logger.debug("The pdf node has been created at {}" + this.pdfName);
}
catch (RepositoryException e) {
this.logger.error("Error while storing pdf at " + this.pdfName, (Throwable)e);
}
finally {
IOUtils.closeQuietly((InputStream)is);
}
}
this.logger.error("Unable to persist PDF at {} : no session available.", (Object)this.pdfName);
}
}
}