use of eu.bcvsolutions.idm.ic.exception.IcRemoteServerException in project CzechIdMng by bcvsolutions.
the class SysSystemController method getSupportedTypes.
/**
* Returns all registered connector types.
*
* @return connector types
*/
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/search/supported")
@PreAuthorize("hasAuthority('" + AccGroupPermission.SYSTEM_READ + "')")
@ApiOperation(value = "Get all supported connector types", nickname = "getSupportedConnectorTypes", tags = { SysSystemController.TAG }, authorizations = { @Authorization(value = SwaggerConfig.AUTHENTICATION_BASIC, scopes = { @AuthorizationScope(scope = AccGroupPermission.SYSTEM_READ, description = "") }), @Authorization(value = SwaggerConfig.AUTHENTICATION_CIDMST, scopes = { @AuthorizationScope(scope = AccGroupPermission.SYSTEM_READ, description = "") }) })
public Resources<ConnectorTypeDto> getSupportedTypes() {
Map<SysConnectorServerDto, List<IcConnectorInfo>> allConnectorInfos = new LinkedHashMap<>();
// All remote connectors - optionally, but with higher priority.
remoteServerService.find(null).forEach(connectorServer -> {
for (IcConfigurationService config : icConfiguration.getIcConfigs().values()) {
try {
connectorServer.setPassword(remoteServerService.getPassword(connectorServer.getId()));
Set<IcConnectorInfo> availableRemoteConnectors = config.getAvailableRemoteConnectors(connectorServer);
if (CollectionUtils.isNotEmpty(availableRemoteConnectors)) {
allConnectorInfos.put(connectorServer, Lists.newArrayList(availableRemoteConnectors));
}
} catch (IcInvalidCredentialException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_INVALID_CREDENTIAL, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcServerNotFoundException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_NOT_FOUND, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcCantConnectException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_CANT_CONNECT, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcRemoteServerException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_UNEXPECTED_ERROR, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
}
}
});
// Local connectors
Map<String, Set<IcConnectorInfo>> availableLocalConnectors = icConfiguration.getAvailableLocalConnectors();
if (availableLocalConnectors != null) {
List<IcConnectorInfo> localConnectorInfos = Lists.newArrayList();
availableLocalConnectors.values().forEach(infos -> {
localConnectorInfos.addAll(infos);
});
SysConnectorServerDto localServer = new SysConnectorServerDto();
localServer.setLocal(true);
allConnectorInfos.put(localServer, localConnectorInfos);
}
//
List<ConnectorTypeDto> resolvedConnectorTypes = Lists.newArrayListWithExpectedSize(allConnectorInfos.values().stream().mapToInt(List::size).sum());
for (ConnectorType supportedConnectorType : connectorManager.getSupportedTypes()) {
// remote connector has higher priority => linked hash map => find first
// Find connector info and set version to the connectorTypeDto.
SysConnectorServerDto connectorServer = null;
IcConnectorInfo info = null;
for (Entry<SysConnectorServerDto, List<IcConnectorInfo>> entry : allConnectorInfos.entrySet()) {
for (IcConnectorInfo connectorInfo : entry.getValue()) {
if (supportedConnectorType.getConnectorName().equals(connectorInfo.getConnectorKey().getConnectorName())) {
connectorServer = entry.getKey();
info = connectorInfo;
break;
}
}
if (info != null) {
break;
}
}
if (info == null) {
// default connector types are resolved bellow
continue;
}
ConnectorTypeDto connectorType = connectorManager.convertTypeToDto(supportedConnectorType);
if (connectorServer != null) {
connectorType.setRemoteServer(connectorServer.getId());
}
connectorType.setLocal(connectorType.getRemoteServer() == null);
connectorType.setVersion(info.getConnectorKey().getBundleVersion());
connectorType.setName(info.getConnectorDisplayName());
resolvedConnectorTypes.add(connectorType);
}
// Find connectors without extension (specific connector type).
List<ConnectorTypeDto> defaultConnectorTypes = Lists.newArrayList();
for (Entry<SysConnectorServerDto, List<IcConnectorInfo>> entry : allConnectorInfos.entrySet()) {
SysConnectorServerDto connectorServer = entry.getKey();
for (IcConnectorInfo connectorInfo : entry.getValue()) {
ConnectorTypeDto connectorType = connectorManager.convertIcConnectorInfoToDto(connectorInfo);
if (!resolvedConnectorTypes.stream().anyMatch(supportedType -> supportedType.getConnectorName().equals(connectorType.getConnectorName()) && supportedType.isHideParentConnector())) {
if (connectorServer != null) {
connectorType.setRemoteServer(connectorServer.getId());
}
connectorType.setLocal(connectorType.getRemoteServer() == null);
defaultConnectorTypes.add(connectorType);
}
}
}
resolvedConnectorTypes.addAll(defaultConnectorTypes);
return new Resources<>(resolvedConnectorTypes.stream().sorted(Comparator.comparing(ConnectorTypeDto::getOrder)).collect(Collectors.toList()));
}
use of eu.bcvsolutions.idm.ic.exception.IcRemoteServerException in project CzechIdMng by bcvsolutions.
the class DefaultConnectorManager method findConnectorKey.
@Override
public IcConnectorKey findConnectorKey(ConnectorTypeDto connectorType) {
Assert.notNull(connectorType, "Connector type cannot be null!");
String connectorName = connectorType.getConnectorName();
Assert.notNull(connectorName, "Connector name cannot be null!");
UUID remoteServer = connectorType.getRemoteServer();
//
if (remoteServer == null) {
// local
Map<String, Set<IcConnectorInfo>> availableLocalConnectors = icConfiguration.getAvailableLocalConnectors();
if (availableLocalConnectors == null) {
return null;
}
List<IcConnectorInfo> connectorInfos = Lists.newArrayList();
for (Set<IcConnectorInfo> icConnectorInfos : availableLocalConnectors.values()) {
connectorInfos.addAll(icConnectorInfos);
}
IcConnectorInfo connectorInfo = connectorInfos.stream().filter(info -> connectorName.equals(info.getConnectorKey().getConnectorName())).findFirst().orElse(null);
return connectorInfo != null ? connectorInfo.getConnectorKey() : null;
}
// remote connector
try {
SysConnectorServerDto connectorServer = remoteServerService.get(remoteServer);
if (connectorServer == null) {
return null;
}
for (IcConfigurationService config : icConfiguration.getIcConfigs().values()) {
connectorServer.setPassword(remoteServerService.getPassword(connectorServer.getId()));
Set<IcConnectorInfo> availableRemoteConnectors = config.getAvailableRemoteConnectors(connectorServer);
if (CollectionUtils.isNotEmpty(availableRemoteConnectors)) {
IcConnectorInfo connectorInfo = availableRemoteConnectors.stream().filter(info -> connectorName.equals(info.getConnectorKey().getConnectorName())).findFirst().orElse(null);
if (connectorInfo != null) {
return connectorInfo.getConnectorKey();
}
}
}
} catch (IcInvalidCredentialException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_INVALID_CREDENTIAL, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcServerNotFoundException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_NOT_FOUND, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcCantConnectException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_CANT_CONNECT, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
} catch (IcRemoteServerException e) {
ExceptionUtils.log(LOG, new ResultCodeException(AccResultCode.REMOTE_SERVER_UNEXPECTED_ERROR, ImmutableMap.of("server", e.getHost() + ":" + e.getPort()), e));
}
return null;
}
Aggregations