Skip to content

Commit

Permalink
TIKA-1270 Move to a common set of logic to decide what to display, so…
Browse files Browse the repository at this point in the history
… the output type bit just deals with formatting it only, and add a browser friendly html view too

git-svn-id: https://svn.apache.org/repos/asf/tika/trunk@1588214 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Gagravarr committed Apr 17, 2014
1 parent ad29351 commit ae83022
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 29 deletions.
125 changes: 96 additions & 29 deletions tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java
Expand Up @@ -16,8 +16,12 @@
*/
package org.apache.tika.server;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand All @@ -31,8 +35,7 @@
import org.eclipse.jetty.util.ajax.JSON;

/*
* TODO Reduce duplication between the two methods, by
* returning structured info that gets encoded two ways
* TODO Provide better support for the HTML based outputs
*/
@Path("/mime-types")
public class TikaMimeTypes {
Expand All @@ -41,31 +44,69 @@ public TikaMimeTypes(TikaConfig tika) {
this.tika = tika;
}

@GET
@Produces("text/html")
public String getMimeTypesHTML() {
StringBuffer html = new StringBuffer();
html.append("<html><head><title>Tika Supported Mime Types</title></head>\n");
html.append("<body><h1>Tika Supported Mime Types</h1>\n");

// Get our types
List<MediaTypeDetails> types = getMediaTypes();

// Get the first type in each section
SortedMap<String,String> firstType = new TreeMap<String, String>();
for (MediaTypeDetails type : types) {
if (! firstType.containsKey(type.type.getType())) {
firstType.put(type.type.getType(), type.type.toString());
}
}
html.append("<ul>");
for (String section : firstType.keySet()) {
html.append("<li><a href=\"#" + firstType.get(section) + "\">" +
section + "</a></li>\n");
}
html.append("</ul>");

// Output all of them
for (MediaTypeDetails type : types) {
html.append("<a name=\"" + type.type + "\"></a>\n");
html.append("<h2>" + type.type + "</h2>\n");

for (MediaType alias : type.aliases) {
html.append("<div>Alias: " + alias + "</div>\n");
}
if (type.supertype != null) {
html.append("<div>Super Type: <a href=\"#" + type.supertype +
"\">" + type.supertype + "</a></div>\n");
}

if (type.parser != null) {
html.append("<div>Parser: " + type.parser + "</div>\n");
}
}

html.append("</body></html>\n");
return html.toString();
}

@GET
@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
public String getMimeTypesJSON() {
Map<String,Object> details = new HashMap<String, Object>();

MediaTypeRegistry registry = tika.getMediaTypeRegistry();
Map<MediaType, Parser> parsers = ((CompositeParser)tika.getParser()).getParsers();

for (MediaType type : registry.getTypes()) {
for (MediaTypeDetails type : getMediaTypes()) {
Map<String,Object> typeDets = new HashMap<String, Object>();

typeDets.put("alias", registry.getAliases(type));
MediaType supertype = registry.getSupertype(type);
if (supertype != null && !MediaType.OCTET_STREAM.equals(supertype)) {
typeDets.put("supertype", supertype);
typeDets.put("alias", type.aliases);
if (type.supertype != null) {
typeDets.put("supertype", type.supertype);
}
Parser p = parsers.get(type);
if (p != null) {
if (p instanceof CompositeParser) {
p = ((CompositeParser)p).getParsers().get(type);
}
typeDets.put("parser", p.getClass().getName());
if (type.parser != null) {
typeDets.put("parser", type.parser);
}

details.put(type.toString(), typeDets);
details.put(type.type.toString(), typeDets);
}

return JSON.toString(details);
Expand All @@ -76,33 +117,59 @@ public String getMimeTypesJSON() {
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: " + alias + "\n");
}
if (type.supertype != null) {
text.append(" supertype: " + type.supertype.toString() + "\n");
}

if (type.parser != null) {
text.append(" parser: " + type.parser + "\n");
}
}

return text.toString();
}

protected List<MediaTypeDetails> getMediaTypes() {
MediaTypeRegistry registry = tika.getMediaTypeRegistry();
Map<MediaType, Parser> parsers = ((CompositeParser)tika.getParser()).getParsers();
List<MediaTypeDetails> types =
new ArrayList<TikaMimeTypes.MediaTypeDetails>(registry.getTypes().size());

for (MediaType type : registry.getTypes()) {
text.append(type);
text.append("\n");
MediaTypeDetails details = new MediaTypeDetails();
details.type = type;
details.aliases = registry.getAliases(type).toArray(new MediaType[0]);

for (MediaType alias : registry.getAliases(type)) {
text.append(" alias: " + alias);
text.append("\n");
}
MediaType supertype = registry.getSupertype(type);
if (supertype != null && !MediaType.OCTET_STREAM.equals(supertype)) {
text.append(" supertype: " + supertype);
text.append("\n");
details.supertype = supertype;
}

Parser p = parsers.get(type);
if (p != null) {
if (p instanceof CompositeParser) {
p = ((CompositeParser)p).getParsers().get(type);
}
text.append(" parser: " + p.getClass().getName());
text.append("\n");
details.parser = p.getClass().getName();
}

types.add(details);
}

return text.toString();

return types;
}

private static class MediaTypeDetails {
private MediaType type;
private MediaType[] aliases;
private MediaType supertype;
private String parser;
}
}
Expand Up @@ -63,6 +63,27 @@ public void testGetPlainText() throws Exception {
assertContains("alias: image/bmp", text);
}

@Test
public void testGetHTML() throws Exception {
Response response = WebClient
.create(endPoint + MIMETYPES_PATH)
.type("text/html")
.accept("text/html")
.get();

String text = getStringFromInputStream((InputStream) response.getEntity());
assertContains("text/plain", text);
assertContains("application/xml", text);
assertContains("video/x-ogm", text);

assertContains("<h2>text/plain", text);
assertContains("name=\"text/plain", text);

assertContains("Super Type: <a href=\"#video/ogg\">video/ogg", text);

assertContains("Alias: image/bmp", text);
}

@Test
@SuppressWarnings("unchecked")
public void testGetJSON() throws Exception {
Expand Down

0 comments on commit ae83022

Please sign in to comment.