-
Notifications
You must be signed in to change notification settings - Fork 24.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HLRC: Create server agnostic request and response (#32912)
The HLRC has historically reused the same Request and Response classes that the server module uses. This commit deprecates the use of any server module Request and Response classes, and adds a small bit of validation logic that differs from server slightly, in that it does not assume a check for a null ValidationException class is not enough to determine if validation failed.
- Loading branch information
Showing
3 changed files
with
208 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
client/rest-high-level/src/main/java/org/elasticsearch/client/Validatable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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.elasticsearch.client; | ||
|
||
/** | ||
* Defines a validation layer for Requests. | ||
*/ | ||
public interface Validatable { | ||
ValidationException EMPTY_VALIDATION = new ValidationException() { | ||
@Override | ||
public void addValidationError(String error) { | ||
throw new UnsupportedOperationException("Validation messages should not be added to the empty validation"); | ||
} | ||
}; | ||
|
||
/** | ||
* Perform validation. This method does not have to be overridden in the event that no validation needs to be done. | ||
* | ||
* @return potentially null, in the event of older actions, an empty {@link ValidationException} in newer actions, or finally a | ||
* {@link ValidationException} that contains a list of all failed validation. | ||
*/ | ||
default ValidationException validate() { | ||
return EMPTY_VALIDATION; | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
client/rest-high-level/src/main/java/org/elasticsearch/client/ValidationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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.elasticsearch.client; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Encapsulates an accumulation of validation errors | ||
*/ | ||
public class ValidationException extends IllegalArgumentException { | ||
private final List<String> validationErrors = new ArrayList<>(); | ||
|
||
/** | ||
* Add a new validation error to the accumulating validation errors | ||
* @param error the error to add | ||
*/ | ||
public void addValidationError(String error) { | ||
validationErrors.add(error); | ||
} | ||
|
||
/** | ||
* Returns the validation errors accumulated | ||
*/ | ||
public final List<String> validationErrors() { | ||
return validationErrors; | ||
} | ||
|
||
@Override | ||
public final String getMessage() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("Validation Failed: "); | ||
int index = 0; | ||
for (String error : validationErrors) { | ||
sb.append(++index).append(": ").append(error).append(";"); | ||
} | ||
return sb.toString(); | ||
} | ||
} |