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 java.nio.charset.StandardCharsets;
19  
20  import org.codelibs.core.lang.StringUtil;
21  import org.codelibs.curl.Curl.Method;
22  import org.codelibs.curl.CurlRequest;
23  import org.codelibs.fess.mylasta.direction.FessConfig;
24  import org.codelibs.fess.util.ComponentUtil;
25  import org.codelibs.fess.util.ResourceUtil;
26  
27  public class CurlHelper {
28  
29      public CurlRequest get(final String path) {
30          return request(Method.GET, path).header("Content-Type", "application/json");
31      }
32  
33      public CurlRequest post(final String path) {
34          return request(Method.POST, path).header("Content-Type", "application/json");
35      }
36  
37      public CurlRequest put(final String path) {
38          return request(Method.PUT, path).header("Content-Type", "application/json");
39      }
40  
41      public CurlRequest delete(final String path) {
42          return request(Method.DELETE, path).header("Content-Type", "application/json");
43      }
44  
45      public CurlRequest request(final Method method, final String path) {
46          final CurlRequest request = new CurlRequest(method, ResourceUtil.getFesenHttpUrl() + path);
47          final FessConfig fessConfig = ComponentUtil.getFessConfig();
48          final String username = fessConfig.getFesenUsername();
49          final String password = fessConfig.getFesenPassword();
50          if (StringUtil.isNotBlank(username) && StringUtil.isNotBlank(password)) {
51              final String value = username + ":" + password;
52              final String basicAuth = "Basic " + java.util.Base64.getEncoder().encodeToString(value.getBytes(StandardCharsets.UTF_8));
53              request.header("Authorization", basicAuth);
54          }
55          return request;
56      }
57  }