use of org.apache.tika.mime.MediaType in project tika by apache.
the class TikaMimeTypes method getMimeTypesPlain.
@GET
@Produces("text/plain")
public String getMimeTypesPlain() {
StringBuffer text = new StringBuffer();
for (MediaTypeDetails type : getMediaTypes()) {
text.append(type.type.toString());
text.append("\n");
for (MediaType alias : type.aliases) {
text.append(" alias: ").append(alias).append("\n");
}
if (type.supertype != null) {
text.append(" supertype: ").append(type.supertype.toString()).append("\n");
}
if (type.parser != null) {
text.append(" parser: ").append(type.parser).append("\n");
}
}
return text.toString();
}
use of org.apache.tika.mime.MediaType in project tika by apache.
the class TikaParsers method parserAsMap.
private void parserAsMap(ParserDetails p, boolean withMimeTypes, Map<String, Object> details) {
details.put("name", p.className);
details.put("composite", p.isComposite);
details.put("decorated", p.isDecorated);
if (p.isComposite) {
List<Map<String, Object>> c = new ArrayList<Map<String, Object>>();
for (Parser cp : p.childParsers) {
Map<String, Object> cdet = new HashMap<String, Object>();
parserAsMap(new ParserDetails(cp), withMimeTypes, cdet);
c.add(cdet);
}
details.put("children", c);
} else if (withMimeTypes) {
List<String> mts = new ArrayList<String>(p.supportedTypes.size());
for (MediaType mt : p.supportedTypes) {
mts.add(mt.toString());
}
details.put("supportedTypes", mts);
}
}
use of org.apache.tika.mime.MediaType in project ddf by codice.
the class MimeTypeMapperImpl method guessMimeType.
@Override
public String guessMimeType(InputStream is, String fileExtension) throws MimeTypeResolutionException {
LOGGER.trace("ENTERING: guessMimeType()");
String mimeType = null;
LOGGER.debug("Looping through{} MimeTypeResolvers", mimeTypeResolvers.size());
// This is to force the TikaMimeTypeResolver to be called
// after the CustomMimeTypeResolvers to prevent Tika default mapping
// from being used when a CustomMimeTypeResolver may be more appropriate.
List<MimeTypeResolver> sortedResolvers = sortResolvers(mimeTypeResolvers);
if (StringUtils.isEmpty(fileExtension)) {
try (TemporaryFileBackedOutputStream tfbos = new TemporaryFileBackedOutputStream()) {
IOUtils.copy(is, tfbos);
try (InputStream inputStream = tfbos.asByteSource().openStream()) {
Detector detector = new DefaultDetector();
MediaType mediaType = detector.detect(inputStream, new Metadata());
fileExtension = getFileExtensionForMimeType(mediaType.toString()).replace(".", "");
} finally {
is = tfbos.asByteSource().openStream();
}
} catch (Exception e) {
LOGGER.debug("Failed to guess mimeType for file without extension.");
}
}
// If file has XML extension, then read root element namespace once so
// each MimeTypeResolver does not have to open the stream and read the namespace
String namespace = null;
if (fileExtension.equals(XML_FILE_EXTENSION)) {
try {
namespace = XMLUtils.getRootNamespace(IOUtils.toString(is));
} catch (IOException ioe) {
LOGGER.debug("Could not read namespace from input stream.", ioe);
}
LOGGER.debug("namespace = {}", namespace);
}
// Once a file extension is find for the given mime type, exit the loop.
for (MimeTypeResolver resolver : sortedResolvers) {
LOGGER.debug("Calling MimeTypeResolver {}", resolver.getName());
try {
// an InputTransformer to create a metacard for that "generic" XML file.
if (fileExtension.equals(XML_FILE_EXTENSION)) {
if (namespace != null && resolver.hasSchema()) {
if (namespace.equals(resolver.getSchema())) {
mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
}
}
} else {
mimeType = resolver.getMimeTypeForFileExtension(fileExtension);
}
} catch (Exception e) {
LOGGER.debug("Error resolving mime type for file extension: {}", fileExtension);
throw new MimeTypeResolutionException(e);
}
if (StringUtils.isNotEmpty(mimeType)) {
LOGGER.debug("mimeType [{}] retrieved from MimeTypeResolver: ", mimeType, resolver.getName());
break;
}
}
LOGGER.debug("mimeType = {}, file extension = [{}]", mimeType, fileExtension);
LOGGER.trace("EXITING: guessMimeType()");
return mimeType;
}
Aggregations