Skip to content

Commit 4b5faa5

Browse files
committed
[bugfix] WebDAV API must consider seralization properties when determining Content-Length
1 parent deb713e commit 4b5faa5

File tree

7 files changed

+46
-49
lines changed

7 files changed

+46
-49
lines changed

extensions/webdav/src/main/java/org/exist/webdav/ExistCollection.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.exist.webdav.exceptions.CollectionDoesNotExistException;
4141
import org.exist.webdav.exceptions.CollectionExistsException;
4242
import org.exist.xmldb.XmldbURI;
43-
import org.xml.sax.InputSource;
4443
import org.xml.sax.SAXException;
4544

4645
import java.io.IOException;
@@ -51,6 +50,7 @@
5150
import java.util.Iterator;
5251
import java.util.List;
5352
import java.util.Optional;
53+
import java.util.Properties;
5454

5555
/**
5656
* Class for accessing the Collection class of the exist-db native API.
@@ -65,13 +65,14 @@ public class ExistCollection extends ExistResource {
6565
* @param uri URI of document
6666
* @param pool Reference to brokerpool
6767
*/
68-
public ExistCollection(XmldbURI uri, BrokerPool pool) {
68+
public ExistCollection(final Properties configuration, final XmldbURI uri, final BrokerPool pool) {
69+
super(configuration);
6970

7071
if (LOG.isTraceEnabled()) {
7172
LOG.trace("New collection object for {}", uri);
7273
}
7374

74-
brokerPool = pool;
75+
this.brokerPool = pool;
7576
this.xmldbUri = uri;
7677
}
7778

extensions/webdav/src/main/java/org/exist/webdav/ExistDocument.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.io.Writer;
5252
import java.net.URISyntaxException;
5353
import java.util.Optional;
54+
import java.util.Properties;
5455

5556
import static java.nio.charset.StandardCharsets.UTF_8;
5657

@@ -71,7 +72,8 @@ public class ExistDocument extends ExistResource {
7172
* @param uri URI of document
7273
* @param pool Reference to brokerpool
7374
*/
74-
public ExistDocument(XmldbURI uri, BrokerPool pool) {
75+
public ExistDocument(final Properties configuration, final XmldbURI uri, final BrokerPool pool) {
76+
super(configuration);
7577

7678
if (LOG.isTraceEnabled()) {
7779
LOG.trace("New document object for {}", uri);
@@ -169,7 +171,10 @@ public void stream(OutputStream os) throws IOException, PermissionDeniedExceptio
169171
// Stream XML document
170172

171173
try {
172-
serialize(broker, document, os);
174+
// Set custom serialization options when available
175+
final Properties properties = configuration.isEmpty() ? new Properties() : configuration;
176+
177+
serialize(broker, properties, document, os);
173178
os.flush();
174179
} catch (SAXException e) {
175180
LOG.error(e);
@@ -198,20 +203,18 @@ public void stream(OutputStream os) throws IOException, PermissionDeniedExceptio
198203

199204
}
200205

201-
private void serialize(final DBBroker broker, final DocumentImpl document, final OutputStream os) throws SAXException, IOException {
206+
private void serialize(final DBBroker broker, final Properties properties, final DocumentImpl document, final OutputStream os) throws SAXException, IOException {
202207
final Serializer serializer = broker.borrowSerializer();
203-
// Set custom serialization options when available
204-
if (!configuration.isEmpty()) {
205-
serializer.setProperties(configuration);
206-
}
207208

208209
SAXSerializer saxSerializer = null;
209210
try {
211+
serializer.setUser(getUser());
212+
serializer.setProperties(properties);
210213
saxSerializer = (SAXSerializer) SerializerPool.getInstance().borrowObject(SAXSerializer.class);
211214

212215
// Serialize document
213216
try (final Writer writer = new OutputStreamWriter(os, UTF_8)) {
214-
saxSerializer.setOutput(writer, configuration.isEmpty() ? null : configuration);
217+
saxSerializer.setOutput(writer, properties);
215218
serializer.setSAXHandlers(saxSerializer, saxSerializer);
216219

217220
serializer.toSAX(document);

extensions/webdav/src/main/java/org/exist/webdav/ExistResource.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public abstract class ExistResource {
5454
protected String ownerUser;
5555
protected String ownerGroup;
5656

57-
protected Properties configuration = new Properties();
57+
protected final Properties configuration;
58+
59+
public ExistResource(final Properties configuration) {
60+
this.configuration = configuration;
61+
}
5862

5963
abstract void initMetadata();
6064

@@ -98,14 +102,6 @@ public String getOwnerUser() {
98102
return ownerUser;
99103
}
100104

101-
public Properties getSerializationConfiguration() {
102-
return configuration;
103-
}
104-
105-
public void setSerializationConfiguration(Properties config) {
106-
configuration = config;
107-
}
108-
109105
/**
110106
* Authenticate subject with password. NULL is returned when
111107
* the subject could not be authenticated.

extensions/webdav/src/main/java/org/exist/webdav/ExistResourceFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,11 @@ public Resource getResource(String host, String path) {
132132
// Return appropriate resource
133133
switch (getResourceType(brokerPool, xmldbUri)) {
134134
case DOCUMENT:
135-
MiltonDocument doc = new MiltonDocument(host, xmldbUri, brokerPool);
136-
doc.setSerializationConfiguration(webDavOptions);
135+
MiltonDocument doc = new MiltonDocument(webDavOptions, host, xmldbUri, brokerPool);
137136
return doc;
138137

139138
case COLLECTION:
140-
return new MiltonCollection(host, xmldbUri, brokerPool);
139+
return new MiltonCollection(webDavOptions, host, xmldbUri, brokerPool);
141140

142141
case IGNORABLE:
143142
if (LOG.isDebugEnabled()) {

extensions/webdav/src/main/java/org/exist/webdav/MiltonCollection.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,27 @@ public class MiltonCollection extends MiltonResource
5858
* Constructor of representation of a Collection in the Milton framework, without subject information.
5959
* To be called by the resource factory.
6060
*
61+
* @param configuration any configuration properties.
6162
* @param host FQ host name including port number.
6263
* @param uri Path on server indicating path of resource
6364
* @param pool Handle to Exist database.
6465
*/
65-
public MiltonCollection(String host, XmldbURI uri, BrokerPool pool) {
66-
this(host, uri, pool, null);
66+
public MiltonCollection(final Properties configuration, String host, XmldbURI uri, BrokerPool pool) {
67+
this(configuration, host, uri, pool, null);
6768
}
6869

6970
/**
7071
* Constructor of representation of a Document in the Milton framework, with subject information.
7172
* To be called by the resource factory.
7273
*
74+
* @param configuration any configuration properties.
7375
* @param host FQ host name including port number.
7476
* @param uri Path on server indicating path of resource.
7577
* @param subject An Exist operation is performed with Subject. Can be NULL.
7678
* @param pool Handle to Exist database.
7779
*/
78-
public MiltonCollection(String host, XmldbURI uri, BrokerPool pool, Subject subject) {
79-
80-
super();
80+
public MiltonCollection(final Properties configuration, String host, XmldbURI uri, BrokerPool pool, Subject subject) {
81+
super(configuration);
8182

8283
if (LOG.isDebugEnabled()) {
8384
LOG.debug("COLLECTION={}", uri.toString());
@@ -87,7 +88,7 @@ public MiltonCollection(String host, XmldbURI uri, BrokerPool pool, Subject subj
8788
brokerPool = pool;
8889
this.host = host;
8990

90-
existCollection = new ExistCollection(uri, brokerPool);
91+
existCollection = new ExistCollection(configuration, uri, brokerPool);
9192

9293
// store simpler type
9394
existResource = existCollection;
@@ -129,15 +130,15 @@ public Resource child(String childName) {
129130
private List<MiltonCollection> getCollectionResources() {
130131
List<MiltonCollection> allResources = new ArrayList<>();
131132
for (XmldbURI path : existCollection.getCollectionURIs()) {
132-
allResources.add(new MiltonCollection(this.host, path, brokerPool, subject));
133+
allResources.add(new MiltonCollection(configuration, this.host, path, brokerPool, subject));
133134
}
134135
return allResources;
135136
}
136137

137138
private List<MiltonDocument> getDocumentResources() {
138139
List<MiltonDocument> allResources = new ArrayList<>();
139140
for (XmldbURI path : existCollection.getDocumentURIs()) {
140-
MiltonDocument mdoc = new MiltonDocument(this.host, path, brokerPool, subject);
141+
MiltonDocument mdoc = new MiltonDocument(configuration, this.host, path, brokerPool, subject);
141142
// Show (restimated) size for PROPFIND only
142143
mdoc.setIsPropFind(true);
143144
allResources.add(mdoc);
@@ -207,7 +208,7 @@ public CollectionResource createCollection(String name)
207208
CollectionResource collection = null;
208209
try {
209210
XmldbURI collectionURI = existCollection.createCollection(name);
210-
collection = new MiltonCollection(host, collectionURI, brokerPool, subject);
211+
collection = new MiltonCollection(configuration, host, collectionURI, brokerPool, subject);
211212

212213
} catch (PermissionDeniedException ex) {
213214
LOG.debug(ex.getMessage());
@@ -239,7 +240,7 @@ public Resource createNew(String newName, InputStream is, Long length, String co
239240
// submit
240241
XmldbURI resourceURI = existCollection.createFile(newName, is, length, contentType);
241242

242-
resource = new MiltonDocument(host, resourceURI, brokerPool, subject);
243+
resource = new MiltonDocument(configuration, host, resourceURI, brokerPool, subject);
243244

244245
} catch (PermissionDeniedException | CollectionDoesNotExistException | IOException e) {
245246
LOG.debug(e.getMessage());

extensions/webdav/src/main/java/org/exist/webdav/MiltonDocument.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,27 @@ public class MiltonDocument extends MiltonResource
7070
* Constructor of representation of a Document in the Milton framework, without subject information.
7171
* To be called by the resource factory.
7272
*
73+
* @param configuration any configuration properties.
7374
* @param host FQ host name including port number.
7475
* @param uri Path on server indicating path of resource
7576
* @param brokerPool Handle to Exist database.
7677
*/
77-
public MiltonDocument(String host, XmldbURI uri, BrokerPool brokerPool) {
78-
this(host, uri, brokerPool, null);
78+
public MiltonDocument(final Properties configuration, String host, XmldbURI uri, BrokerPool brokerPool) {
79+
this(configuration, host, uri, brokerPool, null);
7980
}
8081

8182
/**
8283
* Constructor of representation of a Document in the Milton framework, with subject information.
8384
* To be called by the resource factory.
8485
*
86+
* @param configuration any configuration properties.
8587
* @param host FQ host name including port number.
8688
* @param uri Path on server indicating path of resource.
8789
* @param subject An Exist operation is performed with User. Can be NULL.
8890
* @param pool Handle to Exist database.
8991
*/
90-
public MiltonDocument(String host, XmldbURI uri, BrokerPool pool, Subject subject) {
91-
92-
super();
92+
public MiltonDocument(final Properties configuration, String host, XmldbURI uri, BrokerPool pool, Subject subject) {
93+
super(configuration);
9394

9495
if (userAgentHelper == null) {
9596
userAgentHelper = new DefaultUserAgentHelper();
@@ -103,7 +104,7 @@ public MiltonDocument(String host, XmldbURI uri, BrokerPool pool, Subject subjec
103104
brokerPool = pool;
104105
this.host = host;
105106

106-
existDocument = new ExistDocument(uri, brokerPool);
107+
existDocument = new ExistDocument(configuration, uri, brokerPool);
107108

108109
// store simpler type
109110
existResource = existDocument;
@@ -484,14 +485,5 @@ public void writeXML(XMLStreamWriter writer) throws XMLStreamException {
484485
* StAX serializer
485486
* ================ */
486487

487-
/**
488-
* Set specific WebDAV serialization options
489-
*
490-
* @param config XML serialization options
491-
*/
492-
public void setSerializationConfiguration(Properties config) {
493-
existDocument.setSerializationConfiguration(config);
494-
}
495-
496488
private enum SIZE_METHOD {NULL, EXACT, APPROXIMATE}
497489
}

extensions/webdav/src/main/java/org/exist/webdav/MiltonResource.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.net.URISyntaxException;
3737
import java.util.Date;
3838
import java.util.GregorianCalendar;
39+
import java.util.Properties;
3940

4041
/**
4142
* Generic class representing a Milton Resource.
@@ -56,7 +57,11 @@ public class MiltonResource implements Resource {
5657
// Used for Long to DateTime conversion
5758
private DatatypeFactory datatypeFactory;
5859

59-
public MiltonResource() {
60+
protected final Properties configuration;
61+
62+
public MiltonResource(final Properties configuration) {
63+
this.configuration = configuration;
64+
6065
if (datatypeFactory == null) {
6166
try {
6267
datatypeFactory = DatatypeFactory.newInstance();

0 commit comments

Comments
 (0)