Announcement

Collapse
No announcement yet.

Test for presence of PDF file

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Test for presence of PDF file

    Hello:

    I found this code from a previous post that can go into a deploy script to read in a PDF file;

    globalChannelMap.put('pdf', FileUtil.encode(FileUtil.readBytes('/path/to/pdf')));

    I would like to use this to send the pdf data as an attachment in an SMTP sender

    The problem is there wont always be a pdf created in a previous step. The pdf will only be created in some instances

    How can I test to make sure the PDF exists before sending the email out?

    Thanks

  • #2
    I believe that code will error if the pdf does not exist and the channel will fail to start.

    When you say "there wont always be a pdf created in a previous step" do actually mean before the channel is deployed? Or maybe a previous destination or transformer step?

    Comment


    • #3
      Either wrap your code with try/catch:
      Code:
           try{
              globalChannelMap.put('pdf', FileUtil.encode(FileUtil.readBytes('/path/to/pdf')));
          } catch(ex){}
      or (better) check first if there is a file:
      Code:
           if(new java.io.File("/path/to/pdf").isFile()){
              globalChannelMap.put('pdf', FileUtil.encode(FileUtil.readBytes('/path/to/pdf')));
          }
      or (best) do both to ensure that the file is correctly read if present:
      Code:
          if(new java.io.File("/path/to/pdf").isFile()){
              try{
                  globalChannelMap.put('pdf', FileUtil.encode(FileUtil.readBytes('/path/to/pdf')));
              }catch(exception){
                  // treat reading errors here
              }
          }
      Later, you can send the mail if the attribute is set:
      Code:
          if(globalChannelMap.containsKey("pdf")){
              // send mail
          }
      The global channel map attributes are sustained between messages. If a pdf should be loaded for each message, the channelMap container must be used instead.

      Comment

      Working...
      X