Mule: Http->xml - java

Mule version: 3,5 (Actually, mule studio)
I'm new at using mule and can't seem to figure out how to do the following properly:
Want to achieve:
Send data from a html form to mule which then splits the payload and sends one part to one java program and the other to another.
Html form:
<form method="GET" action="http://localhost:8081">
<input type="text" name="name"/>
<input type="text" name="lastname"/>
<input type="submit" value="Submit" />
</form>
This points to localhost and the port which is set in MuleStudio.
Real Question:
What could I use to transform the data from the inbound http to xml?
Side Question:
I can send the whole payload to one javaprogram. (first a POJO and then to the actual program. - is this the right way or can I send it to the program and skipping the POJO?)

Since your main concern is about transformation, let's only address it.
If you're using the Enterprise Edition, you can use DataMapper to generate the XML you need. Creating a schema representing the target XML would help a lot.
Otherwise, if you're using the Community Edition, you can use different options:
A Groovy component using the excellent MarkupBuilder to generate the target XML right from the inbound message payload,
A chain of standard transformers that first transform the submitted data to a generic XML form (object-to-xml-transformer) then transform it to the desired form (xslt-transformer).
I'm sure there are other options, but these should get you going :)

Related

How to test jmeter's response and pass parameters?

I am researching jmeter and I have a question.
My first question: in case : (github )
input autocapitalize="off" autocorrect="off" autofocus="autofocus" class="form-control input-block" id="login_field" name="login" tabindex="1" type="text"
input class="form-control form-control input-block" id="password" name="password" tabindex="2" type="password"
In case the website don't have field "name", how can I pass the param to website ? can we use css or xpath to pass the param to website ?
My second question:
How to test the response value from the site ? (from the picture the response data not right, still on login page)
Thanks for reading and supporting me to correct this ...
As per HTML Forms article
The Name Attribute
To be submitted correctly, each input field must have a name attribute.
Actually you should't worry about HTML markup, JMeter acts on protocol level and provides record-and-replay functionality. See Apache JMeter Proxy Step by Step for configuration instructions.
You can use Response Assertion to add a check whether response is still login page or not. For example if the user is logged in - he shouldn't see username input any more. See How to Use JMeter Assertions in Three Easy Steps article for more information on conditionally failing JMeter samplers.

How does the Spring forms .jsp tag library work?

So I have a .jsp page which has a form on it, like this (naturally this is a massive simplification):
<form:form commandName="myCommand" method="post">
<form:select path="values" class="select-tall" multiple="multiple" id="mySelect">
<option>first</option>
<option>second</option>
<option>third</option>
</form:select>
<button type="submit" class="button">Save</button>
</form:form>
When the submit button is pressed, the form is submitted(somehow) and the path= attributes are used to bind the data inside the form elements to the properties of an instance of a plain old java object. I understand how this POJO is specified, and I understand how the POST request is routed to the correct controller, but I don't understand where or how the form values are mapped to the given POJO.
What I don't understand is:
How does the spring tag library modify the form such that this binding takes place?
How would one go about doing this in a manual or ad-hoc fashion(using a Javascript onSubmit() method, say)?
Essentially my question is: How does the spring-form.tld tag library work?
Any resources, or even a high-level explanation in your own words, would be extremely helpful.
I've implemented a work-around solution in the mean time (dynamically adding items to a hidden form element), but I feel like this is hack-y and the wrong solution.

Why should I use the form helper and not write my own html forms

I am learning about play 2.0 and I have got a question on the form helpers.
For me it all comes down to what benefit is it actually giving by using it in the templates? Am I using it correctly?
First: using the form helper:
#form(action = routes.Application.addAccount("blank")) {
#inputText(accountForm("id"))
<input type="submit" name="action" value="submit ID"/><br />
}
Why is that better then just defining
Enter your id "<input type="text" name="id"/>"
I know I can use the form model to help with validation on the server side. - that's where I see the great benefits of form helper. But where does it help to actually include the form in the Scala template? Can i use the form helper to automatically generate useful things in the html like client side validation, etc?
Cheers
it helps you generate a lot more than just tags. From the documentation:
You feed them with a form field, and they display the corresponding HTML form control, with a populated value, constraints and errors
and
A rendered field does not only consist of an tag, but may also need a and a bunch of other tags used by your CSS framework to decorate the field.
(http://www.playframework.com/documentation/2.2.x/JavaFormHelpers)
So instead of
<label for=...>
... error mesages
<input ...
</label>
you have just one readable line
#inputText(accountForm("id"))
EDIT:
It will also read constraints on your java beans, e.g
#Constraints.Required
#Constraints.MinLength(5)
public String firstName;
and use html5 browser validations and display the coinstraints to the user.
(http://www.playframework.com/documentation/2.2.x/JavaForms)

How to POST a form using Jersey 2.0

I am trying to get some data from a Form with jersey and i though it would be an easy task to accomplish, however I am getting an error when I try to POST something.
Caused by: java.lang.IllegalStateException: The #FormParam is utilized when the content type of the request entity is not application/x-www-form-urlencoded
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.ensureValidRequest(FormParamValueFactoryProvider.java:126)
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.getForm(FormParamValueFactoryProvider.java:111)
at org.glassfish.jersey.server.internal.inject.FormParamValueFactoryProvider$FormParamValueFactory.get(FormParamValueFactoryProvider.java:94)
at org.glassfish.jersey.server.internal.inject.AbstractHttpContextValueFactory.provide(AbstractHttpContextValueFactory.java:65)
at org.glassfish.jersey.server.spi.internal.ParameterValueHelper.getParameterValues(ParameterValueHelper.java:80)
... 36 more
I think this is the relevant part of the stack trace.
Now for the code I am using:
#POST
#Path("/delete")
#Produces("application/json")
public String delete(#FormParam("id")String id){
And I am trying to POST using a test html page like this:
<form action="<path to the server>/delete" method="post">
primary_id: <input type="text" name="primary_id" /><br />
<input type="submit" value="Submit" />
</form>
I've been trying to make it work but no chance. I have tried adding the #Consumes() annotation with multipart-form-data but can't really make it work. I hope someone can give me a hand.
Thank you all for the help. With some code reviewing i found the problem. Even though I don't think anyone else will make this particular mistake I'll post it anyway for future reference.
The problem was that I am not using a standard web server. I've implemented a netty server with Jersey and the problem was in that said implementation. The problem was that i wasn't passing the headers in the HTTP request to Jersey as I should. I was loosing the Content-Type in the middle of the operation, this means that Jersey couldn't identify the message type.
So, for future reference, for anyone having a similar problem while trying to implement a non-standard server using jersey: when you don't pass the media type correctly (there is a method called getMediaType() in the ContentRequest class that is used, among other things, to validate the Content-Type when #FormParam is used) you will get this type of Exception.
Again, thank you all for the help :)
Your code is working fine except you need to change
<input type="text" name="id" />
Since you havent define #Consumes(), by default delete method consumes mediaType="application/x-www-form-urlencoded" .
I have tested your code and it is working perfectly fine here. My suggestion is check your jersey jar files Specially jsersey-server.jar file in your lib folder.
You are naming your input as "primary_id", but you are receiving "id" name in your #FormParm annotation. Change the id and name in your input tag to "id".
Also if you are consuming application/x-www-form-urlencoded, add this attribute to your form tag: enctype="application/x-www-form-urlencoded"

Receive Linked List from client using Java GET

I'm receiving string from client like following -
String time_S = request.getParameter(Message.KEY_TIME);
Now, If I want to receive a linked list data how should I do that? I tried to use getParameterValues but I don't think I could use it properly.
Thanks in advance.
You can't really retrieve a 'linked list' per se over HTTP - it needs to be serialized (transformed from a Java object to a string). There are plenty of ways of doing this, but you might have them send it to you as a set of comma separated values and then parse it into a linked list or java data structure of your choice.
If you have them send if via JSON, there are several libraries that can be used which will change them to Java standard objects. Such as Simple.JSON, it turns JSON Array's into Java List objects, or JSON Object's into Java Map's.
getParameterValues might be the easiest way, provided that the client can provide each element of the list as a repeated querystring parameter.
For example if the client can send this querystring:
color=red&color=white&color=blue
getParameterValues("color") will return {"red", "white", "blue"}
Depends on how you submit the data. For example, if you submitting data from a web page and you are submitting data using the same parameter name, you can use the getParameterValues method.
For example, take the following inputs:
<input type="text" name="time" />
<input type="text" name="time" />
<input type="text" name="time" />
Then you can access the parameters as follows
String[] times = request.getParameterValues("time");
And if you need that linked list, just do the following
LinkedList<String> timeList = new LinkedList<String>(Arrays.asList(times));

Categories