瀏覽代碼

fix #1493 use api type from servlet path

Shinsuke Sugaya 7 年之前
父節點
當前提交
705ac80ee5

+ 39 - 19
src/main/java/org/codelibs/fess/api/BaseApiManager.java

@@ -20,6 +20,7 @@ import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.PrintWriter;
 import java.util.Locale;
 import java.util.Locale;
 
 
+import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import javax.servlet.http.HttpServletResponse;
 
 
 import org.codelibs.core.exception.IORuntimeException;
 import org.codelibs.core.exception.IORuntimeException;
@@ -29,10 +30,12 @@ import org.lastaflute.web.util.LaResponseUtil;
 
 
 public abstract class BaseApiManager implements WebApiManager {
 public abstract class BaseApiManager implements WebApiManager {
 
 
+    private static final String API_FORMAT_TYPE = "apiFormatType";
+
     protected String pathPrefix;
     protected String pathPrefix;
 
 
     protected enum FormatType {
     protected enum FormatType {
-        SEARCH, LABEL, POPULARWORD, FAVORITE, FAVORITES, OTHER, PING;
+        SEARCH, LABEL, POPULARWORD, FAVORITE, FAVORITES, OTHER, PING, SCROLL;
     }
     }
 
 
     public String getPathPrefix() {
     public String getPathPrefix() {
@@ -43,27 +46,44 @@ public abstract class BaseApiManager implements WebApiManager {
         this.pathPrefix = pathPrefix;
         this.pathPrefix = pathPrefix;
     }
     }
 
 
-    protected FormatType getFormatType(final String formatType) {
-        if (formatType == null) {
-            return FormatType.SEARCH;
+    protected FormatType getFormatType(final HttpServletRequest request) {
+        FormatType formatType = (FormatType) request.getAttribute(API_FORMAT_TYPE);
+        if (formatType != null) {
+            return formatType;
+        }
+        String value = request.getParameter("type");
+        if (value == null) {
+            final String servletPath = request.getServletPath();
+            final String[] values = servletPath.replaceAll("/+", "/").split("/");
+            if (values.length > 2) {
+                value = values[2];
+            }
         }
         }
-        final String type = formatType.toUpperCase(Locale.ROOT);
-        if (FormatType.SEARCH.name().equals(type)) {
-            return FormatType.SEARCH;
-        } else if (FormatType.LABEL.name().equals(type)) {
-            return FormatType.LABEL;
-        } else if (FormatType.POPULARWORD.name().equals(type)) {
-            return FormatType.POPULARWORD;
-        } else if (FormatType.FAVORITE.name().equals(type)) {
-            return FormatType.FAVORITE;
-        } else if (FormatType.FAVORITES.name().equals(type)) {
-            return FormatType.FAVORITES;
-        } else if (FormatType.PING.name().equals(type)) {
-            return FormatType.PING;
+        if (value == null) {
+            formatType = FormatType.SEARCH;
         } else {
         } else {
-            // default
-            return FormatType.OTHER;
+            final String type = value.toUpperCase(Locale.ROOT);
+            if (FormatType.SEARCH.name().equals(type)) {
+                formatType = FormatType.SEARCH;
+            } else if (FormatType.LABEL.name().equals(type)) {
+                formatType = FormatType.LABEL;
+            } else if (FormatType.POPULARWORD.name().equals(type)) {
+                formatType = FormatType.POPULARWORD;
+            } else if (FormatType.FAVORITE.name().equals(type)) {
+                formatType = FormatType.FAVORITE;
+            } else if (FormatType.FAVORITES.name().equals(type)) {
+                formatType = FormatType.FAVORITES;
+            } else if (FormatType.PING.name().equals(type)) {
+                formatType = FormatType.PING;
+            } else if (FormatType.SCROLL.name().equals(type)) {
+                formatType = FormatType.SCROLL;
+            } else {
+                // default
+                formatType = FormatType.OTHER;
+            }
         }
         }
+        request.setAttribute(API_FORMAT_TYPE, formatType);
+        return formatType;
     }
     }
 
 
     public static void write(final String text, final String contentType, final String encoding) {
     public static void write(final String text, final String contentType, final String encoding) {

+ 1 - 2
src/main/java/org/codelibs/fess/api/gsa/GsaApiManager.java

@@ -88,8 +88,7 @@ public class GsaApiManager extends BaseApiManager implements WebApiManager {
     @Override
     @Override
     public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException,
     public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException,
             ServletException {
             ServletException {
-        final String formatType = request.getParameter("type");
-        switch (getFormatType(formatType)) {
+        switch (getFormatType(request)) {
         case SEARCH:
         case SEARCH:
             processSearchRequest(request, response, chain);
             processSearchRequest(request, response, chain);
             break;
             break;

+ 2 - 4
src/main/java/org/codelibs/fess/api/json/JsonApiManager.java

@@ -73,8 +73,7 @@ public class JsonApiManager extends BaseJsonApiManager {
     public boolean matches(final HttpServletRequest request) {
     public boolean matches(final HttpServletRequest request) {
         final FessConfig fessConfig = ComponentUtil.getFessConfig();
         final FessConfig fessConfig = ComponentUtil.getFessConfig();
         if (!fessConfig.isWebApiJson()) {
         if (!fessConfig.isWebApiJson()) {
-            final String formatType = request.getParameter("type");
-            switch (getFormatType(formatType)) {
+            switch (getFormatType(request)) {
             case SEARCH:
             case SEARCH:
             case LABEL:
             case LABEL:
             case POPULARWORD:
             case POPULARWORD:
@@ -95,8 +94,7 @@ public class JsonApiManager extends BaseJsonApiManager {
     @Override
     @Override
     public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException,
     public void process(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws IOException,
             ServletException {
             ServletException {
-        final String formatType = request.getParameter("type");
-        switch (getFormatType(formatType)) {
+        switch (getFormatType(request)) {
         case SEARCH:
         case SEARCH:
             processSearchRequest(request, response, chain);
             processSearchRequest(request, response, chain);
             break;
             break;