Thursday, March 3, 2016

Developing custom build Kaptcha (captcha) in adf

Web applications might often need captcha in order to prevent application from robotic attacks. Following example demonstrates how to build custom captcha in ADF Technology

Display Captcha :

Add following in jspx:

 <af:panelGroupLayout id="pgld11" layout="horizontal">
                  <af:outputLabel value="#{pageFlowScope.backingBean.secretQ}" id="otd14"
                                  inlineStyle="font-weight:normal; font-size:small; color:Black; font-family:Arial;"/>
                  <af:spacer width="18" height="5" id="sd13"/>
                  <af:inputText simple="true" label="secretQ" id="it7capta" contentStyle="width:100px; height:22px; font-family:Arial; font-size:small; color:Black; border-color:#878787;"
                                value="#{pageFlowScope.backingBean.secretUA}"
                                autoSubmit="true"
                                binding="#{pageFlowScope.backingBean.secreatAnswer_binding}">
                     </af:inputText>
            </af:panelGroupLayout>

in backing bean define the variables as:

 private String secretQ, secretA, secretUA;

 public void setSecretQ(String secretQ) {
        this.secretQ = secretQ;
    }

    public String getSecretQ() {
        List<String> secretQandA = CAPTCHAUtil.getQuestionAndAnswer();
        this.setSecretQ(secretQandA.get(0));
        this.setSecretA(secretQandA.get(1));
        this.setSecretUA("");
        return secretQ;
    }

    public void setSecretA(String secretA) {
        this.secretA = secretA;
    }

    public String getSecretA() {
        return secretA;
    }

    public void setSecretUA(String secretUA) {
        this.secretUA = secretUA;
    }

    public String getSecretUA() {
        return secretUA;
    }


please find the captcha util class attached. it can be modified as per user needs.


Validate Captcha:

Call this method to validate captcha:

    private Boolean validateCapta() {
        if(getSecretUA()== null){
            retValue=false;
        }else{
            if(getSecretUA().equals("")){
                retValue=false;
            }else if(!getSecretA().equalsIgnoreCase(getSecretUA())){
                retValue=false;
            }
             
        }
     
        return retValue;
    }

Data Source Not Found

While developing a web application i often come across following exceptions:

javax.naming.NameNotFoundException: Unable to resolve '<Data_Source_Name>'. Resolved ''; remaining name '<Data_Source_Name>'

at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:252)
at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:182)
at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:206)
at weblogic.jndi.internal.WLEventContextImpl.lookup(WLEventContextImpl.java:254)
at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:411)
at javax.naming.InitialContext.lookup(InitialContext.java:411)



oracle.jbo.DMLException: JBO-27200: JNDI failure. Unable to lookup Data Source at context HppConnNonXA
at oracle.jbo.server.DBTransactionImpl.lookupDataSource(DBTransactionImpl.java:1491)
at oracle.jbo.server.DBTransactionImpl2.connectToDataSource(DBTransactionImpl2.java:335)
at oracle.jbo.common.ampool.DefaultConnectionStrategy.connect(DefaultConnectionStrategy.java:203)
at oracle.jbo.server.ApplicationPoolMessageHandler.doPoolConnect(ApplicationPoolMessageHandler.java:620)
at oracle.jbo.server.ApplicationPoolMessageHandler.doPoolMessage(ApplicationPoolMessageHandler.java:425)
at oracle.jbo.server.ApplicationModuleImpl.doPoolMessage(ApplicationModuleImpl.java:9316)
at oracle.jbo.common.ampool.ApplicationPoolImpl.sendPoolMessage(ApplicationPoolImpl.java:4530)
at oracle.jbo.common.ampool.ApplicationPoolImpl.prepareApplicationModule(ApplicationPoolImpl.java:2460)





Reason:
Application Module is configured (App Mod -- > Configurations -- > App Module -->Connection Type)to connect to DB using data source defined in Weblogic(WL Console --> Service -->Data Source). The Data source is unavailable for the application


Where to Look for solution:

If you get the above mentioned error look for following :

1. Make sure the data source is targeted for the default server(in local) or the designated server you are running the application.

Go to WL Console --> Service -->Data Sources --- > Select Data source ---> Targets:




2. This might happen more often then you think. In most of the cases (atleast with me) you might have been disconnected to vpn or the data base might not be reachable due to some circumstance.

Make sure the database connection is up. quickest way to check the same is :

Go to WL Console --> Service -->Data Sources --- > Select Data source -- > Configuration --> Connection Pool and click on save.

If the data base is reachable, you would get all green in case of issue you will find it





The issue mentioned above may sound silly but believe me its occurs more often then you think.

Hope its is useful