Skip to Content
0

issue with PDF upload to sharepoint

Jun 22, 2017 at 08:39 AM

179

avatar image

Dear Experts,

I have a file to sharepoint integration in which i need to upload PDF files to share point folder in base64 format.

I have done java mapping for base64 conversion but post conversion and upload, file is getting blank at sharepoint folder.

Assuming that the issue is with base64 conversion and file is corrupted post conversion as file size is same as processed file at sharepoint.

Please suggest on this.

Thanks in Advance!! :)

PS: I have tested the same using SOAPUI and are able to upload PDF file with file content.

I have inserted file using insert file as base64 option in SOAPUI.

Regards,

Gaurav

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Evgeniy Kolmakov Jun 28, 2017 at 12:56 PM
0

Hi Gaurav!

package com.evk.java.train;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.xml.bind.DatatypeConverter;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class BuildXMLWithBase64 extends AbstractTransformation {
@Override
public void transform(TransformationInput in, TransformationOutput out)throws StreamTransformationException {
executeMapping(in.getInputPayload().getInputStream(), out.getOutputPayload().getOutputStream());
}

public void executeMapping(InputStream in, OutputStream out) throws StreamTransformationException {
String xmlString = "";
String XML_START_TAG = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
XML_START_TAG += "<ns:UploadPlainDocumentWithFolder xmlns:ns=\"http://schemas.abc.com/xyz/2010/11\">";
String XML_END_TAG = "</ns:UploadPlainDocumentWithFolder>";

xmlString = xmlString + XML_START_TAG + "<PlainDoc><DocumentName>" + "YourDocName" + "</DocumentName>";
xmlString += "<DocumentURL>" + "http://yourURL" + "</DocumentURL>";

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1)
  buffer.write(data, 0, nRead);
buffer.flush();
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}

xmlString += "<DocumentBytes>" + DatatypeConverter.printBase64Binary(buffer.toByteArray()) + "</DocumentBytes></PlainDoc>";
xmlString += "<DocLibURL>http://xyz</DocLibURL><ApplicationId>APPL_ID</ApplicationId><UserId>USR_ID</UserId>" + XML_END_TAG;

try {
out.write(xmlString.getBytes("UTF-8"));
}
catch (Exception e) {
throw new StreamTransformationException(e.getMessage());
}
}

public static void main(String[] args) {
try {
FileInputStream inFile = new FileInputStream("C:\\Tests\\FCC_01.png");
FileOutputStream outFile = new FileOutputStream("C:\\Tests\\FCC_01_out.xml");
BuildXMLWithBase64 mapper = new BuildXMLWithBase64();
mapper.executeMapping(inFile, outFile);

outFile.flush();
outFile.close();
}
catch (Exception e) {
e.printStackTrace();
}
}

}

Output:

Regards, Evgeniy.


Show 1 Share
10 |10000 characters needed characters left characters exceeded

Hi Evgeniy,

Thank you so much. It has solved my problem of base64 conversion.

I really appreciate your insights, and am looking forward to implementing many of your suggestions.

Regards,

Gaurav

1
Andrzej Filusz Jun 22, 2017 at 10:26 AM
0

Hi Gaurav,

Are you sure that your Base64 class has the correct implementation? Does it come from a trusted source, like a standard SDK or Apache libraries?

For example: org.apache.xml.security.utils.Base64;

Regards,

Andrzej

Show 7 Share
10 |10000 characters needed characters left characters exceeded

Hi Andrzej,

Thanks!!

I have tried with below jars which belong to itextpdf-5.4.1.jar, com.sap.aii.utilxi.core.jar, java-util-1.3.1.jar respectively.

com.itextpdf.text.pdf.codec.Base64;

com.sap.aii.utilxi.base64.api.Base64;

com.oschrenk.io.Base64;

Regards,

Gaurav

0

Hi Gaurav,

I think you should make sure that you've got a problem with Base64 encoding in your java mapping class.

Please download your message (after mapping) to your disk, remove the XML part - leave only Base64 content, decode it using Notepad++ (for example), save to disk and finally compare it with the source PDF file by contents (using Total Commander for example). Are they identical?

Regards,

Andrzej

0

Hi Andrzej,

Thanks !!

I will try and update on this.

Regards,

Gaurav

0

Hi Andrzej,

I tried with PI base64 content using notepad++ it is getting blank post Base64 decode and saving on disk.

I tried same with SOAPUI content and i am able to see the data post decode and saving on disk.

Regards,

Gaurav

0

Hi Gaurav,

Please try to use the Base64 class from Apache libraries (you can take it from lib folder of your SoapUI as Manoj suggested, your version can be different):

xmlsec-1.4.5.jar 
org.apache.xml.security.utils.Base64 
commons-codec-1.3.jar 
org.apache.commons.codec.binary.Base64

If you would still have a problem with a proper Base64 encoding, then please provide your java mapping code. Maybe there is something wrong with it.

Regards,

Andrzej

1

In addition to Andrzej: did you test your mapping in IDE? Can you confirm that your java code works as required? If no, this means that your code is incorrect. Provide us your java code example.

P.S. I usually use javax.xml.bind.DatatypeConverter class for Base64 encoding/decoding and have no issues.

Regards, Evgeniy.

0

Hi Evgeniy, Andrzej,

PFB code example using for base 64 conversion.

Code:

import java.io.*; //import com.oschrenk.io.Base64; import com.itextpdf.text.pdf.codec.Base64; import com.sap.aii.mapping.api.*; //import com.sap.aii.utilxi.base64.api.Base64; import org.apache.commons.codec.binary.Base64;

public class base64Conversion extends AbstractTransformation { public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException {

try {

String targetxml1 =""; String line =""; String source="";

InputStream ins = in.getInputPayload().getInputStream();

BufferedReader br = new BufferedReader( new InputStreamReader(ins));

String XML_START_TAG = " http://schemas.abc.com/xyz/2010/11\">";

String XML_END_TAG = "</ns:UploadPlainDocumentWithFolder>";

targetxml1 = XML_START_TAG+ " "+ " abc.pdf "+ " http://xyz " + " ";

String targetxml2="</ns:DocumentBytes>" + "</ns:PlainDoc>" + "<ns:DocLibURL>http://xyz</ns:DocLibURL>" + "<ns:ApplicationId>xyz</ns:ApplicationId>" + "<ns:UserId>gaurav</ns:UserId>" + XML_END_TAG;

String base64="";

out.getOutputPayload().getOutputStream().write(targetxml1.getBytes("UTF-8"));

line = br.readLine();

while (line != null)

{ source +=line;

//byte[] str=source.getBytes();

base64=Base64.encodeBytes(source.getBytes());

line=br.readLine();

}

br.close();

out.getOutputPayload().getOutputStream().write(base64.getBytes("UTF-8")); out.getOutputPayload().getOutputStream().write(targetxml2.getBytes("UTF-8"));

}

catch (Exception e) { throw new StreamTransformationException(e.getMessage()); } } }

tags: PFB start and end tags as it is not appearing correctly in code post upload.

Structure for uploading:

Please correct/update if anything wrong with the code...

Thanks alot in advance!!

Regards,

Gaurav

str.png (7.8 kB)
start.png (7.4 kB)
0
Manoj K Jun 22, 2017 at 09:41 AM
0

Gaurav,

Have you tried comparing the RAW data being sent from SOAP UI with the content being sent from PI after execution of soap channel using TraceHTTP ?Do you see any difference in them ?

Br,

Manoj

Show 4 Share
10 |10000 characters needed characters left characters exceeded

Hi Manoj,

Thanks!!

Yes, PI base64 data is different from SOAPUI. I tried with many below base64 jars but all are giving different values post conversion.

com.oschrenk.io.Base64;

com.itextpdf.text.pdf.codec.Base64;

com.sap.aii.utilxi.base64.api.Base64;

Could you please guide me on this?

Regards,

Gaurav

0

Hi Gaurav,

Have you tried encrypting using Commons-code.jar ,

Because i believe this is the one which SOAP UI uses for encoding and decoding.

Br,

Manoj

enc.png (63.7 kB)
0

Hi Manoj,

Thanks!!

I will try with this jar and update.

Regards,

Gaurav

0

Hi Manoj,

I tried with Commons-code-1.3.jar but still getting blank file after processing to SharePoint.

Regards,

Gaurav

0