0

I have a following string which I have to split and replace the value on some condition

http://localhost:8080/api/upload/form/{uploadType}/{uploadName}

in which I have to replace the value only for {uploadType}/{uploadName} . I really don't know how to go about to replace them with value.

The strings which are in {uploadType}/{uploadName} can be of any type to replace.

I've tried something like the following :

package com.test.poc;

public class TestString {
public static void main(String[] args) throws ClassNotFoundException {
    String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
    String[] toReplaceWith =toBeFixed.split("{");
for (String string : toReplaceWith) {
    System.out.println("string : "+string);
}   
}

}

but i get the following exception:

Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition
{
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.closure(Pattern.java:2775)
    at java.util.regex.Pattern.sequence(Pattern.java:1889)
    at java.util.regex.Pattern.expr(Pattern.java:1752)
    at java.util.regex.Pattern.compile(Pattern.java:1460)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at java.lang.String.split(String.java:2292)
    at java.lang.String.split(String.java:2334)
    at com.test.poc.TestString.main(TestString.java:9)

EDIT :

this is the method i tried based on Sean Patrick Floyd answer

public String doPOSTPathVariable(String uri,List paramsList) throws IllegalArgumentException, IllegalAccessException, InstantiationException, Exception{
        String uriString="";
        UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build();
        for (int j = 0; j<= paramsList.size(); j++) {
            System.out.println("path variable");
            MethodParams methodParams;
            methodParams =(MethodParams) paramsList.get(j);

            if(methodParams.isPrimitive() && methodParams.getDataType()=="boolean"){
                  uriString = uriComponents.expand(true).encode().toUriString();
            }
            if(methodParams.isPrimitive() && methodParams.getDataType()=="java.math.BigDecimal"){
                uriString = uriComponents.expand(123).encode().toUriString();
            }
            if(methodParams.isPrimitive() && methodParams.getDataType()=="java.lang.String"){
                uriString = uriComponents.expand("hexgen").encode().toUriString();
            }
       } 
        return uriString;
    }

but i get following exception :

java.lang.IllegalArgumentException: Not enough variable values available to expand 'uploadName'
    at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:1025)
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:443)
    at org.springframework.web.util.UriComponents.access$1(UriComponents.java:431)
    at org.springframework.web.util.UriComponents$FullPathComponent.expand(UriComponents.java:800)
    at org.springframework.web.util.UriComponents.expandInternal(UriComponents.java:413)
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:404)
    at com.hexgen.tools.HexgenClassUtils.doPOSTPathVariable(HexgenClassUtils.java:208)
    at com.hexgen.reflection.HttpClientRequests.handleHTTPRequest(HttpClientRequests.java:77)
    at com.hexgen.reflection.HexgenWebAPITest.main(HexgenWebAPITest.java:115)

Could some one help me on this please?

Java Questions
  • 7,813
  • 41
  • 118
  • 176

5 Answers5

2

Perhaps the UriBuilder?

UriBuilder.fromPath("http://localhost:8080/api/upload/form/{uploadType}/{uploadName}").build("foo", "bar");

(Conveniently happens to use the exact formatting you are using)

Quetzalcoatl
  • 3,037
  • 2
  • 18
  • 27
  • the no of parameter to be replaced will be deducted at the run time only – Java Questions May 02 '13 at 10:25
  • With `UriBuilder` being a builder pattern, you can iteratively add components to it using its convenience methods that return itself. Its exists for your situation exactly like yours, much cleaner than regex. – Quetzalcoatl May 02 '13 at 10:28
2

If you use Spring MVC, this functionality is available out of the box with the new URI builder technology.

UriComponents uriComponents =
    UriComponentsBuilder.fromUriString(
        "http://example.com/hotels/{hotel}/bookings/{booking}").build();

URI uri = uriComponents.expand("42", "21").encode().toUri();
// or:
String uriString = uriComponents.expand("42", "21").encode().toUriString();

(In fact you can still use this technology, even if you don't use Spring MVC, but you'll need to have the Spring MVC jar on the Classpath, obviously)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
1

You can try this one :

public class TestString {
public static void main(String[] args) throws ClassNotFoundException {
String toBeFixed = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}"; 
String[] toReplaceWith =toBeFixed.split("\\{");
for (String string : toReplaceWith) {
System.out.println("string : "+string);
}   
}

}
Madhusudan Joshi
  • 4,438
  • 3
  • 26
  • 42
1

UrlBuilder is the solution, but if you are just looking around delimiters, you can also use substrings:

String url = "http://localhost:8080/api/upload/form/{uploadType}/{uploadName}";
int lastSlash = url.lastIndexOf('/');
int secondLastSlash = url.substring(0, lastSlash).lastIndexOf('/');
System.out.println(url.substring(secondLastSlash+1, lastSlash));
System.out.println(url.substring(lastSlash+1));

To remove the curly braces, you can use String.replace('{', '') and String.replace('}', '') on the strings

Sumit Bisht
  • 1,507
  • 1
  • 16
  • 31
0

A { is a regex meta-character used for range repetitions.

before doing that

toBeFixed = toBeFixed.replaceAll("\\{", "\n");   

see docs also :http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Similar question :Java String ReplaceAll method giving illegal repetition error?

Community
  • 1
  • 1
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • `http://localhost:8080/api/upload/form/ uploadType}/ uploadName}` is the out put if i follow your code, how can i replace those two with value – Java Questions May 02 '13 at 10:40