Skip to content

Commit 50d6eaa

Browse files
committed
[refactor] Use a BrokerPoolService for resolving Internet Media Types
1 parent fcd2829 commit 50d6eaa

48 files changed

Lines changed: 803 additions & 170 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
@@ -437,19 +437,20 @@ private void btnConfSelectActionPerformed(final java.awt.event.ActionEvent evt)
437437
.toFile());
438438
chooser.setCurrentDirectory(dir.resolve("etc").toFile());
439439
chooser.setFileFilter(new FileFilter() {
440+
@Override
440441
public boolean accept(final File f) {
441442
if (f.isDirectory()) {
442443
return (true);
443444
}
444-
final MimeType mime = MimeTable.getInstance().getContentTypeFor(f.getName());
445+
final MimeType mime = pool.getMediaTypeService().getMediaTypeResolver().getContentTypeFor(f.getName());
445446

446447
if (mime == null) {
447448
return false;
448449
}
449450
return mime.isXMLType();
450451
}
451452

452-
453+
@Override
453454
public String getDescription() {
454455
return ("Database XML configuration file");
455456
}

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

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -998,8 +998,8 @@ private void uploadAction(final ActionEvent ev) {
998998
final JFileChooser chooser = new JFileChooser(preferences.get("directory.last", System.getProperty("user.dir")));
999999
chooser.setMultiSelectionEnabled(true);
10001000
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
1001-
chooser.addChoosableFileFilter(new BinaryFileFilter());
1002-
chooser.addChoosableFileFilter(new XMLFileFilter());
1001+
chooser.addChoosableFileFilter(new BinaryFileFilter(client.getMediaTypeResolver()));
1002+
chooser.addChoosableFileFilter(new XMLFileFilter(client.getMediaTypeResolver()));
10031003
if (chooser.showDialog(this, Messages.getString("ClientFrame.146")) == JFileChooser.APPROVE_OPTION) { //$NON-NLS-1$
10041004
// remember directory in preferences
10051005
preferences.put("directory.last", chooser.getCurrentDirectory().getAbsolutePath());
@@ -1887,46 +1887,44 @@ public void mouseReleased(final MouseEvent e) {
18871887
}
18881888

18891889
static class BinaryFileFilter extends FileFilter {
1890+
private final MimeTable mimeTable;
1891+
1892+
public BinaryFileFilter(final MimeTable mimeTable) {
1893+
this.mimeTable = mimeTable;
1894+
}
18901895

1891-
/* (non-Javadoc)
1892-
* @see javax.swing.filechooser.FileFilter#getDescription()
1893-
*/
18941896
@Override
18951897
public String getDescription() {
18961898
return Messages.getString("ClientFrame.220"); //$NON-NLS-1$
18971899
}
18981900

1899-
/* (non-Javadoc)
1900-
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
1901-
*/
19021901
@Override
19031902
public boolean accept(final File f) {
19041903
if (f.isDirectory()) {
19051904
return true;
19061905
}
1907-
return !MimeTable.getInstance().isXMLContent(f.getName());
1906+
return !mimeTable.isXMLContent(f.getName());
19081907
}
19091908
}
19101909

19111910
static class XMLFileFilter extends FileFilter {
1911+
private final MimeTable mimeTable;
1912+
1913+
public XMLFileFilter(final MimeTable mimeTable) {
1914+
this.mimeTable = mimeTable;
1915+
}
19121916

1913-
/* (non-Javadoc)
1914-
* @see javax.swing.filechooser.FileFilter#getDescription()
1915-
*/
19161917
@Override
19171918
public String getDescription() {
19181919
return Messages.getString("ClientFrame.221"); //$NON-NLS-1$
19191920
}
19201921

1921-
/* (non-Javadoc)
1922-
* @see javax.swing.filechooser.FileFilter#accept(java.io.File)
1923-
*/
19241922
@Override
19251923
public boolean accept(final File f) {
19261924
if (f.isDirectory()) {
19271925
return true;
19281926
}
1929-
return MimeTable.getInstance().isXMLContent(f.getName());
1927+
return mimeTable.isXMLContent(f.getName());
19301928
}
19311929
}
19321930

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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ public InteractiveClient(CommandlineOptions options) {
226226
this.options = options;
227227
}
228228

229+
private final MimeTable mimeTable = MimeTable.getInstance();
230+
229231
/**
230232
* Display help on commands
231233
*/
@@ -1311,7 +1313,7 @@ private void reindex() throws XMLDBException {
13111313
private void storeBinary(final String fileName) throws XMLDBException {
13121314
final Path file = Paths.get(fileName).normalize();
13131315
if (Files.isReadable(file)) {
1314-
final MimeType mime = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1316+
final MimeType mime = mimeTable.getContentTypeFor(FileUtils.fileName(file));
13151317
try (final BinaryResource resource = current.createResource(FileUtils.fileName(file), BinaryResource.class)) {
13161318
resource.setContent(file);
13171319
((EXistResource) resource).setMimeType(mime == null ? "application/octet-stream" : mime.getName());
@@ -1320,6 +1322,10 @@ private void storeBinary(final String fileName) throws XMLDBException {
13201322
}
13211323
}
13221324

1325+
MimeTable getMediaTypeResolver() {
1326+
return mimeTable;
1327+
}
1328+
13231329
private synchronized boolean findRecursive(final Collection collection, final Path dir, final XmldbURI base) throws XMLDBException {
13241330
Collection c;
13251331
EXistCollectionManagementService mgtService;
@@ -1348,7 +1354,7 @@ private synchronized boolean findRecursive(final Collection collection, final Pa
13481354
findRecursive(c, file, next);
13491355
} else {
13501356
final long start1 = System.currentTimeMillis();
1351-
mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1357+
mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(file));
13521358
if (mimeType == null) {
13531359
messageln("File " + FileUtils.fileName(file) + " has an unknown suffix. Cannot determine file type.");
13541360
mimeType = MimeType.BINARY_TYPE;
@@ -1424,7 +1430,7 @@ protected synchronized boolean parse(final Path file) throws XMLDBException {
14241430
continue;
14251431
}
14261432
final long start = System.currentTimeMillis();
1427-
mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(files.get(i)));
1433+
mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(files.get(i)));
14281434
if (mimeType == null) {
14291435
mimeType = MimeType.BINARY_TYPE;
14301436
}
@@ -1485,7 +1491,7 @@ private synchronized boolean findGZipRecursive(final Collection collection, fina
14851491
break;
14861492
}
14871493
}
1488-
mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1494+
mimeType = mimeTable.getContentTypeFor(localName);
14891495
if (mimeType == null) {
14901496
messageln("File " + compressedName + " has an unknown suffix. Cannot determine file type.");
14911497
mimeType = MimeType.BINARY_TYPE;
@@ -1580,7 +1586,7 @@ protected synchronized boolean parseGZip(String fileName) throws XMLDBException,
15801586
break;
15811587
}
15821588
}
1583-
mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1589+
mimeType = mimeTable.getContentTypeFor(localName);
15841590
if (mimeType == null) {
15851591
mimeType = MimeType.BINARY_TYPE;
15861592
}
@@ -1658,7 +1664,7 @@ protected synchronized boolean parseZip(final Path zipPath) throws XMLDBExceptio
16581664
if (!ze.isDirectory()) {
16591665
final String localName = pathSteps[pathSteps.length - 1];
16601666
final long start = System.currentTimeMillis();
1661-
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(localName);
1667+
MimeType mimeType = mimeTable.getContentTypeFor(localName);
16621668
if (mimeType == null) {
16631669
mimeType = MimeType.BINARY_TYPE;
16641670
}
@@ -1783,12 +1789,12 @@ private void store(final Collection collection, final Path file, final UploadDia
17831789
final long fileSize = FileUtils.sizeQuietly(file);
17841790
upload.setCurrentSize(fileSize);
17851791

1786-
MimeType mimeType = MimeTable.getInstance().getContentTypeFor(FileUtils.fileName(file));
1792+
MimeType mimeType = mimeTable.getContentTypeFor(FileUtils.fileName(file));
17871793
// unknown mime type, here prefered is to do nothing
17881794
if (mimeType == null) {
17891795
upload.showMessage(file.toAbsolutePath() +
17901796
" - unknown suffix. No matching mime-type found in : " +
1791-
MimeTable.getInstance().getSrc());
1797+
mimeTable.getSrc());
17921798

17931799
// if some one prefers to store it as binary by default, but dangerous
17941800
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
@@ -441,7 +441,7 @@ private void open() {
441441
chooser.setCurrentDirectory(Paths.get(workDir).toFile());
442442
chooser.setMultiSelectionEnabled(false);
443443
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
444-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xquery"));
444+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xquery"));
445445

446446
if (chooser.showDialog(this, Messages.getString("QueryDialog.opendialog")) == JFileChooser.APPROVE_OPTION) {
447447
final Path selectedDir = chooser.getCurrentDirectory().toPath();
@@ -477,10 +477,10 @@ private void save(String stringToSave, String fileCategory) {
477477
chooser.setCurrentDirectory(Paths.get(workDir).toFile());
478478
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
479479
if ("result".equals(fileCategory)) {
480-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xhtml+xml"));
481-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xml"));
480+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xhtml+xml"));
481+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xml"));
482482
} else {
483-
chooser.addChoosableFileFilter(new MimeTypeFileFilter("application/xquery"));
483+
chooser.addChoosableFileFilter(new MimeTypeFileFilter(client.getMediaTypeResolver(), "application/xquery"));
484484
}
485485
if (chooser.showDialog(this, Messages.getString("QueryDialog.savedialogpre") + " " + fileCategory + " " + Messages.getString("QueryDialog.savedialogpost"))
486486
== 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
@@ -1112,9 +1112,9 @@ public void doPut(final DBBroker broker, final Txn transaction, final XmldbURI p
11121112
if (semicolon > 0) {
11131113
contentType = contentType.substring(0, semicolon).trim();
11141114
}
1115-
mime = MimeTable.getInstance().getContentType(contentType);
1115+
mime = broker.getBrokerPool().getMediaTypeService().getMediaTypeResolver().getContentType(contentType);
11161116
} else {
1117-
mime = MimeTable.getInstance().getContentTypeFor(docUri);
1117+
mime = broker.getBrokerPool().getMediaTypeService().getMediaTypeResolver().getContentTypeFor(docUri);
11181118
}
11191119

11201120
// 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)