2021-03-11 14:39:44 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* * Copyright 2021 Red Hat, Inc. and/or its affiliates
|
|
|
|
* * and other contributors as indicated by the @author tags.
|
|
|
|
* *
|
|
|
|
* * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* * you may not use this file except in compliance with the License.
|
|
|
|
* * You may obtain a copy of the License at
|
|
|
|
* *
|
|
|
|
* * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* *
|
|
|
|
* * Unless required by applicable law or agreed to in writing, software
|
|
|
|
* * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* * See the License for the specific language governing permissions and
|
|
|
|
* * limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.keycloak.userprofile;
|
|
|
|
|
2023-01-04 15:16:10 +00:00
|
|
|
import jakarta.ws.rs.core.Response;
|
2021-05-12 13:54:05 +00:00
|
|
|
import java.io.Serializable;
|
2021-03-11 14:39:44 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2021-06-01 14:45:35 +00:00
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.function.Consumer;
|
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
import org.keycloak.validate.ValidationError;
|
|
|
|
|
2021-03-11 14:39:44 +00:00
|
|
|
/**
|
|
|
|
* @author <a href="mailto:psilva@redhat.com">Pedro Igor</a>
|
|
|
|
*/
|
2021-06-01 14:45:35 +00:00
|
|
|
public final class ValidationException extends RuntimeException implements Consumer<ValidationError> {
|
2021-03-11 14:39:44 +00:00
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
private final Map<String, List<Error>> errors = new HashMap<>();
|
|
|
|
|
|
|
|
public List<Error> getErrors() {
|
|
|
|
return errors.values().stream().reduce(new ArrayList<>(), (l, r) -> {
|
|
|
|
l.addAll(r);
|
|
|
|
return l;
|
|
|
|
}, (l, r) -> l);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean hasError(String... types) {
|
|
|
|
if (types.length == 0) {
|
|
|
|
return !errors.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String type : types) {
|
|
|
|
if (errors.containsKey(type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if there are validation errors related to the attribute with the given {@code name}.
|
|
|
|
*
|
|
|
|
* @param name
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public boolean isAttributeOnError(String... name) {
|
|
|
|
if (name.length == 0) {
|
|
|
|
return !errors.isEmpty();
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> names = Arrays.asList(name);
|
|
|
|
|
|
|
|
return errors.values().stream().flatMap(Collection::stream).anyMatch(error -> names.contains(error.getAttribute()));
|
|
|
|
}
|
|
|
|
|
2021-06-01 14:45:35 +00:00
|
|
|
@Override
|
|
|
|
public void accept(ValidationError error) {
|
|
|
|
addError(error);
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
void addError(ValidationError error) {
|
2021-03-11 14:39:44 +00:00
|
|
|
List<Error> errors = this.errors.computeIfAbsent(error.getMessage(), (k) -> new ArrayList<>());
|
2021-06-21 13:39:13 +00:00
|
|
|
errors.add(new Error(error));
|
2021-05-12 13:54:05 +00:00
|
|
|
}
|
2021-06-01 14:45:35 +00:00
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "ValidationException [errors=" + errors + "]";
|
2021-03-11 14:39:44 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
@Override
|
|
|
|
public String getMessage() {
|
|
|
|
return toString();
|
|
|
|
}
|
2021-03-11 14:39:44 +00:00
|
|
|
|
2021-06-01 14:45:35 +00:00
|
|
|
public Response.Status getStatusCode() {
|
|
|
|
for (Map.Entry<String, List<Error>> entry : errors.entrySet()) {
|
|
|
|
for (Error error : entry.getValue()) {
|
|
|
|
if (!Response.Status.BAD_REQUEST.equals(error.getStatusCode())) {
|
|
|
|
return error.getStatusCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Response.Status.BAD_REQUEST;
|
|
|
|
}
|
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
public static class Error implements Serializable {
|
2021-03-11 14:39:44 +00:00
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
private final ValidationError error;
|
2021-03-11 14:39:44 +00:00
|
|
|
|
2021-06-21 13:39:13 +00:00
|
|
|
public Error(ValidationError error) {
|
2021-05-12 13:54:05 +00:00
|
|
|
this.error = error;
|
|
|
|
}
|
2021-03-11 14:39:44 +00:00
|
|
|
|
2021-05-12 13:54:05 +00:00
|
|
|
public String getAttribute() {
|
|
|
|
return error.getInputHint();
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMessage() {
|
|
|
|
return error.getMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
public Object[] getMessageParameters() {
|
2021-06-01 14:45:35 +00:00
|
|
|
return error.getInputHintWithMessageParameters();
|
2021-05-12 13:54:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Error [error=" + error + "]";
|
|
|
|
}
|
2021-06-01 14:45:35 +00:00
|
|
|
|
2021-06-21 13:39:13 +00:00
|
|
|
public String getFormattedMessage(BiFunction<String, Object[], String> messageFormatter) {
|
2021-06-01 14:45:35 +00:00
|
|
|
return messageFormatter.apply(getMessage(), getMessageParameters());
|
|
|
|
}
|
|
|
|
|
|
|
|
public Response.Status getStatusCode() {
|
|
|
|
return error.getStatusCode();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 14:39:44 +00:00
|
|
|
}
|