-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alternative ResponseInterceptor attempt (#1634)
* add ResponseInterceptor support #1126 * Add the license header. Add the license header. Co-authored-by: Dewald de Jager <DewaldDeJager@users.noreply.github.com> * small fix for license header * fix format issue * combine before and after method to one aroundDecode method * Change ResponseInterceptor to use InvocationContext Co-authored-by: Fei,Yanke <yanke.fei@mosi-tech.com> Co-authored-by: feiyanke <feiyanke@126.com> Co-authored-by: Dewald de Jager <DewaldDeJager@users.noreply.github.com>
- Loading branch information
1 parent
6e0afeb
commit 68c5422
Showing
10 changed files
with
199 additions
and
100 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
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
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
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
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
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,58 @@ | ||
/* | ||
* Copyright 2012-2022 The Feign Authors | ||
* | ||
* 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 feign; | ||
|
||
import static feign.FeignException.errorReading; | ||
import feign.codec.DecodeException; | ||
import feign.codec.Decoder; | ||
import java.io.IOException; | ||
import java.lang.reflect.Type; | ||
|
||
public class InvocationContext { | ||
|
||
private final Decoder decoder; | ||
private final Type returnType; | ||
private final Response response; | ||
|
||
InvocationContext(Decoder decoder, Type returnType, Response response) { | ||
this.decoder = decoder; | ||
this.returnType = returnType; | ||
this.response = response; | ||
} | ||
|
||
public Object proceed() { | ||
try { | ||
return decoder.decode(response, returnType); | ||
} catch (final FeignException e) { | ||
throw e; | ||
} catch (final RuntimeException e) { | ||
throw new DecodeException(response.status(), e.getMessage(), response.request(), e); | ||
} catch (IOException e) { | ||
throw errorReading(response.request(), response, e); | ||
} | ||
} | ||
|
||
public Decoder decoder() { | ||
return decoder; | ||
} | ||
|
||
public Type returnType() { | ||
return returnType; | ||
} | ||
|
||
public Response response() { | ||
return response; | ||
} | ||
|
||
} |
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,38 @@ | ||
/* | ||
* Copyright 2012-2022 The Feign Authors | ||
* | ||
* 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 feign; | ||
|
||
import java.io.IOException; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* Zero or One {@code ResponseInterceptor} may be configured for purposes such as verify or modify | ||
* headers of response, verify the business status of decoded object. Once interceptors are applied, | ||
* {@link ResponseInterceptor#aroundDecode(Response, Function)} is called around decode method | ||
* called | ||
*/ | ||
public interface ResponseInterceptor { | ||
|
||
ResponseInterceptor DEFAULT = InvocationContext::proceed; | ||
|
||
/** | ||
* Called for response around decode, must either manually invoke | ||
* {@link InvocationContext#proceed} or manually create a new response object | ||
* | ||
* @param invocationContext information surrounding the response been decoded | ||
* @return decoded response | ||
*/ | ||
Object aroundDecode(InvocationContext invocationContext) throws IOException; | ||
|
||
} |
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.