UriComponentsBuilder query param and fragment - spring

I want to generate the following path: '/app#/fragment1?test=toto' with spring library UriComponentsBuilder.
What I have tried so far:
UriComponentsBuilder ucb = UriComponentsBuilder.fromPath("/app").fragment("fragment1").queryParam("test","toto");
ucb.toUriString(); // -> gives me as result : '/app?test=toto#/fragment1'
Any idea how to achieve this in an elegant way?

I would simply do something like :
// first build fragment part
String fragmentWithQueryParams = UriComponentsBuilder.fromPath("/fragment1").queryParam("test","toto").toUriString();
// then generate full path
String fullPath = UriComponentsBuilder.fromPath("/app").fragment(fragmentWithQueryParams).toUriString());
System.out.println(fullPath); // -> "/app#/fragment1?test=toto"

Related

Convert OkHttp's HttpUrl to a relative url

How can I convert a HttpUrl to a relative url, e.g. https://example.com/fo/o?bar#baz to /fo/o?bar#baz?
There isn't a great solution. Most obviously one of
building it from components your self, like path, query, fragment.
Applying a stripping regex.
Removing a common prefix.
So option 3
val u = "https://example.com/fo/o?bar#baz".toHttpUrl()
val u2 = u.resolve("/")
println(u) // https://example.com/fo/o?bar#baz
println(u2) // https://example.com/
println(u.toString().substring(u2.toString().length - 1)) // /fo/o?bar#baz

Can't properly build HttpUrl which contains hash-bang

I'm trying to build HttpUrl instance which contains hash-bang but can't properly do so. Final URL string value should look like this example: https://www.google.com/mobile/#!/id?platform=android
I've tried few solutions:
https://gist.github.com/novachevskyi/71529d8fdecf120e626af227193a9e0f
When adding hash-bang with HttpUrl.Builder::addEncodedPathSegment then final result would contain encoded hash symbol.
https://gist.github.com/novachevskyi/12b59e53d6162fb1cd4e6236b03fb504
After parsing base URL which contains hash-bang I'm getting next result:
https://www.google.com/mobile/?platform=android#!/id
Is there any way to build HttpUrl instance were string value would contain hash-bang in it?
You can do it with .parse() or with .fragment().
HttpUrl a = HttpUrl.parse("https://www.google.com/mobile/#!/id?platform=android");
HttpUrl b = new HttpUrl.Builder()
.scheme("https")
.host("www.google.com")
.encodedPath("/mobile/")
.fragment("!/id?platform=android")
.build();

TextBox1.Text string on end of a link

I wanted to make an application that checks if a link was blocked on steam.
I used the linkfilter page. (https://steamcommunity.com/linkfilter/?url=)
I tried doing it like this:
WebBrowser1.Url = https://steamcommunity.com/linkfilter/?url=(TextBox1.Text)
But I got two errors. Is there a way to do this?
Did you try setting your string values as actual strings?:
WebBrowser1.Url = new Uri("https://steamcommunity.com/linkfilter/?url=" + TextBox1.Text);
or:
WebBrowser1.Url = new Uri(string.Format("https://steamcommunity.com/linkfilter/?url={0}", TextBox1.Text));

spring mongo creating like query

I can match starting of string i.e clo with keywords and it gives me correct result db.post.find({"keywords":"/^clo/"}).pretty() When I tried to write same query using spring mongo.It not working properly. It gives result as % string %. i.e. matches anywhere in string. I am trying to match only at starting . my code is
String pattern = "/^" + keyword + "/";
Criteria criteria2 = Criteria.where("keywords").is(keyword).regex(pattern);
Where I am missing ?
You can do it like this:
Query.query(Criteria.where("keywords").regex("^clo"))
Or use it as native query:
new BasicQuery("{'keywords' : '/^clo/'}")
Method is() provides the full equals, regex() has to be without / wrappers.
That's is your issue.

DisplayTool installation and usage

I am using Velocity 1.7 to format string and I had some trouble with default values. Velocity by itself has no special syntax for case when value is not set and we want to use some another, default value.
By the means of Velocity it looks like:
#if(!${name})Default John#else${name}#end
which is unconveniant for my case.
After googling I've found DisplayTool, according to documentation it will look like:
$display.alt($name,"Default John")
So I added maven dependency but not sure how to add DisplayTool to my method and it is hard to found instructions for this.
Maybe somebody can help with advice or give useful links?..
My method:
public String testVelocity(String url) throws Exception{
Velocity.init();
VelocityContext context = getVelocityContext();//gets simple VelocityContext object
Writer out = new StringWriter();
Velocity.evaluate(context, out, "testing", url);
logger.info("got first results "+out);
return out.toString();
}
When I send
String url = "http://www.test.com?withDefault=$display.alt(\"not null\",\"exampleDefaults\")&truncate=$display.truncate(\"This is a long string.\", 10)";
String result = testVelocity(url);
I get "http://www.test.com?withDefault=$display.alt(\"not null\",\"exampleDefaults\")&truncate=$display.truncate(\"This is a long string.\", 10)" without changes, but should get
"http://www.test.com?withDefault=not null&truncate=This is...
Please tell me what I am missing. Thanks.
The construction of the URL occurs in your Java code, before you invoke Velocity, so Velocity isn't going to evaluate $display.alt(\"not null\",\"exampleDefaults\"). That syntax will be valid only in a Velocity template (which typically have .vm extensions).
In the Java code, there's no need to use the $ notation, you can just call the DisplayTool methods directly. I've not worked with DisplayTool before, but it's probably something like this:
DisplayTool display = new DisplayTool();
String withDefault = display.alt("not null","exampleDefaults");
String truncate = display.truncate("This is a long string.", 10);
String url = "http://www.test.com?"
+ withDefault=" + withDefault
+ "&truncate=" + truncate;
It might be better, though, to call your DisplayTool methods directly from the Velocity template. That's what is shown in the example usage.

Resources