/** * Writes a JSON string corresponding to the given error to a {@link Response}. * * @param status * the error status. * @param errorKey * the error key that will be used for localization. * @param ex * the technical exception. * @return the response containing JSON entity. */ protected Response toResponse(final Response.StatusType status, final String errorKey, final Throwable ex) { final ServerError serverError = new ServerError(); serverError.setCode(errorKey); if (ex != null) { serverError.setThrowable(ex); } if (ex instanceof AbstractParameteredException && ((AbstractParameteredException) ex).getParameters().length > 0) { serverError.setParameters(((AbstractParameteredException) ex).getParameters()); } return toResponse(status, serverError); }
/** * Build the server error instance. */ private ServerError newServerError(final Throwable exception, final Matcher matcher, final String code) { final ServerError serverError = new ServerError(); serverError.setCode("integrity-" + code); serverError.setThrowable(exception.getCause()); if (matcher != null) { serverError.setMessage(matcher.group(1) + "/" + matcher.group(2)); } return serverError; }
/** * Managed JSON writer * * @param any * Any object to serialize. * @return The JSON string from an object. */ public static String toJSon(final Object any) { try { return MAPPER.writeValueAsString(any); } catch (final JsonProcessingException e) { throw new TechnicalException("Unable to build JSon data from bean " + any, e); } }
/** * Check the given task is not finished. * * @param task * The task to check. */ default void checkNotFinished(final T task) { if (isFinished(task)) { // Request a next step but is already finished (canceled?) throw new BusinessException("Already finished"); } }
/** * Return the status of given task * * @param id * Identifier of the task. * @return status or <code>null</code> when no task matches. */ @GET @Path("{id:\\d+}/status") @OnNullReturn404 public ImportStatus getImportStatus(@PathParam("id") final long id) { return Optional.ofNullable(getImportTask(id)).map(BatchTaskVo::getStatus).orElse(null); }
/** * Set the {@link Throwable} object attached to this error. * * @param throwable * the {@link Throwable} object attached to this error. */ public void setThrowable(@NotNull final Throwable throwable) { this.message = throwable.getMessage(); if (throwable.getCause() != null && throwable.getCause().getMessage() != null) { this.cause = new ServerError(); this.cause.setThrowable(throwable.getCause()); } } }
/** * Writes a JSON string corresponding to the given object a {@link Response}. * * @param status * the error status. * @param object * the error object. * @return the response containing JSON entity. */ protected Response toResponse(final Response.StatusType status, final Object object) { return Response.status(status).type(MediaType.APPLICATION_JSON_TYPE).entity(toEntity(object)).build(); }
} catch (final TechnicalException io) { throw new ValidationJsonException("csv-file", io.getMessage());
/** * Check the related node can be updated by the current principal. * * @param id * The node identifier to check. * @param checker * The function checking the node from its identifier (first parameter) for a given user (second * parameter) and return the check {@link Node} identity. * @return the checked node. */ public Node checkNode(final String id, final BiFunction<String, String, Node> checker) { final Node node = checker.apply(id, securityHelper.getLogin()); if (node == null) { // Node is not readable or does not exists throw new BusinessException("read-only-node", "node", id); } return node; }
/** * Return End Of Input indicator. */ private boolean isEndOfInput() { if (isEndOfWord()) { // Real separator, close definitely the previous word parsing return true; } if (Character.isWhitespace(current)) { return false; } throw new TechnicalException("Invalid character '" + current + "', white space, EOL, EOF or separator was expected near entry " + values); } }
/** * Return the container matching to given name. Case is sensitive. * Visibility is checked against security context. DN is not exposed. * * @param name * the container name. Exact match is required, so a normalized * version. * @return Container (CN) with its type. */ @GET @Path("{container:" + ContainerOrg.NAME_PATTERN + "}") @OnNullReturn404 public ContainerWithScopeVo findByName(@PathParam("container") final String name) { return Optional.ofNullable(findById(name)).map(this::toVo).orElse(null); }
private Snapshotting getSnapshot(final Node node) { return Optional.ofNullable(locator.getResource(node.getId(), Snapshotting.class)) .orElseThrow(() -> new BusinessException("snapshot-no-supported", node.getRefined().getId())); }
/** * Build parameter configuration from the string definition. * * @param content * The content JSON string configuration. May be <code>null</code>. * @param valueTypeRef * The type reference to fix the return type. * @param <T> * The return type. * @return The parameter configuration. */ public static <T> T toConfiguration(final String content, final TypeReference<T> valueTypeRef) { try { return MAPPER.readValue(ObjectUtils.defaultIfNull(content, "{}"), valueTypeRef); } catch (final IOException e) { throw new TechnicalException("Unable to build configuration from " + content, e); } }
/** * Cancel (stop) current the catalog update. Synchronous operation, flag the task as failed. * * @param node * The node (provider) to cancel update. * @return The ended task if present or <code>null</code>. */ @DELETE @Path("{node:service:.+}/task") @OnNullReturn404 default T cancel(@PathParam("node") final String node) { checkVisible(node); return endTask(node, true); } }
/** * Check there is no running task for given subscription before the deletion. * * @param lockedId * The locked entity's identifier. */ default void deleteTask(final I lockedId) { // Check there is no running import Optional.ofNullable(getTaskInternal(lockedId)).filter(t -> !isFinished(t)).ifPresent(t -> { throw new BusinessException("Running import not finished", t.getAuthor(), t.getStart(), lockedId); }); // We can safely delete the tasks getTaskRepository().deleteAllBy("locked.id", lockedId); }
/** * Return the field from the given class. * * @param beanType * Class of bean to build. * @param property * the bean property to get. * @return the {@link Field} of this property. */ protected Field getField(final Class<?> beanType, final String property) { final Field field = FieldUtils.getField(beanType, property, true); if (field == null) { throw new TechnicalException("Unknown property " + property + " in class " + beanType.getName()); } return field; }
/** * Cancel (stop) the current task. Synchronous operation, flag the task as failed. * * @param subscription * The locked subscription identifier. * @return The ended task if present or <code>null</code>. */ @DELETE @Path("{subscription:\\d+}/task") @OnNullReturn404 default T cancel(@PathParam("subscription") final int subscription) { checkVisible(subscription); return endTask(subscription, true); }
/** * Check the parameter is related to the given node. * * @param parameter * The parameter to check. * @param node * The node scope the parameter must be related to. */ public void checkOwnership(final Parameter parameter, final Node node) { if (!equalsOrParentOf(parameter.getOwner(), node)) { // This parameter is detached from the node's hierarchy throw new BusinessException("invalid-parameter-node-ownership", "parameter", parameter.getId(), "node", node.getId()); } }
/** * Return a JSON string corresponding to the given object. * * @param object * the error object. * @return the JSON entity. */ protected Object toEntity(final Object object) { try { return jacksonJsonProvider.locateMapper(object.getClass(), MediaType.APPLICATION_JSON_TYPE).writeValueAsString(object); } catch (final JsonProcessingException e) { // Ignore this error at UI level but trace it throw new TechnicalException("Unable to build a JSON string from a server error", e); } } }
/** * Write a line corresponding to the given object. */ private void writeLine(final String[] headers, final Object o) { boolean first = true; try { for (final String header : headers) { writeSeparator(first); first = false; writeField(o, header); } } catch (final Exception e) { throw new TechnicalException("Unable to describe given object : " + o, e); } }