Integration Server (IS) cannot subtract resource from path

Hi guys, I have the following path url/records/848669637/names/1756

I need to substring 1756 as resource, how can I achieve this? transformer substring doesn’t seem to have indexOf to select my start, the lenght could change and it does not work.

e.g /^names/:SEQUENCE

then get 1756

pub.string:substring does allow you to specify the indices (begin and end), but you need to manipulate the position as follows -

  1. Get the length of your desired string (i.e., /names/)
  2. Use the pub.string:indexOf service to get the index of your desired string (i.e., /names/) in your inputString
  3. Add the value from step 2 with the length from step 1, to arrive at a new beginIndex
  4. Pass beginIndex to the substring service along with your inputString and it pops out “1756” as shown below

KM

1 Like

Thanks, I think that will work out the solution, I didn’t see there is indexOf.

Not necessarily easier (mainly a matter of taste) but another option is to use

pub.string:tokenize

which you can use to get all parts delimited by a specific character (e.g. /) as a string list. You just need to get the last entry of the list. This comes in handy especially if you need more parts than only one.

2 Likes

I was going to suggest tokenize as an alternative, but the array index of the last element varies dynamically depending on the number of resources in the URL, so a static map will be a problem.

KM

A lastIndexOf service would have been quite useful , lastIndexOf("/") , followed by a subString would have done the trick quite easily. @Martin_Wroblinski , @Venkata_Kasi_Viswanath_Mugada1 - have you seen such a requirement many other times?

-NP

2 Likes

I’m quite sure, everybody faced this and just worked around, as it is quite easy, nevertheless I agree, as getting the last part (like filename or extension) is a really common use case.

About the list out of the tokenize service: pub.list:sizeOfList gives you the size of the list, so just substract one and do an index map (you probably know this, but for the others):

When mapping from a list to a single element, you can click the mapping line and enter an index, to select one element to be mapped. This index can be a runtime variable.

3 Likes

You are right of course, @Martin_Wroblinski; I was only referring to tokenize as a standalone step :slight_smile:

@Nagendra_Prasad_A_R, yes I’ve had numerous requirements for string manipulation utilities that I had to build.

The most frequent one that I’ve used is a “search” type functionality to capture the number of ALL occurrences and positions, of a given substring (across multiple language encodings) in a single execution (link).

KM

In general, I avoid tokenize and instead use split. tokenize has some behaviors that may not be desired, depending on the specific needs. I won’t list the differences here – plenty of good material on the web covers that.

WmPublic does not have a split service. But it is trivial to create one in your own “public” package.

Other useful services for your own public package would be splitPath, leveraging java.nio.file.Paths and Path classes to do the work. And splitURL, leveraging java.net.URL.

1 Like