/** * Returns a collection of mimetypes ordered by extension, but unlike the version in MimetypeService * throws an exception if the sourceExtension is supplied but does not match a mimetype. * @param sourceExtension to restrict the collection to one entry * @throws IllegalArgumentException if there is no match. The message indicates this. */ public Collection<String> getSourceMimetypes(String sourceExtension) { Collection<String> sourceMimetypes = mimetypeService.getMimetypes(sourceExtension); if (sourceMimetypes.isEmpty()) { throw new IllegalArgumentException("Unknown source extension "+sourceExtension); } return sourceMimetypes; }
/** * Returns a collection of mimetypes ordered by extension, but unlike the version in MimetypeService * throws an exception if the sourceExtension is supplied but does not match a mimetype. * @param sourceExtension to restrict the collection to one entry * @throws IllegalArgumentException if there is no match. The message indicates this. */ public Collection<String> getSourceMimetypes(String sourceExtension) { Collection<String> sourceMimetypes = mimetypeService.getMimetypes(sourceExtension); if (sourceMimetypes.isEmpty()) { throw new IllegalArgumentException("Unknown source extension "+sourceExtension); } return sourceMimetypes; }
private String getMimetype(String extension, boolean isSource) { String mimetype = null; if (extension != null) { Iterator<String> iterator = mimetypeService.getMimetypes(extension).iterator(); if (iterator.hasNext()) { mimetype = iterator.next(); } } if (mimetype == null) { throw new IllegalArgumentException("Unknown "+(isSource ? "source" : "target")+" extension: "+extension); } return mimetype; }
private String getMimetype(String extension, boolean isSource) { String mimetype = null; if (extension != null) { Iterator<String> iterator = mimetypeService.getMimetypes(extension).iterator(); if (iterator.hasNext()) { mimetype = iterator.next(); } } if (mimetype == null) { throw new IllegalArgumentException("Unknown "+(isSource ? "source" : "target")+" extension: "+extension); } return mimetype; }
@Override public String[] getExtensionsAndMimetypes() { List<String> extensionsAndMimetypes = new ArrayList<String>(); for (String mimetype: mimetypeService.getMimetypes(null)) { String extension = mimetypeService.getExtension(mimetype); extensionsAndMimetypes.add(extension+" - "+mimetype); } return extensionsAndMimetypes.toArray(new String[extensionsAndMimetypes.size()]); }
@Override public String[] getExtensionsAndMimetypes() { List<String> extensionsAndMimetypes = new ArrayList<String>(); for (String mimetype: mimetypeService.getMimetypes(null)) { String extension = mimetypeService.getExtension(mimetype); extensionsAndMimetypes.add(extension+" - "+mimetype); } return extensionsAndMimetypes.toArray(new String[extensionsAndMimetypes.size()]); }
/** * Identical to getSourceMimetypes for the target, but avoids doing the look up if the sourceExtension * is the same as the tragetExtension, so will have the same result. * @param sourceExtension used to restrict the sourceMimetypes * @param targetExtension to restrict the collection to one entry * @param sourceMimetypes that match the sourceExtension * @throws IllegalArgumentException if there is no match. The message indicates this. */ public Collection<String> getTargetMimetypes(String sourceExtension, String targetExtension, Collection<String> sourceMimetypes) { Collection<String> targetMimetypes = (targetExtension == null && sourceExtension == null) || (targetExtension != null && targetExtension.equals(sourceExtension)) ? sourceMimetypes : mimetypeService.getMimetypes(targetExtension); if (targetMimetypes.isEmpty()) { throw new IllegalArgumentException("Unknown target extension "+targetExtension); } return targetMimetypes; }
/** * Identical to getSourceMimetypes for the target, but avoids doing the look up if the sourceExtension * is the same as the tragetExtension, so will have the same result. * @param sourceExtension used to restrict the sourceMimetypes * @param targetExtension to restrict the collection to one entry * @param sourceMimetypes that match the sourceExtension * @throws IllegalArgumentException if there is no match. The message indicates this. */ public Collection<String> getTargetMimetypes(String sourceExtension, String targetExtension, Collection<String> sourceMimetypes) { Collection<String> targetMimetypes = (targetExtension == null && sourceExtension == null) || (targetExtension != null && targetExtension.equals(sourceExtension)) ? sourceMimetypes : mimetypeService.getMimetypes(targetExtension); if (targetMimetypes.isEmpty()) { throw new IllegalArgumentException("Unknown target extension "+targetExtension); } return targetMimetypes; }
public String[] getTestFileExtensionsAndMimetypes() { List<String> sourceExtensions = new ArrayList<String>(); Collection<String> sourceMimetypes = mimetypeService.getMimetypes(null); for (String sourceMimetype: sourceMimetypes) { String sourceExtension = mimetypeService.getExtension(sourceMimetype); if (loadQuickTestFile(sourceExtension) != null) { sourceExtensions.add(sourceExtension+" - "+sourceMimetype); } } return sourceExtensions.toArray(new String[sourceExtensions.size()]); }
public String[] getTestFileExtensionsAndMimetypes() { List<String> sourceExtensions = new ArrayList<String>(); Collection<String> sourceMimetypes = mimetypeService.getMimetypes(null); for (String sourceMimetype: sourceMimetypes) { String sourceExtension = mimetypeService.getExtension(sourceMimetype); if (loadQuickTestFile(sourceExtension) != null) { sourceExtensions.add(sourceExtension+" - "+sourceMimetype); } } return sourceExtensions.toArray(new String[sourceExtensions.size()]); }
/** * Returns a comma separated String of mimetype file extensions. */ private String getExtensions(List<String> origMimetypes) { // Only use the mimetypes we have registered List<String> mimetypes = new ArrayList<String>(origMimetypes); mimetypes.retainAll(getMimetypeService().getMimetypes()); StringBuilder sb = new StringBuilder(); int j = mimetypes.size(); int i=1; for (String mimetype: mimetypes) { sb.append(getMimetypeService().getExtension(mimetype)); if (i < j) { sb.append(++i < j ? ", " : " or "); } } return sb.toString(); }
/** * Gets the extensions of the mimetypes that match the given expression. * However if the expression is "*", only the ANY ("*") extension is returned. * @param expression which may contain '*' to represent zero or more characters. * @param mimetypeService MimetypeService * @return the list of extensions of mimetypes that match */ List<String> getMatchingExtensionsFromMimetypes( String expression, MimetypeService mimetypeService) { if (ANY.equals(expression)) { return Collections.singletonList(ANY); } Pattern pattern = pattern(expression); List<String> matchingMimetypes = new ArrayList<String>(1); for (String mimetype : mimetypeService.getMimetypes()) { if (pattern.matcher(mimetype).matches()) { String ext = mimetypeService.getExtension(mimetype); matchingMimetypes.add(ext); } } return matchingMimetypes; }
/** * Gets the extensions of the mimetypes that match the given expression. * However if the expression is "*", only the ANY ("*") extension is returned. * @param expression which may contain '*' to represent zero or more characters. * @param mimetypeService MimetypeService * @return the list of extensions of mimetypes that match */ List<String> getMatchingExtensionsFromMimetypes( String expression, MimetypeService mimetypeService) { if (ANY.equals(expression)) { return Collections.singletonList(ANY); } Pattern pattern = pattern(expression); List<String> matchingMimetypes = new ArrayList<String>(1); for (String mimetype : mimetypeService.getMimetypes()) { if (pattern.matcher(mimetype).matches()) { String ext = mimetypeService.getExtension(mimetype); matchingMimetypes.add(ext); } } return matchingMimetypes; }
/** * Returns a comma separated String of mimetype file extensions. */ private String getExtensions(List<String> origMimetypes) { // Only use the mimetypes we have registered List<String> mimetypes = new ArrayList<String>(origMimetypes); mimetypes.retainAll(getMimetypeService().getMimetypes()); StringBuilder sb = new StringBuilder(); int j = mimetypes.size(); int i=1; for (String mimetype: mimetypes) { sb.append(getMimetypeService().getExtension(mimetype)); if (i < j) { sb.append(++i < j ? ", " : " or "); } } return sb.toString(); }
/** * Gets the extensions that match the given expression. Only the main extension * of each mimetype is checked. * However if the expression is "*", only the ANY ("*") extension is returned. * @param expression which may contain '*' to represent zero or more characters. * @param mimetypeService MimetypeService * @return the list of extensions that match */ List<String> getMatchingExtensionsFromExtensions( String expression, MimetypeService mimetypeService) { if (ANY.equals(expression)) { return Collections.singletonList(ANY); } Pattern pattern = pattern(expression); List<String> matchingMimetypes = new ArrayList<String>(1); for (String mimetype : mimetypeService.getMimetypes()) { String ext = mimetypeService.getExtension(mimetype); if (pattern.matcher(ext).matches()) { matchingMimetypes.add(ext); } } return matchingMimetypes; }
/** * Gets the extensions that match the given expression. Only the main extension * of each mimetype is checked. * However if the expression is "*", only the ANY ("*") extension is returned. * @param expression which may contain '*' to represent zero or more characters. * @param mimetypeService MimetypeService * @return the list of extensions that match */ List<String> getMatchingExtensionsFromExtensions( String expression, MimetypeService mimetypeService) { if (ANY.equals(expression)) { return Collections.singletonList(ANY); } Pattern pattern = pattern(expression); List<String> matchingMimetypes = new ArrayList<String>(1); for (String mimetype : mimetypeService.getMimetypes()) { String ext = mimetypeService.getExtension(mimetype); if (pattern.matcher(ext).matches()) { matchingMimetypes.add(ext); } } return matchingMimetypes; }
private void checkMimetypeList(boolean hasValue, String line, int i, boolean evenNumberOfEntries) { String value = checkValue(hasValue, line, i); value = value == null ? "" : value.trim(); if (value.length() > 0) { String[] mimetypes = value.split(";"); if (evenNumberOfEntries && mimetypes.length % 2 != 0) { throw unexpectedProperty("There should be an even number of mimetypes.", line); } else { // Check the dericed mimetype on the Node are valid. Tika may have mimetypes we know nothing about. List<String> knownMimetypes = mimetypeService.getMimetypes(); for (int j=0; j<mimetypes.length; j+=2) { String dervivedMimetype = mimetypes[j]; dervivedMimetype = dervivedMimetype.trim(); if (!knownMimetypes.contains(dervivedMimetype)) { throw unexpectedProperty("Unregistered mimetype: "+dervivedMimetype, line); } } } } }
private void checkMimetypeList(boolean hasValue, String line, int i, boolean evenNumberOfEntries) { String value = checkValue(hasValue, line, i); value = value == null ? "" : value.trim(); if (value.length() > 0) { String[] mimetypes = value.split(";"); if (evenNumberOfEntries && mimetypes.length % 2 != 0) { throw unexpectedProperty("There should be an even number of mimetypes.", line); } else { // Check the dericed mimetype on the Node are valid. Tika may have mimetypes we know nothing about. List<String> knownMimetypes = mimetypeService.getMimetypes(); for (int j=0; j<mimetypes.length; j+=2) { String dervivedMimetype = mimetypes[j]; dervivedMimetype = dervivedMimetype.trim(); if (!knownMimetypes.contains(dervivedMimetype)) { throw unexpectedProperty("Unregistered mimetype: "+dervivedMimetype, line); } } } } }
@Test public void getExtensionsAndMimetypesTest() { when(mimetypeService.getMimetypes(null)).thenReturn(Arrays.asList(new String[] { "application/pdf", "image/png" })); when(mimetypeService.getExtension("application/pdf")).thenReturn("pdf"); when(mimetypeService.getExtension("image/png")).thenReturn("png"); String[] actual = mbean.getExtensionsAndMimetypes(); String[] expected = new String[] { "pdf - application/pdf", "png - image/png" }; assertArrayEquals(expected, actual); }
/** * Mock up the responses from the mimetypeService so that it: * a) returns all the supplied mimetypes * b) returns the extension given the mimetype * c) returns the mimetype given the extension. * @param mimetypeService mimetype service * @param mimetypesAndExtensions sequence of mimetypes and extenstions. * @throws IllegalStateException if there is not an extension for every mimetype */ public static void mockMimetypes(MimetypeService mimetypeService, String... mimetypesAndExtensions) { if (mimetypesAndExtensions.length % 2 != 0) { // Not using IllegalArgumentException as this is thrown by classes under test throw new java.lang.IllegalStateException("There should be an extension for every mimetype"); } final Set<String> allMimetypes = new HashSet<String>(); for (int i=0; i < mimetypesAndExtensions.length; i+=2) { allMimetypes.add(mimetypesAndExtensions[i]); when(mimetypeService.getExtension(mimetypesAndExtensions[i])).thenReturn(mimetypesAndExtensions[i+1]); when(mimetypeService.getMimetype(mimetypesAndExtensions[i+1])).thenReturn(mimetypesAndExtensions[i]); } when(mimetypeService.getMimetypes()).thenReturn(new ArrayList<String>(allMimetypes)); }