how to access faces context and backing beans in a servlet filter

// You need an inner class to be able to call FacesContext.setCurrentInstance
// since it's a protected method
private abstract static class InnerFacesContext extends FacesContext
{
protected static void setFacesContextAsCurrentInstance(FacesContext facesContext) {
FacesContext.setCurrentInstance(facesContext);
}
}

private FacesContext getFacesContext(ServletRequest request, ServletResponse response) {
// Try to get it first
FacesContext facesContext = FacesContext.getCurrentInstance();
if (facesContext != null) return facesContext;

FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

// Either set a private member servletContext = filterConfig.getServletContext();
// in you filter init() method or set it here like this:
// ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
// Note that the above line would fail if you are using any other protocol than http

// Doesn’t set this instance as the current instance of FacesContext.getCurrentInstance
facesContext = contextFactory.getFacesContext(servletContext, request, response, lifecycle);

// Set using our inner class
InnerFacesContext.setFacesContextAsCurrentInstance(facesContext);

// set a new viewRoot, otherwise context.getViewRoot returns null
UIViewRoot view = facesContext.getApplication().getViewHandler().createView(facesContext, “yourOwnID”);
facesContext.setViewRoot(view);

return facesContext;
}

Advertisements

Implementing message level security in web service using XWSS

Hi All,

I started learning web services and implementing security into it. So i thought of sharing my experience with you.

I created a contract first web service, based on spring web service (more detail can be found on spring website) and implemented  a simple client using spring integration. When we do not have security implemented with WS then it is quiet easy to create and use, but once security comes then ohh my god. ewww..  what a rock ………….!!!!!!!!!!!!!!

We can implement security into web service 3 ways. Here I am trying to explain XWSS (XML web service security) implementation with spring web service. To implement xwss we need seciryt policy configuration, one interceptor which intercepts each incoming soap request and key store file if you are encrypting and decrypting soap request and response. Here i have taken example of key store files because in in this sample i am encrypting soap request at client side, decrypting soap incoming request using security policy and private key, processing the request, encrypting response, sending encrypted response back to client and decrypting response to see actual response at client end.

Basically security policy files are responsible for soap message encryption and decryption. <xwss: sign /> tag signs your message and <xwss:encrypt /> tag encrypts the message and <xwss:requiresEncryption /> tag decrypts encrypted message.

key store files can be generated using keytool utility. please see attached txt file for help.

I am attaching source code for client and web server.

Message Level Security

 

Creating a custom measure in sonar

Hi All,

I was working on sonar analysis and created a custom measure. (Well I have used reference from sonar repository).

I thought of sharing information on sonar, how we can add a new measure and customize the analysis.

Please refer to the document attached.Sonar Plugin

More plugins can be found at : http://svn.codehaus.org/sonar-plugins/trunk

 

Porting Web application to GGTS IDE

Hello,

When we port any web application into GGTS, sometimes build is not successful using IDE. We just simply add our application to sever and publish it but alas!  😦 Our application is not deployed properly into deployment directory of server.

I have faces this sever issue for long time then i looked into .settings folder and could see in ‘org.eclipse.wst.common.component’ file we have options to set our build option to copy our resources from source to destination deployment directory.

Here you can add your properties.

In case your jar files are not copied to destination and you are using maven for build then you will have problem to provide source folder name, so here is the workaround.

Build your application using maven and find the path of lib folders then give this folder as source.

sample:  <wb-resource deploy-path=”/WEB-INF/lib” source-path=”/target/{context-name}/WEB-INF/lib” />

Sunil

 

Creating a JNDI name on IBM WAS

Hi Mikael,

Here are the steps to setup JNDI name for WAS.
This JNDI name must match name given in MASHAService\src\META-INF\persistence.xml

Open WAS admin console.
Click on Resources->JDBC-JDBC Providers
Click New Button after selecting a proper scope.

Give DB2 Jars path. Containing d22jcc.jar, db2jcc_license_cu.jar and db2jcc_license_cisuz.jar

Now Click on Resources->JDBC->Data Sources. Click on data source name that you have created

Now Click on JAAS – J2C authentication data and click on New and fill in all the details
J2c Authentication data created, will look like given below .

Go Back to data source name that you have created earlier.
Fill in the settings, authentication alias, mapping configuration alias, and data source properties as show below.

Click ok and then Need to Save entire configuration by clicking on save link that will appear on top (middle) of the screen, (this is called master configuration).
After saving select data source name and click on Test connection. If it is successful then the data source can be used in the application running on WAS.


Regards,
Sunil Chauraha

Accepting multiple date formats using rich calendar

Accepting multiple date formats using rich calendar

https://community.jboss.org/wiki/AcceptingMultipleDateFormatsInRichcalendar

Preventing cloning of Object in while using singleton design pattern

Hi All,

Using singleton design pattern is a best way to access a valuable resource. In this case we do use Singleton design pattern and stops clients classes to create multiple instances of the Class.

But there is a loophole in this pattern and one can create instance of the Singleton class using object cloning.

So we should prevent cloning of object in this class as well if required.

Code given below gives a brief idea of how to stop client class to clone an object.

package com.sun;

/** * @author Sunil Chauraha */

class Clone1 implements  Cloneable{

@Override protected Object clone() throws CloneNotSupportedException

throw new CloneNotSupportedException(“Cloning of this class is not supported by me…”);

}
public class CloneTest  {

public static void main(String[] args) {

try{

Clone1 clone1 = new Clone1();

Clone1  clone2 = (Clone1)clone1.clone();

}catch (CloneNotSupportedException e) {

System.err.println(e.getMessage()+”: “+e);

}

}

}

Java interview questions

1. Define your profession and carrer skills.
2. What is the difference between struts and jsf.
3. What the scope of application and request.
4. What is AJAX.
5. Where will you rate yourself in SQL.
6. Do you know any design pattern? if yes describe.
7. What is value binding and component binding.
8. Define lifecycle of richfaces.
9. How does a request completes?
10. What is jsf components?
11. What is component and events in jsf?
12. What did you do for performance inprovement?
13. What is IOC?
14. What is transaction management?
15. What is the life cycle of spring bean?
16. How will you configure spring and hibernate?
17. What is sessionfactory and session?
18. What is generics?
19. what is run time polymerphism?
20. how will you integrate spring in jsf?
21. what is hibernate?
22. what is difference between load and get in hibernate?
23. what is save, update and merge?
24. what is factory pattern?
25. what is spring mvc?
26. how will you integrate hibernate with spring?
27. what is aop?
28. what is criteria?
29. What is criteria and detached criteria?
30. one sql query for group by operation.
31. select second highest salary of an employee from emp table.
32. What is difference between struts and spring.
33 what is difference between jsp and servlet.
34 what is the life cycle of jsp.
35 what is the difference between requestForward method of servlet context and servlet config.

36. How we can instantiate a bean of abstract class having 2 subclasses.

37. How can we maintain a collection having the same order of insertion and unique values of object.

38. How can we use JPA in spring.

39. We have a singleton class and constructor is private, how can we create a bean in spring.

40. What is the mechanism used by Java for maintaining set and HashSet objects.

41. What is the significance of equals and hashCode methods in java and object comparison.

Latest Questions on 16th June 2012.

1. hibernate object joins (per concret class…. etc)
2. list, bag, set, hashmap use in hibernate.
3. How to prevent concurant modification in hibernate.
4. if “null”  is showing in jsp text box. how we can prevent it.
5. how to set initParams in jsp.
6. How to handle runtime exception in jsp.
7. how to show exception on console from jsp.
8. can we pass instances in stateless session bean.
9. what are the importent methods of httprequest object.
10. what is synchronizer token.
11. how do we prevent multiple request submission in struts.
12. what is difference between perform() and execute();
13. What is signifance of SessionFactory in hibernate and Spring.
14. what is action-mapping in struts.
15. how do we pass request from one servlet to another servlet withing the application.
16. How do we use cookies.
17. Define all the operation of Cookies. add, delete, read, upddate and send.
18. How to write jsp custom tag.
19. What is synchronization.
20. What is sql joins.
21. how to get n’th  greatest elemebt from a table.

Ans: select salary from employee e1 where (N-1) = (select count(distinct(e2.salary)) from employee e2 where e2.salary > e1.salary) (N stands for count, which we want to retrieve)
22. What is upcasting and down casting.
23. what is bounded and unbounded in java generics.
24. What is agreegation in java.