Wednesday, November 26, 2014

How to enable CORS in apache tomcat

This is the simplest way to enable CORS in apache tomcat.

 

Have to add the below configuration to your application’s web.xml and restart the tomcat if required.

 

<filter>

  <filter-name>CorsFilter</filter-name>

  <filter-class>org.apache.catalina.filters.CorsFilter</filter-class>

  <init-param>

    <param-name>cors.allowed.origins</param-name>

    <param-value>*</param-value>

  </init-param>

  <init-param>

    <param-name>cors.allowed.methods</param-name>

    <param-value>GET,POST,HEAD,OPTIONS,PUT</param-value>

  </init-param>

  <init-param>

    <param-name>cors.allowed.headers</param-name>

    <param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers</param-value>

  </init-param>

  <init-param>

    <param-name>cors.exposed.headers</param-name>

    <param-value>Access-Control-Allow-Origin,Access-Control-Allow-Credentials</param-value>

  </init-param>

  <init-param>

    <param-name>cors.support.credentials</param-name>

    <param-value>true</param-value>

  </init-param>

  <init-param>

    <param-name>cors.preflight.maxage</param-name>

    <param-value>10</param-value>

  </init-param>

</filter>

<filter-mapping>

  <filter-name>CorsFilter</filter-name>

  <url-pattern>/*</url-pattern>

</filter-mapping>



Thanks & Regards,

Rajakrishna Reddy

MySQL error java.sql.SQLException: Can't call commit when autocommit=true

When you see the error like below, please use the provided solution to get rid of this.

 

Caused by: java.sql.SQLException: Can't call commit when autocommit=true

        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)

        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)

        at com.mysql.jdbc.ConnectionImpl.commit(ConnectionImpl.java:1740)

        at sun.reflect.GeneratedMethodAccessor29.invoke(Unknown Source)

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

        at java.lang.reflect.Method.invoke(Method.java:606)

        at org.apache.tomcat.jdbc.pool.ProxyConnection.invoke(ProxyConnection.java:126)

        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)

        at org.wso2.carbon.ndatasource.rdbms.ConnectionRollbackOnReturnInterceptor.invoke(ConnectionRollbackOnReturnInterceptor.java:51)

        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)

        at org.apache.tomcat.jdbc.pool.interceptor.AbstractCreateStatementInterceptor.invoke(AbstractCreateStatementInterceptor.java:71)

        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)

        at org.apache.tomcat.jdbc.pool.interceptor.ConnectionState.invoke(ConnectionState.java:153)

        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)

        at org.apache.tomcat.jdbc.pool.TrapException.invoke(TrapException.java:41)

        at org.apache.tomcat.jdbc.pool.JdbcInterceptor.invoke(JdbcInterceptor.java:109)

        at org.apache.tomcat.jdbc.pool.DisposableConnectionFacade.invoke(DisposableConnectionFacade.java:80)

        at com.sun.proxy.$Proxy14.commit(Unknown Source)

        at org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO.addAPI(ApiMgtDAO.java:4871)

        ... 65 more

 

 

Solution:

Use the url as shown here: <url>jdbc:mysql://varra:3306/DB_CARBON_IDENTITY?autoReconnect=true&amp;relaxAutoCommit=true</url>



Thanks & Regards,

Rajakrishna Reddy,

 

Tuesday, November 25, 2014

What is the best way to create an API in wso2 API-Manager 1.7.0

There was an issue with API improvements from 1.6.0 to 1.7.0 in multiple resource creation. Please apply attached PATCH-0580 to fix the issue.

 

Follow below mentioned guide to create multiple resources

 

1. To create multiple resources for a single uri-template (for multiple resource methods) refer below sample

 

ADD API:

 

curl -X POST -b cookies http://localhost:9763/publisher/site/blocks/item-add/ajax/add.jag -d "action=addAPI&name=Twitter&visibility=public&version=1.0.0&description=API for twitter&endpointType=nonsecured&http_checked=http&https_checked=https&tags=tweet,test&tier=Bronze&thumbUrl=https://g.twimg.com/Twitter_logo_blue.png&context=/tweet&tiersCollection=Bronze,Gold,Unlimited&resourceCount=0&resourceMethod-0=GET,PUT,POST,DELETE&resourceMethodAuthType-0=Application,Application,Application,Application&uriTemplate-0=/test/*&resourceMethodThrottlingTier-0=Bronze, Bronze,Unlimited,Gold" -d 'endpoint_config={"production_endpoints":{"url":"http://search.twitter.com","config":null},"endpoint_type":"http"}'

 

UPDATE API:

 

curl -X POST -b cookies http://localhost:9763/publisher/site/blocks/item-add/ajax/add.jag -d "action=updateAPI&name=Twitter&provider=provider&visibility=public&version=1.0.0&description=API for twitter changed&endpointType=nonsecured&http_checked=http&https_checked=https&tags=tweet,test&tier=Bronze&thumbUrl=https://g.twimg.com/Twitter_logo_blue.png&context=/tweet&tiersCollection=Bronze,Gold,Unlimited&resourceCount=0&resourceMethod-0=GET,PUT,POST&resourceMethodAuthType-0=Application,Application,Application&uriTemplate-0=/test/*&resourceMethodThrottlingTier-0=Gold,Gold,Gold" -d 'endpoint_config={"production_endpoints":{"url":"http://search.twitter.com","config":null},"endpoint_type":"http"}'

 

 

2. To create multiple resources with multiple uri-templates refer below sample

 

curl -X POST -b cookies http://localhost:9763/publisher/site/blocks/item-add/ajax/add.jag -d "action=addAPI&name=Twitter&visibility=public&version=1.0.0&description=API for twitter&endpointType=nonsecured&http_checked=http&https_checked=https&tags=tweet,test&tier=Bronze&thumbUrl=https://g.twimg.com/Twitter_logo_blue.png&context=/tweet&tiersCollection=Bronze,Gold,Unlimited&resourceCount=2&resourceMethod-0=GET,PUT,POST,DELETE&resourceMethodAuthType-0=Application,Application,Application,Application&uriTemplate-0=/test/*&resourceMethodThrottlingTier-0=Bronze,Bronze,Unlimited,Gold&resourceMethod-1=GET,PUT&resourceMethodAuthType-1=Application User,Application User&uriTemplate-1=/test2/*&resourceMethodThrottlingTier-1=Unlimited,Unlimited" -d 'endpoint_config={"production_endpoints":{"url":"http://search.twitter.com","config":null},"endpoint_type":"http"}'

 

*NOTE : in this case follow following convention

resourceCount=[number of resources]

 

and for each resource parameters

[resource parameter]-[resource number 0,1,...]=[value]

example:

resourceMethod-1=GET

esourceMethodAuthType-1=Application .... etc.

 

Following configurations are also provided to manage api

default_version_checked, wsdl, wadl, inSequence, outSequence, faultSequence, responseCache, cacheTimeout, subscriptions, tenants,.. etc.


Thanks & Regards,

Rajakrishna Reddy,

 

Sunday, November 23, 2014

How to forward OAuth headers (Authorization) to backend Service using wso2 API-Manager

Needs a configuration change in api-manager.xml file under ${PRODUCT_HOME}/repository/conf/api-manager.xml

Eg:

 

<APIConsumerAuthentication>

        <!--

            Name of the security context header to be added to the validated requests.

        -->

        <SecurityContextHeader>X-JWT-Assertion</SecurityContextHeader>

 

        <!--

                Fully qualified name of the class that will retrieve additional user claims

                to be appended to the JWT. If not specified no claims will be appended.

                The DefaultClaimsRetriever class adds user claims from the default carbon user store.

        -->

 

 

        <!--

                Remove OAuth headers from outgoing message or keep with it.

        -->

 

        <RemoveOAuthHeadersFromOutMessage>false</RemoveOAuthHeadersFromOutMessage>

 

    </APIConsumerAuthentication>



Thanks & Regards,

Rajakrishna Reddy,

 

Sunday, November 16, 2014

Refresh token is missing in the token request with grant type is 'password'

This could be the possible scenario happens with wso2 AM.

1. If the token is generated thru UI for the first time, Refresh token may miss in the next token request with grant type as ‘password’.

2. Once that token is revoke with the curl request we were able to get the refresh token.
Sample curl request we used:
curl -k -d "token=<ACCESS_TOKEN_TO_BE_REVOKED>" -H "Authorization: Basic Base64Encoded(Consumer key:consumer secret)" http://localhost:8280/revoke

3. Even the "RefreshTokenValidityPeriod" expires server didn't generate the refresh token.We found that we need to investigate further on this point.

Following the configuration we found in the "identity.xml"

<AccessTokenDefaultValidityPeriod>3600</AccessTokenDefaultValidityPeriod>
<RefreshTokenValidityPeriod>84600</RefreshTokenValidityPeriod>
<RenewRefreshTokenForRefreshGrant>true</RenewRefreshTokenForRefreshGrant>

Enabling SSL in Tomcat 8

Keep the below entry in Tomcat 8 server under server.xml in conf dir.
eg: {$TOMCAT_HOME}/conf/server.xml

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS"
               keystoreFile="{your_cert(jks)_path}" keystorePass="{password}" />


Thursday, November 6, 2014

Error while executing API in wso2: ServiceContext in OperationContext does not match !

This error occurs when you try to execute an api with POST method rather GET, where this api accepts only GET method.

TID: [0] [AM] [2014-11-07 06:46:28,274] ERROR {org.apache.synapse.transport.passthru.TargetHandler} -  Unexpected error. {org.apache.synapse.transport.passthru.TargetHandler}
TID: [0] [AM] [2014-11-07 06:46:28,277] ERROR {org.apache.axis2.transport.base.threads.NativeWorkerPool} -  Uncaught exception {org.apache.axis2.transport.base.threads.NativeWorkerPool}
org.apache.axis2.engine.AxisError: ServiceContext in OperationContext does not match !
        at org.apache.axis2.context.MessageContext.setOperationContext(MessageContext.java:1404)
        at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.processResponse(MultitenantMessageReceiver.java:129)
        at org.wso2.carbon.core.multitenancy.MultitenantMessageReceiver.receive(MultitenantMessageReceiver.java:81)
        at org.apache.synapse.transport.passthru.TargetErrorHandler$1.run(TargetErrorHandler.java:134)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
        at java.lang.Thread.run(Thread.java:662)