use of org.killbill.xmlloader.ValidationException in project killbill by killbill.
the class VersionedCatalogLoader method loadDefaultCatalog.
@Override
public VersionedCatalog loadDefaultCatalog(final String uriString) throws CatalogApiException {
try {
final List<URI> xmlURIs;
if (uriString.endsWith(XML_EXTENSION)) {
// Assume its an xml file
xmlURIs = new ArrayList<URI>();
xmlURIs.add(new URI(uriString));
} else {
// Assume its a directory
final URL url = getURLFromString(uriString);
final String directoryContents = UriAccessor.accessUriAsString(uriString);
xmlURIs = findXmlReferences(directoryContents, url);
}
final VersionedCatalog result = new VersionedCatalog(clock);
for (final URI u : xmlURIs) {
final StandaloneCatalog catalog = XMLLoader.getObjectFromUri(u, StandaloneCatalog.class);
result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, InternalCallContextFactory.INTERNAL_TENANT_RECORD_ID, internalCallContextFactory));
}
// Perform initialization and validation for VersionedCatalog
XMLLoader.initializeAndValidate(new URI(uriString), result);
return result;
} catch (final ValidationException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (final JAXBException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (IllegalArgumentException e) {
logger.warn("Failed to load default catalog", e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_DEFAULT, uriString);
} catch (Exception e) {
logger.warn("Failed to load default catalog", e);
throw new IllegalStateException(e);
}
}
use of org.killbill.xmlloader.ValidationException in project killbill by killbill.
the class DefaultCatalogUserApi method uploadCatalog.
@Override
public void uploadCatalog(final String catalogXML, final CallContext callContext) throws CatalogApiException {
final InternalTenantContext internalTenantContext = createInternalTenantContext(callContext);
try {
final VersionedCatalog versionedCatalog = (VersionedCatalog) catalogService.getFullCatalog(false, true, internalTenantContext);
// Validation purpose: Will throw if bad XML or catalog validation fails
final InputStream stream = new ByteArrayInputStream(catalogXML.getBytes());
final StaticCatalog newCatalogVersion = XMLLoader.getObjectFromStream(new URI("dummy"), stream, StandaloneCatalog.class);
if (versionedCatalog != null) {
// currentCatalog.getCatalogName() could be null if tenant was created with a default catalog
if (versionedCatalog.getCatalogName() != null && !newCatalogVersion.getCatalogName().equals(versionedCatalog.getCatalogName())) {
final ValidationErrors errors = new ValidationErrors();
errors.add(String.format("Catalog name '%s' should match previous catalog name '%s'", newCatalogVersion.getCatalogName(), versionedCatalog.getCatalogName()), new URI("dummy"), StandaloneCatalog.class, "");
// Bummer ValidationException CTOR is private to package...
//final ValidationException validationException = new ValidationException(errors);
//throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
logger.info("Failed to load new catalog version: " + errors.toString());
throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
}
for (StandaloneCatalog c : versionedCatalog.getVersions()) {
if (c.getEffectiveDate().compareTo(newCatalogVersion.getEffectiveDate()) == 0) {
final ValidationErrors errors = new ValidationErrors();
errors.add(String.format("Catalog version for effectiveDate '%s' already exists", newCatalogVersion.getEffectiveDate()), new URI("dummy"), StandaloneCatalog.class, "");
// Bummer ValidationException CTOR is private to package...
//final ValidationException validationException = new ValidationException(errors);
//throw new CatalogApiException(errors, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
logger.info("Failed to load new catalog version: " + errors.toString());
throw new CatalogApiException(ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
}
}
}
catalogCache.clearCatalog(internalTenantContext);
tenantApi.addTenantKeyValue(TenantKey.CATALOG.toString(), catalogXML, callContext);
} catch (final TenantApiException e) {
throw new CatalogApiException(e);
} catch (final ValidationException e) {
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
} catch (final JAXBException e) {
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, internalTenantContext.getTenantRecordId());
} catch (final IOException e) {
throw new IllegalStateException(e);
} catch (final TransformerException e) {
throw new IllegalStateException(e);
} catch (final URISyntaxException e) {
throw new IllegalStateException(e);
} catch (final SAXException e) {
throw new IllegalStateException(e);
} catch (final InvalidConfigException e) {
throw new IllegalStateException(e);
}
}
use of org.killbill.xmlloader.ValidationException in project killbill by killbill.
the class VersionedCatalogLoader method load.
public VersionedCatalog load(final Iterable<String> catalogXMLs, final boolean filterTemplateCatalog, final Long tenantRecordId) throws CatalogApiException {
final VersionedCatalog result = new VersionedCatalog(clock);
final URI uri;
try {
uri = new URI("/tenantCatalog");
for (final String cur : catalogXMLs) {
final InputStream curCatalogStream = new ByteArrayInputStream(cur.getBytes());
final StandaloneCatalog catalog = XMLLoader.getObjectFromStream(uri, curCatalogStream, StandaloneCatalog.class);
if (!filterTemplateCatalog || !catalog.isTemplateCatalog()) {
result.add(new StandaloneCatalogWithPriceOverride(catalog, priceOverride, tenantRecordId, internalCallContextFactory));
}
}
// Perform initialization and validation for VersionedCatalog
XMLLoader.initializeAndValidate(uri, result);
return result;
} catch (final ValidationException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
} catch (final JAXBException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new CatalogApiException(e, ErrorCode.CAT_INVALID_FOR_TENANT, tenantRecordId);
} catch (final IOException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final TransformerException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final URISyntaxException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final SAXException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
} catch (final InvalidConfigException e) {
logger.warn("Failed to load catalog for tenantRecordId='{}'", tenantRecordId, e);
throw new IllegalStateException(e);
}
}
Aggregations