Search in sources :

Example 1 with WebAuthenticationService

use of org.codelibs.fess.app.service.WebAuthenticationService in project fess by codelibs.

the class WebConfig method initializeClientFactory.

@Override
public Map<String, Object> initializeClientFactory(final CrawlerClientFactory clientFactory) {
    final WebAuthenticationService webAuthenticationService = ComponentUtil.getComponent(WebAuthenticationService.class);
    final RequestHeaderService requestHeaderService = ComponentUtil.getComponent(RequestHeaderService.class);
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    // HttpClient Parameters
    final Map<String, Object> paramMap = new HashMap<>();
    clientFactory.setInitParameterMap(paramMap);
    final Map<String, String> clientConfigMap = getConfigParameterMap(ConfigName.CLIENT);
    if (clientConfigMap != null) {
        paramMap.putAll(clientConfigMap);
    }
    // robots txt enabled
    if (paramMap.get(HcHttpClient.ROBOTS_TXT_ENABLED_PROPERTY) == null) {
        paramMap.put(HcHttpClient.ROBOTS_TXT_ENABLED_PROPERTY, !fessConfig.isCrawlerIgnoreRobotsTxt());
    }
    final String userAgent = getUserAgent();
    if (StringUtil.isNotBlank(userAgent)) {
        paramMap.put(HcHttpClient.USER_AGENT_PROPERTY, userAgent);
    }
    final List<WebAuthentication> webAuthList = webAuthenticationService.getWebAuthenticationList(getId());
    final List<Authentication> basicAuthList = new ArrayList<>();
    for (final WebAuthentication webAuth : webAuthList) {
        basicAuthList.add(webAuth.getAuthentication());
    }
    paramMap.put(HcHttpClient.BASIC_AUTHENTICATIONS_PROPERTY, basicAuthList.toArray(new Authentication[basicAuthList.size()]));
    // request header
    final List<RequestHeader> requestHeaderList = requestHeaderService.getRequestHeaderList(getId());
    final List<org.codelibs.fess.crawler.client.http.RequestHeader> rhList = new ArrayList<>();
    for (final RequestHeader requestHeader : requestHeaderList) {
        rhList.add(requestHeader.getCrawlerRequestHeader());
    }
    paramMap.put(HcHttpClient.REQUERT_HEADERS_PROPERTY, rhList.toArray(new org.codelibs.fess.crawler.client.http.RequestHeader[rhList.size()]));
    // proxy credentials
    if (paramMap.get("proxyUsername") != null && paramMap.get("proxyPassword") != null) {
        paramMap.put(HcHttpClient.PROXY_CREDENTIALS_PROPERTY, new UsernamePasswordCredentials(paramMap.remove("proxyUsername").toString(), paramMap.remove("proxyPassword").toString()));
    }
    return paramMap;
}
Also used : WebAuthenticationService(org.codelibs.fess.app.service.WebAuthenticationService) RequestHeaderService(org.codelibs.fess.app.service.RequestHeaderService) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Authentication(org.codelibs.fess.crawler.client.http.Authentication)

Example 2 with WebAuthenticationService

use of org.codelibs.fess.app.service.WebAuthenticationService in project fess by codelibs.

the class WebConfig method initializeClientFactory.

@Override
public CrawlerClientFactory initializeClientFactory(final Supplier<CrawlerClientFactory> creator) {
    if (crawlerClientFactory != null) {
        return crawlerClientFactory;
    }
    final CrawlerClientFactory factory = creator.get();
    final WebAuthenticationService webAuthenticationService = ComponentUtil.getComponent(WebAuthenticationService.class);
    final RequestHeaderService requestHeaderService = ComponentUtil.getComponent(RequestHeaderService.class);
    final FessConfig fessConfig = ComponentUtil.getFessConfig();
    // HttpClient Parameters
    final Map<String, Object> paramMap = new HashMap<>();
    factory.setInitParameterMap(paramMap);
    final Map<String, String> clientConfigMap = getConfigParameterMap(ConfigName.CLIENT);
    if (clientConfigMap != null) {
        paramMap.putAll(clientConfigMap);
    }
    // robots txt enabled
    if (paramMap.get(Param.Client.ROBOTS_TXT_ENABLED) == null) {
        paramMap.put(Param.Client.ROBOTS_TXT_ENABLED, !fessConfig.isCrawlerIgnoreRobotsTxt());
    }
    final String userAgent = getUserAgent();
    if (StringUtil.isNotBlank(userAgent)) {
        paramMap.put(Client.USER_AGENT, userAgent);
    }
    final List<WebAuthentication> webAuthList = webAuthenticationService.getWebAuthenticationList(getId());
    final List<Authentication> basicAuthList = new ArrayList<>();
    for (final WebAuthentication webAuth : webAuthList) {
        basicAuthList.add(webAuth.getAuthentication());
    }
    paramMap.put(HcHttpClient.BASIC_AUTHENTICATIONS_PROPERTY, basicAuthList.toArray(new Authentication[basicAuthList.size()]));
    // request header
    final List<RequestHeader> requestHeaderList = requestHeaderService.getRequestHeaderList(getId());
    final List<org.codelibs.fess.crawler.client.http.RequestHeader> rhList = new ArrayList<>();
    for (final RequestHeader requestHeader : requestHeaderList) {
        rhList.add(requestHeader.getCrawlerRequestHeader());
    }
    paramMap.put(HcHttpClient.REQUERT_HEADERS_PROPERTY, rhList.toArray(new org.codelibs.fess.crawler.client.http.RequestHeader[rhList.size()]));
    final String proxyHost = (String) paramMap.get(Param.Client.PROXY_HOST);
    final String proxyPort = (String) paramMap.get(Param.Client.PROXY_PORT);
    if (StringUtil.isNotBlank(proxyHost) && StringUtil.isNotBlank(proxyPort)) {
        // proxy credentials
        if (paramMap.get(Param.Client.PROXY_USERNAME) != null && paramMap.get(Param.Client.PROXY_PASSWORD) != null) {
            paramMap.put(HcHttpClient.PROXY_CREDENTIALS_PROPERTY, new UsernamePasswordCredentials(paramMap.remove(Param.Client.PROXY_USERNAME).toString(), paramMap.remove(Param.Client.PROXY_PASSWORD).toString()));
        }
    } else {
        initializeDefaultHttpProxy(paramMap);
    }
    crawlerClientFactory = factory;
    return factory;
}
Also used : WebAuthenticationService(org.codelibs.fess.app.service.WebAuthenticationService) RequestHeaderService(org.codelibs.fess.app.service.RequestHeaderService) HashMap(java.util.HashMap) CrawlerClientFactory(org.codelibs.fess.crawler.client.CrawlerClientFactory) ArrayList(java.util.ArrayList) FessConfig(org.codelibs.fess.mylasta.direction.FessConfig) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Authentication(org.codelibs.fess.crawler.client.http.Authentication)

Aggregations

ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)2 RequestHeaderService (org.codelibs.fess.app.service.RequestHeaderService)2 WebAuthenticationService (org.codelibs.fess.app.service.WebAuthenticationService)2 Authentication (org.codelibs.fess.crawler.client.http.Authentication)2 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)2 CrawlerClientFactory (org.codelibs.fess.crawler.client.CrawlerClientFactory)1