Skip to Content

Get content type from file in FTP --> PI --> Proxy scenario

Hi all,

I have a requirement to pick up files from an external FTP host and attach them to the GOS of an invoice in ERP.

There are probably a few ways to do this, currently I'm picking up the file as binary, use java mapping to read the input payload and create an output attachment for that. I read the filename from dynamic configuration as well.

This all works well, and I'm able to pick up the attachment on the receiver ABAP proxy, too.

However, when calling outputAttachments.create() I have to specify the content type of the file. Currently I'm just using a (dirty) hack to "determine" the file name from the file extension. This will work for now but break if the sender happens to put other files than PDF or XML on the FTP host.

//access dynamic configuration
DynamicConfiguration conf = in.getDynamicConfiguration();


//read filename
String fileName = conf.get(KEY_FILENAME);
if( fileName == null){
  getTrace().addInfo("Could not get file name from dynamic configuration");
  getTrace().addInfo("Using default filename payload_yyyyMMddHHmmssSSS.dat");
  Date now = new Date();
  fileName = "payload_" + simpleDateFormatMilliseconds.format(now) + ".dat";
}


//TODO: Find a better way to get the content type then using file extensions
String fileExtension = "";
String contentType = "";
int fileExtenstionIndex = fileName.lastIndexOf(".");
if (fileExtenstionIndex > 0) {
  fileExtension = fileName.substring(fileExtenstionIndex+1).toLowerCase();
}


if (fileExtension == "pdf") {
  contentType = "application/pdf";
} else if(fileExtension == "xml") {
  contentType = "application/xml";
}
else{
  contentType = "application/octet-stream";
}

When I then create the attachment from the input payload I give this as a content type

//get handle of output attachments
OutputAttachments outputAttachments = out.getOutputAttachments();


//read input payload
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
InputStream inputStream = in.getInputPayload().getInputStream();
int nRead;
byte[] data = new byte[16384];


try {
  while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
  buffer.write(data, 0, nRead);
  }
} catch (IOException e) {
  getTrace().addWarning(e.getMessage());
}


//create new attachment from input payload
Attachment attachment = outputAttachments.create(fileName, contentType, buffer.toByteArray());

//assign new attachment to outbound attachment collection
outputAttachments.setAttachment(attachment);

My question goes along this:

Is there any possibility to access the content type via some meta data / module data / adapter specific attributes, whatnot... of the file adapter itself? I think that would be way better than just guessing via file name extension.

I know there are more sophisticated libraries that can inspect the stream for magic mime type numbers but currently I'd like to cope with this without deploying a 3rd party library to PIs J2EE.

If there are other lean solutions to solve my overall requirement (see beginning), please don't hesitate to let me know.

Find attached the complete sourcecode of the mapping for reference

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on Nov 06, 2015 at 07:35 AM

    oh, and btw, I'm quite half-baked when it comes to java, so don't be too harsh on my when I did something terribly wrong 🤪

    EDIT:

    Did I mention that I was not particularly strong in Java? The equality check has to be for value, not for reference, of course 🤪

    if (fileExtension.equals("pdf")) {
      contentType = "application/pdf";
    } else if(fileExtension.equals("xml")) {
      contentType = "application/xml";
    }
    else{
      contentType = "application/octet-stream";
    }
    

    Cheers

    Jens

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.