My project (we have Spring 3) needs to rewrite URLs from the form
localhost:8888/testing/test.htm?param1=val1¶mN=valN
to
localhost:8888/nottestinganymore/test.htm?param1=val1¶mN=valN
My current rule looks like:
<from>^/testing/(.*/)?([a-z0-9]*.htm.*)$</from>
<to type="passthrough">/nottestinganymore/$2</to>
But my query parameters are being doubled, so I am getting param1=val1,val1 and paramN=valN,valN...please help! This stuff is a huge pain.
To edit/add, we have use-query-string=true on the project and I doubt I can change that.
The regular expression needs some tweaking. Tuckey uses the java regular expression engine unless specified otherwise. Hence the best way to deal with this is to write a small test case that will confirm if your regular expression is correct. For e.g. a slightly tweaked example of your regular expression with a test case is below.
#Test public void testRegularExpression()
{
String regexp = "/testing/(.*)([a-z0-9]*.htm.*)$";
String url = "localhost:8888/testing/test.htm?param1=val1¶mN=valN";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(url);
if (matcher.find())
{
System.out.println("$1 : " + matcher.group(1) );
System.out.println("$2 : " + matcher.group(2) );
}
}
The above will print the output as follows :
$1 : test
$2 : .htm?param1=val1¶mN=valN
You can modify the expression now to see what "groups" you want to extract from URL and then form the target URL.
Related
I have created an endpoint that accepts a string in its request param
#GetMapping(value = "/validate")
private void validateExpression(#RequestParam(value = "expression") String expression) {
System.out.println(expression);
// code to validate the input string
}
While sending the request from postman as
https://localhost:8443/validate?expression=Y07607=Curr_month:Y07606/Curr_month:Y07608
// lets say this is a valid input
console displays as
Y07607=Curr_month:Y07606/Curr_month:Y07608 Valid
But when i send
https://localhost:8443/validate?expression=Y07607=Curr_month:Y07606+Curr_month:Y07608
//which is also an valid input
console displays as
Y07607=Curr_month:Y07606 Curr_month:Y07608 Invalid
I am not understanding why "+" is not accepted as parameter.
"+" just vanishes till it reaches the api! Why?
I suggest to add this regular expression to your code to handle '+' char :
#GetMapping(value = "/validate")
private void validateExpression(#RequestParam(value = "expression:.+") String expression) {
System.out.println(expression);
// code to validate the input string
}
I didn't find any solution but the reason is because + is a special character in a URL escape for spaces. Thats why it is replacing + with a " " i.e. a space.
So apparently I have to encode it from my front-end
Its wise to encode special characters in a URL. Characters like \ or :, etc.
For + the format or value is %2. You can read more about URL encoding here. This is actually the preferred method because these special characters can sometimes cause unintended events to occur, like / or = which can mean something else in the URL.
And you need not worry about manually decoding it in the backend or server because it is automatically decoded, in most cases and frameworks. In your case, I assume you are using Spring Boot, so you don't need to worry about decoding.
I am trying to match the sentence against rules.
I am able to compile multiple rules and match it against CoreLabel using the following method :
TokenSequencePattern pattern1 = TokenSequencePattern.compile("([{tag:/NN.*//*}])");
TokenSequencePattern pattern2 = TokenSequencePattern.compile("([{tag:/NN.*//*}])");
List<TokenSequencePattern> tokenSequencePatterns = new ArrayList<>();
tokenSequencePatterns.add(pattern1);
tokenSequencePatterns.add(pattern2);
MultiPatternMatcher multiMatcher = TokenSequencePattern.getMultiPatternMatcher(tokenSequencePatterns);
List<SequenceMatchResult<CoreMap>> matched=multiMatcher.findNonOverlapping(tokens);
I have many rules inside a file. Is there any way to load the rule file?
I have seen a method to load the rules from file using the following method:
CoreMapExpressionExtractor extractor = CoreMapExpressionExtractor.createExtractorFromFiles(TokenSequencePattern.getNewEnv(), "en.rules");
List<MatchedExpression> matched = extractor.extractExpressions((CoreMap)sentence);
But it accepts CoreMap as its argument. But I need to match it against CoreLabel
Please see this comprehensive write up on TokensRegex:
https://stanfordnlp.github.io/CoreNLP/tokensregex.html
Is it possible to parse an expression like the one shown below, where I call a method and would like to have text after the result of that method call?
String expression = "obj.someMethod()'test'";
ExpressionParser parser = new SpelExpressionParser();
Expression expression = parser.parseExpression(expression);
When I run code like the one below I get the following error:
org.springframework.expression.spel.SpelParseException: EL1041E:(pos 23): After parsing a valid expression, there is still more data in the expression: 'test'
If I remove the 'test' string, then it parsers and evaluates correctly.
Even if it is Expression language it is based on the core programming language and follows with its rules.
So, if you use + operator to concat method result with the string in Java, you should do the same in SpEL:
"obj.someMethod() + 'test'"
is correct answer for you.
As well as you can use :
"obj.someMethod().concat('test')"
if someMethod() returns String, of course.
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.
I'm using URL rewrite module, but i do not know how to apply a pattern for my requirement, please help
My URLs are :
mychick.com/Cat/Prod/chicken-skin-clean.html
&
mychick.com/Cat/Prod/chicken-head-crown.html
I need this URL should be rewritten to
mychick.com/Cat/Prod/chicken-skin-clean
mychick.com/Cat/Prod/chicken-head-crown
I need a single pattern to rewrite these two URLS
if you looking doing it in java , you can split it by ".html"
public static void main(String[] args) {
String s="mychick.com/Cat/Prod/chicken-skin-clean.html";
System.out.println(s);
System.out.println(s.split(".html")[0]);
}
on output you have
"mychick.com/Cat/Prod/chicken-skin-clean.html"
"mychick.com/Cat/Prod/chicken-skin-clean"
bet, it can be done better and its only works to cut .html, but you asked for it.
problem will by if in url is syntax to cut, but if so, you can join tab elements from 0 to lenght-1 and puting back ".html" between strings