-
Notifications
You must be signed in to change notification settings - Fork 967
Add Implement JsonRpcService #6289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 35 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
bd03700
Draft
patcher454 b34715e
Fix Prevent blocking and handle Notifications correctly
patcher454 e2433cb
Fix errors
patcher454 01e1997
Fix correctly parse object parameters in JsonRpcRequest
patcher454 f6e0ea5
Fix Rename to SimpleJsonRpcResponse
patcher454 25ad292
Add jsonRpcResponse of parameter type checking
patcher454 7a69c1f
Fix jsonRpcServie notification error
patcher454 2f65ec1
Test jsonRpcRequest
patcher454 f1842b0
Test jsonRpcResponse
patcher454 529171e
Test jsonRpcService
patcher454 9087bfb
Fix jsonRpcRequest params allow a null element
patcher454 25b89d0
Add requireNonNull
patcher454 04622c7
Fix incorrect variable name
patcher454 6475fdf
Fix jsonproperty name
patcher454 544921a
Fix Javadoc
patcher454 cb7d97b
Fix remove nullable
patcher454 3b41429
Fix requireNonNull
patcher454 047a343
Fix Error Message
patcher454 eea7afa
Fix Javadoc
patcher454 5a4cb2b
Fix Property final
patcher454 35186e1
Fix Shorten the code
patcher454 57aff5b
Fix test case
patcher454 ef47d49
fix
patcher454 dd3f83f
Merge branch 'json-rpc' into json_rpc
jrhee17 7317cad
fix
patcher454 2b558b3
Merge branch 'json_rpc' of https://github.com/patcher454/armeria into…
patcher454 4b2a6dc
fix
patcher454 cd1d60a
fix: Restore indentation
patcher454 1cb9eaf
fix: missing
patcher454 1443994
Merge branch 'json_rpc' of https://github.com/patcher454/armeria into…
patcher454 56aaee4
fix: DefaultJsonRpcResponse hashcode
patcher454 6606d68
fix: make parameters immutable and non-null
patcher454 a0b743b
fix: remove json rpc flag
patcher454 52344f3
fix
patcher454 176a7ea
fix: remove remain flag
patcher454 e4c355b
fix: add javadoc
patcher454 185079c
Merge branch 'json-rpc' into json_rpc
jrhee17 94560fe
lint
minwoox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
core/src/main/java/com/linecorp/armeria/common/jsonrpc/AbstractJsonRpcResponse.java
This file contains hidden or 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,63 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation 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: | ||
| * | ||
| * https://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 com.linecorp.armeria.common.jsonrpc; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.Nullable; | ||
| import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
|
||
| /** | ||
| * The base for JsonRpcResponse. | ||
| */ | ||
| @UnstableApi | ||
| public abstract class AbstractJsonRpcResponse implements JsonRpcResponse { | ||
| @Nullable | ||
| private final Object result; | ||
|
|
||
| @Nullable | ||
| private final JsonRpcError error; | ||
|
|
||
| /** | ||
| * Creates a new instance with result. | ||
| */ | ||
| protected AbstractJsonRpcResponse(Object result) { | ||
| this.result = requireNonNull(result, "result"); | ||
| this.error = null; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a new instance with error. | ||
| */ | ||
| protected AbstractJsonRpcResponse(JsonRpcError error) { | ||
| this.result = null; | ||
| this.error = requireNonNull(error, "error"); | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty | ||
| public final @Nullable Object result() { | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty | ||
| public final @Nullable JsonRpcError error() { | ||
| return error; | ||
| } | ||
| } |
201 changes: 201 additions & 0 deletions
201
core/src/main/java/com/linecorp/armeria/common/jsonrpc/DefaultJsonRpcRequest.java
This file contains hidden or 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,201 @@ | ||
| /* | ||
| * Copyright 2025 LY Corporation | ||
| * | ||
| * LY Corporation 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: | ||
| * | ||
| * https://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 com.linecorp.armeria.common.jsonrpc; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.google.common.base.MoreObjects; | ||
| import com.google.common.collect.ImmutableList; | ||
|
|
||
| import com.linecorp.armeria.common.annotation.Nullable; | ||
| import com.linecorp.armeria.internal.common.JacksonUtil; | ||
|
|
||
| /** | ||
| * Default {@link JsonRpcRequest} implementation. | ||
| */ | ||
| final class DefaultJsonRpcRequest implements JsonRpcRequest { | ||
| static final ObjectMapper objectMapper = JacksonUtil.newDefaultObjectMapper(); | ||
|
|
||
| @Nullable | ||
| private final Object id; | ||
| private final String method; | ||
| private final JsonRpcParameter params; | ||
| private final JsonRpcVersion version; | ||
|
|
||
| DefaultJsonRpcRequest(@Nullable Object id, String method, Iterable<?> params) { | ||
| this(id, method, copyParams(params), JsonRpcVersion.JSON_RPC_2_0.getVersion()); | ||
| } | ||
|
|
||
| DefaultJsonRpcRequest(@Nullable Object id, String method, Object... params) { | ||
| this(id, method, copyParams(params), JsonRpcVersion.JSON_RPC_2_0.getVersion()); | ||
| } | ||
|
|
||
| DefaultJsonRpcRequest(@Nullable Object id, String method, Map<String, Object> params) { | ||
| this(id, method, copyParams(params), JsonRpcVersion.JSON_RPC_2_0.getVersion()); | ||
| } | ||
|
|
||
| private DefaultJsonRpcRequest( | ||
| @Nullable Object id, | ||
| String method, | ||
| Object params, | ||
| String version) { | ||
| checkArgument(JsonRpcVersion.JSON_RPC_2_0.getVersion().equals(version), | ||
| "jsonrpc: %s (expected: 2.0)", version); | ||
| checkArgument(id == null || id instanceof Number || id instanceof String, | ||
| "id type: %s (expected: Null or Number or String)", | ||
| id != null ? id.getClass().getName() : "null"); | ||
| checkArgument(params instanceof List || params instanceof Map, | ||
| "params type: %s (expected: List or Map)", | ||
| params != null ? params.getClass().getName() : "null"); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| final JsonRpcParameter rpcParams = | ||
| params instanceof List ? JsonRpcParameter.of((List<Object>) params) | ||
| : JsonRpcParameter.of((Map<String, Object>) params); | ||
|
|
||
| this.id = id; | ||
| this.method = requireNonNull(method, "method"); | ||
| this.params = rpcParams; | ||
| this.version = JsonRpcVersion.JSON_RPC_2_0; | ||
| } | ||
|
|
||
| @JsonCreator | ||
| private static DefaultJsonRpcRequest fromJson( | ||
| @JsonProperty("id") @Nullable Object id, | ||
| @JsonProperty("method") String method, | ||
| @JsonProperty("params") @Nullable Object params, | ||
| @JsonProperty("jsonrpc") String version) { | ||
|
|
||
| if (params == null) { | ||
| return new DefaultJsonRpcRequest(id, method, ImmutableList.of()); | ||
| } | ||
|
|
||
| if (params instanceof Iterable) { | ||
| return new DefaultJsonRpcRequest(id, method, (Iterable<?>) params); | ||
| } | ||
|
|
||
| return new DefaultJsonRpcRequest(id, method, params, version); | ||
| } | ||
|
|
||
| private static List<Object> copyParams(Iterable<?> params) { | ||
| requireNonNull(params, "params"); | ||
| if (params instanceof ImmutableList) { | ||
| //noinspection unchecked | ||
| return (List<Object>) params; | ||
| } | ||
|
|
||
| // Note we do not use ImmutableList.copyOf() here, | ||
| // because it does not allow a null element and we should allow a null argument. | ||
| final List<Object> copy; | ||
| if (params instanceof Collection) { | ||
| copy = new ArrayList<>(((Collection<?>) params).size()); | ||
| } else { | ||
| copy = new ArrayList<>(8); | ||
| } | ||
|
|
||
| for (Object p : params) { | ||
| copy.add(p); | ||
| } | ||
|
|
||
| return Collections.unmodifiableList(copy); | ||
| } | ||
|
|
||
| private static List<Object> copyParams(Object... params) { | ||
| if (params.length == 0) { | ||
| return ImmutableList.of(); | ||
| } | ||
|
|
||
| final List<Object> copy = new ArrayList<>(params.length); | ||
| Collections.addAll(copy, params); | ||
| return Collections.unmodifiableList(copy); | ||
| } | ||
|
|
||
| private static Map<String, Object> copyParams(Map<String, Object> params) { | ||
| requireNonNull(params, "params"); | ||
| if (params.isEmpty()) { | ||
| return Collections.emptyMap(); | ||
| } | ||
|
|
||
| return Collections.unmodifiableMap(new HashMap<>(params)); | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty | ||
| public @Nullable Object id() { | ||
| return id; | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty | ||
| public String method() { | ||
| return method; | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty | ||
| public JsonRpcParameter params() { | ||
| return params; | ||
| } | ||
|
|
||
| @Override | ||
| @JsonProperty("jsonrpc") | ||
| public JsonRpcVersion version() { | ||
| return version; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(id, method, params, version); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(@Nullable Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
|
|
||
| if (!(obj instanceof JsonRpcRequest)) { | ||
| return false; | ||
| } | ||
|
|
||
| final JsonRpcRequest that = (JsonRpcRequest) obj; | ||
| return Objects.equals(id, that.id()) && | ||
| Objects.equals(method, that.method()) && | ||
| Objects.equals(params, that.params()) && | ||
| Objects.equals(version, that.version()); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return MoreObjects.toStringHelper(this) | ||
| .add("id", id()) | ||
| .add("method", method()) | ||
| .add("params", params()) | ||
| .add("jsonrpc", version()).toString(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.