This topic describes how to configure mobile gateway Remote Procedure Call (RPC) interceptors, request headers, cookies, and signatures.
The RPC signature feature was added in baseline 10.2.3.
RPC interception
During development, you may need to control the client's network requests. For example, you might want to intercept network requests, block access to specific interfaces, or implement rate limiting. You can achieve this using RPC interceptors.
Create a global interceptor
public class CommonInterceptor implements RpcInterceptor {
/**
* Pre-interception: a callback invoked before an RPC is sent.
* @param proxy The RPC agent object.
* @param clazz The rpcface model class. You can use the clazz parameter to determine which RPC model class is being called.
* @param method The method of the current RPC call.
* @throws RpcException
* @return Returns true to continue the execution. Returns false to break the current request and throw an RpcException with error code 9.
*/
@Override
public boolean preHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<?> clazz,
Method method,
Object[] args,
Annotation annotation,
ThreadLocal<Map<String, Object>> extParams)
throws RpcException {
//Do something...
return true;
}
/**Post-interception: a callback invoked after a successful RPC call.
*@return Returns true to continue the execution. Returns false to break the current request and throw an RpcException with error code 9.
*/
@Override
public boolean postHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<?> clazz,
Method method,
Object[] args,
Annotation annotation) throws RpcException {
//Do something...
return true;
}
/**
* Exception interception: a callback invoked after a failed RPC call.
* @param exception The exception that occurred during the RPC call.
* @return Returns true to continue throwing the current exception. Returns false to suppress the exception and return normally. Do not return false unless you have a specific reason.
*/
@Override
public boolean exceptionHandle(Object proxy,
ThreadLocal<Object> retValue,
byte[] retRawValue,
Class<?> clazz,
Method method,
Object[] args,
RpcException exception,
Annotation annotation) throws RpcException {
//Do something...
return true;
}
}Register an interceptor
When the framework starts, register the interceptor during the initialization of RpcService. For example:
public class MockLauncherApplicationAgent extends LauncherApplicationAgent {
public MockLauncherApplicationAgent(Application context, Object bundleContext) {
super(context, bundleContext);
}
@Override
public void preInit() {
super.preInit();
}
@Override
public void postInit() {
super.postInit();
RpcService rpcService = getMicroApplicationContext().findServiceByInterface(RpcService.class.getName());
rpcService.addRpcInterceptor(OperationType.class, new CommonInterceptor());
}
}Set RPC request headers
In the initRpcConfig method of the MainActivity class, you can set the RPC request headers. For more information, see the code sample.
private void initRpcConfig(RpcService rpcService) {
// Set request headers.
Map<String, String> headerMap = new HashMap<>();
headerMap.put("key1", "val1");
headerMap.put("key2", "val2");
rpcInvokeContext.setRequestHeaders(headerMap);
}Set RPC cookies
Set a cookie
You can call the following interface to set an RPC cookie. For Your domain, use the part of the gateway URL between the first . and the first subsequent /. For example, if the gateway URL is http://test-cn-hangzhou-mgs-gw.cloud.alipay.com/mgw.htm, then Your domain is .cloud.alipay.com.
GwCookieCacheHelper.setCookies(Your domain, cookiesMap);Remove a cookie
You can call the following interface to remove the cookie.
GwCookieCacheHelper.removeAllCookie();Set SM3 signature verification
After RPC initialization, you can call the setGlobalSignType method of the MPRpc class to set the global signature verification method to SM3.
MPRpc.setGlobalSignType(TransportConstants.SIGN_TYPE_SM3);Set RPC signatures
Interface
class TransportConstants {
public static final int SIGN_TYPE_DEFAULT = 0; // Default signature method, which is md5
public static final int SIGN_TYPE_MD5 = 1; // md5
public static final int SIGN_TYPE_HMACSHA256 = 3; // hmacsha256
public static final int SIGN_TYPE_SHA256 = 4; // sha256
public static final int SIGN_TYPE_SM3 = 5; // sm3
}
// Global RPC signature algorithm settings
MPRpc.setGlobalSignType(int signType);Example
Set the global RPC signType. This setting applies to all RPCs.
// Set the signature method to SM3.
MPRpc.setGlobalSignType(TransportConstants.SIGN_TYPE_SM3);