I create custom proxy service in WSO2 ESB 490:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="write_2_greg"
transports="https,http"
statistics="disable"
trace="enable"
startOnLoad="true">
<target>
<inSequence>
<payloadFactory media-type="json">
<format>
{"name":"2rest_test","context":"/ressttest2","type":"restservice","version":"1.0.0"}
</format>
<args/>
</payloadFactory>
<property name="DISABLE_CHUNKING"
value="true"
scope="axis2"
type="STRING"/>
<property name="Accept"
expression="$trp:Accept"
scope="default"
type="STRING"/>
<property name="messageType"
value="application/json"
scope="axis2"
type="STRING"/>
<property name="Authorization"
expression="fn:concat('Basic ',base64Encode('admin:admin'))"
scope="transport"
type="STRING"/>
<call>
<endpoint>
<http trace="enable"
method="POST"
uri-template="https://localhost:9443/governance/restservices"/>
</endpoint>
</call>
<property xmlns:ns="http://org.apache.synapse/xsd"
name="__Status"
expression="$axis2:HTTP_SC"
scope="default"
type="STRING"/>
<enrich>
<source type="body" clone="true"/>
<target type="property" property="res_body"/>
</enrich>
<log level="custom">
<property name="__Status" expression="$ctx:__Status"/>
<property name="res_body--" expression="get-property('res_body')"/>
</log>
</inSequence>
<outSequence/>
<faultSequence/>
</target>
<description/>
</proxy>
This simple proxy just create new restservice to GREG, it uses the GREG REST API. But when I run this proxy service, the GREG response 500 status code, and check GREG log, it seems Jackson error:
Caused by: org.codehaus.jackson.map.JsonMappingException: Can not construct instance of org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact, problem: abstract types can only be instantiated with additional type information
at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1#4d4489c7; line: 1, column: 1]
at org.codehaus.jackson.map.JsonMappingException.from(JsonMappingException.java:163)
at org.codehaus.jackson.map.deser.StdDeserializationContext.instantiationException(StdDeserializationContext.java:212)
at org.codehaus.jackson.map.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:97)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2376)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1166)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:410)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBodyReader(JAXRSUtils.java:1262)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.readFromMessageBody(JAXRSUtils.java:1209)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameter(JAXRSUtils.java:757)
at org.apache.cxf.jaxrs.utils.JAXRSUtils.processParameters(JAXRSUtils.java:716)
at org.apache.cxf.jaxrs.interceptor.JAXRSInInterceptor.processRequest(JAXRSInInterceptor.java:253)
... 40 more
But I can create new restservice used by "Advanced Rest Client Application"(Chrome plugin)
BTW, I test this by ESB 490 , GREG 510 and GREG 520 .
How can I achive this used by ESB?
After research the carbon-governace source code, I found that the method "isReadable" in class "org.wso2.carbon.governance.rest.api.internal.GenericArtifactMessageBodyReader"
#Override
public boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
if (GenericArtifact.class.getName().equals(type.getName()) || GovernanceArtifact.class.getName().
equals(type.getName())) {
if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType) || MediaType.APPLICATION_XML_TYPE.equals(mediaType)) {
return true;
}
}
return false;
}
When post the json to request, this method is called to determine the MediaType. The MediaType accept
application/json
But the proxy I wrote it sent the content-type
application/json; charset=UTF-8
They aren't the same, so the method return false, and not process the json post.
I try to reset the ESB proxy content type like this:
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<property name="ContentType" scope="default" type="STRING" value="application/json"/>
But the content-type still "application/json;charset=UTF-8"
I think this is the reson, but how can we fix it ?
Related
I'm using wso2 integration studio (version 7.2). I'm trying to post a JSON payload factory to an endpoint. This JSON message has a JSON array with a square bracket but wso2 keeps removing that before sending it out. The receiver API doesn't accept it in this format.
See my code below:
<api context="/test" name="testAPi" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<property name="setCharacterEncoding" value="false" scope="axis2"/>
<payloadFactory media-type="json">
<format>{"field1": 5,"field2": 2022,"field3": "22","contacts": [{"id":1014,"surname": "N"}],"applicants": [{"nameId": 111111}]}
</format>
<args/>
</payloadFactory>
<header name="Content-Type" scope="transport" value="application/json"/>
<property name="contentType" scope="axis2" type="STRING" value="application/json"/>
<log level="full"/>
<header name="Accept" scope="transport" value="*/*"/>
<property name="messageType" scope="axis2" type="STRING" value="application/json"/>
<header name="Connection" scope="transport" value="keep-alive"/>
<header name="Accept-Encoding" scope="transport" value="gzip, deflate, br"/>
<property name="DISABLE_CHUNKING" scope="axis2" type="STRING" value="true"/>
<log level="full"/>
<call blocking="true">
<endpoint key="tcpmon"/>
</call>
<log level="full"/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
I used the tcpmon to see what is sending out. and I can see wso2 just remove the [] from the message and send it out.
Do you know how can I fix this?
I'm currently trying to write a java mediator in WSO2 API manager to perform some processing before sending the message to the proxy.
The normal use cases work fine, but I'm having some trouble with exceptions.
I would like to be able to send a message back to the user from the mediator, with a message and a HTTP status code, but I can't see a way to do it.
import org.apache.synapse.mediators.transform.PayloadFactoryMediator;
public class MessageMediator extends PayloadFactoryMediator
{
#Override
public boolean mediate(org.apache.synapse.MessageContext synapseMessageContext)
{
boolean success = true;
try{
.... some processing
}
catch(Exception e)
{
success = false;
handleException(e.getMessage(), e, synapseMessageContext);
//write message back to user
}
return success;
}
}
This is my proxy:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="AMQPProxy"
transports="https http"
startOnLoad="true"
trace="enable">
<description/>
<target>
<endpoint>
<default />
</endpoint>
<inSequence>
<sequence key="MessageMediator"/>
</inSequence>
<outSequence>
<send/>
</outSequence>
</target>
</proxy>
What is the correct procedure to return an error to the user?
Thanks
If you are returning the error from the insequence itself without going out of wso2 esb calling another service you can do it as :
<payloadFactory media-type="json">
<format>{"Error":{"errorType":"BusinessError","details":"some details"}}</format>
<args/>
</payloadFactory>
<header name="To" scope="default" action="remove"/>
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<property name="HTTP_SC" value="400" scope="axis2" type="STRING"/>
<property name="messageType" expression="$trp:Accept" scope="axis2" type="STRING"/>
<send/>
You can do similarly with media-type as xml see payload factory mediator
If you are going to return error from outsequence you can use makefault see here Fault mediator you can try using fault mediator in insequence as well i haven't tried yet but when sending in insequence you need to use
<header name="To" scope="default" action="remove"/>
in my case, if the username is empty(you can use any http code(in my sample 406)):
<filter source="json-eval($.loginStatic.request.username)" regex="^null|$">
<then>
<property name="HTTP_SC" value="406" scope="axis2"/>
<property name="messageType" value="application/json" scope="axis2" type="STRING"/>
<payloadFactory media-type="json">
<format>{ "status": "ERROR!"}</format>
<args/>
</payloadFactory>
<send/>
</then>
</filter>
I am developing a proxy service with a soap webservice that performs soap to rest conversion, the message is sent to a servlet that response with a string in flat format (not xml), just a secuence of characters like
OIUW|ECHNOWE|RFHQWIUE|FBPQW|EFHAO|IEFH
I am invoking with SOAP UI and I get this response fine, now I would like to receive it in "SOAP format", wrapping the message into a soap:body, I've tried with a XSLT and with a PayloadFactory Mediator, but as soon as I use any of them (even doing nothing) I get a
[2014-07-31 09:30:41,847] ERROR - RelayUtils Error while building Passthrough stream
org.apache.axiom.om.OMException: javax.xml.stream.XMLStreamException: ParseError at [row,col]:[1,1]
Message: Content is not allowed in prolog.
What do I do wrong ? How can I achieve a message transformation without this exception?
Thank you!
UPDATE: My proxy as requested by Ratha
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="SCL3"
transports="http"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<log level="custom">
<property name="MyTrace" value="--- REQUEST ---"/>
</log>
<log level="full"/>
<property name="REST_URL_POSTFIX"
value="x4?msg=x4|0003|0000000021|0|0|0400002081020224849"
scope="axis2"
type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<property name="SOAPAction" scope="default" action="remove"/>
<header name="Action" scope="default" action="remove"/>
<send>
<endpoint>
<address uri="http://localhost:8087/X4" format="pox"/>
</endpoint>
</send>
</inSequence>
<outSequence>
<log level="custom">
<property name="MyTrace" value="--- RESPONSE ---"/>
</log>
<property name="ContentType"
value="application/soap+xml"
scope="transport"
type="STRING"/>
<property name="messageType"
value="application/soap+xml"
scope="transport"
type="STRING"/>
<payloadFactory media-type="xml">
<format>
<a xmlns="">$1</a>
</format>
<args>
<arg value="my value"/>
</args>
</payloadFactory>
<send/>
</outSequence>
</target>
<publishWSDL uri="file:/C:/wso2/wso2esb-4.8.1/repository/workspaces/myproject/SCL3.wsdl"/>
</proxy>
I've seen that my servlet was setting content type to "text/xml" instead of "text/plain", I've changed it to "text/plain" and everything is working fine now.
Therefore I deduce that the error message
"Content is not allowed in prolog"
actually means
"Unexpected content type"
When you have following outsequence configuration what your log prints?
<outSequence>
<log level="full">
<property name="MyTrace" value="--- RESPONSE ---"/>
</log>
<send/>
</outSequence>
My aim is i have to receive the url's like www.google.com from client in proxy service and send response back to client.
How can i solve this.Here i am sending my proxy service.
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="Geturl"
transports="https http"
startOnLoad="true"
trace="enable"
statistics="enable">
<description/>
<target>
<inSequence onError="fault">
<property name="RESPONSE" value="true" scope="default" type="STRING"/>
<property name="REQUEST_HOST_HEADER" value="www.google.com" scope="axis2"/>
<property name="querystrings"
expression="get-property('axis2', 'REST_URL_POSTFIX')"/>
<property name="REST_URL_POSTFIX"
expression="get-property('REQUEST_HOST_HEADER')"
scope="axis2"
type="STRING"/>
<property name="HTTP_METHOD" value="GET" scope="axis2" type="STRING"/>
<header name="To"
expression="get-property('www.google.com')"/>
<property name="message" value="Response message"/>
<property name="Sender Address" expression="get-property('www.google.com')"/>
<log level="full"/>
<log level="full">
<property name="REQUEST_HOST_HEADER" value="www.wso2.org"/>
</log>
<send/>
</inSequence>
<outSequence onError="fault">
<log level="full"/>
<log level="full">
<property name="REQUEST_HOST_HEADER" value="www.google.com"/>
</log>
<log level="full">
<property name="success" value="success"/>
</log>
<send/>
</outSequence>
</target>
</proxy>
Thanks
In the outSequence of a proxy service, when you call <send/> , the response will be sent back to the client. Try sample proxy service [1], and invoke using Try-It or SOAP UI. You will see that the response comming to the client.
[1]http://docs.wso2.org/wiki/display/ESB460/Sample+150%3A+Introduction+to+Proxy+Services
i am trying to send a message to my email for that i am using send mediator as well i setup required configurations in AXIS2 file
is it work for below proxy
if not what is the way to give xpath to send mediator
<proxy xmlns="http://ws.apache.org/ns/synapse" name="mailCheck" transports="http" statistics="disable" trace="disable" startOnLoad="true">
<target>
<inSequence>
<property name="Subject" value="Alert Message From WSO2 ESB - Service Down !!!" scope="transport" type="STRING"/>
<property name="messageType" value="text/html" scope="axis2" type="STRING"/>
<property name="ContentType" value="text/html" scope="axis2" type="STRING"/>
<property name="Mail" value="mailto:faisal.shaik#youtility.in" scope="default" type="STRING"/>
<log level="full">
<property name="Mail" value="mailto:faisal.shaik#youtility.in"/>
</log>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<send>
<endpoint key-expression="get-property('Mail')"/>
</send>
</inSequence>
<outSequence/>
</target>
<description></description>
</proxy>
if need any changes pls let me know
if you want to get the email address from a property value then use the header mediator to set value of "To" to "mailto:faisal.shaik#youtility.in".
You can do that adding the following before the send mediator
<header name="To" expression="fn:concat('mailto:', get-property('Mail'))"/>
You can use this:
<property name="To" expression="get-property('uri.var.to')" scope="transport"/>
<send>
<endpoint>
<address uri="mailto:"/>
</endpoint>
</send>
<send>
<address uri="mailto:xxx#yyy"/>
</send>
key-expression also can be used..