View Javadoc
1   /*
2    * Copyright 2012-2021 CodeLibs Project and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.codelibs.fess.helper;
17  
18  import static org.codelibs.core.stream.StreamUtil.stream;
19  
20  import java.util.function.Function;
21  
22  import org.codelibs.core.lang.StringUtil;
23  import org.codelibs.core.misc.Tuple3;
24  import org.codelibs.fess.mylasta.direction.FessConfig;
25  import org.codelibs.fess.util.ComponentUtil;
26  import org.lastaflute.web.response.next.HtmlNext;
27  import org.lastaflute.web.util.LaRequestUtil;
28  
29  public class VirtualHostHelper {
30  
31      public HtmlNext getVirtualHostPath(final HtmlNext page) {
32          return processVirtualHost(s -> {
33              final String basePath = getVirtualHostBasePath(s, page);
34              return new HtmlNext(basePath + page.getRoutingPath());
35          }, page);
36      }
37  
38      protected String getVirtualHostBasePath(final String s, final HtmlNext page) {
39          return StringUtil.isBlank(s) ? StringUtil.EMPTY : "/" + s;
40      }
41  
42      public String getVirtualHostKey() {
43          return LaRequestUtil.getOptionalRequest().map(req -> (String) req.getAttribute(FessConfig.VIRTUAL_HOST_VALUE)).orElseGet(() -> {
44              final String value = processVirtualHost(s -> s, StringUtil.EMPTY);
45              LaRequestUtil.getOptionalRequest().ifPresent(req -> req.setAttribute(FessConfig.VIRTUAL_HOST_VALUE, value));
46              return value;
47          });
48      }
49  
50      protected <T> T processVirtualHost(final Function<String, T> func, final T defaultValue) {
51          final Tuple3<String, String, String>[] vHosts = ComponentUtil.getFessConfig().getVirtualHosts();
52          return LaRequestUtil.getOptionalRequest().map(req -> {
53              for (final Tuple3<String, String, String> host : vHosts) {
54                  final String headerValue = req.getHeader(host.getValue1());
55                  if (host.getValue2().equalsIgnoreCase(headerValue)) {
56                      return func.apply(host.getValue3());
57                  }
58              }
59              return defaultValue;
60          }).orElse(defaultValue);
61      }
62  
63      public String[] getVirtualHostPaths() {
64          return stream(ComponentUtil.getFessConfig().getVirtualHosts())
65                  .get(stream -> stream.map(h -> "/" + h.getValue3()).toArray(n -> new String[n]));
66      }
67  }