Search in sources :

Example 1 with ValidationException

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);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) JAXBException(javax.xml.bind.JAXBException) URI(java.net.URI) URL(java.net.URL) TransformerException(javax.xml.transform.TransformerException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) URISyntaxException(java.net.URISyntaxException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) SAXException(org.xml.sax.SAXException) ValidationException(org.killbill.xmlloader.ValidationException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) PriceOverride(org.killbill.billing.catalog.override.PriceOverride) StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)

Example 2 with ValidationException

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);
    }
}
Also used : ValidationException(org.killbill.xmlloader.ValidationException) ValidationErrors(org.killbill.xmlloader.ValidationErrors) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) StaticCatalog(org.killbill.billing.catalog.api.StaticCatalog) URI(java.net.URI) SAXException(org.xml.sax.SAXException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) TransformerException(javax.xml.transform.TransformerException)

Example 3 with ValidationException

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);
    }
}
Also used : StandaloneCatalogWithPriceOverride(org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride) ValidationException(org.killbill.xmlloader.ValidationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) InvalidConfigException(org.killbill.billing.catalog.api.InvalidConfigException) URI(java.net.URI) SAXException(org.xml.sax.SAXException) VersionedCatalog(org.killbill.billing.catalog.VersionedCatalog) ByteArrayInputStream(java.io.ByteArrayInputStream) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

IOException (java.io.IOException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 JAXBException (javax.xml.bind.JAXBException)3 TransformerException (javax.xml.transform.TransformerException)3 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)3 VersionedCatalog (org.killbill.billing.catalog.VersionedCatalog)3 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)3 InvalidConfigException (org.killbill.billing.catalog.api.InvalidConfigException)3 ValidationException (org.killbill.xmlloader.ValidationException)3 SAXException (org.xml.sax.SAXException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 StandaloneCatalogWithPriceOverride (org.killbill.billing.catalog.StandaloneCatalogWithPriceOverride)2 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 StaticCatalog (org.killbill.billing.catalog.api.StaticCatalog)1 PriceOverride (org.killbill.billing.catalog.override.PriceOverride)1 TenantApiException (org.killbill.billing.tenant.api.TenantApiException)1