Skip to content

Java VS Code Case API Single Page Console Application

MsaasAPI edited this page Oct 5, 2018 · 31 revisions

CONTENTS

PREREQUISITE

INSTALL DEMO01

  1. Download the project source code by partially downloading the repository advisable or by downloading the entire API repository not advisable.

  2. Unzip/Extract the folder.

  3. Open VS Code. Then open the project folder by either a. clicking Open Folder button, b. hotkey Ctrl K + Ctrl O, or c. File > Open Folder….

  4. Select target folder (extracted zip file folder) on File Explorer, and click Select Folder button.

  5. Build project - Open Command Palette by either a. hotkey F1, b. hotkey Ctrl + Shift P, or c. View > Command Palette….

  6. The following popup palette will appear at top center of VS Code window.

  7. Type the following command, and press Enter key.

    Tasks: Run Build Task
    

  8. Then type the following command, and press Enter key.

    Compile
    

  9. Then type the following command, and press Enter key.

    Continue without scanning the task output
    

  10. On the pop-up Integrated Terminal, verify the build is successful.

  11. Press F5 key to launch the debugger. During the first-time launch, select Java for the following pop-up query if prompted.

  12. If log4j verbose option is activated, hundreds of message lines may appear on console. Verify there is no error.

  13. Click Explorer icon on activity bar.

  14. Click Logs folder (This folder shall be automatically created during the project initial run).

  15. Click the first ever log file created under Logs folder, and log file will be opened on the right.

  16. The log generally contains at least one of the following:

    • Case number of a new case just created.
    • Case JSON of the case just created.
    • HTTP Status Code (200, 201, 204, etc).
  17. If no error is found in log file and on the console, we are ready to take deep look at the source code.

DEMO01 ANATOMY

  1. This project is architected in such a way that all Java codes are condensed in one single file Project.java, at the expense of poor object-oriented design. Only two files warrants our attention:
    • Program.java: Java codes.
    • pom.xml: dependencies.

Program.java

  1. On Explorer, navigate to src > main > java > com > contoso > The Project Name > Program.java. Program.java will open on the right.

  2. In this application, the basic flow is:

    • Get access token (Bearer Token).
    GetRequestToken()
    
    • Compile header and payload (if any).
    SetPayloads()
    AddMetaDataToHttpRequest()
    
    • Make HTTP request (GET, POST, or PATCH).
    Run()
    MakeRequest()
    
    • Log returns.
  3. In Get access token stage, the ADAL for Java library is leveraged. The steps are as follow:

    • Retrieve SSL certificate from keystore configured in Set up SSL Keystore.
    • Extract private key and compile JWT to instantiate authContext.
    • Make POST request for token.
    • Receive and store access token.
  4. In Compile header and payload stage, the steps are as follow:

    • In SetPayloads(), pre-drafted attributes are added to _payloads cache for later use.
    • In AddMetaDataToHttpRequest(), header(s) and payload(s) are added to request instance.
  5. In Make HTTP request stage, the steps are as follow:

    • In Run(), it serves as a wrapper/facade for MakeRequest() by configuring/converting argument to proper values to MakeRequest(). At the end, it also log results.
    • In MakeRequest(), it invokes AddMetaDataToHttpRequest() configure header/payload, execute request, and cache transactions for later use.
    • Note that logging and caching is different in that the former persists returned payload into file system, while the latter persists some important values in memory for immediate later use during the same session.
    • It is worth-noting that, besides individual scenario runs, there is also a Demo() run, which has this flow
      • Invokes Run().
      Run(Api.DEMO, "");
      
      • Which in turn invokes Demo().
      Demo();
      
      • In Demo(), the following methods are invoked to simulate a common scenario which any partner would use:
      Scenario100_1a_CreateCase()
      Scenario100_2a_GetNewCase()
      Scenario100_2a_GetPartnerCaseReferenceIdGuidFromNewCase()
      Scenario100_2a_GetDefaultCustomerFromNewCase()
      Scenario100_2a_GetDefaultCustomerIdGuidFromDefaultCustomer()
      Scenario100_2a_GetContactIdGuidByKeywordFromDefaultCustomer()
      Scenario110_AssignReassignPartnerCaseReferencesAgent()
      Scenario115_ChangePartnerCaseReferencesPartnerCaseState()
      Scenario120_CreateNote()
      Scenario145_UpdateContact()
      Scenario190_CloseCase()
      

pom.xml

  1. On Explorer, click pom.xml, which will then open on the right.

  2. It contains the following dependencies: