use of org.killbill.commons.metrics.TimedResource in project killbill by killbill.
the class InvoiceResource method getPayments.
@TimedResource
@GET
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + PAYMENTS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve payments associated with an invoice", response = InvoicePaymentJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid invoice id supplied"), @ApiResponse(code = 404, message = "Invoice not found") })
public Response getPayments(@PathParam("invoiceId") final String invoiceId, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_WITH_ATTEMPTS) @DefaultValue("false") final Boolean withAttempts, @javax.ws.rs.core.Context final HttpServletRequest request) throws PaymentApiException, InvoiceApiException {
final TenantContext tenantContext = context.createContext(request);
final Invoice invoice = invoiceApi.getInvoice(UUID.fromString(invoiceId), tenantContext);
// Extract unique set of paymentId for this invoice
final Set<UUID> invoicePaymentIds = ImmutableSet.copyOf(Iterables.transform(invoice.getPayments(), new Function<InvoicePayment, UUID>() {
@Override
public UUID apply(final InvoicePayment input) {
return input.getPaymentId();
}
}));
if (invoicePaymentIds.isEmpty()) {
return Response.status(Status.OK).entity(ImmutableList.<InvoicePaymentJson>of()).build();
}
final List<Payment> payments = new ArrayList<Payment>();
for (final UUID paymentId : invoicePaymentIds) {
final Payment payment = paymentApi.getPayment(paymentId, withPluginInfo, withAttempts, ImmutableList.<PluginProperty>of(), tenantContext);
payments.add(payment);
}
final Iterable<InvoicePaymentJson> result = INVOICE_PAYMENT_ORDERING.sortedCopy(Iterables.transform(payments, new Function<Payment, InvoicePaymentJson>() {
@Override
public InvoicePaymentJson apply(final Payment input) {
return new InvoicePaymentJson(input, invoice.getId(), null);
}
}));
return Response.status(Status.OK).entity(result).build();
}
use of org.killbill.commons.metrics.TimedResource in project killbill by killbill.
the class InvoiceResource method getTags.
@TimedResource
@GET
@Path("/{invoiceId:" + UUID_PATTERN + "}/" + TAGS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve invoice tags", response = TagJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid invoice id supplied"), @ApiResponse(code = 404, message = "Invoice not found") })
public Response getTags(@PathParam(ID_PARAM_NAME) final String invoiceIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_INCLUDED_DELETED) @DefaultValue("false") final Boolean includedDeleted, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagDefinitionApiException, InvoiceApiException {
final UUID invoiceId = UUID.fromString(invoiceIdString);
final TenantContext tenantContext = context.createContext(request);
final Invoice invoice = invoiceApi.getInvoice(invoiceId, tenantContext);
return super.getTags(invoice.getAccountId(), invoiceId, auditMode, includedDeleted, tenantContext);
}
use of org.killbill.commons.metrics.TimedResource in project killbill by killbill.
the class AccountResource method deleteTags.
@TimedResource
@DELETE
@Path("/{accountId:" + UUID_PATTERN + "}/" + TAGS)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Remove tags from account")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied or account does not have a default payment method (AUTO_PAY_OFF tag only)") })
public Response deleteTags(@PathParam(ID_PARAM_NAME) final String id, @QueryParam(QUERY_TAGS) final String tagList, @HeaderParam(HDR_CREATED_BY) final String createdBy, @HeaderParam(HDR_REASON) final String reason, @HeaderParam(HDR_COMMENT) final String comment, @javax.ws.rs.core.Context final HttpServletRequest request) throws TagApiException, AccountApiException {
final CallContext callContext = context.createContext(createdBy, reason, comment, request);
// Look if there is an AUTO_PAY_OFF for that account and check if the account has a default paymentMethod
// If not we can't remove the AUTO_PAY_OFF tag
final Collection<UUID> tagDefinitionUUIDs = getTagDefinitionUUIDs(tagList);
boolean isTagAutoPayOff = false;
for (final UUID cur : tagDefinitionUUIDs) {
if (cur.equals(ControlTagType.AUTO_PAY_OFF.getId())) {
isTagAutoPayOff = true;
break;
}
}
final UUID accountId = UUID.fromString(id);
if (isTagAutoPayOff) {
final Account account = accountUserApi.getAccountById(accountId, callContext);
if (account.getPaymentMethodId() == null) {
throw new TagApiException(ErrorCode.TAG_CANNOT_BE_REMOVED, ControlTagType.AUTO_PAY_OFF, " the account does not have a default payment method");
}
}
return super.deleteTags(UUID.fromString(id), tagList, callContext);
}
use of org.killbill.commons.metrics.TimedResource in project killbill by killbill.
the class AccountResource method getAccountTimeline.
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + TIMELINE)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account timeline", response = AccountTimelineJson.class)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getAccountTimeline(@PathParam("accountId") final String accountIdString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @QueryParam(QUERY_PARALLEL) @DefaultValue("false") final Boolean parallel, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException, SubscriptionApiException, InvoiceApiException, CatalogApiException {
final TenantContext tenantContext = context.createContext(request);
final UUID accountId = UUID.fromString(accountIdString);
final Account account = accountUserApi.getAccountById(accountId, tenantContext);
final Callable<List<SubscriptionBundle>> bundlesCallable = new Callable<List<SubscriptionBundle>>() {
@Override
public List<SubscriptionBundle> call() throws Exception {
return subscriptionApi.getSubscriptionBundlesForAccountId(accountId, tenantContext);
}
};
final Callable<List<Invoice>> invoicesCallable = new Callable<List<Invoice>>() {
@Override
public List<Invoice> call() throws Exception {
return invoiceApi.getInvoicesByAccount(accountId, false, tenantContext);
}
};
final Callable<List<InvoicePayment>> invoicePaymentsCallable = new Callable<List<InvoicePayment>>() {
@Override
public List<InvoicePayment> call() throws Exception {
return invoicePaymentApi.getInvoicePaymentsByAccount(accountId, tenantContext);
}
};
final Callable<List<Payment>> paymentsCallable = new Callable<List<Payment>>() {
@Override
public List<Payment> call() throws Exception {
return paymentApi.getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), tenantContext);
}
};
final Callable<AccountAuditLogs> auditsCallable = new Callable<AccountAuditLogs>() {
@Override
public AccountAuditLogs call() throws Exception {
return auditUserApi.getAccountAuditLogs(accountId, auditMode.getLevel(), tenantContext);
}
};
final AccountTimelineJson json;
List<Invoice> invoices = null;
List<SubscriptionBundle> bundles = null;
List<InvoicePayment> invoicePayments = null;
List<Payment> payments = null;
AccountAuditLogs accountAuditLogs = null;
if (parallel) {
final ExecutorService executor = jaxrsExecutors.getJaxrsExecutorService();
final Future<List<SubscriptionBundle>> futureBundlesCallable = executor.submit(bundlesCallable);
final Future<List<Invoice>> futureInvoicesCallable = executor.submit(invoicesCallable);
final Future<List<InvoicePayment>> futureInvoicePaymentsCallable = executor.submit(invoicePaymentsCallable);
final Future<List<Payment>> futurePaymentsCallable = executor.submit(paymentsCallable);
final Future<AccountAuditLogs> futureAuditsCallable = executor.submit(auditsCallable);
final ImmutableList<Future> toBeCancelled = ImmutableList.<Future>of(futureBundlesCallable, futureInvoicesCallable, futureInvoicePaymentsCallable, futurePaymentsCallable, futureAuditsCallable);
final int timeoutMsec = 100;
final long ini = System.currentTimeMillis();
do {
bundles = (bundles == null) ? waitOnFutureAndHandleTimeout("bundles", futureBundlesCallable, timeoutMsec, toBeCancelled) : bundles;
invoices = (invoices == null) ? waitOnFutureAndHandleTimeout("invoices", futureInvoicesCallable, timeoutMsec, toBeCancelled) : invoices;
invoicePayments = (invoicePayments == null) ? waitOnFutureAndHandleTimeout("invoicePayments", futureInvoicePaymentsCallable, timeoutMsec, toBeCancelled) : invoicePayments;
payments = (payments == null) ? waitOnFutureAndHandleTimeout("payments", futurePaymentsCallable, timeoutMsec, toBeCancelled) : payments;
accountAuditLogs = (accountAuditLogs == null) ? waitOnFutureAndHandleTimeout("accountAuditLogs", futureAuditsCallable, timeoutMsec, toBeCancelled) : accountAuditLogs;
} while ((System.currentTimeMillis() - ini < jaxrsConfig.getJaxrsTimeout().getMillis()) && (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null));
if (bundles == null || invoices == null || invoicePayments == null || payments == null || accountAuditLogs == null) {
Response.status(Status.SERVICE_UNAVAILABLE).build();
}
} else {
invoices = runCallable("invoices", invoicesCallable);
payments = runCallable("payments", paymentsCallable);
bundles = runCallable("bundles", bundlesCallable);
accountAuditLogs = runCallable("accountAuditLogs", auditsCallable);
invoicePayments = runCallable("invoicePayments", invoicePaymentsCallable);
}
json = new AccountTimelineJson(account, invoices, payments, invoicePayments, bundles, accountAuditLogs);
return Response.status(Status.OK).entity(json).build();
}
use of org.killbill.commons.metrics.TimedResource in project killbill by killbill.
the class AccountResource method getPaymentMethods.
@TimedResource
@GET
@Path("/{accountId:" + UUID_PATTERN + "}/" + PAYMENT_METHODS)
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Retrieve account payment methods", response = PaymentMethodJson.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid account id supplied"), @ApiResponse(code = 404, message = "Account not found") })
public Response getPaymentMethods(@PathParam("accountId") final String accountId, @QueryParam(QUERY_WITH_PLUGIN_INFO) @DefaultValue("false") final Boolean withPluginInfo, @QueryParam(QUERY_PLUGIN_PROPERTY) final List<String> pluginPropertiesString, @QueryParam(QUERY_AUDIT) @DefaultValue("NONE") final AuditMode auditMode, @javax.ws.rs.core.Context final HttpServletRequest request) throws AccountApiException, PaymentApiException {
final Iterable<PluginProperty> pluginProperties = extractPluginProperties(pluginPropertiesString);
final TenantContext tenantContext = context.createContext(request);
final Account account = accountUserApi.getAccountById(UUID.fromString(accountId), tenantContext);
final List<PaymentMethod> methods = paymentApi.getAccountPaymentMethods(account.getId(), withPluginInfo, pluginProperties, tenantContext);
final AccountAuditLogs accountAuditLogs = auditUserApi.getAccountAuditLogs(account.getId(), auditMode.getLevel(), tenantContext);
final List<PaymentMethodJson> json = new ArrayList<PaymentMethodJson>(Collections2.transform(methods, new Function<PaymentMethod, PaymentMethodJson>() {
@Override
public PaymentMethodJson apply(final PaymentMethod input) {
return PaymentMethodJson.toPaymentMethodJson(account, input, accountAuditLogs);
}
}));
return Response.status(Status.OK).entity(json).build();
}
Aggregations