CommandNotSupportedException.java
/*
* Copyright (c) 2017 Bosch Software Innovations GmbH.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/org/documents/epl-2.0/index.php
*
* Contributors:
* Bosch Software Innovations GmbH - initial contribution
*/
package org.eclipse.ditto.signals.commands.base;
import java.net.URI;
import java.text.MessageFormat;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.model.base.common.HttpStatusCode;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeException;
import org.eclipse.ditto.model.base.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
/**
* Thrown if a {@link Command} is not supported by the version called.
*/
@Immutable
public final class CommandNotSupportedException extends DittoRuntimeException {
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = "api.version.notsupported";
private static final String MESSAGE_TEMPLATE = "The requested resource is not supported by version ''{0}''.";
private static final String DEFAULT_DESCRIPTION = "Check if you specified the correct version of the API.";
private static final long serialVersionUID = 317333904099138238L;
private CommandNotSupportedException(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
super(ERROR_CODE, HttpStatusCode.NOT_FOUND, dittoHeaders, message, description, cause, href);
}
/**
* A mutable builder for a {@code CommandNotSupportedException}.
*
* @param version the version number.
* @return the builder.
*/
public static Builder newBuilder(final int version) {
return new Builder(version);
}
/**
* Constructs a new {@code CommandNotSupportedException} object with given message.
*
* @param message detail message. This message can be later retrieved by the {@link #getMessage()} method.
* @param dittoHeaders the headers of the command which resulted in this exception.
* @return the new CommandNotSupportedException.
* @throws NullPointerException if {@code dittoHeaders} is {@code null}.
*/
public static CommandNotSupportedException fromMessage(@Nullable final String message,
final DittoHeaders dittoHeaders) {
return new Builder()
.dittoHeaders(dittoHeaders)
.message(message)
.build();
}
/**
* Constructs a new {@code CommandNotSupportedException} object with the exception message extracted from the
* given JSON object.
*
* @param jsonObject the JSON to read the {@link JsonFields#MESSAGE} field from.
* @param dittoHeaders the headers of the command which resulted in this exception.
* @return the new CommandNotSupportedException.
* @throws NullPointerException if any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if the {@code jsonObject} does not have the {@link
* JsonFields#MESSAGE} field.
*/
public static CommandNotSupportedException fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) {
return fromMessage(readMessage(jsonObject), dittoHeaders);
}
/**
* A mutable builder with a fluent API for a {@link CommandNotSupportedException}.
*
*/
@NotThreadSafe
public static final class Builder extends DittoRuntimeExceptionBuilder<CommandNotSupportedException> {
private Builder() {
description(DEFAULT_DESCRIPTION);
}
private Builder(final int version) {
this();
message(MessageFormat.format(MESSAGE_TEMPLATE, version));
}
@Override
protected CommandNotSupportedException doBuild(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
return new CommandNotSupportedException(dittoHeaders, message, description, cause, href);
}
}
}