use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.
the class ContributorTypeAPI method deleteContributorTypesByContributorTypeId.
@Validate
@Override
public void deleteContributorTypesByContributorTypeId(String contributorTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
PostgresClient postgres = PostgresClient.getInstance(vertxContext.owner(), tenantId);
postgres.delete(CONTRIBUTOR_TYPE_TABLE, contributorTypeId, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringDelete(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withPlainBadRequest(msg)));
return;
}
int updated = reply.result().getUpdated();
if (updated != 1) {
String msg = messages.getMessage(lang, MessageConsts.DeletedCountError, 1, updated);
log.error(msg);
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
return;
}
asyncResultHandler.handle(Future.succeededFuture(DeleteContributorTypesByContributorTypeIdResponse.withNoContent()));
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringDelete(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.
the class ContributorTypeAPI method getContributorTypes.
@Validate
@Override
public void getContributorTypes(String query, int offset, int limit, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
/**
* http://host:port/contributor-types
*/
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
CQLWrapper cql = getCQL(query, limit, offset);
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.class, new String[] { "*" }, cql, true, true, reply -> {
try {
if (reply.succeeded()) {
ContributorTypes contributorTypes = new ContributorTypes();
@SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
contributorTypes.setContributorTypes(contributorType);
contributorTypes.setTotalRecords(reply.result().getResultInfo().getTotalRecords());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withJsonOK(contributorTypes)));
} else {
log.error(reply.cause().getMessage(), reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainBadRequest(reply.cause().getMessage())));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainInternalServerError(messages.getMessage(lang, MessageConsts.InternalServerError))));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
String message = messages.getMessage(lang, MessageConsts.InternalServerError);
if (e.getCause() instanceof CQLParseException) {
message = " CQL parse error " + e.getLocalizedMessage();
}
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesResponse.withPlainInternalServerError(message)));
}
});
}
use of org.folio.rest.annotations.Validate in project mod-inventory-storage by folio-org.
the class ContributorTypeAPI method getContributorTypesByContributorTypeId.
@Validate
@Override
public void getContributorTypesByContributorTypeId(String contributorTypeId, String lang, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
vertxContext.runOnContext(v -> {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
Criterion c = new Criterion(new Criteria().addField(idFieldName).setJSONB(false).setOperation("=").setValue("'" + contributorTypeId + "'"));
PostgresClient.getInstance(vertxContext.owner(), tenantId).get(CONTRIBUTOR_TYPE_TABLE, ContributorType.class, c, true, reply -> {
try {
if (reply.failed()) {
String msg = PgExceptionUtil.badRequestMessage(reply.cause());
if (msg == null) {
internalServerErrorDuringGetById(reply.cause(), lang, asyncResultHandler);
return;
}
log.info(msg);
asyncResultHandler.handle(Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(msg)));
return;
}
@SuppressWarnings("unchecked") List<ContributorType> contributorType = (List<ContributorType>) reply.result().getResults();
if (contributorType.isEmpty()) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withPlainNotFound(contributorTypeId)));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetContributorTypesByContributorTypeIdResponse.withJsonOK(contributorType.get(0))));
}
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
} catch (Exception e) {
internalServerErrorDuringGetById(e, lang, asyncResultHandler);
}
});
}
use of org.folio.rest.annotations.Validate in project raml-module-builder by folio-org.
the class AdminAPI method getAdminPostgresLoad.
@Validate
@Override
public void getAdminPostgresLoad(String dbname, Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
PostgresClient.getInstance(vertxContext.owner()).select("SELECT pg_stat_reset()", reply -> {
if (reply.succeeded()) {
/* wait 10 seconds for stats to gather and then query stats table for info */
vertxContext.owner().setTimer(10000, new Handler<Long>() {
@Override
public void handle(Long timerID) {
PostgresClient.getInstance(vertxContext.owner(), "public").select("SELECT numbackends as CONNECTIONS, xact_commit as TX_COMM, xact_rollback as " + "TX_RLBCK, blks_read + blks_hit as READ_TOTAL, " + "blks_hit * 100 / (blks_read + blks_hit) " + "as BUFFER_HIT_PERCENT FROM pg_stat_database WHERE datname = '" + dbname + "'", reply2 -> {
if (reply2.succeeded()) {
OutStream stream = new OutStream();
stream.setData(reply2.result().getRows());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetAdminPostgresLoadResponse.withJsonOK(stream)));
} else {
log.error(reply2.cause().getMessage(), reply2.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetAdminPostgresLoadResponse.withPlainInternalServerError(reply2.cause().getMessage())));
}
});
}
});
} else {
log.error(reply.cause().getMessage(), reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.failedFuture(reply.cause().getMessage()));
}
});
}
use of org.folio.rest.annotations.Validate in project raml-module-builder by folio-org.
the class AdminAPI method getAdminMemory.
@Validate
@Override
public void getAdminMemory(boolean history, java.util.Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) throws Exception {
final StringBuilder dump = new StringBuilder();
vertxContext.owner().executeBlocking(code -> {
try {
dump.append("<br><table>");
for (MemoryPoolMXBean pool : ManagementFactory.getMemoryPoolMXBeans()) {
MemoryUsage mem = pool.getCollectionUsage();
MemoryUsage curMem = pool.getUsage();
MemoryUsage peakMem = pool.getPeakUsage();
long usageAfterGC = -1;
long currentUsage = -1;
double usageAfterGCPercent = -1;
double currentUsagePercent = -1;
long peakUsage = -1;
if (mem != null) {
usageAfterGC = mem.getUsed() / 1024 / 1024;
// Mimic jstat behavior
usageAfterGCPercent = (double) mem.getUsed() / (double) mem.getCommitted() * 100;
}
if (curMem != null) {
currentUsage = curMem.getUsed() / 1024 / 1024;
if (curMem.getMax() > 0) {
currentUsagePercent = (double) curMem.getUsed() / (double) curMem.getCommitted() * // Mimic jstat behavior
100;
}
}
if (peakMem != null) {
peakUsage = peakMem.getUsed() / 1024 / 1024;
}
dump.append("<tr><td>name: ").append(pool.getName()).append(" </td>").append("<td>memory usage after latest gc: <b>").append(usageAfterGC).append("</b>MB. </td>").append("<td>type: ").append(pool.getType()).append(" </td>").append("<td>estimate of memory usage: <b>").append(currentUsage).append("</b>MB. </td>").append("<td>peak usage: ").append(peakUsage).append("MB. </td>").append("<td> % used memory after GC: <b>").append(DECFORMAT.format(usageAfterGCPercent)).append("</b> </td>").append("<td> % used memory current: <b>").append(DECFORMAT.format(currentUsagePercent)).append("</b> </td></tr>");
}
dump.append("</table><br><br>");
final MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
final MemoryUsage memInfo = memoryMXBean.getHeapMemoryUsage();
long memCommittedToJVMByOS = memInfo.getCommitted();
long memUsedByJVM = memInfo.getUsed();
dump.append("<b>Total: </b> Memory used/available(MB): ").append((memUsedByJVM / 1024 / 1024)).append("/").append(memCommittedToJVMByOS / 1024 / 1024).append("<br>");
if (history) {
StringBuilder historyMem = new StringBuilder();
jvmMemoryHistory.put(new Date(), dump.toString());
BiConsumer<Date, String> biConsumer = (key, value) -> historyMem.append(key.toInstant().toString() + "<br>" + value);
jvmMemoryHistory.forEach(biConsumer);
code.complete(historyMem);
} else {
jvmMemoryHistory.clear();
code.complete(dump);
}
} catch (Exception e) {
log.error(e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetAdminMemoryResponse.withPlainInternalServerError("ERROR" + e.getMessage())));
}
}, result -> {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(GetAdminMemoryResponse.withHtmlOK(result.result().toString())));
});
}
Aggregations