Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
GauravKant
Contributor

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.

3 Comments
Former Member
0 Kudos

Good one Gaurav.

suman_sourabh
Participant
0 Kudos

Hi Gourav,

for the line "int index = length-8;"

I think it is specific to your scenario.

Please update the code for more generic use.

Regards,
Suman

GauravKant
Contributor
0 Kudos

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

Labels in this area