Skip to content

Commit 4a5d0d9

Browse files
committed
[refactor] Use a BrokerPoolService for resolving Internet Media Types
1 parent 87bc3ff commit 4a5d0d9

48 files changed

Lines changed: 804 additions & 172 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

exist-core/pom.xml

Lines changed: 44 additions & 0 deletions
Large diffs are not rendered by default.

exist-core/src/main/java/org/exist/backup/CreateBackupDialog.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
* admin@evolvedbinary.com
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
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; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -24,6 +48,7 @@
2448
import org.exist.client.Messages;
2549
import org.exist.client.MimeTypeFileFilter;
2650
import org.exist.security.PermissionDeniedException;
51+
import org.exist.util.MimeTable;
2752
import org.exist.xmldb.XmldbURI;
2853
import org.xmldb.api.DatabaseManager;
2954
import org.xmldb.api.base.Collection;
@@ -147,7 +172,8 @@ private void actionSelect() {
147172
final JFileChooser chooser = new JFileChooser();
148173
chooser.setMultiSelectionEnabled(false);
149174
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
150-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/zip"));
175+
final MimeTable mimeTable = MimeTable.getInstance();
176+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(mimeTable, "application/zip"));
151177
chooser.setSelectedFile(Paths.get("eXist-backup.zip").toFile());
152178
chooser.setCurrentDirectory(backupDir.toFile());
153179

exist-core/src/main/java/org/exist/backup/ExportGUI.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,19 +405,20 @@ private void btnConfSelectActionPerformed(final java.awt.event.ActionEvent evt)
405405
.toFile());
406406
chooser.setCurrentDirectory(dir.resolve("etc").toFile());
407407
chooser.setFileFilter(new FileFilter() {
408+
@Override
408409
public boolean accept(final File f) {
409410
if (f.isDirectory()) {
410411
return (true);
411412
}
412-
final MimeType mime = MimeTable.getInstance().getContentTypeFor(f.getName());
413+
final MimeType mime = pool.getMediaTypeService().getMediaTypeResolver().getContentTypeFor(f.getName());
413414

414415
if (mime == null) {
415416
return false;
416417
}
417418
return mime.isXMLType();
418419
}
419420

420-
421+
@Override
421422
public String getDescription() {
422423
return ("Database XML configuration file");
423424
}

exist-core/src/main/java/org/exist/client/ClientFrame.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,8 +1017,8 @@ private void uploadAction(final ActionEvent ev) {
10171017
final JFileChooser chooser = new JFileChooser(preferences.get("directory.last", System.getProperty("user.dir")));
10181018
chooser.setMultiSelectionEnabled(true);
10191019
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
1020-
chooser.addChoosableFileFilter(new BinaryFileFilter());
1021-
chooser.addChoosableFileFilter(new XMLFileFilter());
1020+
chooser.addChoosableFileFilter(new BinaryFileFilter(client.getMediaTypeResolver()));
1021+
chooser.addChoosableFileFilter(new XMLFileFilter(client.getMediaTypeResolver()));
10221022
if (chooser.showDialog(this, Messages.getString("ClientFrame.146")) == JFileChooser.APPROVE_OPTION) { //$NON-NLS-1$
10231023
// remember directory in preferences
10241024
preferences.put("directory.last", chooser.getCurrentDirectory().getAbsolutePath());
@@ -1919,46 +1919,44 @@ public void mouseReleased(final MouseEvent e) {
19191919
}
19201920

19211921
static class BinaryFileFilter extends FileFilter {
1922+
private final MimeTable mimeTable;
1923+
1924+
public BinaryFileFilter(final MimeTable mimeTable) {
1925+
this.mimeTable = mimeTable;
1926+
}
19221927

1923-
/* (non-Javadoc)
1924-
* @see javax.swing.filechooser.FileFilter#getDescription()
1925-
*/
19261928
@Override
19271929
public String getDescription() {
19281930
return Messages.getString("ClientFrame.220"); //$NON-NLS-1$
19291931
}
19301932

1931-
/* (non-Javadoc)
1932-
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
1933-
*/
19341933
@Override
19351934
public boolean accept(final File f) {
19361935
if (f.isDirectory()) {
19371936
return true;
19381937
}
1939-
return !MimeTable.getInstance().isXMLContent(f.getName());
1938+
return !mimeTable.isXMLContent(f.getName());
19401939
}
19411940
}
19421941

19431942
static class XMLFileFilter extends FileFilter {
1943+
private final MimeTable mimeTable;
1944+
1945+
public XMLFileFilter(final MimeTable mimeTable) {
1946+
this.mimeTable = mimeTable;
1947+
}
19441948

1945-
/* (non-Javadoc)
1946-
* @see javax.swing.filechooser.FileFilter#getDescription()
1947-
*/
19481949
@Override
19491950
public String getDescription() {
19501951
return Messages.getString("ClientFrame.221"); //$NON-NLS-1$
19511952
}
19521953

1953-
/* (non-Javadoc)
1954-
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
1955-
*/
19561954
@Override
19571955
public boolean accept(final File f) {
19581956
if (f.isDirectory()) {
19591957
return true;
19601958
}
1961-
return MimeTable.getInstance().isXMLContent(f.getName());
1959+
return mimeTable.isXMLContent(f.getName());
19621960
}
19631961
}
19641962

exist-core/src/main/java/org/exist/client/InteractiveClient.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ public class InteractiveClient {
223223
private static final NamedThreadGroupFactory clientThreadGroupFactory = new NamedThreadGroupFactory("java-admin-client");
224224
private final ThreadGroup clientThreadGroup = clientThreadGroupFactory.newThreadGroup(null);
225225

226+
private final MimeTable mimeTable = MimeTable.getInstance();
227+
226228
/**
227229
* Display help on commands
228230
*/
@@ -1353,14 +1355,18 @@ private void reindex() throws XMLDBException {
13531355
private void storeBinary(final String fileName) throws XMLDBException {
13541356
final Path file = Paths.get(fileName).normalize();
13551357
if (Files.isReadable(file)) {
1356-
final MimeType mime = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1358+
final MimeType mime = mimeTable.getContentTypeFor(FileUtils.fileName(file));
13571359
final BinaryResource resource = (BinaryResource) current.createResource(FileUtils.fileName(file), BinaryResource.RESOURCE_TYPE);
13581360
resource.setContent(file);
13591361
((EXistResource) resource).setMimeType(mime == null ? "application/octet-stream" : mime.getName());
13601362
current.storeResource(resource);
13611363
}
13621364
}
13631365

1366+
MimeTable getMediaTypeResolver() {
1367+
return mimeTable;
1368+
}
1369+
13641370
private synchronized boolean findRecursive(final Collection collection, final Path dir, final XmldbURI base) throws XMLDBException {
13651371
Collection c;
13661372
Resource document;
@@ -1390,7 +1396,7 @@ private synchronized boolean findRecursive(final Collection collection, final Pa
13901396
findRecursive(c, file, next);
13911397
} else {
13921398
final long start1 = System.currentTimeMillis();
1393-
mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1399+
mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(file));
13941400
if (mimeType == null) {
13951401
messageln("File " + FileUtils.fileName(file) + " has an unknown suffix. Cannot determine file type.");
13961402
mimeType = MimeType.BINARY_TYPE;
@@ -1466,7 +1472,7 @@ protected synchronized boolean parse(final Path file) throws XMLDBException {
14661472
continue;
14671473
}
14681474
final long start = System.currentTimeMillis();
1469-
mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(files.get(i)));
1475+
mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(files.get(i)));
14701476
if (mimeType == null) {
14711477
mimeType = MimeType.BINARY_TYPE;
14721478
}
@@ -1528,7 +1534,7 @@ private synchronized boolean findGZipRecursive(final Collection collection, fina
15281534
break;
15291535
}
15301536
}
1531-
mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1537+
mimeType = mimeTable.getContentTypeFor(localName);
15321538
if (mimeType == null) {
15331539
messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
15341540
mimeType = MimeType.BINARY_TYPE;
@@ -1623,7 +1629,7 @@ protected synchronized boolean parseGZip(String fileName) throws XMLDBException,
16231629
break;
16241630
}
16251631
}
1626-
mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1632+
mimeType = mimeTable.getContentTypeFor(localName);
16271633
if (mimeType == null) {
16281634
mimeType = MimeType.BINARY_TYPE;
16291635
}
@@ -1702,7 +1708,7 @@ protected synchronized boolean parseZip(final Path zipPath) throws XMLDBExceptio
17021708
if (!ze.isDirectory()) {
17031709
final String localName = pathSteps[pathSteps.length - 1];
17041710
final long start = System.currentTimeMillis();
1705-
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1711+
MimeType mimeType = mimeTable.getContentTypeFor(localName);
17061712
if (mimeType == null) {
17071713
mimeType = MimeType.BINARY_TYPE;
17081714
}
@@ -1826,12 +1832,12 @@ private void store(final Collection collection, final Path file, final UploadDia
18261832
final long fileSize = FileUtils.sizeQuietly(file);
18271833
upload.setCurrentSize(fileSize);
18281834

1829-
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1835+
MimeType mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(file));
18301836
// unknown mime type, here prefered is to do nothing
18311837
if (mimeType == null) {
18321838
upload.showMessage(file.toAbsolutePath() +
18331839
" - unknown suffix. No matching mime-type found in : " +
1834-
MimeTable.getInstance().getSrc());
1840+
mimeTable.getSrc());
18351841

18361842
// if some one prefers to store it as binary by default, but dangerous
18371843
mimeType = MimeType.BINARY_TYPE;

exist-core/src/main/java/org/exist/client/MimeTypeFileFilter.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,28 @@
11
/*
2+
* Elemental
3+
* Copyright (C) 2024, Evolved Binary Ltd
4+
*
5+
* admin@evolvedbinary.com
6+
* https://www.evolvedbinary.com | https://www.elemental.xyz
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; version 2.1.
11+
*
12+
* This library is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
* Lesser General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public
18+
* License along with this library; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20+
*
21+
* NOTE: Parts of this file contain code from 'The eXist-db Authors'.
22+
* The original license header is included below.
23+
*
24+
* =====================================================================
25+
*
226
* eXist-db Open Source Native XML Database
327
* Copyright (C) 2001 The eXist-db Authors
428
*
@@ -41,13 +65,13 @@ public class MimeTypeFileFilter extends FileFilter {
4165
private String description = null;
4266
private List<String> extensions = null;
4367

44-
public MimeTypeFileFilter(String mimeType) {
45-
description = MimeTable.getInstance().getContentType(mimeType).getDescription();
46-
extensions = MimeTable.getInstance().getAllExtensions(mimeType);
68+
public MimeTypeFileFilter(final MimeTable mimeTable, final String mimeType) {
69+
this.description = mimeTable.getContentType(mimeType).getDescription();
70+
this.extensions = mimeTable.getAllExtensions(mimeType);
4771
}
4872

4973
@Override
50-
public boolean accept(File file) {
74+
public boolean accept(final File file) {
5175
if(file.isDirectory()){ //permit directories to be viewed
5276
return true;
5377
}

exist-core/src/main/java/org/exist/client/QueryDialog.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ private void open() {
439439
chooser.setCurrentDirectory(Paths.get(workDir).toFile());
440440
chooser.setMultiSelectionEnabled(false);
441441
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
442-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xquery"));
442+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xquery"));
443443

444444
if (chooser.showDialog(this, Messages.getString("QueryDialog.opendialog")) == JFileChooser.APPROVE_OPTION) {
445445
final Path selectedDir = chooser.getCurrentDirectory().toPath();
@@ -475,10 +475,10 @@ private void save(String stringToSave, String fileCategory) {
475475
chooser.setCurrentDirectory(Paths.get(workDir).toFile());
476476
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
477477
if ("result".equals(fileCategory)) {
478-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xhtml+xml"));
479-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xml"));
478+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xhtml+xml"));
479+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xml"));
480480
} else {
481-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xquery"));
481+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xquery"));
482482
}
483483
if (chooser.showDialog(this, Messages.getString("QueryDialog.savedialogpre") + " " + fileCategory + " " + Messages.getString("QueryDialog.savedialogpost"))
484484
== JFileChooser.APPROVE_OPTION) {

exist-core/src/main/java/org/exist/http/RESTServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,9 +1089,9 @@ public void doPut(final DBBroker broker, final Txn transaction, final XmldbURI p
10891089
if (semicolon > 0) {
10901090
contentType = contentType.substring(0, semicolon).trim();
10911091
}
1092-
mime = MimeTable.getInstance().getContentType(contentType);
1092+
mime = broker.getBrokerPool().getMediaTypeService().getMediaTypeResolver().getContentType(contentType);
10931093
} else {
1094-
mime = MimeTable.getInstance().getContentTypeFor(docUri);
1094+
mime = broker.getBrokerPool().getMediaTypeService().getMediaTypeResolver().getContentTypeFor(docUri);
10951095
}
10961096

10971097
// TODO(AR) in storeDocument need to handle mime == null and use MimeType.BINARY_TYPE

exist-core/src/main/java/org/exist/http/servlets/XQueryServlet.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,12 +520,14 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
520520

521521
final String mediaType = outputProperties.getProperty(OutputKeys.MEDIA_TYPE);
522522
if (mediaType != null) {
523-
if (!response.isCommitted())
524-
{if (MimeTable.getInstance().isTextContent(mediaType)) {
523+
if (!response.isCommitted()) {
524+
if (isTextContent(mediaType)) {
525525
response.setContentType(mediaType + "; charset=" + getFormEncoding());
526526
response.setCharacterEncoding(getFormEncoding());
527-
} else
528-
response.setContentType(mediaType);}
527+
} else {
528+
response.setContentType(mediaType);
529+
}
530+
}
529531

530532
} else {
531533
String contentType = this.contentType;
@@ -538,8 +540,9 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
538540
contentType = this.contentType;
539541

540542
} finally {
541-
if (MimeTable.getInstance().isTextContent(contentType))
542-
{contentType += "; charset=" + getFormEncoding();}
543+
if (isTextContent(contentType)) {
544+
contentType += "; charset=" + getFormEncoding();
545+
}
543546
response.setContentType(contentType );
544547
}
545548
}
@@ -596,6 +599,11 @@ protected void process(HttpServletRequest request, HttpServletResponse response)
596599
output.close();
597600
}
598601

602+
private boolean isTextContent(final String mediaType) {
603+
final MimeTable mimeTable = getPool().getMediaTypeService().getMediaTypeResolver();
604+
return mimeTable.isTextContent(mediaType);
605+
}
606+
599607
private String getSessionAttribute(HttpSession session, String attribute) {
600608
final Object obj = session.getAttribute(attribute);
601609
return getValue(obj);

0 commit comments

Comments
 (0)