JAX-RS/Jersey path parameter regex for a simple string - java

I am trying to match strings v1 and v2. For that, I am trying the following regex : ^v(1|2) (I also tried with $ which is probably what I need). When I test it in http://www.regextester.com/, it seems to work fine. But when I used it in JAX-RS path expression it doesn't work. The expression I use is below:
#Path("/blah/{ver:^v(1|2)}/ep")
Is there anything specific to JAX-RS that I am missing?

Your attempt does not work because of the anchor ^. Quoting from the JAX-RS specification, chapter 3.7.3 (emphasis mine):
The function R(A) converts a URI path template annotation A into a regular expression as follows:
URI encode the template, ignoring URI template variable specifications.
Escape any regular expression characters in the URI template, again ignoring URI template variable specifications.
Replace each URI template variable with a capturing group containing the specified regular expression or ‘([ˆ/]+?)’ if no regular expression is specified.
If the resulting string ends with ‘/’ then remove the final character.
Append ‘(/.*)?’ to the result.
Because each URI templates is placed inside a capturing group, you can't embed anchors in it.
As such, the following will work and will match v1 or v2:
#Path("/blah/{ver:v[12]}/ep")

Try the following (without anchors):
#Path("/blah/{ver : v(1|2)}/ep")
Also, if the change is a single character only, use character set instead of the | operator:
#Path("/blah/{ver : v[12]}/ep")

Related

Passing two forward slashes as path param in Rest API

#Path("/{code:.+}")
public Response getTemplateForCode(#PathParam("code") String code)
I want to pass URI in path parameter eg(ABC://HELLO). But iam getting only single slash ABC:/HELLO only. Can anyone tell how to get double slashes with full uri ABC://HELLO. Is there any REGEX to get double slashes?
I don't want to pass in query params
In Java regex we can use a backslash to escape the special meaning of a slash.
The pattern
\w+:\/\/\w+
matches
ABC://HELLO
See https://regex101.com/r/5vRHHN/1

I am trying to match Japanese characters' Unicode range, but it throws a PatternSyntaxException

I am trying to match Regex for certain Japanese characters blocks based on this post using the String.matches( String regex ) method in the class String.
But both the range regex [\\x3041-\\x3096] and the property regex \p{Hiragana} throw a PatternSyntaxException.
My IDE also recommends properties, but none of them Japanese characters seem to be recommended.
The code that throws this error is:
c.matches( "[\x3041-\x3096]" )
The StackTrace is:
[\x3041-\x3096]
^
at java.base/java.util.regex.Pattern.error(Pattern.java:2015)
at java.base/java.util.regex.Pattern.range(Pattern.java:2813)
at java.base/java.util.regex.Pattern.clazz(Pattern.java:2701)
at java.base/java.util.regex.Pattern.sequence(Pattern.java:2126)
at java.base/java.util.regex.Pattern.expr(Pattern.java:2056)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1778)
at java.base/java.util.regex.Pattern.<init>(Pattern.java:1427)
at java.base/java.util.regex.Pattern.compile(Pattern.java:1068)
at java.base/java.util.regex.Pattern.matches(Pattern.java:1173)
at java.base/java.lang.String.matches(String.java:2024)
at lib.UIE.TextInput.valid(TextInput.java:49)
For property regex try using \p{IsHiragana} instead. The Is prefix is used to distinguish unicode scripts and categories from blocks which use In prefix.
UPDATE For \x3041 as #VGR mentioned \x in the original post had nothing to do with java and \u3041 should be used instead.
Pattern.matches("\\p{IsHiragana}", "ど"); //true
Pattern.matches("[\u3041-\u3096]", "ど"); //true
Unicode support - Oracle Java Tutorials

Is a URI containing a comma valid in a HTTP Link header?

Is the following HTTP Link header, containing a comma, valid?
Link: <http://www.example.com/foo,bar.html>; rel="canonical"
RFC5988 says:
Note that extension relation types are REQUIRED to be absolute URIs in
Link headers, and MUST be quoted if they contain a semicolon (";") or
comma (",") (as these characters are used as delimiters in the header
itself).
This doesn't cover the #link-value however. That must be a URI-Reference as per RFC 3987 which seems to allow this. The link header itself can also have multiple values, from RFC5988 section 5.5:
Link: </TheBook/chapter2>;
rel="previous"; title*=UTF-8'de'letztes%20Kapitel,
</TheBook/chapter4>;
rel="next"; title*=UTF-8'de'n%c3%a4chstes%20Kapitel
I'm parsing this link header in Java using BasicHeaderValueParser from Apache HttpCore 4.4.9 using the following code:
final String linkHeader = "<http://www.example.com/foo,bar.html>; rel=\"canonical\"";
final HeaderElement[] parsedHeaders = BasicHeaderValueParser.parseElements(linkHeader, null);
for (HeaderElement headerElement : parsedHeaders)
{
System.out.println(headerElement);
}
which tokenises on the comma and prints the following:
<http://www.example.com/foo
bar.html>; rel=canonical
Is this valid behaviour?
The comma is of course valid.
What you're missing is that the BasicHeaderValueParser is not generic. It only supports certain HTTP header fields, and "Link" isn't one of them (see syntax description in https://hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/apache/http/message/HeaderValueParser.html.
RFC 3986, section 3.3 clearly mentions, that a URI may contain sub-delimiters, which are defined in section 2.2 and may contain a comma ,.
RFC 5988 clearly states that the relation types must be quoted if they contain a comma and not the URI.
I think there is very little room for interpretation and it's IMHO an incomplete implementation on the HttpCore side.
The BasicHeaderValueParser uses the ',' as element delimiter, neglecting the fact that this character is a valid character for the header fields - which is probably ok for most cases, although not 100% compliant.
You may however provide your own custom parser as second argument (instead of null)

Replace and modify String using regex in java

I have a part of HTML from a website in the below String format:
srcset=" /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#200w.jpg?20170808 200w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#338w.jpg?20170808 338w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#445w.jpg?20170808 445w, tesla_theme/assets/img/homepage/mobile/homepage-models--touch#542w.jpg?20170808 542w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w"
I want to add http://tesla.com in front of all the urls in the srcset element like http://tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w
I believe this could be done using regex, but I am not sure.
How do I do this using Java if I have multiple srcset elements in a html string variable, and I want to replace all of the srcset url.'s and add the server url in front?
Note: The /tesla_theme will not be consistent, so I cannot use replaceAll, instead, i will have to use regex.
You can simply use String Class replace method as below, It will replace all "/_tesla" in the given String. No special regex required unless you have a kind of pattern instead of "/tesla"
String srcset=" /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#200w.jpg?20170808 200w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#338w.jpg?20170808 338w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#445w.jpg?20170808 445w, tesla_theme/assets/img/homepage/mobile/homepage-models--touch#542w.jpg?20170808 542w, /tesla_theme/assets/img/homepage/mobile/homepage-models--touch#750w.jpg?20170808 750w";
String requiredSrcSet = srcset.replace("/tesla_", "http://tesla_");

Is this the correct xml regex for URL?

I am implementing urlReWriter into my Java web project.
I want to change this url: /read-post.jsp?id=1&title=some-cool-blog-title
into this shortened/cleaner url: /read-post/1/some-cool-blog-title
This is the rule I have implemented:
<rule>
<from>^/read-post/([0-9]+)/([0-9][a-z][A-Z]+)</from>
<to>/read-post.jsp?id=$1&title=$2</to>
</rule>
The problem is it isn't re writing the url and I suspect it is because the xml regex I've used is incorrect?
How do i format it correctly when there can be any number for the id and any number, character or special character - for the title?
Your regular expression for the title ([0-9][a-z][A-Z]+) is for sure not correct since the + refers to the [A-Z] only. In addition to this the - your are mentioning in the question is missing. You could try this instead: ([0-9a-zA-Z\-]+)

Categories