You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ni...@apache.org on 2014/04/30 22:04:18 UTC

svn commit: r1591463 - in /tika/trunk/tika-server/src/main: java/org/apache/tika/server/ resources/

Author: nick
Date: Wed Apr 30 20:04:17 2014
New Revision: 1591463

URL: http://svn.apache.org/r1591463
Log:
TIKA-1270 Provide a slightly better way to handle the user-facing HTML output, we may well want to replace it with something either better or more JAXRS like shortly!

Added:
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/HTMLHelper.java
    tika/trunk/tika-server/src/main/resources/tikaserver-template.html
Modified:
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaDetectors.java
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/UnpackerResource.java

Added: tika/trunk/tika-server/src/main/java/org/apache/tika/server/HTMLHelper.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/HTMLHelper.java?rev=1591463&view=auto
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/HTMLHelper.java (added)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/HTMLHelper.java Wed Apr 30 20:04:17 2014
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.tika.server;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.tika.io.IOUtils;
+
+/**
+ * Helps produce user facing HTML output.
+ * 
+ * TODO Decide if this would be better done as a MessageBodyWriter
+ */
+public class HTMLHelper {
+    private static final String PATH = "/tikaserver-template.html";
+    private static final String TITLE_VAR = "[[TITLE]]";
+    private static final String BODY_VAR = "[[BODY]]";
+    private String PRE_BODY;
+    private String POST_BODY;
+    
+    public HTMLHelper() {
+        InputStream htmlStr = getClass().getResourceAsStream(PATH);
+        if (htmlStr == null) {
+            throw new IllegalArgumentException("Template Not Found - " + PATH);
+        }
+        try {
+            String html = IOUtils.toString(htmlStr, "UTF-8");
+            int bodyAt = html.indexOf(BODY_VAR);
+            PRE_BODY = html.substring(0, bodyAt);
+            POST_BODY = html.substring(bodyAt + BODY_VAR.length());
+        } catch (IOException e) {
+            throw new IllegalStateException("Unable to read template");
+        }
+    }
+    
+    /**
+     * Generates the HTML Header for the user facing page, adding
+     *  in the given title as required
+     */
+    public void generateHeader(StringBuffer html, String title) {
+        html.append(PRE_BODY.replace(TITLE_VAR, title));
+    }
+    public void generateFooter(StringBuffer html) {
+        html.append(POST_BODY);
+    }
+}
\ No newline at end of file

Modified: tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaDetectors.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaDetectors.java?rev=1591463&r1=1591462&r2=1591463&view=diff
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaDetectors.java (original)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaDetectors.java Wed Apr 30 20:04:17 2014
@@ -33,27 +33,25 @@ import org.eclipse.jetty.util.ajax.JSON;
 /**
  * <p>Provides details of all the {@link Detector}s registered with
  *  Apache Tika, similar to <em>--list-detectors</em> with the Tika CLI.
- *  
- * <p>TODO Provide better support for the HTML based outputs
  */
 @Path("/detectors")
 public class TikaDetectors {
     private TikaConfig tika;
+    private HTMLHelper html;
+    
     public TikaDetectors(TikaConfig tika) {
         this.tika = tika;
+        this.html = new HTMLHelper();
     }
     
     @GET
     @Produces("text/html")
     public String getDectorsHTML() {
-        StringBuffer html = new StringBuffer();
-        html.append("<html><head><title>Apache Tika Available Detectors</title></head>\n");
-        html.append("<body><h1>Detectors available to Apache Tika</h1>\n");
-
-        detectorAsHTML(tika.getDetector(), html, 2);
-        
-        html.append("</body></html>\n");
-        return html.toString();
+        StringBuffer h = new StringBuffer();
+        html.generateHeader(h, "Detectors available to Apache Tika");
+        detectorAsHTML(tika.getDetector(), h, 2);
+        html.generateFooter(h);
+        return h.toString();
     }
     private void detectorAsHTML(Detector d, StringBuffer html, int level) {
         html.append("<h");

Modified: tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java?rev=1591463&r1=1591462&r2=1591463&view=diff
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java (original)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaMimeTypes.java Wed Apr 30 20:04:17 2014
@@ -37,22 +37,22 @@ import org.eclipse.jetty.util.ajax.JSON;
 /**
  * <p>Provides details of all the mimetypes known to Apache Tika,
  *  similar to <em>--list-supported-types</em> with the Tika CLI.
- *  
- * <p>TODO Provide better support for the HTML based outputs
  */
 @Path("/mime-types")
 public class TikaMimeTypes {
     private TikaConfig tika;
+    private HTMLHelper html;
+    
     public TikaMimeTypes(TikaConfig tika) {
         this.tika = tika;
+        this.html = new HTMLHelper();
     }
     
     @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");
+        StringBuffer h = new StringBuffer();
+        html.generateHeader(h, "Apache Tika Supported Mime Types");
         
         // Get our types
         List<MediaTypeDetails> types = getMediaTypes();
@@ -64,33 +64,33 @@ public class TikaMimeTypes {
                 firstType.put(type.type.getType(), type.type.toString());
             }
         }
-        html.append("<ul>");
+        h.append("<ul>");
         for (String section : firstType.keySet()) {
-            html.append("<li><a href=\"#" + firstType.get(section) + "\">" + 
+            h.append("<li><a href=\"#" + firstType.get(section) + "\">" + 
                         section + "</a></li>\n");
         }
-        html.append("</ul>");
+        h.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");
+            h.append("<a name=\"" + type.type + "\"></a>\n");
+            h.append("<h2>" + type.type + "</h2>\n");
             
             for (MediaType alias : type.aliases) {
-                html.append("<div>Alias: " + alias + "</div>\n");
+                h.append("<div>Alias: " + alias + "</div>\n");
             }
             if (type.supertype != null) {
-                html.append("<div>Super Type: <a href=\"#" + type.supertype + 
+                h.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");
+                h.append("<div>Parser: " + type.parser + "</div>\n");
             }
         }
 
-        html.append("</body></html>\n");
-        return html.toString();
+        html.generateFooter(h);
+        return h.toString();
     }
     
     @GET

Modified: tika/trunk/tika-server/src/main/java/org/apache/tika/server/UnpackerResource.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/UnpackerResource.java?rev=1591463&r1=1591462&r2=1591463&view=diff
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/UnpackerResource.java (original)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/UnpackerResource.java Wed Apr 30 20:04:17 2014
@@ -60,7 +60,6 @@ import org.xml.sax.ContentHandler;
 import org.xml.sax.SAXException;
 import org.xml.sax.helpers.DefaultHandler;
 
-@Path("/")
 public class UnpackerResource {
   private static final Log logger = LogFactory.getLog(UnpackerResource.class);
   public static final String TEXT_FILENAME = "__TEXT__";
@@ -71,7 +70,7 @@ public class UnpackerResource {
       this.tikaConfig = tikaConfig;
   }
 
-  @Path("unpacker{id:(/.*)?}")
+  @Path("/unpacker{id:(/.*)?}")
   @PUT
   @Produces({"application/zip", "application/x-tar"})
   public Map<String, byte[]> unpack(
@@ -82,7 +81,7 @@ public class UnpackerResource {
     return process(is, httpHeaders, info, false);
   }
 
-  @Path("all{id:(/.*)?}")
+  @Path("/all{id:(/.*)?}")
   @PUT
   @Produces({"application/zip", "application/x-tar"})
   public Map<String, byte[]> unpackAll(

Added: tika/trunk/tika-server/src/main/resources/tikaserver-template.html
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/resources/tikaserver-template.html?rev=1591463&view=auto
==============================================================================
--- tika/trunk/tika-server/src/main/resources/tikaserver-template.html (added)
+++ tika/trunk/tika-server/src/main/resources/tikaserver-template.html Wed Apr 30 20:04:17 2014
@@ -0,0 +1,32 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<!--
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+ 
+  Unless required by applicable law or agreed to in writing,
+  software distributed under the License is distributed on an
+  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  KIND, either express or implied.  See the License for the
+  specific language governing permissions and limitations
+  under the License.
+-->
+<html xmlns="http://www.w3.org/1999/xhtml">
+  <head>
+    <title>[[TITLE]]</title>
+    <!-- TODO Some nice CSS -->
+  </head>
+  <body>
+    <h1>[[TITLE]]</h1>
+    <div id="content">
+      [[BODY]]
+    </div>
+  </body>
+</html>