Search in sources :

Example 1 with TenantApiException

use of org.killbill.billing.tenant.api.TenantApiException in project killbill by killbill.

the class PushNotificationListener method triggerPushNotifications.

@AllowConcurrentEvents
@Subscribe
public void triggerPushNotifications(final ExtBusEvent event) {
    final TenantContext context = contextFactory.createTenantContext(event.getTenantId());
    try {
        final List<String> callbacks = getCallbacksForTenant(context);
        if (callbacks.isEmpty()) {
            // Optimization - see https://github.com/killbill/killbill/issues/297
            return;
        }
        dispatchCallback(event.getTenantId(), event, callbacks);
    } catch (final TenantApiException e) {
        log.warn("Failed to retrieve push notification callback for tenant {}", event.getTenantId());
    } catch (final IOException e) {
        log.warn("Failed to retrieve push notification callback for tenant {}", event.getTenantId());
    }
}
Also used : TenantApiException(org.killbill.billing.tenant.api.TenantApiException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantContext(org.killbill.billing.util.callcontext.TenantContext) IOException(java.io.IOException) AllowConcurrentEvents(com.google.common.eventbus.AllowConcurrentEvents) Subscribe(com.google.common.eventbus.Subscribe)

Example 2 with TenantApiException

use of org.killbill.billing.tenant.api.TenantApiException in project killbill by killbill.

the class DefaultCatalogUserApi method createDefaultEmptyCatalog.

@Override
public void createDefaultEmptyCatalog(final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
        final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext);
        final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(BillingMode.IN_ADVANCE, effectiveDate, null);
        catalogCache.clearCatalog(internalTenantContext);
        tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext);
    } catch (TenantApiException e) {
        throw new CatalogApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) CatalogUpdater(org.killbill.billing.catalog.CatalogUpdater)

Example 3 with TenantApiException

use of org.killbill.billing.tenant.api.TenantApiException 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 4 with TenantApiException

use of org.killbill.billing.tenant.api.TenantApiException in project killbill by killbill.

the class DefaultCatalogUserApi method addSimplePlan.

@Override
public void addSimplePlan(final SimplePlanDescriptor descriptor, final DateTime effectiveDate, final CallContext callContext) throws CatalogApiException {
    try {
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
        final StandaloneCatalog currentCatalog = getCurrentStandaloneCatalogForTenant(internalTenantContext);
        final CatalogUpdater catalogUpdater = (currentCatalog != null) ? new CatalogUpdater(currentCatalog) : new CatalogUpdater(BillingMode.IN_ADVANCE, effectiveDate, descriptor.getCurrency());
        catalogUpdater.addSimplePlanDescriptor(descriptor);
        catalogCache.clearCatalog(internalTenantContext);
        tenantApi.updateTenantKeyValue(TenantKey.CATALOG.toString(), catalogUpdater.getCatalogXML(), callContext);
    } catch (TenantApiException e) {
        throw new CatalogApiException(e);
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) StandaloneCatalog(org.killbill.billing.catalog.StandaloneCatalog) CatalogApiException(org.killbill.billing.catalog.api.CatalogApiException) CatalogUpdater(org.killbill.billing.catalog.CatalogUpdater)

Example 5 with TenantApiException

use of org.killbill.billing.tenant.api.TenantApiException in project killbill by killbill.

the class TenantFilter method doFilter.

@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    // Lookup tenant information in the headers
    String apiKey = null;
    String apiSecret = null;
    if (request instanceof HttpServletRequest) {
        final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
        apiKey = httpServletRequest.getHeader(JaxrsResource.HDR_API_KEY);
        apiSecret = httpServletRequest.getHeader(JaxrsResource.HDR_API_SECRET);
    }
    // Multi-tenancy is enabled if this filter is installed, we can't continue without credentials
    if (apiKey == null || apiSecret == null) {
        final String errorMessage = String.format("Make sure to set the %s and %s headers", JaxrsResource.HDR_API_KEY, JaxrsResource.HDR_API_SECRET);
        handleAuthenticationError(errorMessage, chain, request, response);
        return;
    }
    // Verify the apiKey/apiSecret combo
    final AuthenticationToken token = new UsernamePasswordToken(apiKey, apiSecret);
    try {
        modularRealmAuthenticator.authenticate(token);
    } catch (final AuthenticationException e) {
        final String errorMessage = e.getLocalizedMessage();
        handleAuthenticationError(errorMessage, chain, request, response);
        return;
    }
    try {
        // Load the tenant in the request object (apiKey is unique across tenants)
        final Tenant tenant = tenantUserApi.getTenantByApiKey(apiKey);
        request.setAttribute(TENANT, tenant);
        // Create a dummy context, to set the MDC very early for LoggingFilter
        context.createContext(request);
        chain.doFilter(request, response);
    } catch (final TenantApiException e) {
        // Should never happen since Shiro validated the credentials?
        log.error("Couldn't find the tenant? - should never happen!", e);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) AuthenticationToken(org.apache.shiro.authc.AuthenticationToken) Tenant(org.killbill.billing.tenant.api.Tenant) AuthenticationException(org.apache.shiro.authc.AuthenticationException) TenantApiException(org.killbill.billing.tenant.api.TenantApiException) UsernamePasswordToken(org.apache.shiro.authc.UsernamePasswordToken)

Aggregations

TenantApiException (org.killbill.billing.tenant.api.TenantApiException)8 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)5 StandaloneCatalog (org.killbill.billing.catalog.StandaloneCatalog)3 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)3 IOException (java.io.IOException)2 CatalogUpdater (org.killbill.billing.catalog.CatalogUpdater)2 AllowConcurrentEvents (com.google.common.eventbus.AllowConcurrentEvents)1 Subscribe (com.google.common.eventbus.Subscribe)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JAXBException (javax.xml.bind.JAXBException)1 TransformerException (javax.xml.transform.TransformerException)1 AuthenticationException (org.apache.shiro.authc.AuthenticationException)1 AuthenticationToken (org.apache.shiro.authc.AuthenticationToken)1 UsernamePasswordToken (org.apache.shiro.authc.UsernamePasswordToken)1 SimpleHash (org.apache.shiro.crypto.hash.SimpleHash)1 ByteSource (org.apache.shiro.util.ByteSource)1