What does .+ mean in Path Variable? - spring

in a Spring Boot REST API Project I came across a Route which was somewhat like this:
#DeleteMapping("api/{variable:.+}")
What does the .+ stand for?

It stands for: one or more characters.
See https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html for reference.

Related

What does 'antMatchers' mean?

I am learning spring-boot and for my brain, in order to accept some things, it needs to find a meaningful explanation of those things. Could anybody tell me what "ant" in "antMatchers" means? What has an insect like an "ant" got to do with the mapping between a resource and the path of the REST-call?
I know that this is not a language research forum, but I think that developers have also the right to understand or refuse logical/illogical things.
Thank you ;)
It comes from Apache Ant Project, which is a java build system that utilises an xml scripting language. Here is the website Apache Ant Home and in Spring Doc for AntPathMatcher it says "Part of this mapping code has been kindly borrowed from Apache Ant." So "antMatchers" means an implementation of Ant-style path patterns in mappings.
The term comes from the archaic build system, Apache Ant. In Ant paths were matched against a simple pattern containing * symbols meaning any string, and ** meaning 'recursive' descending any number of directories/folders. So, an ant-matcher like this: /a/b/*/d/**/z could match: /a/b/w/d/x/y/z because the w bit is matched by * and the /x/y/ bit is matched by **.

camel substring operation- n characters from end of message body

This will probably be a layup for most of you, so I apologize in advance.
I am using Apache camel with spring DSL.
My message body has been converted to a string. I want everything from the 9th to 998th character, preferably using a simple expression. I have tried
<transform>
<simple>${body.substring(8,${body.length}-1)}</simple>
</transform>
but Camel doesn't recognize the subtraction. As such, it will try to convert the string "1045-2" to an integer, and obviously fail. Is there a workaround here?
The following code snippet will work.
<transform>
<simple>${body.substring(8,${body.length()-1})}</simple>
</transform>
Use groovy, javascript etc which is more powerful dynamic programming language
<groovy>request.body.substring(8, request.body.length-1)</groovy>
You need to add camel-groovy as a dependency together with groovy also.
Camel Groovy documentation

Django with Windows: Double backslash or front slash?

So, I'm a beginner trying to learn django right now with "Practical Django Projects" with windows in eclipse pydev.
Anyways, main issue is I use windows and it seems to suggest I should use front slash in the comments for settings.py. But by default, the databases is already set to:
'NAME': 'C:\\Users\\dtc\\Documents\\Eclipse\\cms\\sqlite.db'
And while I was going through the book, it wants me to add this:
url(r'^tiny_mce/(?P<path>.*)$', 'django.views.static.serve',
{ 'document_root': '/path/to/tiny_mce/' }),
But that path didn't work until I changed to double backslash \\path\\to\\...so I'm thinking I should just not worry about using double backslash.
It would be great if someone gave me a little insight on this because it's giving me a total headache while trying to learn django.
Use python to get current directory and call a join on it with whatever you need to add. This will make it cross platform as python will take care of converting backslashes and forward slashes for you.
import os
CURRENT_DIR = os.path.dirname(__file__)
TEMPLATE_DIRS = (
os.path.join(CURRENT_DIR, 'templates')
)
That will save you on typing and the path will be correct. If you look in settings.py that django generates it will tell you to always use / even on Windows.
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
One last thing, your urls should use forward slashes because that is how django is going to use them.
Hope that helps

Freemarker ".vars" names can't contain dashes?

We're using Freemarker version 2.3.16, and I've just tracked down a weird bug in one of our apps. It came down to there now being hyphens in some of our product code strings. The codes are used to pull hashes of localized text from the global scope using .vars.
Reducing the issue brought me to an example that anyone can try:
${.vars["foo-bar"]} in a template outputs 0
${.vars["foo+bar"]} outputs nullnull
${.vars["foobar"]} correctly triggers an InvalidReferenceException
All three should trigger exceptions. Instead, it appears the .vars parameter string is being evaluated! :-(
http://freemarker.sourceforge.net/docs/app_faq.html#faq_strange_variable_name implies this should work.
I saw mention of a similar issue a few weeks ago on the Freemarker mailing list, and it was suggested to prefix the parameter string with "#". That might work with other hashes, but it does NOT work with .vars. I just took a working example (.vars["resources_title"]) and changing it made it throw an InvalidReferenceException (.vars["#resources_title"]). I also tried it on the hyphenated reference, and it also threw the exception.
Upgrading to 2.3.18 did not seem to make a difference.
Sorry for the delay. After some good mailing-list help on places to put breakpoints, here's I wrote back to the list on June 10th:
Short story: It's not a Freemarker issue. Rather the Struts team chose to hard-wire Freemarker to treat .vars names as OGNL expressions, and there seems no way to tell OGNL to not parse them. So under Struts, "-" and "+" (and possibly other characters) cannot appear in .vars names.
Long story...
freemarker.core.BuiltinVariable (line 192) is where Freemarker starts to process .vars expressions
freemarker.core.Environment (line 1088) hands control over to the "rootDataModel" which the Struts team hard-wired to be an instance of org.apache.struts2.views.freemarker.ScopesHashModel
line 70 of that class (using version 2.1.8.1 of Struts) calls "stack.findValue"; "stack" has been wired to be an instance of com.opensymphony.xwork2.ognl.OgnlValueStack
at line 236 this class in turn asks an instance of OgnlUtil to find the object, and that's where the name is assumed to be an OGNL expression and is parsed, turning "foo-bar" into ( foo - bar )
At no point along the way does there seem to be a choice to NOT treat the .vars name as an expression (a comment in FreemarkerResult hints at the possibility, but the code doesn't follow through). In theory I could have my implementation of FreemarkerManager create a variant of ScopesHashModel, but that would take a lot of work to change all the associated classes with it.
(Nor does there seem to be a way to escape "-" characters in OGNL expressions. Seems there was discussion 5-6 years ago to do this, but.... .vars( "foo\\-bar" ) fails on finding "-" after "\", so presumably "-" isn't escapable?)
:-(
I'm not clear what the use-case is for treating .vars names as expressions... but I don't think Struts is going to change, now. Rather than override a half dozen Struts classes, I instead changed the code that loads our ResourceBundles into the value stack: it now changes the names to replace "-" and "_", and likewise my .vars names are changed the same way in the template and... tada. It works. Woo.
Works for me. And like already mentioned on the freemarker-user mailing list: maybe you use a strange data model, or even a fancy ObjectWrapper. But a discussion like this is probably better suited for the freemarker-user mailing list...
It works if it added with escape foo\-bar.
"Only single backslash"
Since freemarker version 2.3.22 is it possible to use dot (.), minus sign (-) or colon (:) in a variable name (details here).
In my case, it fails if I tried to use with freemarker 2.3.21 variables like :
api["x-link"]
If I change freemarker to version 2.3.22 it works.

Creating user/search engine friendly URLs

I want to create a url like www.facebook.com/username just like Facebook does it. Can we use mod_rewrite to do it. Username is name of the user in a table. It is not a sub directory. Please advise.
Sure, mod_rewrite can do that. Here is a tutorial on it.
Yes you can do this but you might have a couple of initial hurdles to get it going correctly.
The first is that you will have to use a regular expression to match it. If you don't know regex then this can be confusing at first.
The second is that you will need to take into account that of you are going to rewrite the top path on the domain you will have to have some mechanism for only rewriting if the file doesn't exist.
I guess if mod_rewrite supports testing if the url points at a real file that will be easy. If not you might have to use a blacklist of words that it wont rewrite as you will need to have some reserved words.
This would include at the least the folder that contains your images, css, js, etc and the index.php your site runs off, plus any other php files you have kicking around.
I would like to be more help but I am a .net guy and I usually help out in asp.net url rewriting issues with libraries such as UrlRewriter.net which have different configurations than mod_rewrite.
To match the username I would use a regex like this:
^/(\w*)/?$
this would then put the bit in the brackets into a variable you can use in the rewrite like
/index.php?profileName={0}
The regex I provided means:
^ nothing before this
/ forward slash
(\w*) any number of letters or numbers
/? optional forward slash
$ nothing after this

Resources