OA Framework With Captcha Webservice

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Apps

OA Framework with Captcha Webservice


Contributed by Anil Passi
Saturday, 20 January 2007

In this article, I am explaining a step by step approach to implement a Captcha Web Service from Oracle's Java based
OA Framework. In this OA Framework tutorial, we will display a captcha image within a OA Framework page. Also, the
user will be able to enter the response to Captcha Image. The response will be validated against the Captcha
Webservice.

Will I be using BPEL for this exercise?


Not really, however you can implement something similar in BPEL too.
I prefer using BPEL when there are multiple Web Services involved.

Why use a Webservice, when you can get Open Source software like jCaptcha.
This is your personal choice. Both options are equally feasible and usefull depending upon your need.
However in my case, I preferred Captcha Webservice for below reasons:-
1. All functionality enhancements are available to you by Webservice Provider.
2. No maintenance on our system, to do bug-fixes[if any] to captcha generation logic.

Which Webservice will I be using?


I will be using free WebService from Textdisguise.com
Their WSDL is http: //www.textdisguise.com/TextDisguise/CaptchaService/CaptchaService.asmx?WSDL
You must register on their website, and they will email you username and password.
I quite liked Web Service from Capchas.net too, however you can decide to use whatever suits your need.

Before we go into details, what are the steps in brief?


Firstly, you may like to read this design approach on link Web Service Design approach in OA Framework.
1. Generate the stubs[java api for remote WebService] using WSDL URL.
This will generate some java files which internally make calls to captcha Web Service
2. Create a field of type image, into which Captcha Image will be displayed.
3. In controller class processRequest[executed when page renders], we make a call to Webservice API [ stubs generated
in Step-1].
The webservice will return the URL of the image on remote machine
4. Assign the URL to "image field" defined in step 2
5. When user submits the page, call webservice again to validate users response.

In jDeveloper, right click on project to create a Webservice Stub from your WSDL.

Enter the URL of Webservice [or call it a WSDL as they say].I am creating these in package named xxfreecaptcha01.
However, you can create these classes in any package as you wish.

The IDE of jDeveloper will create the classes based on definition of WebService in WSDL Document.
All complexities related to SOAP etc are taken care of by these Stubs.
For us, usage of Captcha is like calling any ordinary java Program.
NOTE: Very Important: The Stub classes and methods created by jDeveloper 9.0.3 is different from Netbeans, Eclipse
for the very SAME WSDL.
Infact, I found that even jDeveloper 10.1.3 would create different definition of Stubs. This is not a problem though, as
they all work. However this experience of mine proves that different vendors/products parse WSDL differently. All this
despite all of these following same specification.

http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44


Apps

Anyway, here are the java files generated, to make calls to Captcha remote Web Service.

Now, in OA Framework, lets create basic page/screen.


For the steps on how to create a simple HelloWorld Page in OAFramework, please visit [link for simple OAF Page ]
In this example, the OA Framework page
Lets create three extra fields.
Field 1:- Named "CaptchaImageField" of style Image
You may even Right click on corporateBrandingImage, and paste that into RNforButton.
Field 2:- A text field into which user will enter their response.
Field 3:-A button Go of type Submit Form
You can optionally create another field, for debugging, that displays the results of Captcha WebService validation.

Modify the controller code, [a piece of code that is executed when page renders].
In that code, we make a call to Webservice. The sourcecode is provided at the bottom of the article.
However, I will explain steps briefly here:-
1. Create instance of ClientAuthModel() and set userid and password.
2. Create instance of CaptchaServiceStub() and invoke Webservice method GetNewWord()
3. The result of the GetNewWord() is an Object of type CaptchaImageWordModel [word]
4. The returned object[named word], has URL of the Image, and also the KEY used for validation.
The KEY along with user response will be sent to CAPTCHA Webservice. Hence I am setting these two in session
variables.
Alternately, you can store these in some global variable or a hidden sets of fields.
5. Assign the image URL returned by Web Service to CaptchaImageField.

The response text entered by user is sent to Web Service for validation, alongwith the KEY of Image.
This is done by
ValidateImageWord(cm, pageContext.getSessionValue("captchaGUIDvalue").toString() ,
pageContext.getParameter("captchaWordResponseField"));
If the WebService API returns false, then I am displaying the debug text "Failure with CAPTCHA"

In case the users response is incorrect, we render the page again[by redirecting the page to itself].
This causes the Captcha Webservice to be called again, this time with new Image URL and a new KEY.
Hence with every wrong attempt, the user will see a new image appearing on the page.
This gives users a possibility of attempting Captcha text entry as many times.

Where is the Controller Code for OA Framework, to implement Captcha Webservice?


/*===========================================================================+
| Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA |
| All rights reserved. |
+===========================================================================+
| HISTORY |
+===========================================================================*/
package oracle.apps.ak.hello_01.webui;

http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44


Apps

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;
import oracle.apps.fnd.framework.webui.beans.OAImageBean ;
import oracle.apps.fnd.framework.OAException;
import xxfreecaptcha01.CaptchaImageWordModel;
import xxfreecaptcha01.CaptchaServiceStub;
import xxfreecaptcha01.ClientAuthModel;
import com.sun.java.util.collections.HashMap;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;

/**
* Controller for ...
*/
public class MainRNCO extends OAControllerImpl {
public static final String RCS_ID="$Header$";
public static final boolean RCS_ID_RECORDED =
VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

/**
* Layout and page setup logic for a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
OAMessageTextInputBean fieldHelloName =
(OAMessageTextInputBean)webBean.findChildRecursive("HelloName");

OAMessageTextInputBean fieldcaptchaGUID =
(OAMessageTextInputBean)webBean.findChildRecursive("captchaImageGUID");

// fieldHelloName.setText(pageContext,"Welcome. Good Luck this time");

OAImageBean cimage = (OAImageBean)webBean.findChildRecursive("CaptchaImageField");


//note, i am changing authentication details here. You need to get one for yourself from textdisguise.com
try {
String clientGuid = "9Xfaf972b-ff85-483e-a132-b6c4f72e27e4";
String clientPassword = "!:ARlmLn,P;CDKN&%Q{s_h|u<r";

CaptchaServiceStub cs;
ClientAuthModel cm;
CaptchaImageWordModel word;

cm = new ClientAuthModel();
cm.setGuid(clientGuid);
cm.setPassword(clientPassword);

cs = new CaptchaServiceStub();
word = cs.GetNewWord(cm);
pageContext.putSessionValue("captchaGUIDvalue",word.getCaptchaImageGuid());
cimage.setSource(word.getUrl());
} catch ( Exception exx ) {
fieldHelloName.setText(pageContext," !!!! Exception " + exx);
}
super.processRequest(pageContext, webBean);
}

/**
* Procedure to handle form submissions for form elements in
* a region.
* @param pageContext the current OA page context
* @param webBean the web bean corresponding to the region
*/
http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44
Apps

public void processFormRequest(OAPageContext pageContext, OAWebBean webBean) {

try {
//note, i am changing authentication details here. You need to get one for yourself from textdisguise.com
String clientGuid = "9Xfaf972b-ff85-483e-a132-b6c4f72e27e4";
String clientPassword = "!:ARlmLn,P;CDKN&%Q{s_h|u<r";
CaptchaServiceStub cs;
CaptchaServiceStub cs1;
ClientAuthModel cm;
java.lang.Boolean ret;

OAMessageTextInputBean fieldHelloName =
(OAMessageTextInputBean)webBean.findChildRecursive("HelloName");

OAMessageTextInputBean fieldcaptchaGUID =
(OAMessageTextInputBean)webBean.findChildRecursive("captchaImageGUID");

OAMessageTextInputBean fieldcaptchaResponse =
(OAMessageTextInputBean)webBean.findChildRecursive("captchaWordResponseField");

OAImageBean cimage = (OAImageBean)webBean.findChildRecursive("CaptchaImageField");


CaptchaImageWordModel word;

cm = new ClientAuthModel();
cm.setGuid(clientGuid);
cm.setPassword(clientPassword);

cs = new CaptchaServiceStub();
ret = cs.ValidateImageWord(cm, pageContext.getSessionValue("captchaGUIDvalue").toString() ,
pageContext.getParameter("captchaWordResponseField"));

if(ret.toString() =="true") {
fieldHelloName.setText(pageContext,"Success with CAPTCHA" );
} else {
fieldHelloName.setText(pageContext,"Failure with CAPTCHA" );
}

super.processFormRequest(pageContext, webBean);
if (ret.toString() == "false") {
cs1 = new CaptchaServiceStub();
word = cs1.GetNewWord(cm);
HashMap params = new HashMap();
pageContext.forwardImmediatelyToCurrentPage(params,true,OAWebBeanConstants.ADD_BREAD_CRUMB_NO);
My GUID was=>" + pageContext.getSessionValue("captchaGUIDvalue") );

} catch ( Exception exx ) {throw new OAException("Exception occured" + exx, OAException.INFORMATION);}


}

How about the Stub Files, that will be used by Java OA Framework?
Please find the three classes below
package xxfreecaptcha01;
/**
* Generated by the Oracle9i JDeveloper Web Services Stub/Skeleton Generator.
* Date Created: Sun Jan 03 12:52:32 GMT 2007
*
* <pre>
http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44
Apps

* <s:complexType name="CaptchaImageWordModel">
* <s:sequence>
* <s:element minOccurs="0" maxOccurs="1" name="CaptchaImageGuid" type="s:string"/>
* <s:element minOccurs="0" maxOccurs="1" name="Url" type="s:string"/>
* </s:sequence>
* </s:complexType>
* </pre>
*/

public class CaptchaImageWordModel


{
private String m_CaptchaImageGuid;

private String m_Url;

public CaptchaImageWordModel()
{
}

public CaptchaImageWordModel(String CaptchaImageGuid, String Url)


{
m_CaptchaImageGuid = CaptchaImageGuid;
m_Url = Url;
}

public void setCaptchaImageGuid(String CaptchaImageGuid)


{
m_CaptchaImageGuid = CaptchaImageGuid;
}

public String getCaptchaImageGuid()


{
return m_CaptchaImageGuid;
}

public void setUrl(String Url)


{
m_Url = Url;
}

public String getUrl()


{
return m_Url;
}
}

package xxfreecaptcha01;
import oracle.soap.transport.http.OracleSOAPHTTPConnection;
import org.apache.soap.encoding.soapenc.BeanSerializer;
import org.apache.soap.encoding.SOAPMappingRegistry;
import org.apache.soap.util.xml.QName;
import java.util.Vector;
import org.w3c.dom.Element;
import java.net.URL;
import org.apache.soap.Body;
import org.apache.soap.Envelope;
import org.apache.soap.messaging.Message;
import oracle.jdeveloper.webservices.runtime.WrappedDocLiteralStub;
/**
* Generated by the Oracle9i JDeveloper Web Services Stub/Skeleton Generator.
* Date Created: Wed Jan 03 12:52:33 GMT 2007
* WSDL URL: http://www.textdisguise.com/TextDisguise/CaptchaService/CaptchaService.asmx?WSDL
*
* Web service that allows users to create CAPTCHA-like ("Completely Automated Public Turing test to Tell Computers
and Humans Apart") images and validate the response entered. Current version is v1.0.0. Release Date: 2005-12-22
http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44
Apps

*/

public class CaptchaServiceStub extends WrappedDocLiteralStub


{
public CaptchaServiceStub()
{
m_httpConnection = new OracleSOAPHTTPConnection();
}

public String endpoint = "http://www.textdisguise.com/TextDisguise/CaptchaService/CaptchaService.asmx";


private OracleSOAPHTTPConnection m_httpConnection = null;
private SOAPMappingRegistry m_smr = null;

public CaptchaImageWordModel GetNewWord(ClientAuthModel cam) throws Exception


{
URL endpointURL = new URL(endpoint);

Envelope requestEnv = new Envelope();


Body requestBody = new Body();
Vector requestBodyEntries = new Vector();

String wrappingName = "GetNewWord";


String targetNamespace = "http://www.textdisguise.com/TextDisguise/CaptchaService/";
Vector requestData = new Vector();
requestData.add(new Object[] {"cam", cam});

requestBodyEntries.addElement(toElement(wrappingName, targetNamespace, requestData));


requestBody.setBodyEntries(requestBodyEntries);
requestEnv.setBody(requestBody);

Message msg = new Message();


msg.setSOAPTransport(m_httpConnection);
msg.send(endpointURL, "http://www.textdisguise.com/TextDisguise/CaptchaService/GetNewWord", requestEnv);

Envelope responseEnv = msg.receiveEnvelope();


Body responseBody = responseEnv.getBody();
Vector responseData = responseBody.getBodyEntries();

return (CaptchaImageWordModel)fromElement((Element)responseData.elementAt(0),
xxfreecaptcha01.CaptchaImageWordModel.class);
}

public Boolean ValidateImageWord(ClientAuthModel cam, String captchaImageGuid, String word) throws Exception
{
URL endpointURL = new URL(endpoint);

Envelope requestEnv = new Envelope();


Body requestBody = new Body();
Vector requestBodyEntries = new Vector();

String wrappingName = "ValidateImageWord";


String targetNamespace = "http://www.textdisguise.com/TextDisguise/CaptchaService/";
Vector requestData = new Vector();
requestData.add(new Object[] {"cam", cam});
requestData.add(new Object[] {"captchaImageGuid", captchaImageGuid});
requestData.add(new Object[] {"word", word});

requestBodyEntries.addElement(toElement(wrappingName, targetNamespace, requestData));


requestBody.setBodyEntries(requestBodyEntries);
requestEnv.setBody(requestBody);

Message msg = new Message();


msg.setSOAPTransport(m_httpConnection);
msg.send(endpointURL, "http://www.textdisguise.com/TextDisguise/CaptchaService/ValidateImageWord", requestEnv);

http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44


Apps

Envelope responseEnv = msg.receiveEnvelope();


Body responseBody = responseEnv.getBody();
Vector responseData = responseBody.getBodyEntries();

return (Boolean)fromElement((Element)responseData.elementAt(0), java.lang.Boolean.class);


}

public Boolean AbandonImageWord(ClientAuthModel cam, String captchaImageGuid) throws Exception


{
URL endpointURL = new URL(endpoint);

Envelope requestEnv = new Envelope();


Body requestBody = new Body();
Vector requestBodyEntries = new Vector();

String wrappingName = "AbandonImageWord";


String targetNamespace = "http://www.textdisguise.com/TextDisguise/CaptchaService/";
Vector requestData = new Vector();
requestData.add(new Object[] {"cam", cam});
requestData.add(new Object[] {"captchaImageGuid", captchaImageGuid});

requestBodyEntries.addElement(toElement(wrappingName, targetNamespace, requestData));


requestBody.setBodyEntries(requestBodyEntries);
requestEnv.setBody(requestBody);

Message msg = new Message();


msg.setSOAPTransport(m_httpConnection);
msg.send(endpointURL, "http://www.textdisguise.com/TextDisguise/CaptchaService/AbandonImageWord", requestEnv);

Envelope responseEnv = msg.receiveEnvelope();


Body responseBody = responseEnv.getBody();
Vector responseData = responseBody.getBodyEntries();

return (Boolean)fromElement((Element)responseData.elementAt(0), java.lang.Boolean.class);


}
}

package xxfreecaptcha01;
/**
* Generated by the Oracle9i JDeveloper Web Services Stub/Skeleton Generator.
* Date Created: Wed Jan 03 12:52:32 GMT 2007
*
* <pre>
* <s:complexType name="ClientAuthModel">
* <s:sequence>
* <s:element minOccurs="0" maxOccurs="1" name="Guid" type="s:string"/>
* <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string"/>
* </s:sequence>
* </s:complexType>
* </pre>
*/

public class ClientAuthModel


{
private String m_Guid;

private String m_Password;

public ClientAuthModel()
{
}

public ClientAuthModel(String Guid, String Password)


{
m_Guid = Guid;
http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44
Apps

m_Password = Password;
}

public void setGuid(String Guid)


{
m_Guid = Guid;
}

public String getGuid()


{
return m_Guid;
}

public void setPassword(String Password)


{
m_Password = Password;
}

public String getPassword()


{
return m_Password;
}
}

http://oracle.anilpassi.com Powered by Joomla! Generated: 30 March, 2011, 08:44

You might also like