use of org.niis.xroad.restapi.config.audit.RestApiAuditProperty in project X-Road by nordic-institute.
the class ServiceDescriptionService method updateWsdlUrl.
/**
* Update the WSDL url of the selected ServiceDescription.
* Refreshing a WSDL is also an update of wsdl,
* it just updates to the same URL value
*
* @param serviceDescriptionType
* @param url the new url
* @return ServiceDescriptionType
* @throws WsdlParser.WsdlNotFoundException if a wsdl was not found at the url
* @throws WrongServiceDescriptionTypeException if SD with given id was not a WSDL based one
* @throws InvalidWsdlException if WSDL at the url was invalid
* @throws UnhandledWarningsException if there were warnings that were not ignored
* @throws InvalidUrlException if url was empty or invalid
* @throws InvalidServiceUrlException if the WSDL has services with invalid urls
* @throws WsdlUrlAlreadyExistsException conflict: another service description has same url
* @throws ServiceAlreadyExistsException conflict: same service exists in another SD
* @throws InterruptedException if the thread running the WSDL validator is interrupted. <b>The
* interrupted thread has already been handled with so you can choose to ignore this exception if you so
* please.</b>
*/
private ServiceDescriptionType updateWsdlUrl(ServiceDescriptionType serviceDescriptionType, String url, boolean ignoreWarnings) throws InvalidWsdlException, WsdlParser.WsdlNotFoundException, WrongServiceDescriptionTypeException, UnhandledWarningsException, ServiceAlreadyExistsException, InvalidUrlException, WsdlUrlAlreadyExistsException, InterruptedException, InvalidServiceUrlException {
auditDataHelper.put(serviceDescriptionType.getClient().getIdentifier());
Map<RestApiAuditProperty, Object> wsdlAuditData = auditDataHelper.putMap(RestApiAuditProperty.WSDL);
auditDataHelper.putServiceDescriptionUrl(serviceDescriptionType);
if (auditDataHelper.dataIsForEvent(RestApiAuditEvent.EDIT_SERVICE_DESCRIPTION)) {
auditDataHelper.put(RestApiAuditProperty.URL_NEW, url);
}
// Shouldn't be able to edit e.g. REST service descriptions with a WSDL URL
if (serviceDescriptionType.getType() != DescriptionType.WSDL) {
throw new WrongServiceDescriptionTypeException("Existing service description (id: " + serviceDescriptionType.getId().toString() + " is not WSDL");
}
ClientType client = serviceDescriptionType.getClient();
WsdlProcessingResult wsdlProcessingResult = processWsdl(client, url, serviceDescriptionType.getId());
List<ServiceType> newServices = wsdlProcessingResult.getParsedServices().stream().map(serviceInfo -> serviceInfoToServiceType(serviceInfo, serviceDescriptionType)).collect(Collectors.toList());
// find what services were added or removed
ServiceChangeChecker.ServiceChanges serviceChanges = serviceChangeChecker.check(serviceDescriptionType.getService(), newServices);
// On refresh the service properties (URL, timeout, SSL authentication) should not change
// so the existing values must be kept. This applies to a case when 1) the WSDL URL remains the same
// and 2) the WSDL URL is changed. When the WSDL URL is changed (2), the service properties must keep
// the same values in case the WSDL fetched from the new URL contains services with the same service code.
updateServicePoperties(serviceDescriptionType, newServices);
wsdlAuditData.put(RestApiAuditProperty.SERVICES_ADDED, serviceChanges.getAddedFullServiceCodes());
wsdlAuditData.put(RestApiAuditProperty.SERVICES_DELETED, serviceChanges.getRemovedFullServiceCodes());
// collect all types of warnings, throw Exception if not ignored
List<WarningDeviation> allWarnings = new ArrayList<>();
allWarnings.addAll(wsdlProcessingResult.getWarnings());
if (!serviceChanges.isEmpty()) {
allWarnings.addAll(createServiceChangeWarnings(serviceChanges));
}
if (!ignoreWarnings && !allWarnings.isEmpty()) {
throw new UnhandledWarningsException(allWarnings);
}
serviceDescriptionType.setRefreshedDate(new Date());
serviceDescriptionType.setUrl(url);
List<String> newServiceCodes = newServices.stream().map(ServiceType::getServiceCode).collect(Collectors.toList());
// service codes that will be REMOVED
List<String> removedServiceCodes = serviceChanges.getRemovedServices().stream().map(ServiceType::getServiceCode).collect(Collectors.toList());
// replace all old services with the new ones
serviceDescriptionType.getService().clear();
serviceDescriptionType.getService().addAll(newServices);
// clear AccessRights that belong to non-existing services
client.getAcl().removeIf(accessRightType -> {
String serviceCode = accessRightType.getEndpoint().getServiceCode();
return removedServiceCodes.contains(serviceCode) && !newServiceCodes.contains(serviceCode);
});
// remove related endpoints
client.getEndpoint().removeIf(endpointType -> removedServiceCodes.contains(endpointType.getServiceCode()));
// add new endpoints
Collection<EndpointType> endpointsToAdd = resolveNewEndpoints(client, serviceDescriptionType);
client.getEndpoint().addAll(endpointsToAdd);
return serviceDescriptionType;
}
Aggregations