In this blog, i am going to tell you how to remove extension of a file using java mapping while placing it to folder.
Using this we can change the file extension in our required format while saving.
Eg: incoming file name : abc.csv.pgp
if we want .csv file or .txt file in our folder we can do it by using below java mapping.
Steps:
- Create datatype, message type and service interface. (dummy )
- Create a jar file for the below java code.
- create imported archives and import the jar file.
- create operation mapping.
Code:
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Map;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.DynamicConfiguration;
import com.sap.aii.mapping.api.DynamicConfigurationKey;
import com.sap.aii.mapping.api.StreamTransformationConstants;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class DynamicFileName_JavaMapping extends AbstractTransformation
{
public void transform(TransformationInput transformationInput, TransformationOutput transformationOutput)
throws StreamTransformationException
{
try
{
InputStream inputstream = transformationInput.getInputPayload().getInputStream();
OutputStream outputstream = transformationOutput.getOutputPayload().getOutputStream();
Map mapParameters = (Map) transformationInput.getInputHeader().getAll();
// a) Set Output File name
mapParameters.put(DynamicConfigurationKey.create("http://sap.com/xi/XI/Dynamic",StreamTransformationConstants.DYNAMIC_CONFIGURATION), "");
DynamicConfiguration conf = (DynamicConfiguration) mapParameters.get(StreamTransformationConstants.DYNAMIC_CONFIGURATION);
DynamicConfigurationKey key = DynamicConfigurationKey.create("http://sap.com/xi/XI/System/File", "FileName");
String Fname=conf.get(key);
int length=Fname.length();
int index=length-8;
String newName=Fname.substring(0,index);
//Depending on your requirement edit this logic. Here, NewDynamicName + CurrentDate will be output file name.
conf.put(key, (newName+".csv"));
// b) Just copy Input file content to Output file content
byte[] b = new byte[inputstream.available()];
inputstream.read(b);
outputstream.write(b);
} catch (Exception exception)
{
getTrace().addDebugMessage(exception.getMessage());
throw new StreamTransformationException(exception.toString());
}
}
}
create the ID part using created SI and activate all the object.