Delegate in Ruby on Rails

January 14, 2012 Leave a comment

This is the technique where object expose some method or behavior which delegate take care of implementing that to an associated object.

Example : There are two models User and Department. User belongs to department.

class User

belongs_to  :department

delegate :name, :to => :department, :prefix => true, :allow_nil => true

end

class Department

has_many  :users

end

If you want to access department name for an user

user.department_name

Options for delegate

:prefix => true

You have to call the method “name” by prefixing the model name as “user.department_name”

:prefix => “dept”

now you have to call as “user.dept_name”

:allow_nil => true

if there is no name then it will return nil. If you have not mentioned this option and name is not present then this method will give no method error

Include Html file inside another Html file

January 13, 2012 Leave a comment

To include the html content into another html file I have used java script.
Below is the example where I have used header content and footer content into different file
and called these two files into all files wherever header and footer requires.

Main page content

Content.html

<html>

<body>

<script type=”text/javascript” src=”header.js”> </script>

<p>

Content of the page

</p>

<script type=”text/javascript” src=”footer.js”> </script>

</body>

</html>

header.js file content

document.write(“<h1>Header</h1>”);
document.write(“<h2>Tag Line</h2>”);

footer.js file content

document.write(“<h2>Contact us</h2>”);
document.write(“<h3>9876543210</h3>”);

Out put of content.html file in Browser

Header
Tag Line

Content of the page in details

Contact us
9876543210

Multiselector checkAll selects all options instead of filtered list

November 1, 2011 Leave a comment

I am using multiselector and whenever I try to select all filtered result by clicking checkAll then it selects all options not only filter list but all.

Example: I have list like

abc1, abc2, abc3, xyz1, xyz2

and I filtered by “abc” text and I got three result abc1, abc2 and abc3

Then I clicked on checkAll link it shows it selected 3 options but it is selecting all 6 options, for this problem I have done general method and it is working fine

Added this function call in that html file

<script>

$(document).ready(function() {

checkAllCorrection(‘#selctboxid’);

});

</script>

General method in application.js file

function checkAllCorrection(fieldName){

$(fieldName).bind(“multiselectcheckall”, function(event, ui){

    var checkedValueList = [];

    $(fieldName).multiselect(“getChecked”).each(function(){

         checkedValueList.push(this.value);

     });

    $(fieldName).multiselect(“refresh”);

    $(fieldName).multiselect(‘widget’).find(“:checkbox”).each(

          function(){

               if(checkedValueList.indexOf(this.value) == -1

&& this.checked)        this.click();

           }

    );

    });

}

Digital Signature

Digital signature

Digital signature algorithm is the one that makes the receiver believe that the message was sent by the claimed sender, and trust the message.

There are different types of signing techniques for Example Text signer, Xml signer etc.. We used the XML Signer. First I will explain the signing process how it will take place.

User will pass the Data along with the certificate and signing process will sign the Data using the certificate and pass the data with signature and certificate. We are calling this data as digitally signed Data. Receiver will separate the data and signature form the digitally signed data. And make another signature from that data and compare the both signature.

CAPICOM is a discontinued ActiveX control created by Microsoft to help expose a select set of Microsoft Cryptographic Application Programming Interface (CryptoAPI) functions through Microsoft Component Object Model (COM). CAPICOM can be used to digitally sign data, inspect, verify and display their digital signature and/or digital certificate. CAPICOM Version 2.1.0.2, the latest and last version of CAPICOM, is officially supported on Windows Vista. We are using CAPICOM Version 2.1.0.2

Signing Process

  • We are passing Data for signing and the certificate information.
  • CAPICOM will take the certificate details like Hash algorithm and apply that algorithm to data and get the Hash called it as Message Digest (MD).
  • Using this MD and primary key which is present in certificate, it will create the signature
  • It will integrate signature certificate and Data. This data is called digitally signed data.

Verify Process

  • We are passing the digitally signed data for verification
  • It will separate the Data signature and certificate.
  • Using data it will create once again the hash (message Digest) let’s say MD1, using certificate primary key and hash algorithm.
  • It will get another MD using signature which is passed though digitally signed data, using local key of certificate. Let’s say this MD as MD2
  • For verifying the signing it will check the MD1 is equal to MD2 or not.

Struts Life Cycle EXample

February 13, 2011 1 comment

Request process in Struts Framework

  • When any struts based web application loaded into serverinitially it will load the ActionServlet class, due to<load-on-startup> tag in web.xml.
  • It will read struts-config.xml file, due to <init-param> tag inweb.xml.
  • When anybody makes request to the respective webapplication it will display the file which is mentioned in<welcome-file-list> tag of web.xml file.
  • When the view page(.jsp) is displayed to the user internally the strutsframework (ActionServlet) will create an object for the formbean class
  • Which extends the ActionForm by calling zero argument constructor(default construtor) which is mapped with the Action class path mentioned in .jsp file <html:form action=”/path”> later it will call the reset() and all the getter methods of the form bean class.
  • When the user fills the form and submit the input, our form bean class again calls the default constructor, reset(), all setter() methods, validate() methods respectively.
  • Validate method return ActionErrors object. If the ActionErrors object contains any error values(ActionError/ActionMessage values) then the ActionServlet calls the input page mapped with respective action class(<action input=”/input.jps”) that page will display to the user with error messages.
  • If ActionErrors object is null(doesn’t contains any ActionError/ ActionMessage) then the ActionServlet calls the execute() method of the Action class which is bind with input request (<action path=”/path” type=”package.OurActionClass”).
  • execute() method will return ActionForward object. Depend onthe value, again the ActionServlet will call the respectiveview(.jps page) as response and send it back to user.

Web.xml

<servlet>

<servlet-name>action</servlet-name>

<servlet-class> org.apache.struts.action.ActionServlet </servlet-class>

<init-param>

<param-name>config</param-name>

<param-value>/WEB-INF/struts-config.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>

<servlet-name>action</servlet-name>

<url-pattern>*.do</url-pattern>

</servlet-mapping>

<taglib>

<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>

<taglib-location>/WEB-INF/struts-html.tld</taglib-location>

</taglib>

struts-Config.xml

<action path=”/Name” type=”example.NameAction” name=”nameForm” input=”/index.jsp”>

<forward name=”success” path=”/displayname.jsp”/>

<forward name=”failure” path=”/index.jsp”/>

</action>

NameAction.java

public class NameAction extends Action {

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

String target = new String(“success”);

if ( form != null ) {

// Use the NameForm to get the request parameters

NameForm nameForm = (NameForm)form;

String name = nameForm.getName();

}

// if no mane supplied Set the target to failure

if ( name == null ) {

target = new String(“failure”);

}   else {

request.setAttribute(“NAME”, name);

}

return (mapping.findForward(target));

}

}

NameForm.java

public class NameForm extends ActionForm {

private String name = null;

public String getName() {

return (name);

}

public void setName(String name) {

this.name = name;

}

public void reset(ActionMapping mapping, HttpServletRequest request) {

this.name = null;

}

}

Index.jsp

<html>

<head>

<title>Sample Struts Application</title>

</head>

<body>

<html:form action=”Name” name=”nameForm” type=”example.NameForm”>

<table width=”80%” border=”0″>

<tr>

<td>Name:</td>

<td><html:text property=”name” /></td>

</tr>

<tr>

<td><html:submit /></td>

</tr>

</table>

</html:form>

</body>

</html>

displayName.jsp

<html>

<head>

<title>Sample Struts Display Name</title>

</head>

<body>

<table width=”80%” border=”0″>

<tr>

<td>Hello <%= request.getAttribute(“NAME”) %> !!</td>

</tr>

</table>

</body>

</html>

More info Read more…

Categories: Java Tags: , ,
Follow

Get every new post delivered to your Inbox.