RESTEasy: what is a path parameter? - resteasy

Is a path parameter #PathParam("make") or make that is after the #PathParam or is it make, model, and year meaning any variable that is in the path surrounded by braces:
#Path("/cars/{make}")
#Produces("image/jpeg")
public class CarResource
{
#GET
#Path("/{model}/{year}")
public returnType Fx (#PathParam("make") String make { ... }
}
Thanks in advance.

All of {make}, {model} and {year} can be injected via #PathParam as they are all "arguments supplied via the [URL] path". As pointed out they are all annotated for extraction via #Path and curly braces.
However, the value is only bound to a parameter when (and where) it is consumed. In the given code, only make is a parameter which is bound to the /cars/{make} path; neither {model} or {year} are parameters in context.
While I argue the case that it the final binding that makes it a parameter, there is also an argument for it being a parameter when it is merely part of the path template - that is, the path parsing itself "uses" the value of the URI and supplies a parameter "binding", even if not directly exposed. Consider this wording:
#QueryParam: value - Defines the name of the URI template parameter whose value will be used to initialize the value of the annotated method parameter, class field or property.

Related

Confusing behavior of SimpleEvaluationContext.forReadOnlyDataBinding() in SpEL

I am reading Spring 5.1.3 reference docs, and SpEL Type Conversion gets following sample code:
class Simple {
public List<Boolean> booleanList = new ArrayList<Boolean>();
}
Simple simple = new Simple();
simple.booleanList.add(true);
EvaluationContext context = SimpleEvaluationContext.forReadOnlyDataBinding().build();
// false is passed in here as a string. SpEL and the conversion service
// correctly recognize that it needs to be a Boolean and convert it
parser.parseExpression("booleanList[0]").setValue(context, simple, "false");
// b is false
Boolean b = simple.booleanList.get(0);
It works as the documents mentioned, and changes the value of the property, but according to Javadocs forReadOnlyDataBinding()
Create a {#code SimpleEvaluationContext} for read-only access to public properties via {#link DataBindingPropertyAccessor}.
Shouldn't the SpEL Expression be read-only, and does not change the property value?
The field's contents are mutable but the field itself is immutable.
i.e. you are not allowed to replace booleanList with a new array but there is nothing to prevent the contents of the existing array from being mutated.

When using Ado.Net interfaces such as IDbDataParameter is it correct to still prefix the ParameterName with "#"

In a seperate post, Is it necessary to add a # in front of an SqlParameter name?, a discussion is had about prefixing the ParameterName with the "#" sign.
If you are abstracting all of your ADO access behind interfaces such as IDbCommand and using IDbCommand.CreateParameter() to return instances of IDbDataParameter, is it still correct to prefix the ParameterName with "#".
My gut feeling is no, since the # is required by SqlServer and the point of using interfaces to remove the implementation details.
I'd also suggest this is perhaps why the undocumented feature of automatically checking for the prefix character exists, if you are only using ADO.NET via interfaces and are removed from knowing exactly what kind of database you are using ?
Simply as a demonstration that you need to take this little detail into account even when abstracting, if you look at Microsoft's Data Access Block which for years has provided ADO abstraction using the System.Data.Common base, you'll see that they address this very issue by including virtual method in the abstract base class Database that is then overridden by the provider specific derived classes.
So the base class Database.cs has this method:
/// <summary>Builds a value parameter name for the current database.</summary>
/// <param name="name">The name of the parameter.</param>
/// <returns>A correctly formated parameter name.</returns>
public virtual string BuildParameterName(string name){ return name; }
(if the provider uses positional parameters or has no need of a prefix, there is nothing more to override)
and then the SqlClient specific provider implementation SqlDatabase.cs overrides it as such:
/// <summary>Gets the parameter token used to delimit parameters for the SQL Server database.</summary>
protected char ParameterToken{ get { return '#'; } }
public override string BuildParameterName(string name)
{
if (name == null) throw new ArgumentNullException("name");
if (name[0] != ParameterToken)
return name.Insert(0, new string(ParameterToken, 1));
return name;
}
Notice that this implementation allows the calling code to use sql parameter names with the '#' prefix or not, thus freeing the devs from having to know/remember what the api actually does to the name under the covers.
I don't use the DAAB directly, but their overall approach to abstracting behind the System.Data and System.Data.Common interfaces and classes is a great guideline for small data access api's.

Convert String into an object instance name

I'm trying to turn an string into an instance name.
stage.focus = ["box_"+[i+1]];
this gives me back = box_2;
but I need it to be an object not a string.
In as2 I could use eval. How do I do it in as3?
The correct syntax is:
this["box_"+(i+1)]
For example if you would like to call the function "start" in your main class, you'd do it this way:
this["start"]();
Same thing goes for variables. Since all classes are a subclass of Object you can retrieve their variables like you would with an ordinary object. A class like this:
package{
import flash.display.Sprite;
public class Main extends Sprite{
public var button:Sprite;
public function Main(){
trace(this["button"]);
}
}
}
Would output:
[object Sprite]
If you want to access a member of the current class, the answers already given will work. But if the instance you are looking isn't part of the class, you are out of luck.
For example:
private function foo():void {
var box_2:Sprite;
trace(this["box_"+(i+1)]);
}
Won't work, because box_2 isn't a part of the class. In that case, it is highly recommended to use an array.
If you want to access a DisplayObject (for example, a Sprite or a MovieClip) you also can use getChildByName. But in that case, box_2 will be the name of the object, instead of the name of the variable. You set the name like
var box:Sprite;
box.name = "box_2";
But again, I recommend an array.

iBatis multiple parameter mapper method

Let's say I have a query getUser with two parameters - userName and password. I'd like to have a mapper method looking like this:
public UserBean getUser(String userName, String password);
Is there any way I can achieve something like that? Or maybe I should pass in my mapper method map of parameters (and some parameterMap in my xml mapper)?
public UserBean getUser(Map<String, Object> paramMap);
I'm looking forward for some hints and explanations.
Without any special configuration, you can refer to the first and second parameter as #{1} and #{2}, respectively.
If you would like to name the parameters, rather than referring to them numerically, do the following: In the XML mapping for your SELECT statement, set parameterType="map", and in the interface file, annotate the parameters with #Param. For example, public UserBean getUser( #Param( "user_name" String userName, #Param( "password" ) String password); would allow you to refer to the username and password, in the XML mapping, as #{user_name#} and #{password}, respectively.
You shouldn't change the signature of your DAO method, the only issue to consider is how you build your mapping. iBatis support just one input parameter, and you must decide your class (attribute parameterType) to pack your two original parameters in one.
In this scenario you might (among other options) place the two parameters in a Map (HashMap, typically), or (if the parameters correspond to properties of the UserBean class) pass a dummy UserBean with those two properties set.
In both cases the packing (building the HashMap or the dummy UserBean which hold the two parameters) would be done inside your public UserBean getUser(String userName, String password) method.

What use has RoutedCommand' class constructor ownertype argument?

The constructor of the RoutedCommand has "owner type" as a last argument. What is its significance? When it is used?
MSDN documentation gives completely no clue about why it's needed and whether I could use one type for all commands
Quote from MSDN
ownerType
Type: System.Type The type
which is registering the command.
There is one more thing. What type should I use when creating new routed commands dynamically from array of names. It looks like that any type works, so I'm using UIElement, but if there is a more suited type for this I would like to know.
The source for RoutedCommand shows that the type becomes the OwnerType property. This property is queried ultimately by the following private method when getting InputGestures. So it looks as though this type is being used to lookup a (hard-coded) set of Commands based on the type that created the RoutedCommand.
private InputGestureCollection GetInputGestures()
{
if (this.OwnerType == typeof(ApplicationCommands))
{
return ApplicationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(NavigationCommands))
{
return NavigationCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(MediaCommands))
{
return MediaCommands.LoadDefaultGestureFromResource(this._commandId);
}
if (this.OwnerType == typeof(ComponentCommands))
{
return ComponentCommands.LoadDefaultGestureFromResource(this._commandId);
}
return new InputGestureCollection();
}
I know this is a very old question, but it's the top search hit for "routedcommand ownertype".
Storing an OwnerType and Name within each RoutedCommand object gives you a hint on how to find references to it in code. Suppose you are running the debugger on some method that has an arbitrary ICommandSource parameter. You can examine the Command property, and if you see that OwnerType is CommonCommands and Name is "DoSomething", you can navigate to the DoSomething field of the CommonCommands class, where there might be a useful comment, or search for references to CommonCommands.DoSomething to find associated CommandBindings or something. Without those properties, the RoutedCommand would just be an anonymous object.
I don't know if that reason was what the API designers actually had in mind when they included the argument, but it has been useful to me at least.

Resources