Skip to Content

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 )

DT.PNG

  • Create a jar file for the below java code.
  • create imported archives and import the jar file.

IA.PNG

  • create operation mapping.

OM.PNG

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());

  }

}

}

J_code.PNG

create the ID part using created SI and activate all the object.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

    1. Gaurav Kant Post author

      Hi Sourabh,

      I have taken an example of abc.csv.pgp and if we want filename as abc.txt or any other extension for the above incoming file. we can use the same code.

      If any change in incoming file you can change the index value accordingly.

      for eg. if incoming file is coming as abc.pgp and you want it as abc.txt than

      “int index=lenght-4;”

      Hope it will help for other incoming file extension.

      Regards,

      Gaurav

      (0) 

Leave a Reply