I configured an inbound endpoint, but I want to manage java.naming.provider.url value in a conf.xml file that under config directory in WSO2.
In each environment I have a conf.xml file which includes the URL values for that environment like:
<environment>
<JNPU>test</JNPU>
</environment>
<test>
<JNPU>failover:tcp://localhost:61616</JNPU>
</test>
I just want first read this environment value of the JNPU which is test in this example. Then, I want to change the java.naming.provider.url value in inbound endpoint with this value.
In a sequence, I can read the values with using these properties:
<property name="confFile" expression="get-property('registry','conf:endpoints/conf.xml')" scope="default" type="OM" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="JNPUEnvValue" expression="evaluate(fn:concat('$confFile//environment//','JNPU'))" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
<property name="JNPUValue" expression="evaluate(fn:concat('$ctx:epConfiguration','//',get-property('JNPUEnvValue'),'//','JNPU'))" scope="default" type="STRING" xmlns:ns="http://org.apache.synapse/xsd"/>
I used them as parameters in inbound endpoint but I could not get the value of java.naming.provider.url in conf.xml file.
Thanks for any idea.
We can dynamically configure inbound endpoint parameters, if we save only the respective parameter value as the content of the file saved in registry. For example, in your case, the value "failover:tcp://localhost:61616" should be saved in the file. You can keep separate files for each parameter in a registry location. And, you need to only change file content for each environment.
Please refer the section Specifying inbound endpoint parameters as registry values at https://docs.wso2.com/display/EI650/WSO2+EI+Inbound+Endpoints.
If you can keep the url in the registry, then you can use it from the registry as follows.
<parameter name="java.naming.provider.url" key="gov:/Path/javaNamingProviderURL"/>
Related
I am using wso2 API manager 3.2 and in my API there are some headers and Query Parameter.
My API has some path variable in URL in this way
http://example.com/data/readsomeData/{entityId}/{someId}
for the path variable I use the following mediator
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
and it works fine.
also I change some of incoming header's name . For example incoming header is IN_HEADER , I translate it to OUT_HEADER ,my backend expects OUT_HEADER.
I also use this mediator
<property name="IN_HEADER" expression="get-property('transport', 'IN_HEADER')"/>
<property name="OUT_HEADER" expression="get-property('IN_HEADER')" scope="transport"/>
<property name="IN_HEADER" scope="transport" action="remove"/>
It also works fine too.
The given API describe above also has some optional query parameters (limit, max, min) . For example if I use limit=10 in my API, I have to get 10 records, with out limit I get just one record.
In WSO2 API Manager Publisher I defined the above query parameters as the other parameters.
The problem is when I use each of query parameter I get the result same as the way I do not use the query parameters. I get only one record.
I think the API manager does not pass the query parameters to backend.
I don't know this problem related to the mediator I use or not!
You can approach this in a few different ways, below examples should also work on 3.2.0.
Because you remove the REST_URL_POSTFIX the APIM also removes the query parameters.
So option 1:
You could do something similar to what you did for the other headers.
Grab the query parameters and add them as headers for further use. (the $url shorthand should also be available in 3.2.0 as far as I know - it grabs a query parameter by name.)
<property name="limit" expression="$url:limit" scope="transport"/>
<property name="min" expression="$url:min" scope="transport"/>
<property name="max" expression="$url:max" scope="transport"/>
Or you could grab the query parameters from your URL and put them back after removing the POSTFIX.
<property name="newUrlPostfix" expression="substring after($axis2:REST_URL_POSTFIX, '?')"/>
<property name="REST_URL_POSTFIX" scope="axis2" action="remove"/>
<property name="REST_URL_POSTFIX" scope="axis2" expression="concat('?', $ctx:newUrlPostfix"/>
Is possible generated a mask in services in API Manager?, similar to:
Original Services in API:
http://API/v1/profile
http://API/v1/account
With mask:
http://API/v1/user-profile
http://API/v1/user-account
My ideas are:
In Publisher Page -> Runtime Configurations add a Message Mediation that deleted the user- of the service with mask.
In API Definition, in Publisher Page, edit the JSON Swagger in the services put some mask. This is a example whit TAG in Swagger:
tags:
- "/v1/kyc-perfil"
Is Possible or not generated a mask in service in API Manager?
EDIT:
After to read the answer of Bee, works but in two or more service the request in every service is the same and diferent of the service original.
I tried to put similar this:
<property name="/v1/kyc-{DYNAMIC-PARAMETER}" expression="get-property('axis2', 'REST_URL_POSTFIX')"/>
<property name="REST_URL_POSTFIX" value="/v1/{DYNAMIC-PARAMETER}" scope="axis2"/>
You can do option 1. Use the REST_URL_POSTFIX property. Try this.
Read:
<property name="post_fix" expression="get-property('axis2', 'REST_URL_POSTFIX')"/>
Write:
<property name="REST_URL_POSTFIX" value="new_post_fix" scope="axis2"/>
Ref: https://docs.wso2.com/display/ESB470/HTTP+Transport+Properties#HTTPTransportProperties-Property:REST_URL_POSTFIXREST_URL_POSTFIX
I am implementing handler for REST API in Java (org.apache.synapse.rest.Handler interface). And there is a case, when I need to access Secure Vault and get a value.
I know that you are able to achieve this by expression="wso2:vault-lookup('YOUR.KEY.HERE')" in sequence, but can't find api to do this in handler. I believe that org.apache.synapse.MessageContext can help, but not sure how.
You can use below code segment in the custom handler.
public String getSecretPassword(String alias, MessageContext messageContext){
RegistrySecretRepository regRepo = new RegistrySecretRepository();
regRepo.setSynCtx(messageContext);
return regRepo.getSecret(alias);
}
Dependency for pom.xml, the version needs to be changed according to your product version.
<dependency>
<groupId>org.wso2.carbon</groupId>
<artifactId>org.wso2.carbon.mediation.security</artifactId>
<version>4.2.0</version>
</dependency
Please refer - http://malantech.blogspot.com/2016/10/basic-authentication-handler-with.html
Thanks
I believe you will not be able to get the value of the security vault directly from your handler so I advise you to recover the password and put it in a property and inside your handler to retrieve the property.
<property name="passwordvault"
expression="wso2:vault-lookup('YOUR.KEY.HERE')"
scope="default"/>
And use the MessageContext to get the propertie like this:
context.getProperty("passwordvault");
That's just a workaround which is not advisable , i believe you can try below code as i have used similar earlier as well and it worked
<property expression="wso2:vault-lookup('ei.training.userid')" name="UserID" scope="default" type="STRING"/>
<log>
<property expression="wso2:vault-lookup('ei.training.userid')" name="UID"/>
</log>
And I will answer my own question.
I've created a dummy sequence and placed it into Registry
<sequence name="SecureVaultSeq" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
<property expression="wso2:vault-lookup('MY.PASS')" name="NAME"
scope="default" type="STRING"
xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd"/>
</sequence>
Then in my handler i retrieved it like this:
messageContext.getConfiguration().getSequence("conf:Resources/sequences/SecureVaultSeq.xml").mediate(messageContext);
key = (String) messageContext.getProperty("NAME");
Hope this will help someone.
I am using the inline javascript prototype feature in the WSO2 API manager and I'm trying to set different HTTP response statuses. Is this possible? If so how is it done?
So far I have tried setting the HTTP_SC property but this doesn't seem to have any effect.
mc.setProperty('HTTP_SC', "404");
I had the same requirement and after much exploring under the hood was able to find a workable solution.
The reason why setting the property:
mc.setProperty('HTTP_SC', "404");
didn't work is that the property needs to be set in the axis2 scope (as Abimaran said). mc.setProperty doesn't set it on that scope. Moreover, the MessageContext object doesn't provide a way to set the scope.
The 'Deploy as Prototype' action actually creates the API definition file by merging the specified in-line script into the a velocity template and storing the resulting API definition into a file.
Template: ./repository/resources/api_templates/prototype_template.xml
Output location: repository/deployment/server/synapse-configs/default/api/
The output file will have a name in the format:
provider--API Name-vVERSION.xml
where provider appears to be the username of the API creator.
What I did was add a filter to the template:
<filter source="boolean(get-property('HTTP_SC'))" regex="false">
<then>
<property name="HTTP_SC" value="200" scope="axis2"/>
</then>
<else>
<property name="HTTP_SC" expression="get-property('HTTP_SC')" scope="axis2"/>
</else>
</filter>
I added it immediately after a similar block (for handling CONTENT_TYPE) at the start of the inSequence element.
You need to add following properties before <send/> mediator
<header name="To" action="remove"/>
<property name="RESPONSE" value="true"/>
<property name="HTTP_SC" value="403" scope="axis2"/>
I have the config with development and production sections. This sections contain the URLs of backends. In my inSequence i need it to Callout to these backends several times per request.
<config>
<env>prod</env>
<backend env="prod">http://localhost:1234/</backend>
<backend env="dev">http://localhost:2345/</backend>
</config>
I read this config from Local Entry (as XML) and want to set Callout's URL as an Property.
I don't want to hardcode these backends inside my code with "Switch" statement, because it's possible to use more than two environments.
Could you please show me an example?
Thank you.
You can read xml file in registry. Simply define property of OM type like this:
<property name="test" expression="get-property('registry','conf:/test.xml')" scope="default" type="OM" />
Then you can see the value by logging like this:
<log level="custom"> <property name="test.b" expression="$ctx:test//b" /> </log>
And in the xml file that you have put in the root of registry, you would fill it like:
<a>Hello<b>WSO2</b></a>
I have learned it from this link.
I found the answer. According to source of Callout mediator:
CalloutMediator.java
It uses "To" header if URL is not specified.