1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.codelibs.fess.es.config.exentity;
17
18 import java.util.Map;
19
20 import org.apache.http.auth.AuthScheme;
21 import org.apache.http.auth.AuthScope;
22 import org.apache.http.auth.Credentials;
23 import org.apache.http.auth.NTCredentials;
24 import org.apache.http.auth.UsernamePasswordCredentials;
25 import org.apache.http.impl.auth.BasicScheme;
26 import org.apache.http.impl.auth.DigestScheme;
27 import org.apache.http.impl.auth.NTLMScheme;
28 import org.codelibs.core.lang.StringUtil;
29 import org.codelibs.fess.Constants;
30 import org.codelibs.fess.app.service.WebConfigService;
31 import org.codelibs.fess.crawler.client.http.Authentication;
32 import org.codelibs.fess.crawler.client.http.form.FormScheme;
33 import org.codelibs.fess.crawler.client.http.impl.AuthenticationImpl;
34 import org.codelibs.fess.crawler.client.http.ntlm.JcifsEngine;
35 import org.codelibs.fess.crawler.exception.CrawlerSystemException;
36 import org.codelibs.fess.es.config.bsentity.BsWebAuthentication;
37 import org.codelibs.fess.util.ComponentUtil;
38 import org.codelibs.fess.util.ParameterUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45 public class WebAuthentication extends BsWebAuthentication {
46
47 private static final long serialVersionUID = 1L;
48
49 private static final Logger logger = LoggerFactory.getLogger(WebAuthentication.class);
50
51 private WebConfig webConfig;
52
53 public Authentication getAuthentication() {
54 return new AuthenticationImpl(getAuthScope(), getCredentials(), getAuthScheme());
55 }
56
57 private AuthScheme getAuthScheme() {
58 final String scheme = getProtocolScheme();
59 if (Constants.BASIC.equals(scheme)) {
60 return new BasicScheme();
61 } else if (Constants.DIGEST.equals(scheme)) {
62 return new DigestScheme();
63 } else if (Constants.NTLM.equals(scheme)) {
64 return new NTLMScheme(new JcifsEngine());
65 } else if (Constants.FORM.equals(scheme)) {
66 final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
67 return new FormScheme(parameterMap);
68 }
69 return null;
70 }
71
72 private AuthScope getAuthScope() {
73 if (StringUtil.isBlank(getHostname())) {
74 return AuthScope.ANY;
75 }
76
77 int p;
78 if (getPort() == null) {
79 p = AuthScope.ANY_PORT;
80 } else {
81 p = getPort().intValue();
82 }
83
84 String r = getAuthRealm();
85 if (StringUtil.isBlank(r)) {
86 r = AuthScope.ANY_REALM;
87 }
88
89 String s = getProtocolScheme();
90 if (StringUtil.isBlank(s) || Constants.NTLM.equals(s)) {
91 s = AuthScope.ANY_SCHEME;
92 }
93
94 return new AuthScope(getHostname(), p, r, s);
95 }
96
97 private Credentials getCredentials() {
98 if (StringUtil.isEmpty(getUsername())) {
99 throw new CrawlerSystemException("username is empty.");
100 }
101
102 if (Constants.NTLM.equals(getProtocolScheme())) {
103 final Map<String, String> parameterMap = ParameterUtil.parse(getParameters());
104 final String workstation = parameterMap.get("workstation");
105 final String domain = parameterMap.get("domain");
106 return new NTCredentials(getUsername(), getPassword(), workstation == null ? StringUtil.EMPTY : workstation,
107 domain == null ? StringUtil.EMPTY : domain);
108 }
109
110 return new UsernamePasswordCredentials(getUsername(), getPassword() == null ? StringUtil.EMPTY : getPassword());
111 }
112
113 public WebConfig getWebConfig() {
114 if (webConfig == null) {
115 final WebConfigService webConfigService = ComponentUtil.getComponent(WebConfigService.class);
116 try {
117 webConfig = webConfigService.getWebConfig(getWebConfigId()).get();
118 } catch (final Exception e) {
119 logger.warn("Web Config " + getWebConfigId() + " does not exist.", e);
120 }
121 }
122 return webConfig;
123 }
124
125 public String getId() {
126 return asDocMeta().id();
127 }
128
129 public void setId(final String id) {
130 asDocMeta().id(id);
131 }
132
133 public Long getVersionNo() {
134 return asDocMeta().version();
135 }
136
137 public void setVersionNo(final Long version) {
138 asDocMeta().version(version);
139 }
140
141 @Override
142 public String toString() {
143 return "WebAuthentication [webConfig=" + webConfig + ", authRealm=" + authRealm + ", createdBy=" + createdBy + ", createdTime="
144 + createdTime + ", hostname=" + hostname + ", parameters=" + parameters + ", port=" + port + ", protocolScheme="
145 + protocolScheme + ", updatedBy=" + updatedBy + ", updatedTime=" + updatedTime + ", username=" + username
146 + ", webConfigId=" + webConfigId + ", docMeta=" + docMeta + "]";
147 }
148 }