Can two #Path point to the same method in WS(rest) - web-services

I have two paths #Path("{gpid}") and #Path("{gpid}/vip") I want both to be pointing to the same method but within the method they will be using two different daos, is this possible? Am using web-services using jersey

Yes you can include part of the path as a parameter and then use it inside the method. In Java, this is how you do it:
#Path("/{gpid}{isvip : (/vip)?}")
public Response myMethod(#PathParam("gpid") String gpid,
#PathParam("isvip") String isvip)
Note the use of the regular expression. In the body, check whether the value of isvip is null/empty or the string "vip".

Related

Django| generate random url based on URl or PATH from urlconfig

I have a question regarding generating example urls according the Django url and path.
For example if I have 2 urls like:
path('example/<int:example_id>/', views.some_view, name='example')
url(r'^example/(?P<example_id>[0-9]+)/$', views.some_view, name='example')
Is it possible to generate somehow by Django built-in means example full-urls for tests like:
example/234/
example/93/
example/228/
etc…
randomly.
For different urls based on concrete name + regex or converter parameters?
In other words where in source code Django understands that <int:example_id> is an integer and so on. If I have something like : 1) I expect example/ 2) I expect int - I would be able to use it to generate random example url.
Hopefully this is clear...
These urls are just an example. Real urls will differ.
Thank you.
In other words where in source code Django understands that <int:example_id> is an integer and so on.
This is in essence looking for <…:…> patterns in the path. Django has furthermore a set of path converters. The standard ones are int, str, slug, uuid, path, but you can define your own path converters. Indeed, the Django documentation has a section registering custom path converters that explains how you can design your own path converters.
What a path basically does is constructing a regex. It thus searches for patterns like <int:example_id>. Since you here used int, the IntConverter [GitHUb] is used:
class IntConverter:
regex = '[0-9]+'
def to_python(self, value):
return int(value)
def to_url(self, value):
return str(value)
Here it will thus replace the <int:example_id> by (?<example_id>[0-9]+), where the regex part thus will be used.
A path(…) does not only constructs a regex. It also automatically maps the captured items to a Python object by calling .to_python(…). This thus means that if you work with re_path(r'^example/(?P<example_id>[0-9]+)/$', …), then example_id will be a string, but for path('example/<int:example_id>/', …), it will automatically call int(…) on the captured string, and thus pass an int object. Often that does not make much difference.
This also holds when you serialize an object: you can pass a value, and it will call .to_url(…) on the object to convert it to a string representation for the generated URL. This thus means that you can write custom path converters that thus perform more sophisticated conversions.
I expect example/ 2) I expect int - I would be able to use it to generate random example url.
Well eventually it boils down to a regular expression. A regular expression can be converted into a finite state machine, so we can generate random valid strings. For example exrex [GitHub] is capable to generate all valid strings (this is often an generator that will keep proposing new strings), or random strings that match the regular expression. It is however not said that because it matches the path(…), that it will be sensical for the view. If you for example use this int as the primary key of a Post object it is fetching, then for random URLs, it is likely that you will generate URLs for non-existing Posts.

Regex to differentiate APIs

I need to create a regex to help determine the number the number of times an API is called. We have multiple APIs and this API is of the following format:
/foo/bar/{barId}/id/{id}
The above endpoint also supports query parameters so the following requests would be valid:
/foo/bar/{barId}/id/{id}?start=0&limit=10
The following requests are also valid:
/foo/bar/{barId}/id/{id}/
/foo/bar/{barId}/id/{id}
We also have the following endpoints:
/foo/bar/{barId}/id/type/
/foo/bar/{barId}/id/name/
/foo/bar/{barId}/id/{id}/price
My current regex to extract calls made only to /foo/bar/{barId}/id/{id} looks something like this:
\/foo\/bar\/(.+)\/id\/(?!type|name)(.+)
But the above regex also includes calls made to /foo/bar/{barId}/id/{id}/price endpoint.
I can check if the string after {id}/ isn't price and exclude calls made to price but it isn't a long term solution since if we add another endpoint we may need to update the regex.
Is there a way to filter calls made only to:
/foo/bar/{barId}/id/{id}
/foo/bar/{barId}/id/{id}/
/foo/bar/{barId}/id/{id}?start=0&limit=10
Such that /foo/bar/{barId}/id/{id}/price isn't also pulled in?
\/foo\/bar\/(.+)\/id\/(?!type|name)(.+)
There is something in your RegEx which is the cause to your problem. "(.+)" RegEx code matches every character after it. So replace it with "[^/]" and add the following code "/?(?!.+)". This is working for me.
/foo/bar/([^/]+)/id/(?!type|name)([^/]+)/?(?!.+)

Remove substring from string in D

How to remove all occurrences of a string in another? I can do this using the following code:
std.array.replace: "the string".replace("the", "")
But I wonder if there is a dedicated function for this in phobos?
Yes. It's correct function. But you might want to use it from std.string. Because if in future version something changes you'll still be using correct function.
From documentation of std.string:
The following functions are publicly imported:
std.array: replace replaceInPlace ...

Getting Number of Occurrences of String in JMeter

I am analyzing a website that returns text (JSON array), which I'm using HTTP Request element for. What I'm trying to do is check the number of times a string appears in the response, for example a field called "itemname". So I have added a Regular Expression Extractor, put ItemNameVar as the Reference Name, ^itemname$ as the Regular Expression, $1$ for the Template, -1 for Match No, and "NOT FOUND" for Default Value. I've also added an If Controller, which says "${ItemNameVar_matchNr}" == "1", because I expect it to occur only one time. However, it never fails if I set it to a different number. What am I doing wrong here? Thank you.
It looks like to be an issue with your regular expression.
I would suggest using Beanshell Post Processor instead of Regular Expression Extractor as JSON structures aren't very handy to parse with regexes.
Reference Beanshell code will look as follows:
import org.apache.commons.lang3.StringUtils;
int matches = StringUtils.countMatches(new String(data), "itemname");
vars.put("ItemNameVar_matchNr", String.valueOf(matches));
Explanation:
First line - import necessary helper class
Second line - data is a short-hand for byte-array representation of Sampler response. Method countMatches counts number of occurrences of itemname criteria in string representation of response data. Result is saved as matches integer variable
Third line - value of matches is saved as JMeter Variable called ItemNameVar_matchNr
See How to use BeanShell: JMeter's favorite built-in component guide for more detailed explanation of Beanshell scripting in JMeter and small Beanshell cookbook containing JMeter API usage examples.
The approach you are following is absolutely correct.
Please check your Jmeter script. Make sure the next request you are trying to execute after
IF Controller is branch or within Controller
Jmeter IF Controller
Hope this will help.

use of String.matches(regex) in smartgwt

I am working with smartgwt since three months.
I have encountered a problem with the specific method of the string class.
It seems that the matches never works, even with the simplest one:
String regex = "CEDD";
String input = "CEDD";
input.matches(regex);
this will always returns false. Such piece of code is within a class extending the smartgwt Layout class, and therefore got converted to js and used in front end.
The same fragment obviously works when used in a simple java main standalone class.
Could you point out what to investigate to solve this problem ?
thanks
It looks like GWT doesn't support normal Java regular expressions (i.e. doesn't support Pattern, Matcher and classes/methods that use it.
There is a RegExp class that provides those features, however.