org.eclipse.ditto.gateway.api.GatewayDuplicateHeaderException Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ditto-gateway-api Show documentation
Show all versions of ditto-gateway-api Show documentation
Eclipse Ditto is a framework for creating and managing digital twins in the IoT.
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.gateway.api;
import static java.util.Objects.requireNonNull;
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.base.model.common.HttpStatus;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeException;
import org.eclipse.ditto.base.model.exceptions.DittoRuntimeExceptionBuilder;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.base.model.json.JsonParsableException;
import org.eclipse.ditto.json.JsonObject;
/**
* This exception indicates that a request with duplicate header fields was sent.
*/
@Immutable
@JsonParsableException(errorCode = GatewayDuplicateHeaderException.ERROR_CODE)
public final class GatewayDuplicateHeaderException extends DittoRuntimeException implements GatewayException {
/**
* Error code of this exception.
*/
public static final String ERROR_CODE = ERROR_CODE_PREFIX + "duplicate.header.field";
private static final String DEFAULT_MESSAGE =
"The message contains headers which include a duplicate header field!";
private static final String MESSAGE_TEMPLATE_WITH_KNOWN_DUPLICATED_HEADERNAME =
"The message contains headers which include a duplicate header field: ''{0}''";
private static final String DEFAULT_DESCRIPTION =
"A request must not contain multiple header fields with the same field name. " +
"For more information see RFC 7230 section 3.2.2.";
private static final String DEFAULT_HREF = "https://tools.ietf.org/rfc/rfc7230.txt";
private static final long serialVersionUID = -1141581276920590766L;
private GatewayDuplicateHeaderException(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
super(ERROR_CODE, HttpStatus.BAD_REQUEST, dittoHeaders, message, description, cause, href);
}
/**
* A mutable builder for a {@code GatewayDuplicateHeaderException}.
*
* @return the builder.
*/
public static Builder newBuilder() {
return new Builder();
}
/**
* A mutable builder for a {@code GatewayDuplicateHeaderException} with a known duplicated header key.
*
* @param duplicatedHeaderName the name of the duplicated header.
* @throws NullPointerException if {@code duplicatedHeaderName} parameter is null.
* @return the builder.
*/
public static Builder newBuilder(final CharSequence duplicatedHeaderName) {
return new Builder(requireNonNull(duplicatedHeaderName));
}
/**
* Constructs a new {@code GatewayDuplicateHeaderException} 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 GatewayDuplicateHeaderException.
* @throws NullPointerException if {@code dittoHeaders} is {@code null}.
*/
public static GatewayDuplicateHeaderException fromMessage(@Nullable final String message,
final DittoHeaders dittoHeaders) {
return DittoRuntimeException.fromMessage(message, dittoHeaders, new Builder());
}
/**
* Constructs a new {@code GatewayDuplicateHeaderException} object with the exception message extracted from the given
* JSON object.
*
* @param jsonObject the JSON to read the {@link org.eclipse.ditto.base.model.exceptions.DittoRuntimeException.JsonFields#MESSAGE} field from.
* @param dittoHeaders the headers of the command which resulted in this exception.
* @return the new GatewayDuplicateHeaderException.
* @throws NullPointerException if any argument is {@code null}.
* @throws org.eclipse.ditto.json.JsonMissingFieldException if this JsonObject did not contain an error message.
* @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected
* format.
*/
public static GatewayDuplicateHeaderException fromJson(final JsonObject jsonObject,
final DittoHeaders dittoHeaders) {
return DittoRuntimeException.fromJson(jsonObject, dittoHeaders, new Builder());
}
@Override
public DittoRuntimeException setDittoHeaders(final DittoHeaders dittoHeaders) {
return new Builder()
.message(getMessage())
.description(getDescription().orElse(null))
.cause(getCause())
.href(getHref().orElse(null))
.dittoHeaders(dittoHeaders)
.build();
}
/**
* A mutable builder with a fluent API for a {@link GatewayDuplicateHeaderException}.
*/
@NotThreadSafe
public static final class Builder extends DittoRuntimeExceptionBuilder {
private Builder() {
description(DEFAULT_DESCRIPTION);
message(DEFAULT_MESSAGE);
href(DEFAULT_HREF);
}
private Builder(final CharSequence duplicatedHeaderName) {
description(DEFAULT_DESCRIPTION);
message(MessageFormat.format(MESSAGE_TEMPLATE_WITH_KNOWN_DUPLICATED_HEADERNAME, duplicatedHeaderName));
href(DEFAULT_HREF);
}
@Override
protected GatewayDuplicateHeaderException doBuild(final DittoHeaders dittoHeaders,
@Nullable final String message,
@Nullable final String description,
@Nullable final Throwable cause,
@Nullable final URI href) {
return new GatewayDuplicateHeaderException(dittoHeaders, message, description, cause, href);
}
}
}