cancel
Showing results for 
Search instead for 
Did you mean: 

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

JaySchwendemann
Active Contributor
0 Kudos

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

Accepted Solutions (1)

Accepted Solutions (1)

JaySchwendemann
Active Contributor
0 Kudos

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

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Jens,

In this thread, they suggest a lot of ways

Programmers Lounge: How to read a file MIME Type in Java

But since most of the suggestions are getting the mime type from the http header, your best bet is to use Apache Tikia. I would like to try that one out before posting, but I do not have time at the moment

Regards,

Mark

JaySchwendemann
Active Contributor
0 Kudos

Hi Mark,

thanks for the link provided. I also found this thread on stack overflow dealing with the same problem. Apache Tika seems indeed the best bet, however its really heavy with many dependencies.

So of course, just determining the mime type / content type from the files raw content is possible in java.

However, do you / does anybody know if the file adapter happens to provide the content type of the file it picked up? I fear that this is not the case but who knows what PI does behind the scenes

Cheers

Jens

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Jens,

Searching for the FileAdapter context objects, I can only find a few

Your best bet would be FileType, but I think it would only be this.

Edit: After trying it out, it is indeed returning Bin or Text. So back to Tika

Regards,

Mark

JaySchwendemann
Active Contributor
0 Kudos

Thanks Mark,

assuming that using a java library would as of now be the only way to really look into the binary stream for those magic mime type numbers, e.g. Apache Tika.

@All: However, if there happens to be a "built-in" solution at the end, please feel free to post here.

Cheers

Jens

Answers (0)