浏览代码

fix #1008 add virtual.host.headers

Shinsuke Sugaya 8 年之前
父节点
当前提交
479803e852

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/RootAction.java

@@ -40,7 +40,7 @@ public class RootAction extends FessSearchAction {
             return redirectToLogin();
         }
 
-        return asHtml(path_IndexJsp).useForm(SearchForm.class, op -> {
+        return asHtml(virtualHost(path_IndexJsp)).useForm(SearchForm.class, op -> {
             op.setup(form -> {
                 buildFormParams(form);
             });

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/base/FessLoginAction.java

@@ -21,7 +21,7 @@ import org.lastaflute.web.response.HtmlResponse;
 
 public abstract class FessLoginAction extends FessSearchAction {
     protected HtmlResponse getHtmlResponse() {
-        return getUserBean().map(user -> redirectByUser(user)).orElse(asHtml(path_Login_IndexJsp));
+        return getUserBean().map(user -> redirectByUser(user)).orElse(asHtml(virtualHost(path_Login_IndexJsp)));
     }
 
     protected HtmlResponse redirectByUser(final FessUserBean user) {

+ 5 - 0
src/main/java/org/codelibs/fess/app/web/base/FessSearchAction.java

@@ -46,6 +46,7 @@ import org.dbflute.optional.OptionalThing;
 import org.lastaflute.web.login.LoginManager;
 import org.lastaflute.web.response.ActionResponse;
 import org.lastaflute.web.response.HtmlResponse;
+import org.lastaflute.web.response.next.HtmlNext;
 import org.lastaflute.web.ruts.process.ActionRuntime;
 
 public abstract class FessSearchAction extends FessBaseAction {
@@ -220,4 +221,8 @@ public abstract class FessSearchAction extends FessBaseAction {
         final String contextPath = request.getServletContext().getContextPath();
         return newHtmlResponseAsRediect(StringUtil.isBlank(contextPath) ? "/" : contextPath);
     }
+
+    protected HtmlNext virtualHost(HtmlNext path) {
+        return fessConfig.getVirtualHostPath(path);
+    }
 }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/cache/CacheAction.java

@@ -47,7 +47,7 @@ public class CacheAction extends FessSearchAction {
     //                                                                      ==============
     @Execute
     public ActionResponse index(final CacheForm form) {
-        validate(form, messages -> {}, () -> asHtml(path_Error_ErrorJsp));
+        validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
         if (isLoginRequired()) {
             return redirectToLogin();
         }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/error/ErrorAction.java

@@ -34,6 +34,6 @@ public class ErrorAction extends FessSearchAction {
     //                                                                      ==============
     @Execute
     public HtmlResponse index(final ErrorForm form) {
-        return asHtml(path_Error_ErrorJsp);
+        return asHtml(virtualHost(path_Error_ErrorJsp));
     }
 }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/error/ErrorBadrequrestAction.java

@@ -35,6 +35,6 @@ public class ErrorBadrequrestAction extends FessSearchAction {
 
     @Execute
     public HtmlResponse index(final ErrorForm form) {
-        return asHtml(path_Error_BadRequestJsp);
+        return asHtml(virtualHost(path_Error_BadRequestJsp));
     }
 }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/error/ErrorNotfoundAction.java

@@ -35,6 +35,6 @@ public class ErrorNotfoundAction extends FessSearchAction {
 
     @Execute
     public HtmlResponse index(final ErrorForm form) {
-        return asHtml(path_Error_NotFoundJsp);
+        return asHtml(virtualHost(path_Error_NotFoundJsp));
     }
 }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/error/ErrorSystemerrorAction.java

@@ -35,6 +35,6 @@ public class ErrorSystemerrorAction extends FessSearchAction {
 
     @Execute
     public HtmlResponse index(final ErrorForm form) {
-        return asHtml(path_Error_SystemJsp);
+        return asHtml(virtualHost(path_Error_SystemJsp));
     }
 }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/go/GoAction.java

@@ -67,7 +67,7 @@ public class GoAction extends FessSearchAction {
     //                                                                      ==============
     @Execute
     public ActionResponse index(final GoForm form) throws IOException {
-        validate(form, messages -> {}, () -> asHtml(path_Error_ErrorJsp));
+        validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
         if (isLoginRequired()) {
             return redirectToLogin();
         }

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/help/HelpAction.java

@@ -45,7 +45,7 @@ public class HelpAction extends FessSearchAction {
             return redirectToLogin();
         }
 
-        return asHtml(path_HelpJsp).useForm(SearchForm.class, op -> {
+        return asHtml(virtualHost(path_HelpJsp)).useForm(SearchForm.class, op -> {
             op.setup(form -> {
                 buildFormParams(form);
             });

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/login/LoginAction.java

@@ -37,7 +37,7 @@ public class LoginAction extends FessLoginAction {
         if (form != null) {
             form.clearSecurityInfo();
         }
-        return asHtml(path_Login_IndexJsp).renderWith(data -> {
+        return asHtml(virtualHost(path_Login_IndexJsp)).renderWith(data -> {
             RenderDataUtil.register(data, "notification", fessConfig.getNotificationLogin());
             saveToken();
         });

+ 2 - 2
src/main/java/org/codelibs/fess/app/web/profile/ProfileAction.java

@@ -96,7 +96,7 @@ public class ProfileAction extends FessSearchAction {
     }
 
     protected HtmlResponse asIndexHtml() {
-        return getUserBean().map(u -> asHtml(path_Profile_IndexJsp).useForm(ProfileForm.class))
-                .orElseGet(() -> redirect(LoginAction.class));
+        return getUserBean().map(u -> asHtml(virtualHost(path_Profile_IndexJsp)).useForm(ProfileForm.class)).orElseGet(
+                () -> redirect(LoginAction.class));
     }
 }

+ 2 - 2
src/main/java/org/codelibs/fess/app/web/search/SearchAction.java

@@ -98,7 +98,7 @@ public class SearchAction extends FessSearchAction {
     }
 
     protected HtmlResponse doSearch(final SearchForm form) {
-        validate(form, messages -> {}, () -> asHtml(path_SearchJsp));
+        validate(form, messages -> {}, () -> asHtml(virtualHost(path_SearchJsp)));
         if (isLoginRequired()) {
             return redirectToLogin();
         }
@@ -126,7 +126,7 @@ public class SearchAction extends FessSearchAction {
             request.setAttribute(Constants.REQUEST_QUERIES, form.q);
             final WebRenderData renderData = new WebRenderData();
             searchService.search(form, renderData, getUserBean());
-            return asHtml(path_SearchJsp).renderWith(data -> {
+            return asHtml(virtualHost(path_SearchJsp)).renderWith(data -> {
                 renderData.register(data);
                 // favorite or thumbnail
                     if (favoriteSupport || thumbnailSupport) {

+ 1 - 1
src/main/java/org/codelibs/fess/app/web/thumbnail/ThumbnailAction.java

@@ -46,7 +46,7 @@ public class ThumbnailAction extends FessSearchAction {
     //                                                                      ==============
     @Execute
     public ActionResponse index(final ThumbnailForm form) {
-        validate(form, messages -> {}, () -> asHtml(path_Error_ErrorJsp));
+        validate(form, messages -> {}, () -> asHtml(virtualHost(path_Error_ErrorJsp)));
         if (isLoginRequired()) {
             return redirectToLogin();
         }

+ 26 - 0
src/main/java/org/codelibs/fess/mylasta/direction/FessConfig.java

@@ -136,6 +136,9 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
     /** The key of the configuration. e.g. Radmin-api */
     String API_ADMIN_ACCESS_PERMISSIONS = "api.admin.access.permissions";
 
+    /** The key of the configuration. e.g.  */
+    String VIRTUAL_HOST_HEADERS = "virtual.host.headers";
+
     /** The key of the configuration. e.g. 50 */
     String CRAWLER_DOCUMENT_MAX_SITE_LENGTH = "crawler.document.max.site.length";
 
@@ -1445,6 +1448,21 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
      */
     String getApiAdminAccessPermissions();
 
+    /**
+     * Get the value for the key 'virtual.host.headers'. <br>
+     * The value is, e.g.  <br>
+     * @return The value of found property. (NotNull: if not found, exception but basically no way)
+     */
+    String getVirtualHostHeaders();
+
+    /**
+     * Get the value for the key 'virtual.host.headers' as {@link Integer}. <br>
+     * The value is, e.g.  <br>
+     * @return The value of found property. (NotNull: if not found, exception but basically no way)
+     * @throws NumberFormatException When the property is not integer.
+     */
+    Integer getVirtualHostHeadersAsInteger();
+
     /**
      * Get the value for the key 'crawler.document.max.site.length'. <br>
      * The value is, e.g. 50 <br>
@@ -4963,6 +4981,14 @@ public interface FessConfig extends FessEnv, org.codelibs.fess.mylasta.direction
             return get(FessConfig.API_ADMIN_ACCESS_PERMISSIONS);
         }
 
+        public String getVirtualHostHeaders() {
+            return get(FessConfig.VIRTUAL_HOST_HEADERS);
+        }
+
+        public Integer getVirtualHostHeadersAsInteger() {
+            return getAsInteger(FessConfig.VIRTUAL_HOST_HEADERS);
+        }
+
         public String getCrawlerDocumentMaxSiteLength() {
             return get(FessConfig.CRAWLER_DOCUMENT_MAX_SITE_LENGTH);
         }

+ 32 - 0
src/main/java/org/codelibs/fess/mylasta/direction/FessProp.java

@@ -52,6 +52,7 @@ import org.elasticsearch.search.sort.SortBuilders;
 import org.elasticsearch.search.sort.SortOrder;
 import org.lastaflute.job.LaJob;
 import org.lastaflute.job.subsidiary.JobConcurrentExec;
+import org.lastaflute.web.response.next.HtmlNext;
 import org.lastaflute.web.util.LaRequestUtil;
 import org.lastaflute.web.validation.RequiredValidator;
 import org.lastaflute.web.validation.theme.typed.DoubleTypeValidator;
@@ -61,6 +62,8 @@ import org.lastaflute.web.validation.theme.typed.LongTypeValidator;
 
 public interface FessProp {
 
+    public static final String VIRTUAL_HOST_HEADERS = "virtualHostHeaders";
+
     public static final String QUERY_COLLAPSE_INNER_HITS_SORTS = "queryCollapseInnerHitsSorts";
 
     public static final String USER_CODE_PATTERN = "userCodePattern";
@@ -1552,4 +1555,33 @@ public interface FessProp {
         return ot;
     }
 
+    String getVirtualHostHeaders();
+
+    @SuppressWarnings("unchecked")
+    public default HtmlNext getVirtualHostPath(final HtmlNext page) {
+        Tuple3<String, String, String>[] hosts = (Tuple3<String, String, String>[]) propMap.get(VIRTUAL_HOST_HEADERS);
+        if (hosts == null) {
+            hosts = split(getVirtualHostHeaders(), "\n").get(stream -> stream.map(s -> {
+                final String[] v1 = s.split("=");
+                if (v1.length == 2) {
+                    final String[] v2 = v1[0].split(":");
+                    if (v2.length == 2) {
+                        return new Tuple3<String, String, String>(v2[0].trim(), v2[0].trim(), "/" + v1[1].trim());
+                    }
+                }
+                return null;
+            }).filter(v -> v != null).toArray(n -> new Tuple3[n]));
+            propMap.put(VIRTUAL_HOST_HEADERS, hosts);
+        }
+        final Tuple3<String, String, String>[] vHosts = hosts;
+        return LaRequestUtil.getOptionalRequest().map(req -> {
+            for (Tuple3<String, String, String> host : vHosts) {
+                final String headerValue = req.getHeader(host.getValue1());
+                if (host.getValue2().equalsIgnoreCase(headerValue)) {
+                    return new HtmlNext(host.getValue3() + page.getRoutingPath());
+                }
+            }
+            return page;
+        }).orElse(page);
+    }
 }

+ 3 - 0
src/main/resources/fess_config.properties

@@ -80,6 +80,9 @@ api.access.token.length=60
 api.access.token.required=false
 api.admin.access.permissions=Radmin-api
 
+# Virtual Host: Host:fess.codelibs.org=fess
+virtual.host.headers=
+
 # ========================================================================================
 #                                                                                   Index
 #                                                                                     ====