forked from line/armeria
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
461 additions
and
86 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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/com/linecorp/armeria/client/ClientPreprocessor.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,32 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import com.linecorp.armeria.common.Request; | ||
import com.linecorp.armeria.common.Response; | ||
|
||
/** | ||
* TBU. | ||
*/ | ||
@FunctionalInterface | ||
public interface ClientPreprocessor<I extends Request, O extends Response> { | ||
|
||
/** | ||
* TBU. | ||
*/ | ||
O execute(ClientRequestContext ctx, I req, RequestOptions requestOptions); | ||
} |
82 changes: 82 additions & 0 deletions
82
core/src/main/java/com/linecorp/armeria/client/ClientPreprocessors.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,82 @@ | ||
/* | ||
* Copyright 2016 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import java.util.function.Function; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
/** | ||
* A set of {@link Function}s that transforms a {@link Client} into another. | ||
*/ | ||
public final class ClientPreprocessors { | ||
|
||
private static final ClientPreprocessors NONE = new ClientPreprocessors(Function.identity(), Function.identity()); | ||
|
||
/** | ||
* Returns an empty {@link ClientPreprocessors} which does not decorate a {@link Client}. | ||
*/ | ||
public static ClientPreprocessors of() { | ||
return NONE; | ||
} | ||
|
||
/** | ||
* Creates a new instance from a single decorator {@link Function}. | ||
* | ||
* @param decorator the {@link Function} that transforms an {@link HttpClient} to another | ||
*/ | ||
public static ClientPreprocessors of( | ||
Function<? super HttpPreprocessor, ? extends HttpPreprocessor> decorator) { | ||
return new ClientPreprocessors(decorator, Function.identity()); | ||
} | ||
|
||
/** | ||
* Creates a new instance from a single decorator {@link Function}. | ||
* | ||
* @param decorator the {@link Function} that transforms an {@link RpcClient} to another | ||
*/ | ||
public static ClientPreprocessors ofRpc(Function<? super RpcPreprocessor, ? extends RpcPreprocessor> decorator) { | ||
return new ClientPreprocessors(Function.identity(), decorator); | ||
} | ||
|
||
private final Function<? super HttpPreprocessor, ? extends HttpPreprocessor> preprocessorFn; | ||
private final Function<? super RpcPreprocessor, ? extends RpcPreprocessor> rpcPreprocessorFn; | ||
|
||
ClientPreprocessors(Function<? super HttpPreprocessor, ? extends HttpPreprocessor> preprocessorFn, | ||
Function<? super RpcPreprocessor, ? extends RpcPreprocessor> rpcPreprocessorFn) { | ||
this.preprocessorFn = preprocessorFn; | ||
this.rpcPreprocessorFn = rpcPreprocessorFn; | ||
} | ||
|
||
/** | ||
* Decorates the specified {@link HttpPreprocessor} using the decorator. | ||
* | ||
* @param preprocessor the {@link HttpPreprocessor} being decorated | ||
*/ | ||
public HttpPreprocessor decorate(HttpPreprocessor preprocessor) { | ||
return preprocessorFn.apply(preprocessor); | ||
} | ||
|
||
/** | ||
* Decorates the specified {@link RpcPreprocessor} using the decorator. | ||
* | ||
* @param rpcPreprocessor the {@link RpcPreprocessor} being decorated | ||
*/ | ||
public RpcPreprocessor rpcDecorate(RpcPreprocessor rpcPreprocessor) { | ||
return rpcPreprocessorFn.apply(rpcPreprocessor); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
core/src/main/java/com/linecorp/armeria/client/DecoratingClientPreprocessor.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,28 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import com.linecorp.armeria.common.Request; | ||
import com.linecorp.armeria.common.Response; | ||
import com.linecorp.armeria.common.annotation.UnstableApi; | ||
|
||
@UnstableApi | ||
@FunctionalInterface | ||
public interface DecoratingClientPreprocessor<I extends Request, O extends Response> { | ||
|
||
O execute(ClientPreprocessor<I, O> delegate, ClientRequestContext ctx, I req, RequestOptions requestOptions); | ||
} |
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
24 changes: 24 additions & 0 deletions
24
core/src/main/java/com/linecorp/armeria/client/HttpPreprocessor.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,24 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
|
||
@FunctionalInterface | ||
public interface HttpPreprocessor extends ClientPreprocessor<HttpRequest, HttpResponse> { | ||
} |
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
24 changes: 24 additions & 0 deletions
24
core/src/main/java/com/linecorp/armeria/client/RpcPreprocessor.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,24 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import com.linecorp.armeria.common.RpcRequest; | ||
import com.linecorp.armeria.common.RpcResponse; | ||
|
||
@FunctionalInterface | ||
public interface RpcPreprocessor extends ClientPreprocessor<RpcRequest, RpcResponse> { | ||
} |
70 changes: 70 additions & 0 deletions
70
core/src/main/java/com/linecorp/armeria/client/TailClientPreprocessor.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,70 @@ | ||
/* | ||
* Copyright 2024 LINE Corporation | ||
* | ||
* LINE 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.client; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import com.linecorp.armeria.client.endpoint.EndpointGroup; | ||
import com.linecorp.armeria.common.HttpRequest; | ||
import com.linecorp.armeria.common.HttpResponse; | ||
import com.linecorp.armeria.common.Request; | ||
import com.linecorp.armeria.common.Response; | ||
import com.linecorp.armeria.common.RpcRequest; | ||
import com.linecorp.armeria.common.RpcResponse; | ||
import com.linecorp.armeria.internal.client.ClientRequestContextExtension; | ||
import com.linecorp.armeria.internal.client.ClientUtil; | ||
import com.linecorp.armeria.internal.client.PreprocessorAttributeKeys; | ||
|
||
public final class TailClientPreprocessor<I extends Request, O extends Response> implements ClientPreprocessor<I, O> { | ||
|
||
private final Client<I, O> delegate; | ||
|
||
private TailClientPreprocessor(Client<I, O> delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public static HttpPreprocessor of(HttpClient httpClient) { | ||
final TailClientPreprocessor<HttpRequest, HttpResponse> tail = new TailClientPreprocessor<>(httpClient); | ||
return tail::execute; | ||
} | ||
|
||
public static RpcPreprocessor ofRpc(RpcClient rpcClient) { | ||
final TailClientPreprocessor<RpcRequest, RpcResponse> tail = new TailClientPreprocessor<>(rpcClient); | ||
return tail::execute; | ||
} | ||
|
||
@Override | ||
public O execute(ClientRequestContext ctx, I req, RequestOptions requestOptions) { | ||
final Function<CompletableFuture<O>, O> futureConverter = | ||
(Function<CompletableFuture<O>, O>) requestOptions.attrs().get( | ||
PreprocessorAttributeKeys.FUTURE_CONVERTER_KEY); | ||
final BiFunction<ClientRequestContext, Throwable, O> errorResponseFactory = | ||
(BiFunction<ClientRequestContext, Throwable, O>) requestOptions.attrs().get( | ||
PreprocessorAttributeKeys.ERROR_RESPONSE_FACTORY_KEY); | ||
final EndpointGroup endpointGroup = (EndpointGroup) requestOptions.attrs().get( | ||
PreprocessorAttributeKeys.ENDPOINT_GROUP_KEY); | ||
assert futureConverter != null; | ||
assert errorResponseFactory != null; | ||
assert endpointGroup != null; | ||
final ClientRequestContextExtension ctxExt = ctx.as(ClientRequestContextExtension.class); | ||
assert ctxExt != null; | ||
return ClientUtil.initContextAndExecuteWithFallback(delegate, ctxExt, endpointGroup, | ||
futureConverter, errorResponseFactory, req); | ||
} | ||
} |
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
Oops, something went wrong.