SPEL: How to download attachment using SPEL

Document created by cdtj Champion on Feb 17, 2017
Version 1Show Document
  • View in full screen mode

Previous doc: SPEL: How to upload attachment using SPEL 

 

SPEL code

int downloadAttmnt(int attmnt_id) {
     object boplog, ctx;
     string currentUser;
     int i, bopsid;

     boplog = (object)"@|BOP-LOGIN|validate_user|0|0";
     // Create new rep object
     rep = (object)format("@|rep_daemon:%s|top|0|0", getenv("NX_SERVER"));

     printf("Getting current user: ");
     send_wait(0, top_object(), "call_attr", "cnt", "current_user_id");
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d] : %s\n", i, (string)msg[i]);
          }
          return -1;
     } else {
          currentUser = expand(format("&{U'%s' = cnt.id->userid}", msg[0]));
     }
     printf(currentUser + "\n");

     printf("Getting user's bopsid: ");
     send_wait(0, boplog, "get_bopsid", currentUser);
     if (msg_error()) {
          for (i=0;i<msg_length();i++) {
               printf("\tERROR[%d] : %s\n", i, (string)msg[i]);
          }
          return -2;
     } else {
          bopsid = msg[0];
     }
     printf((string)bopsid + "\n");

     ctx = create_context();
     printf("Catching\n");
     send_catch("fetchAttmnt", -1, rep, "download_file", bopsid, attmnt_id, 0);

     printf("Waiting EOF\n");
     wait_context();

     printf("Freeing\n");
     free_context();
     return 0;
}

void fetchAttmnt(...) {
     int msg_i, arg_i;

     printf("Context messages:\n");
     for (msg_i=0;msg_i<msg_length();msg_i++) {
          printf(format("\t\tmsg[%d]: %s\n", msg_i, msg[msg_i]));
     }
     /*
          msg[0]: bytes array (byte[]) could be passed to BufferedOutputStream
          msg[1]: file name
          msg[2]: zip flag
          msg[3]: fetching status:
                         OBTAIN - fetching in progress
                         EOF - end of file
          msg[4]: sequence = iteration count, big files are downloading in multiple iterations (1024kb per iteration)
     */


     if ( (msg[3] == "EOF") || (msg_error()) ) {
          printf("Context error: %d \n", msg_error());
          if (msg_error()) {
               context_set_error(1, (string)msg[0]);
          } else {
               printf("\n\nEOF!\n\n");
               context_set_error(0, "");
          }
     }
}

 

Output

I used Internet Explorer installer (~10mb) for example.

Looks like SPEL interpreter can't cast byte[] as String and showing empty message at index 0.

Getting current user: ServiceDesk
Getting user's bopsid: 1095428175
Catching
Waiting EOF
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 0
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 1
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 2
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 3
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 4
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 5
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 6
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 7
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 8
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: OBTAIN
          msg[4]: 9
Context messages:
          msg[0]:
          msg[1]: multiple-ie-setup.exe
          msg[2]: 1
          msg[3]: EOF
          msg[4]: 10
Context error: 0


EOF!

Freeing

 

PS: Hope you will not need this in real life but now you know how this works

1 person found this helpful

Attachments

Outcomes