Where to place java handler for securing WSO2 API - java

i'm following this tutorial in wso2 website, but there's no information where to put java Handler to make Authorization.
Is anyone know where to put that ? and how to make it working with API Resource ?

You can put this in handlers tag under the API, please check the tutorial sample code. This handler will apply to all resources.
<api xmlns="http://ws.apache.org/ns/synapse"
name="TestGoogle"
context="/search">
<resource methods="GET">
<inSequence>
<log level="full">
<property name="STATUS" value="***** REQUEST HITS IN SEQUENCE *****"/>
</log>
<send>
<endpoint>
<http method="get" uri-template="https://www.google.lk/search?q=wso2"/>
</endpoint>
</send>
</inSequence>
</resource>
<handlers>
<handler class="org.wso2.handler.SimpleOAuthHandler"/>
</handlers>
</api>

You can add the handler before the closing tag of the API, after saving it, API may not show the handler, but if you check the source view then you can see the handler assigned to the API. (Main -> Source View)

You have to copy the jar containing the handler to [ESB_HOME]/repository/component/lib and restart the server. The handler is added to the API configuration as a child to <api> as
<handlers>
<handler class="org.wso2.rest.BasicAuthHandler"/>
</handlers>

Related

Default route for Apache Camel

I'm using Apache Camel. Using XML DSL, I mean something like
<rests id="rests" xmlns="http://camel.apache.org/schema/spring">
<rest id="rest-custom">
<get uri="my_method" method="">
<description>...</description>
<param name="..." ... />
<route>
<process ref="..." />
<to uri="..." />
</route>
</get>
<post uri="another_method" method="" >
<description>...</description>
<param name="..." .../>
<route>
<process ref="..." />
<to uri="..." />
</route>
</post>
...
So if I want new route, I will just add new <get> or <post> and it works fine.
But now I want to add some DEFAULT method. I mean, something like <get uri="*"> and <post uri="*"> in the bottom of all configuration. So if my url doesn't match any from list - it goes to default one and I can handle it with custom processor (this is the behaviour I want).
For now I don't know, how to do it. Tried to handle 404 responses, but still no success. Looks like solution should be simple, but can't find it yet.
I see only one possible use case for such a default: if you got multiple URLs for the same functionality.
If this is the case and your clients don't want or can't switch to a single URL, you could still use URL rewriting on the incoming request before it reaches your Camel application.
If you want to "catch" all unknown URLs (errors), I guess you should use standard Camel error handling (see Error Handler and Exception clause) because these REST DSL blocks are internally converted to standard Camel routes.
Finally found the solution.
<get uri="/?matchOnUriPrefix=true&bridgeEndpoint=true" method="">
<description>Default GET method</description>
<route>
...
</route>
</get>
Parameters matchOnUriPrefix=true&bridgeEndpoint=true did the trick.

Will this quartz connector I configured pick only one file at a time

This is the mule config file for a service of a class , that basically reads order file and processes it. Sometime the code is confused and it processes two or more file and getting freezed. I want the code to read only one file at a time.
<quartz-connector name = "oneThreadQuartzConnector">
<quartz:factory-property key ="org.quartz.threadpool.threadcount" value="1"/>
</quartz-connector>
<service name="Retail Transfer Request Service">
<inbound>
<file:inbound-endpoint path="#{es.dir.008}" moveToDirectory="#{es.dir.008}/archive/ORD">
<file:filename-wildcard-filter pattern="OR*" />
</file:inbound-endpoint>
</inbound>
<component>
<spring-object bean="retailTransferRequestAction" />
</component>
<default-service-exception-strategy>
<vm:outbound-endpoint path="found.error.queue" />
</default-service-exception-strategy>
</service>
The quartz will pick data one by one if you want to make a synchronous call the best option is to choose processing strategy in the flow and make it synchronous
<flow name="sampleFlow" processingStrategy="synchronous">

Error while running mule server

I have created a flow to compress a file from the Mule end, but I got an error:
java.lang.IllegalStateException: There are at least 2 connectors matching protocol "file", so the connector to use must be specified on the endpoint using the 'connector' property/attribute. Connectors in your configuration that support "file" are: input, output,
This is the flow:
<flow name="GZipCompress" doc:name="GZipCompress">
<file:inbound-endpoint path="C:backuptest" responseTimeout="10000" doc:name="File">
<file:filename-regex-filter pattern="abc.doc" caseSensitive="false" />
</file:inbound-endpoint> <string-to-byte-array-transformer doc:name="String to Byte Array" />
<logger message="Payload size before compression : #[Integer.parseInt(payload.size())/1024] KB" level="INFO" doc:name="Logger" />
<!-- If you send gzip a String then it gets serialized and mess ends up in the gzip file. To avoid this convert to byte[] first -->
<gzip-compress-transformer /> <logger message="Payload size after compression : #[Integer.parseInt(payload.size())/1024] KB" level="INFO" doc:name="Logger" />
<file:outbound-endpoint path="C:backuptestnewfolder" responseTimeout="10000" doc:name="File" /> </flow>
There are at least 2 connectors matching protocol "file", so the connector to use must be specified on the endpoint using the 'connector' property/attribute. Connectors in your configuration that support "file" are: input, output,
This error is stating that you've defined you file global connectors and now mule doesn't know which one to attach your stated file endpoint to. One solution is to define connector-ref property in your file inbound endpoint and refer to input connector you've defined.
<file:inbound-endpoint connector-ref="input" path="C:backuptest" responseTimeout="10000" doc:name="File"/>

Maintaining Mule Message

Similar to this discussion Maintain Payload State during mule flow execution
I would like to know how to maintain an entire Mule Message throughout my flow.
I am trying to call a jersey resource component after I have received information that the user is authorized to call it. In this authorization request the payload and original http request gets altered.
My predicament is that I don't want to call the resource first as that would be inefficient and insecure however I cannot see any other plausible way to do this.
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- sub-flow here which changes the MuleMessage and loses all original inbound properties -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Thanks for any help in advance
Mule 3 includes two scopes that will allow you to preserve the original message, while executing other message processors:
If you must wait for the sub-flow to complete because you need to use information produced by the sub-flow, use a message enricher like so. This example assigns the variable "authorized" to whatever the payload is once the sub-flow finishes processing the message.
<enricher target="#[variable:authorized]">
<flow-ref name="checkAuthorization" />
</enricher>
If you do not need to wait for the sub-flow to complete before allowing your Jersey Resource to run, use the async scope like so:
<async>
<flow-ref name="goForthAndDoSomething" />
</async>
There are multiple ways to save a mule message and it's properties so that it could be retrieve when required .. In your case you could save the entire Mule message in a variable and retrieve it when required for example :
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You save your entire message in a session variable named entireMessage before calling a subflow-->
<set-session-variable variableName="entireMessage" value="#[message.payload]" />
<!-- You can now call your Sub flow -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Here in the above flow you store your Mule message in a session variable named entireMessage .. You can easily retrieve the value of this session variable whenever you need in any flow anywhere like the following :-
<logger level="INFO" message="#[sessionVars['entireMessage']]"/>
This will print you Mule message
There is also an alternative way to store the http headers before it get altered ..
you can also use the following :-
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You copy all the HTTP headers before calling a subflow-->
<copy-properties propertyName="http.*" doc:name="Copy All HTTP Headers"/>
<!-- You can now call your Sub flow -->
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
The above flow will copy all the HTTP headers before calling any other flow
UPDATED FLOW :-
If you need the original unaltered payload for your Jersy component , please overwrite the current payload with the original payload stored in session variable using set payload component
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<!-- You save your entire message in a session variable named entireMessage before calling a subflow-->
<set-session-variable variableName="entireMessage" value="#[message.payload]" />
<!-- You can now call your Sub flow -->
<!-- overwrite current payload with original unaltered payload -->
<set-payload value="#[sessionVars['entireMessage']]" doc:name="Set Payload"/>
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
Try somthing like this to store the original message in session:
<message-properties-transformer scope="session" doc:name="Save original message">
<add-message-property key="originalMessage" value="#[message:payload]"></add-message-property>
</message-properties-transformer>
Then try something like this to get it from session:
<expression-transformer evaluator="..." expression="SESSION:originalMessage" doc:name="Restore original message"></expression-transformer>
Thanks for all your help. The final solution answer was:
<flow name="test">
<http:inbound-endpoint exchange-pattern="request-response" host="0.0.0.0" port="1234" path="test" doc:name="HTTP"/>
<enricher target="#[variable:unwantedPayload]" source="#[payload]">
<flow-ref name="anotherFlowCalled" />
</enricher>
<jersey:resources doc:name="REST">
<component class="com.Test" />
</jersey:resources>
</flow>
best way to store the mule message into session , whether you flow contains subflows are private flow session vars carry till end of the execeution.

Sending an simple E-mail using WSO2 ESB

I am trying to send an e-mail to a specific address using WSO2 ESB.
I configured my axis2.xml by applying following settings to mailto transport sender.
<transportSender name="mailto" class="org.apache.axis2.transport.mail.MailTransportSender">
<parameter name="mail.smtp.host">smtp.gmail.com</parameter>
<parameter name="mail.smtp.port">587</parameter>
<parameter name="mail.smtp.starttls.enable">true</parameter>
<parameter name="mail.smtp.auth">true</parameter>
<parameter name="mail.smtp.user">myusername#gmail.com</parameter>
<parameter name="mail.smtp.password">mypassword</parameter>
<parameter name="mail.smtp.from">myusername#gmail.com</parameter>
</transportSender>
This is my fragment of the sequence which is responsible for sending mail.
<log level="custom">
<property name="Mail status" value="===============enter the mail============="/>
</log>
<property name="messageType" value="text/html" scope="axis2"/>
<property name="ContentType" value="text/html" scope="axis2"/>
<property name="Subject" value="File Received" scope="transport"/>
<property name="OUT_ONLY" value="true" scope="default" type="STRING"/>
<log level="full"/>
<send>
<endpoint>
<address uri="mailto:myreciveemail#gmail.com"/>
</endpoint>
</send>
<log level="custom">
<property name="flag" value="=====After======="/>
</log>
I am pretty much sure that other parts of my proxy service are working just fine.
But the problem is when I triggered the proxy service, it is going through all parts of the sequence without throwing an error but not sending the mail
This is my console output relevant to the fragment of sequence I stated earlier.
[2013-01-29 17:07:15,552] INFO - LogMediator Mail status = ===============enter
the mail=============
[2013-01-29 17:07:15,552] INFO - LogMediator To: , WSAction: urn:mediate, SOAPA
ction: urn:mediate, MessageID: urn:uuid:a12fd64c-f5c5-4b22-b092-e15af960a3d2, Di
rection: request, Envelope: <?xml version='1.0' encoding='utf-8'?><soapenv:Envel
ope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body>
<geo:getZipCode xmlns:geo="http://geo.wso2">
<geo:longitude>1</geo:longitude>
<geo:latitude>3</geo:latitude>
</geo:getZipCode>
</soapenv:Body></soapenv:Envelope>
[2013-01-29 17:07:15,567] INFO - LogMediator flag = =====After=======
Guys please any one knows what's the issue is????
Try to use
<parameter name="mail.smtp.user">myusername</parameter>
without #gmail.com
For those of you who are trying to send an email using the configuration stated in the question via wso2 esb, apart from all that you will need to include a axis2-transport-mail-1.0.0.jar under wso2esb-5.0.0\repository\axis2\client\lib. This jar contains the classes necessary for axis2 to send out an email. Hope this helps someone.

Categories

Resources