Friday, November 11, 2011

Exporting / Generating CSV Files in Java

I have had to generate CSV files in several projects in last few months and I have settled on using OpenCSV library as it make the task very simple. All you need is the opencsv.jar and a few lines of code.


Don' forget to click the +1 button below if this post was helpful.

Reading All Items from an Amazon SimpleDB Query Result

As you may have noticed select queries in SimpleDB have a maximum limit to the number of results returned and you will have to use the nextToken mechanism to get all results from a query. A solution was posted on aws forum but since I have modified it a bit, thought I will post it here again.



The variable sdb is the AmazonSimpleDB implementation class which in my case is the AmazonSimpleDBClient initialized and injected using Spring. Following is the relevant Spring configuration section (I am initializing an AmazonS3Client too which is not required if you are only using SimpleDB).


AWS ID and Secret values are read from the aws.properties file in the classpath, which looks like below.







Don' forget to click the +1 button below if this post was helpful.

Friday, December 7, 2007

Changing View JSP In doView Of A Portlet With JSF

I'm playing around with JBoss portal server, Portlets and JSF these days and I had to change the view of a portlet when a value in database changed. The value is changed from another portlet and the portlet I was developing had to look for the change every time it was rendered and reset it self if the value had changed. Looked simple enough but I had to waste a good part of a day to find a good solution.

I'm new to all these technologies and depend on Google to find me solutions but I couldn't find anything that helped me to do this with Sun Reference Implementation of JSF. I found one that described how to do it in IBM implementation of JSF which obviously didn't work on Sun RI. The problem is you just can't redirect to a JSP in the doView method if you are using JSF. I tried to do it by overriding the doView method in FacesPortlet.

Then I found this article on JSF Central which described how to redirect a user to a login page if the user is not logged in and that's exactly what I wanted. All I had to do was

  1. Write a PhaseListener and in the beforePhase method do my database check and set the view ID to my start page view id if the value had changed.
  2. Change the getPhaseId method to return PhaseId.RENDER_RESPONSE which makes the PhaseListner fire every time a page is rendered. The PhaseId.RESTORE_VIEW event is not fired every time a portlet is rendered.
  3. Configure the PhaseListener in faces-config.xml
That was it, pretty easy and would have been if I knew a little bit more about JSF :)

public class DBPhaseListener implements PhaseListener {

public void beforePhase(PhaseEvent event) {
FacesContext fc = event.getFacesContext();

System.out.println("Current View ID" + fc.getViewRoot().getViewId());
if (changed()) {
System.out.println("Changing view");
NavigationHandler nh = fc.getApplication().getNavigationHandler();
nh.handleNavigation(fc, null, "main");
}
}

private boolean changed() {
// code to check db changes
}

public void afterPhase(PhaseEvent event) {
}

public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
}


And the faces-config.xml

<faces-config>
<lifecycle>
<phase-listener>com.test.DBPhaseListener</phase-listener>
</lifecycle>

<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>main</from-outcome>
<to-view-id>/WEB-INF/jsp/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

<!-- rest of the faces-config -->
</faces-config>

Don' forget to click the +1 button below if this post was helpful.

Tuesday, September 25, 2007

Testing HTTPS Secured Actions with StrutsTestCase

If you have secured some Struts actions using SSLEXT's Struts plugin (org.apache.struts.action.SecurePlugIn) you will have to invoke the action over https, else SSLEXT will redirect to the configured HTTPS port.

So in order to test secured actions using MockStruts test cases you need to inform SecurePlugIn that the request came over https. This is quite easy as the HttpServletRequestSimulator class that's used by MockStruts has a setScheme method. All you have to do is set the scheme to HTTPS in the inherited request object before calling the actionPerform method in the test class.

e.g.


public class LoginTest extends MockStrutsTestCase {
public void testlogin() {
setConfigFile("/WEB-INF/struts-config.xml");
setRequestPathInfo("/login");
addRequestParameter("username", "test");
addRequestParameter("password", "test1234");

//set the request's scheme to HTTPS
request.setScheme("HTTPS");
//invoke action
actionPerform();

verifyNoActionErrors();
verifyForward("success");
}
}

Don' forget to click the +1 button below if this post was helpful.