struts2 request encoding - utf-8

I am sending a XML in HTTP POST body.
Question:
Does struts2 support processing request in utf-8 encoding?
Reference:
http://www.experts-exchange.com/Programming/Languages/Java/Q_24061148.html
(Around bottom of the page)

Yes you shouldn't have any problem, just define the enconding in your action when sending the stream to the view, in case you are using the stream result type.
STRUTS.XML
<result name="xml" type="stream">
<param name="inputName">inputStream</param>
</result>
ACTION
setInputStream(new ByteArrayInputStream(xmlObject.toString().getBytes("UTF-8")));
return "xml";

Related

Spring integration setting content type as 'text/xml' for int:http inbound gateway

Our client is sending a http post request at our int:http inbound gateway which has the following signature:-
<int:channel id="http-Input-channel"/>
<int:channel id="http-Output-channnel"/>
<int-http:inbound-gateway id="HttpInboundGateway"
supported-methods="POST,GET"
request-payload-type="java.lang.String"
request-channel="http-Input-channel"
reply-channel="http-Output-channnel"
path="/Response.do"
mapped-request-headers="*"/>
<int:service-activator input-channel="http-Input-channel" ref="ResponseHandler" output-channel="http-Output-channnel" method="handleMessage"/>
</beans>
thus,i am receiving the message via the int:inbound gateway in the service activator's(which is the ResponseHandler bean) method handleMessage() as follows:
public Message handleMessage(Message message) {}
the message contains the results and having the base64 string of the pdf.
when i am debugging i found that the message comes as in the encoded form and content type as 'content-type => application/x-www-form-urlencoded'.
In order to get the actual payload i have to decode that which causes the '+' in the format sent by the client to replace with the space in the whole XML.
if i replace all the spaces with the '+' sign again all works well.i can see the correct format for the whole base64 string of pdf and it opens.
However this is not the actual fix.as client is not sending us the content-type => application/x-www-form-urlencoded in the headers. they are sending that as the content-type => 'text/xml'.
my question is why the int:http inbound gateway changes the content type and encoded the result XML.can we change content type to 'text/xml' of the int http inbound gateway in its configuration before it actually hits the service activator and start processing the XML.
i tired using the message converters but it just duplicates the content type and give preference the application/x-www-form-urlencoded.
can you please suggest what should be used along with the int:http inbound gateway in order to get the result XML having the text/xml as the content type and result XML not in the encoded form.i think changing the content type will solve the problem.but how??
Thanks
dev

Struts2 interceptor for ajax requests

Application has two packages one for jsp (struts.xml) and another for json results (struts-json.xml) . Actions in both packages share the same interceptors. When interceptor interrupts and returns error then for ajax request I want to return serialized json object back. Object has error code and description.
To achieve that I redirected to another method :
<result name="errorFromInterceptor" type="redirectAction">
Is there a better way to populate and return object when request is captured by interceptor?

Mule ESB: How to use xpath expression on session variable?

I have a requirement to extract some information form session variables. The session variables Type is String and contains XML. i tried XPATH expression like -
xpath('//tns:CreateUpdateRQ/tns:RequestInfo/sns:requestSourceID',sessionVars.originalMessage)
however I get exception stating "Could not find transformer NULL Payload to Document". I tried MEL like - #[sessionVars.originalMessage.CreateUpdateRQ.RequestInfo.requestID]. But it's not working either.
How can I extract information from session variable of Type string holding XML.
P.S. I can set the payload to sesion variable & use xpath but it's not suitable for my case because I need to extract the information from session variable 7 pass it as argument to datamapper.
Appreciate you help on this.
To facilitate reading the xml file, I recommend transforming String (Object) to xml, for example:
<xml:object-to-xml-transformer />
then
<logger level="WARN" message="#[xpath://soapenv:Envelope/soapenv:Body//...]" />
In short, make sure the payload is XML type, you can by setting it
<set-property propertyName="Content-Type" value="text/xml" />

Struts 2 Clarification needed

I wonder how struts 2 validation is performed without specifying Validate = true in struts config xml. Can you anyone tell me the flow of Struts 2 validation using validation framework.
Validation happens through a combination of the "validation" and "workflow" interceptors. There is no "validate" setup in the Struts 2 config file, because it's unnecessary.
Struts core has the validation framework that assists the application to run the rules to perform validation before the action method is executed.
Actions class works as a domain data and it looks for the properties in its Action Mapping File and it searches the field validators in theFileName-Validation.xml and all validators work as per the field defined in validation.xml. If there is any mismatching of data, it picks the message from the validation.xml and displays it to user.
Sample Employee-validation.xml :
<validators>
<field name="name">
<field-validator type="required">
<message>
The name is required.
</message>
</field-validator>
</field>
<field name="age">
<field-validator type="int">
<param name="min">29</param>
<param name="max">64</param>
<message>
Age must be in between 28 and 65
</message>
</field-validator>
</field>
</validators>
This is the sample validation file for Employee model and request will be validated for properties name and age. If name field left empty validation will gives the error message as "The name is required" above the name input box And if the age entered is outside the limit of 29-64 validation will show an error as "Age must be in between 28 and 65" above the age input box.

problem in multiple validation.xml for single action in struts2

I am working on struts2 application.
I have a jsp page having 3 textfields. And I am validating each field through action-validation.xml file. Now I want if validation get fail at first field it should not check the other two fileds and result directly go to jsp page (say a.jsp) showing error message for that single filed only. And if validation does not fail at first field then it should check rest of the fileds, i.e second and third fileld and now if validation fails here then also result directly go to jsp page but different one (say b.jsp) showing error message.
Is it possible? If yes then please make me aware with this.
I have tried this but action-validation.xml is validating all fields in single shot and error messages for all fields is showing in a jsp page that I have written under a.jsp
Thanks in advance.
You can add a short-circuit in your field-validator
e.g.
<!-- Field Validators for email field -->
<field name="email">
<field-validator type="required" short-circuit="true">
<message>You must enter a value for email.</message>
</field-validator>
<field-validator type="email" short-circuit="true">
<message>Not a valid e-mail.</message>
</field-validator>
</field>
<!-- Field Validators for email2 field -->
<field name="email2">
<field-validator type="required">
<message>You must enter a value for email2.</message>
</field-validator>
<field-validator type="email">
<message>Not a valid e-mail2.</message>
</field-validator>
</field>
if email is empty or not valid, email2 will not be validated
http://struts.apache.org/2.x/docs/validation.html
"Failure of a particular validator marked as short-circuit will prevent the evaluation of subsequent validators"

Resources