Skip to content

Commit 6b81c5d

Browse files
committed
[feature] Provide mime type on store XMLResource
The mime type that is available on the remote resource was not provided when calling the parse() procedure on the backend and has now been added. When calculating the mime type being stored the following order will be used: - mime type as given by the method call - mime type caclulated based on the file name extension - default mime type 'application/octet-stream' is used Co-authored-by: Otmar Humbel <[email protected]> Signed-off-by: Patrick Reinhart <[email protected]>
1 parent 831829e commit 6b81c5d

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

exist-core/src/main/java/org/exist/xmldb/RemoteCollection.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ private void store(final RemoteXMLResource res) throws XMLDBException {
521521
} catch (final URISyntaxException e) {
522522
throw new XMLDBException(ErrorCodes.INVALID_URI, e);
523523
}
524+
params.add(res.getMimeType());
524525
params.add(1);
525526
if (res.getCreationTime() != null) {
526527
params.add(res.getCreationTime());

exist-core/src/main/java/org/exist/xmlrpc/RpcAPI.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,18 +545,27 @@ boolean parse(byte[] xmlData, String docName)
545545
*
546546
* @param xmlData The document data
547547
* @param docName The path where the document will be stored
548+
* @param mimeType the mimeType to check for
548549
* @param overwrite Overwrite an existing document with the same path?
550+
* @param created Specifies the creattion date
551+
* @param modified Specifies the last modification date
549552
* @return true, if the document is valid XML
550553
* @throws EXistException If an internal error occurs
551554
* @throws PermissionDeniedException If the current user is not allowed to perform this action
552555
* @throws URISyntaxException If the URI contains syntax errors
553556
*/
554-
boolean parse(byte[] xmlData, String docName, int overwrite)
557+
boolean parse(byte[] xmlData, String docName, String mimeType, int overwrite, Date created, Date modified)
555558
throws EXistException, PermissionDeniedException, URISyntaxException;
556559

557560
boolean parse(byte[] xmlData, String docName, int overwrite, Date created, Date modified)
558561
throws EXistException, PermissionDeniedException, URISyntaxException;
559562

563+
boolean parse(byte[] xmlData, String docName, String mimeType, int overwrite)
564+
throws EXistException, PermissionDeniedException, URISyntaxException;
565+
566+
boolean parse(byte[] xmlData, String docName, int overwrite)
567+
throws EXistException, PermissionDeniedException, URISyntaxException;
568+
560569
boolean parse(String xml, String docName, int overwrite)
561570
throws EXistException, PermissionDeniedException, URISyntaxException;
562571

exist-core/src/main/java/org/exist/xmlrpc/RpcConnection.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,18 +1326,24 @@ private boolean hasCollection(final XmldbURI collUri) throws EXistException, Per
13261326

13271327
@Override
13281328
public boolean parse(byte[] xml, String documentPath, int overwrite) throws URISyntaxException, EXistException, PermissionDeniedException {
1329-
return parse(xml, documentPath, overwrite, null, null);
1329+
return parse(xml, documentPath, null, overwrite, null, null);
1330+
}
1331+
1332+
@Override
1333+
public boolean parse(final byte[] xml, final String documentPath, final String mimeType, final int overwrite) throws URISyntaxException, EXistException, PermissionDeniedException {
1334+
return parse(xml, documentPath, mimeType, overwrite, null, null);
13301335
}
13311336

13321337
@Override
13331338
public boolean parse(final byte[] xml, final String documentPath,
13341339
final int overwrite, final Date created, final Date modified) throws URISyntaxException, EXistException, PermissionDeniedException {
1335-
return parse(xml, XmldbURI.xmldbUriFor(documentPath), overwrite, created, modified);
1340+
return parse(xml, documentPath, null, overwrite, created, modified);
13361341
}
13371342

1338-
private boolean parse(final byte[] xml, final XmldbURI docUri,
1339-
final int overwrite, @Nullable final Date created, @Nullable final Date modified) throws EXistException, PermissionDeniedException {
1340-
1343+
@Override
1344+
public boolean parse(final byte[] xml, final String documentPath, @Nullable String mimeType,
1345+
final int overwrite, @Nullable final Date created, @Nullable final Date modified) throws URISyntaxException, EXistException, PermissionDeniedException {
1346+
final XmldbURI docUri = XmldbURI.xmldbUriFor(documentPath);
13411347
return this.<Boolean>writeCollection(docUri.removeLastSegment()).apply((collection, broker, transaction) -> {
13421348

13431349
try(final ManagedDocumentLock lockedDocument = broker.getBrokerPool().getLockManager().acquireDocumentWriteLock(docUri)) {
@@ -1356,7 +1362,7 @@ private boolean parse(final byte[] xml, final XmldbURI docUri,
13561362

13571363
final long startTime = System.currentTimeMillis();
13581364

1359-
final MimeType mime = MimeTable.getInstance().getContentTypeFor(docUri.lastSegment());
1365+
final MimeType mime = lookupMimeType(mimeType, docUri.lastSegment());
13601366
broker.storeDocument(transaction, docUri.lastSegment(), source, mime, created, modified, null, null, null, collection);
13611367

13621368
// NOTE: early release of Collection lock inline with Asymmetrical Locking scheme
@@ -1369,6 +1375,14 @@ private boolean parse(final byte[] xml, final XmldbURI docUri,
13691375
});
13701376
}
13711377

1378+
private MimeType lookupMimeType(@Nullable final String mimeType, final XmldbURI fileName) {
1379+
final MimeTable mimeTable = MimeTable.getInstance();
1380+
if (mimeType == null) {
1381+
return Optional.ofNullable(mimeTable.getContentTypeFor(fileName)).orElse(MimeType.BINARY_TYPE);
1382+
}
1383+
return Optional.ofNullable(mimeTable.getContentType(mimeType)).orElse(MimeType.BINARY_TYPE);
1384+
}
1385+
13721386
/**
13731387
* Parse a file previously uploaded with upload.
13741388
*
@@ -1482,7 +1496,7 @@ private boolean parseLocal(final String localFile, final XmldbURI docUri, final
14821496

14831497
// parse the source
14841498
try (final FileInputSource source = sourceSupplier.get()) {
1485-
final MimeType mime = Optional.ofNullable(MimeTable.getInstance().getContentType(mimeType)).orElse(MimeType.BINARY_TYPE);
1499+
final MimeType mime = lookupMimeType(mimeType, docUri.lastSegment());
14861500

14871501
broker.storeDocument(transaction, docUri.lastSegment(), source, mime, created, modified, null, null, null, collection);
14881502

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* eXist-db Open Source Native XML Database
3+
* Copyright (C) 2001 The eXist-db Authors
4+
*
5+
6+
* http://www.exist-db.org
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21+
*/
22+
package org.exist.xmlrpc;
23+
24+
import static org.junit.Assert.assertEquals;
25+
import static org.junit.Assert.assertNotNull;
26+
27+
import org.exist.TestUtils;
28+
import org.exist.test.ExistWebServer;
29+
import org.junit.AfterClass;
30+
import org.junit.BeforeClass;
31+
import org.junit.ClassRule;
32+
import org.junit.Test;
33+
import org.xml.sax.SAXException;
34+
import org.xmldb.api.DatabaseManager;
35+
import org.xmldb.api.base.Collection;
36+
import org.xmldb.api.base.Database;
37+
import org.xmldb.api.base.Resource;
38+
import org.xmldb.api.base.XMLDBException;
39+
import org.xmldb.api.modules.CollectionManagementService;
40+
import org.xmldb.api.modules.XMLResource;
41+
42+
public class MimeTypeTest {
43+
44+
@ClassRule
45+
public final static ExistWebServer existWebServer = new ExistWebServer(true, false, true, true);
46+
47+
private final static String COLLECTION_NAME = "rpctest";
48+
private static final String DOCUMENT_NAME = "myxmldoc";
49+
private final static String XML_CONTENT = "<xml><it><is></is></it></xml>";
50+
51+
private static String getBaseUri() {
52+
return "xmldb:exist://localhost:" + existWebServer.getPort() + "/xmlrpc";
53+
}
54+
55+
@Test
56+
public void testXMLMimeType() throws XMLDBException {
57+
// store an XML document without an .xml extension
58+
try(Collection collection = DatabaseManager.getCollection(getBaseUri() + "/db/" + COLLECTION_NAME, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD)){
59+
final XMLResource resource = (XMLResource)collection.createResource(DOCUMENT_NAME, XMLResource.RESOURCE_TYPE);
60+
resource.setContent(XML_CONTENT);
61+
collection.storeResource(resource);
62+
assertEquals(XMLResource.RESOURCE_TYPE, resource.getResourceType());
63+
}
64+
65+
// retrieve the document and verify its resource type
66+
try(Collection collection = DatabaseManager.getCollection(getBaseUri() + "/db/" + COLLECTION_NAME, TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD)){
67+
Resource resource = collection.getResource(DOCUMENT_NAME);
68+
assertEquals(XMLResource.RESOURCE_TYPE, resource.getResourceType());
69+
}
70+
}
71+
72+
@BeforeClass
73+
public static void startServer() throws ClassNotFoundException, IllegalAccessException, InstantiationException, XMLDBException, SAXException {
74+
// initialize XML:DB driver
75+
Class<?> cl = Class.forName("org.exist.xmldb.DatabaseImpl");
76+
Database database = (Database) cl.newInstance();
77+
DatabaseManager.registerDatabase(database);
78+
79+
Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
80+
81+
CollectionManagementService mgmt = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
82+
assertNotNull(mgmt.createCollection(COLLECTION_NAME));
83+
}
84+
85+
@AfterClass
86+
public static void stopServer() throws XMLDBException {
87+
Collection root = DatabaseManager.getCollection(getBaseUri() + "/db", TestUtils.ADMIN_DB_USER, TestUtils.ADMIN_DB_PWD);
88+
CollectionManagementService mgmt = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
89+
mgmt.removeCollection(COLLECTION_NAME);
90+
91+
Collection config = DatabaseManager.getCollection(getBaseUri() + "/db/system/config/db", "admin", "");
92+
mgmt = (CollectionManagementService) config.getService("CollectionManagementService", "1.0");
93+
mgmt.removeCollection(COLLECTION_NAME);
94+
}
95+
}

0 commit comments

Comments
 (0)