All Products
Search
Document Center

Advanced guide

Last Updated: Apr 07, 2021

This article describes the settings of mobile gateway RPC interceptor, RPC request header, and RPC Cookie.

Intercept RPC requests

In some situations during business development, it is required to control the network requests from clients (for example, intercept network requests, forbid the access to some interfaces or limit traffic), and you can do that with RPC interceptor.

Create a global interceptor

  1. public class CommonInterceptor implements RpcInterceptor {
  2. /**
  3. * Pre-interception: Call back before sending RPC.
  4. * @param proxy: RPC proxy object.
  5. * @param clazz: Model class of rpcface, you can judge which RPC model class is called through clazz.
  6. * @param method: The method called by the current RPC.
  7. * @throws RpcException
  8. * @return true means proceeding; false means interrupting the current request and throwing RpcException, with error code: 9.
  9. */
  10. @Override
  11. public boolean preHandle(Object proxy,
  12. ThreadLocal<Object> retValue,
  13. byte[] retRawValue,
  14. Class<? > clazz,
  15. Method method,
  16. Object[] args,
  17. Annotation annotation,
  18. ThreadLocal<Map<String, Object>> extParams)
  19. throws RpcException {
  20. //Do something...
  21. return true;
  22. }
  23. /**Post-interception: Call back when RPC is successfully initiated.
  24. * @return true means proceeding; false means interrupting the current request and throwing RpcException, with error code: 9.
  25. */
  26. @Override
  27. public boolean postHandle(Object proxy,
  28. ThreadLocal<Object> retValue,
  29. byte[] retRawValue,
  30. Class<? > clazz,
  31. Method method,
  32. Object[] args,
  33. Annotation annotation) throws RpcException {
  34. //Do something...
  35. return true;
  36. }
  37. /**
  38. Exception interception: Call back when initiating RPC failed.
  39. * @param exception: Error occurs on the current RPC.
  40. * @return true means proceeding to throw the current exception; false means returning as normal without throwing any exception. If there is no special requirement, don’t return false.
  41. */
  42. @Override
  43. public boolean exceptionHandle(Object proxy,
  44. ThreadLocal<Object> retValue,
  45. byte[] retRawValue,
  46. Class<? > clazz,
  47. Method method,
  48. Object[] args,
  49. RpcException exception,
  50. Annotation annotation) throws RpcException {
  51. //Do something...
  52. return true;
  53. }
  54. }

Register an interceptor

During framework startup, register the interceptor when RpcService is being initialized, for example:

  1. public class MockLauncherApplicationAgent extends LauncherApplicationAgent {
  2. public MockLauncherApplicationAgent(Application context, Object bundleContext) {
  3. super(context, bundleContext);
  4. }
  5. @Override
  6. public void preInit() {
  7. super.preInit();
  8. }
  9. @Override
  10. public void postInit() {
  11. super.postInit();
  12. RpcService rpcService = getMicroApplicationContext().findServiceByInterface(RpcService.class.getName());
  13. rpcService.addRpcInterceptor(OperationType.class, new CommonInterceptor());
  14. }
  15. }

Set RPC request header

Set the RPC request header in the initRpcConfig method of the MainActivity class. For details, see Code sample.

  1. private void initRpcConfig(RpcService rpcService) {
  2. //Set the request header
  3. Map<String, String> headerMap = new HashMap<>();
  4. headerMap.put("key1", "val1");
  5. headerMap.put("key2", "val2");
  6. rpcInvokeContext.setRequestHeaders(headerMap);
  7. }

Set RPC cookie

Set cookie

Set RPC cookie by calling the following interface. Among them, the rule of Your domain is all the contents between the first . of the gateway URL and the first / after the dot. 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.

  1. GwCookieCacheHelper.setCookies(Your domain, cookiesMap);

Remove cookie

Call the following interface to remove the set cookies.

  1. GwCookieCacheHelper.removeAllCookie();