2

I want to extract the path parameter value in HttpServletRequest

Sample URL: abc.api.com/learn/sections/{sectionId}/assignments/{assignmentId}

sectionString = Optional.ofNullable(request.getServletPath().split("/")[3]);

I try to extract section id by splitting the URI path, but this URI can be changed. Does anybody have solid solutions for this? Thanks

ERK
  • 344
  • 6
  • 27

3 Answers3

4

Assuming that sectionId comes always after sections, you can use a simple regex, e.g.

String uri = "abc.api.com/learn/sections/asdf-987/assignments/dsfwq98r7sdfg";
Matcher m = Pattern.compile(".*\\/sections\\/([a-zA-Z0-9-]+)(\\/?).*").matcher(uri);
if (m.matches()) {
    System.out.println(m.group(1)); //asdf-987
}

(This assumes that the UUID has a format aaa-AAAA-9999)

You can do it similarly for assignmentId, or even do it in 1 regex

radoh
  • 4,554
  • 5
  • 30
  • 45
1

Here is a solution

unit test to see how it works:

public class ParameterResolverTest {

    @Test
    public void testParameterExtraction() {
        final ParameterResolver parameterResolver = new ParameterResolver("abc.api.com/learn/sections/{sectionId}/assignments/{assignmentId}");
        final Map<String, String> resultMap = parameterResolver.parametersByName("abc.api.com/learn/sections/0000-0000/assignments/1111-1111");
        Assert.assertEquals("0000-0000", resultMap.get("sectionId"));
        Assert.assertEquals("1111-1111", resultMap.get("assignmentId"));
    }

}

ParameterResolver class:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by dkis on 2016.02.19..
 */
public class ParameterResolver {

    private static final Pattern PARAMETER_PATTERN = Pattern.compile("(\\{[a-zA-Z]+\\})");
    private final List<String> parameterNames = new ArrayList<>();
    private final Pattern pattern;

    public ParameterResolver(final String parameterTemplate) {

        final Matcher matcher = PARAMETER_PATTERN.matcher(parameterTemplate);

        while (matcher.find()) {
            if(matcher.groupCount()==1) {
                final String group = matcher.group(1);
                if(group.length()>2) {
                    parameterNames.add(group.substring(1, group.length() - 1));
                } else {
                    parameterNames.add(group);
                }
            }
        }
        pattern = Pattern.compile(Pattern.quote(matcher.replaceAll("_____PARAM_____")).replace("_____PARAM_____", "\\E([^/]*)\\Q"));

    }

    public Map<String, String> parametersByName(final String uriString) {
        final Matcher matcher = pattern.matcher(uriString);
        if(!matcher.matches()) {
            throw new IllegalArgumentException("Uri not matches!");
        }
        final Map<String, String> map = new HashMap<>();
        for(int i = 1;i<=matcher.groupCount();i++) {
            map.put(parameterNames.get(i-1), matcher.group(i));
        }
        return map;

    }

}
Dániel Kis
  • 2,341
  • 5
  • 28
  • 51
-2

As far as I know, Java SDK don't have default solution for it. If you use some framework like Spring MVC/Apache CXF. It should have solution for it

user1668302
  • 137
  • 1
  • 5