Customizing existing REST handlers

You can customize existing REST request handlers using a post-process command.

About this task

You can inject the custom post-process command for the existing REST command by using the @ResourceHandlerPostprocessAnnotation annotation.

Procedure

  1. Create a custom command interface that has @ResourceHandlerPostprocessAnnotation that is defined as follows.
    @ResourceHandlerPostprocessAnnotation(
    path="XXX",
    method="GET",
    query="param=value",
    accessCheck=true)
    public interface CustomPostCommand {
    ...
    }
    Where
    path
    Required. Specifies the REST API path. Specifies REST API path. The relevant part of the path is the string that follows storeId in the rest API URI.
    method
    Required. Specifies the HTTP method to be used, which is either GET, POST, PUT, or DELETE.
    query
    Optional. Specify a REST QPI query string after the path. For instance, if a REST API short path is self/usable_billing_address/{orderId}?q=nameXXX, you can add the REST query into ResourceHandlerPostprocess using the parameter query="q=nameXXX".
    accessCheck
    Optional. Specify if access control is needed for the post-process controller command. If true, then the access control check is required. In all other cases, the check is not performed. The default value is false.
  2. Create a custom command implementation class, which extends the ControllerCommandImpl and implements the new created custom interface.
    public class CustomPostCommandImpl extends ControllerCommandImpl implements CustomPostCommand {
    // Add the logic to get more customized info
    // Add the customized info into responseProperties
    // You can get request param by calling requestProperties.get("request")
    }
    
  3. Restart the server and test your post-process command by calling the corresponding rest API.

Example

Following are two sets of sample code. Both examples show how to use the request parameters and the original response from the extended REST APIs.
Example A:
public class CreateSocialAccountCmdImpl extends ControllerCommandImpl implements CreateSocialAccountCmd {

   public void performExecute() throws ECException {
      System.out.println("CreateSocialAccountCmdImpl start...");

     HttpServletRequest requeset = (HttpServletRequest) requestProperties.get("request");
      Map<String, Object> bodyMap = (Map) requeset.getAttribute("resolvedBodyParams");
      Response originResponse = (Response) requeset.getAttribute("originResponse");

     String facebookId = (String) bodyMap.get("ext_facebookId");
      String facebookName = (String) bodyMap.get("ext_facebookName");

      Map map1=(Map)originResponse.getEntity();
      Map map2=(Map)map1.get("resultData");
      Long userId=Long.valueOf((String)map2.get("userId"));

      EntityDao<SocialAccount, Long> socialAccountDao = new SocialAccountDaoImpl();
      SocialAccount socialAccount = new SocialAccount();
      socialAccount.setFacebookId(facebookId);
      socialAccount.setName(facebookName);
      socialAccount.setMemberId(userId);
      socialAccount.setStoreId(Integer.valueOf(1));
      socialAccount.setSocialAccountId(socialAccountDao.generatePrimaryKey("xsocialaccount"));
      socialAccountDao.persist(socialAccount);

      System.out.println("CreateSocialAccountCmdImpl: bodyMap:" + bodyMap);
   }
}
Example B:
public class GetSocialAccountCmdImpl extends ControllerCommandImpl implements GetSocialAccountCmd {

   public void performExecute() throws ECException {

      System.out.println("GetSocialAccountCmdImpl start...");
      if (responseProperties == null) {
         responseProperties = new TypedProperty();
      }

      HttpServletRequest requeset = (HttpServletRequest) requestProperties.get("request");
      Response originResponse = (Response) requeset.getAttribute("originResponse");
      Map map1=(Map)originResponse.getEntity();
      List dataList=(List)map1.get("dataList");
      Map map2=(Map)dataList.get(0);
      ShowPersonDataAreaType person=(ShowPersonDataAreaType)map2.get("dataObject");
      System.out.println("person:"+person);
      PersonType personSDO=(PersonType)person.getPerson().get(0);
      Long memberId=Long.valueOf(personSDO.getPersonIdentifier().getUniqueID());

      EntityDao<SocialAccount, Long> socialAccountDao = new SocialAccountDaoImpl();
      List<SocialAccount> socialAccountList = socialAccountDao.query("SocialAccount.getSocialAccountsByMemberId", memberId);
      System.out.println("find socialAccount:" + socialAccountList);

      if(socialAccountList!=null && socialAccountList.size()>0){
         SocialAccount socialAccount=(SocialAccount)socialAccountList.get(0);
         if(socialAccount!=null){
            responseProperties.put("ext_facebookId", socialAccount.getFacebookId());
            responseProperties.put("ext_facebookName", socialAccount.getName());
         }
      }

      System.out.println("GetSocialAccountCmdImpl: responseProperties:" + responseProperties);
   }
}