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/05/05 23:17:24 UTC

svn commit: r1592634 - in /tika/trunk/tika-server/src: main/java/org/apache/tika/server/TikaServerCli.java main/java/org/apache/tika/server/TikaWelcome.java test/java/org/apache/tika/server/TikaWelcomeTest.java

Author: nick
Date: Mon May  5 21:17:24 2014
New Revision: 1592634

URL: http://svn.apache.org/r1592634
Log:
TIKA-1269 Have the human-facing welcome page tell you roughly what the different endpoints are

Modified:
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaServerCli.java
    tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaWelcome.java
    tika/trunk/tika-server/src/test/java/org/apache/tika/server/TikaWelcomeTest.java

Modified: tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaServerCli.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaServerCli.java?rev=1592634&r1=1592633&r2=1592634&view=diff
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaServerCli.java (original)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaServerCli.java Mon May  5 21:17:24 2014
@@ -94,6 +94,7 @@ public class TikaServerCli {
               TikaResource.class, UnpackerResource.class, 
               TikaDetectors.class, TikaMimeTypes.class, 
               TikaVersion.class, TikaWelcome.class);
+      sf.setResourceClasses(TikaWelcome.class); 
 
       List<Object> providers = new ArrayList<Object>();
       providers.add(new TarWriter());

Modified: tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaWelcome.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaWelcome.java?rev=1592634&r1=1592633&r2=1592634&view=diff
==============================================================================
--- tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaWelcome.java (original)
+++ tika/trunk/tika-server/src/main/java/org/apache/tika/server/TikaWelcome.java Mon May  5 21:17:24 2014
@@ -16,9 +16,21 @@
  */
 package org.apache.tika.server;
 
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
+import javax.ws.rs.DELETE;
 import javax.ws.rs.GET;
+import javax.ws.rs.HEAD;
+import javax.ws.rs.OPTIONS;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
 import javax.ws.rs.Path;
 import javax.ws.rs.Produces;
 
@@ -28,13 +40,22 @@ import org.apache.tika.config.TikaConfig
 
 /**
  * <p>Provides a basic welcome to the Apache Tika Server.</p>
- * <p>TODO Should ideally also list the endpoints we have defined,
- *  see TIKA-1269 for details of all that.</p>
  */
 @Path("/")
 public class TikaWelcome {
     private static final String DOCS_URL = "https://wiki.apache.org/tika/TikaJAXRS";
     
+    private static final Map<Class<? extends Annotation>, String> HTTP_METHODS =
+            new HashMap<Class<? extends Annotation>, String>();
+    static {
+        HTTP_METHODS.put(DELETE.class , "DELETE");
+        HTTP_METHODS.put(GET.class,     "GET");
+        HTTP_METHODS.put(HEAD.class,    "HEAD");
+        HTTP_METHODS.put(OPTIONS.class, "OPTIONS");
+        HTTP_METHODS.put(POST.class,    "POST");
+        HTTP_METHODS.put(PUT.class,     "PUT");
+    }
+    
     private Tika tika;
     private HTMLHelper html;
     private List<Class<?>> endpoints;
@@ -42,7 +63,49 @@ public class TikaWelcome {
     public TikaWelcome(TikaConfig tika, JAXRSServerFactoryBean sf) {
         this.tika = new Tika(tika);
         this.html = new HTMLHelper();
-        this.endpoints = sf.getResourceClasses(); 
+        this.endpoints = sf.getResourceClasses();
+    }
+    
+    protected List<Endpoint> identifyEndpoints() {
+        List<Endpoint> found = new ArrayList<Endpoint>();
+        for (Class<?> endpoint : endpoints) {
+            Path p = endpoint.getAnnotation(Path.class);
+            String basePath = null;
+            if (p != null)
+                basePath = p.value();
+
+            for (Method m : endpoint.getMethods()) {
+                String httpMethod = null;
+                String methodPath = null;
+                String[] produces = null;
+                
+                for (Annotation a : m.getAnnotations()) {
+                    for (Class<? extends Annotation> httpMethAnn : HTTP_METHODS.keySet()) {
+                        if (httpMethAnn.isInstance(a)) {
+                            httpMethod = HTTP_METHODS.get(httpMethAnn);
+                        }
+                    }
+                    if (a instanceof Path) {
+                        methodPath = ((Path)a).value();
+                    }
+                    if (a instanceof Produces) {
+                        produces = ((Produces)a).value();
+                    }
+                }
+                
+                if (httpMethod != null) {
+                    String mPath = basePath;
+                    if (mPath == null) {
+                        mPath = "";
+                    }
+                    if (methodPath != null) {
+                        mPath += methodPath;
+                    }
+                    found.add(new Endpoint(endpoint, m, mPath, httpMethod, produces));
+                }
+            }
+        }
+        return found;
     }
     
     @GET
@@ -55,8 +118,28 @@ public class TikaWelcome {
         h.append(DOCS_URL);
         h.append("\">");
         h.append(DOCS_URL);
-        h.append("</a></p>");
-        h.append("<p>Please see TIKA-1269 for details of what should be here...</p>");
+        h.append("</a></p>\n");
+
+        h.append("<ul>\n");
+        for (Endpoint e : identifyEndpoints()) {
+            h.append("<li><b>");
+            h.append(e.httpMethod);
+            h.append("</b> <i><a href=\"");
+            h.append(e.path);
+            h.append("\">");
+            h.append(e.path);
+            h.append("</a></i><br />");
+            h.append("Class: ");
+            h.append(e.className);
+            h.append("<br />Method: ");
+            h.append(e.methodName);
+            for (String produces : e.produces) {
+                h.append("<br />Produces: ");
+                h.append(produces);
+            }
+            h.append("</li>\n");
+        }
+        h.append("</ul>\n");
 
         html.generateFooter(h);
         return h.toString();
@@ -72,7 +155,35 @@ public class TikaWelcome {
         text.append("For endpoints, please see ");
         text.append(DOCS_URL);
         text.append("\n");
+        
+        for (Endpoint e : identifyEndpoints()) {
+            text.append(e.httpMethod);
+            text.append(" @ ");
+            text.append(e.path);
+            text.append("\n");
+            for (String produces : e.produces) {
+                text.append(" => ");
+                text.append(produces);
+                text.append("\n");
+            }
+        }
 
         return text.toString();
     }
+    
+    protected class Endpoint {
+        public final String className;
+        public final String methodName;
+        public final String path;
+        public final String httpMethod;
+        public final List<String> produces;
+        protected Endpoint(Class<?> endpoint, Method method, String path,
+                           String httpMethod, String[] produces) {
+            this.className = endpoint.getCanonicalName();
+            this.methodName = method.getName();
+            this.path = path;
+            this.httpMethod = httpMethod;
+            this.produces = Collections.unmodifiableList(Arrays.asList(produces));
+        }
+    }
 }

Modified: tika/trunk/tika-server/src/test/java/org/apache/tika/server/TikaWelcomeTest.java
URL: http://svn.apache.org/viewvc/tika/trunk/tika-server/src/test/java/org/apache/tika/server/TikaWelcomeTest.java?rev=1592634&r1=1592633&r2=1592634&view=diff
==============================================================================
--- tika/trunk/tika-server/src/test/java/org/apache/tika/server/TikaWelcomeTest.java (original)
+++ tika/trunk/tika-server/src/test/java/org/apache/tika/server/TikaWelcomeTest.java Mon May  5 21:17:24 2014
@@ -35,7 +35,7 @@ public class TikaWelcomeTest extends CXF
        sf.setResourceClasses(TikaWelcome.class);
        sf.setResourceProvider(
                TikaWelcome.class,
-           new SingletonResourceProvider(new TikaWelcome(tika))
+           new SingletonResourceProvider(new TikaWelcome(tika, sf))
        );
    }