Thursday, October 7, 2010

Spring 3 REST Web Services - json, xml and html

Recently I was trying to get a simple web service up and running with Spring 3. I wanted to be able to return either json, xml or html representations of the data. In order to do that I had to visit several sites and review bits and pieces of documentation. I now have something viable so I figured I would share it with others in hopes it would save them some time.

The following examples are going off of the tutorial posted here. You can download the source from the link above and update the Spring configuration based on the following. NOTE: The package names I am using are different from the original example.

Spring Configuration w/out Tiles

The following configuration assumes that you have the project setup from the URL provided above and that you have adapted the package names.

Here is how my Spring configuration file ended up looking after all my changes:



 
 
 
 
 
  
   
    gov.usda.plants.model.Employee
    gov.usda.plants.model.EmployeeList
   
  
 
 
  
 
  
   
    
    
    
   
  
  
   
    
    
     
     
     
    
   
  
  
   
    
     
    
   
  
 
 
 
  
 

Spring Configuration with Tiles The following assumes you have the WEB-INF/layouts and WEB-INF/views directories and that you have copied the employees.jsp page from the WEB-INF/jsp folder to the WEB-INF/views folder. You will also need the following files configured for tiles:
WEB-INF/views/views.xml



 

 
  
  
 



WEB-INF/layouts/layouts.xml



 
 
 
 



WEB-INF/layouts/main_template.jsp (tiles template) I won't provide content for this since it's just a template JSP page, which can be anything you want it to be.


 
 
 
 
 
  
   
    gov.usda.plants.model.Employee
    gov.usda.plants.model.EmployeeList
   
  
 
 
  
 
  
 
   
    
    
    
   
  
  
   
    
    
    
     
     
     
    
   
  
  
   
    
     
    
   
  
 
 
 
 
  
  
 
 
 
 
 
 
   
     
       /WEB-INF/layouts/layouts.xml
       /WEB-INF/views/views.xml
     
   
 
 
 
 
  
 

Testing the Application
To test the application I used the "Simple Rest Client" extension in Google Chrome. It allows you to change the header values so you can ask for different representations (json, xml, html). To do this you change the "Accept" header to either text/html, application/xml or application/json (e.g. Accept: application/xml). You can also use CURL to make HTTP requests and modify the header information.

Wednesday, September 15, 2010

Tomcat Context Configuration and JNDI

A coworker discovered a feature of Tomcat and Spring that may make some configuration easier. It turns out that it’s not just the <Resource> tags in the context.xml file that get set in the JNDI context but also <Environment> tags. So we can do something like the following in the context.xml file


Then I add the following reference in my spring xml files where I need it:


Since it’s a string, it can be assigned as a property that expects a string on any bean. This isn’t specifically a Spring solution as much as a it is a JEE solution. The JEE spec says that any items defined in the context should be stored in the JNDI context. Tomcat does this via context.xml files but you could also do it in the web.xml file. By doing it in the context.xml file you can achieve some degree of deployment independence for multiple web apps hosted in one container.

Friday, September 10, 2010

Dead simple hide/show progress bar


Problem:

Need a simple solution for hiding and showing a continuous progress bar with minimal JS.

Solution:

A small JS function and an animated GIF. Dead simple.


So, what's it look like ...

Place this JS function in the body element



Call the JS function from an "onClick" operation

When a user clicks the form button, the button will disappear and the progress animated gif will be displayed.  Ajax Load has tons of animated gifs that can be customized to fit your needs.

Friday, September 3, 2010

Rolling Tomcat Log Files

After some brief research I found that Tomcat 6 seems to already roll the log files based upon date and size; however, the catalina.out file doesn't seem to clean up. In other words, the catalina.out file continues to grow while days pass by, yet the other files seem to roll on a daily basis. The following are a couple of links I found useful in researching this issue.

Thursday, September 2, 2010

Siteminder lessons learned

Issues

  • Siteminder message bus is failing to initialize errors in event viewer
  • Page loads hanging and coming back with system cannot find the file specified
  • Duplicate LLAWP processes when site is refreshed multiple times within a certain period of time


After hours of double, triple and quadruple checking configuration we finally got to the root cause. I'm no Siteminder guru, but apparently when the agent starts up it gets it base set of configuration from some server defined in the siteminder configuration files. There are also several backup servers that can be used in the event the first one is unavailable. Well, when the first server is unavailable and the timeout is set to something fairly high (e.g. 60 sec) then page loads are slow and siteminder fails to initialize. So, being unable to reach the server was causing these issues. After the policies were changed to point to a server that was accessible, everything was happy.

Lessons Learned

Make absolutely sure all servers can be accessed from the Siteminder agent machine. Even if policy servers defined in the SmHost.conf file can be accessed that doesn't mean the initial configuration can be obtained.

Building Groovy Projects with Maven

I built a prototype project just to prove I could get Maven to build and test a Groovy project in Eclipse.  Here's what I ended up using in the pom.xml file:


 4.0.0
 com.practice
 groovy-first
 1.0-SNAPSHOT

 
  
   junit
   junit
   4.4
  
  
   org.codehaus.gmaven.runtime
   gmaven-runtime-1.7
   1.3
  
 

 
  
   
    org.codehaus.gmaven
    gmaven-plugin
    1.3
    
     
      
       generateStubs
       compile
       generateTestStubs
       testCompile
      
     
    
   
  
 

From here you can run any standard Maven goals, compile, test, package, etc.  The gmaven-plugin translates those and performs the corresponding action within the plugin.

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Unnamed - com.practice:groovy-first:jar:1.0-SNAPSHOT 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- gmaven-plugin:1.3:generateStubs (default) @ groovy-first ---
[INFO] Generated 6 Java stubs
[INFO] 
[INFO] --- maven-resources-plugin:2.4.1:resources (default-resources) @ groovy-first ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\sandbox\practice\groovy-first\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) @ groovy-first ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- gmaven-plugin:1.3:compile (default) @ groovy-first ---
[INFO] Compiled 14 Groovy classes
[INFO] 
[INFO] --- gmaven-plugin:1.3:generateTestStubs (default) @ groovy-first ---
[INFO] Generated 1 Java stub
[INFO] 
[INFO] --- maven-resources-plugin:2.4.1:testResources (default-testResources) @ groovy-first ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory C:\sandbox\practice\groovy-first\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.0.2:testCompile (default-testCompile) @ groovy-first ---
[INFO] Compiling 1 source file to C:\sandbox\practice\groovy-first\target\test-classes
[INFO] 
[INFO] --- gmaven-plugin:1.3:testCompile (default) @ groovy-first ---
[INFO] Compiled 1 Groovy class
[INFO] 
[INFO] --- maven-surefire-plugin:2.4.3:test (default-test) @ groovy-first ---
[INFO] Surefire report directory: C:\sandbox\practice\groovy-first\target\surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.practice.FileIOTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.25 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.2:jar (default-jar) @ groovy-first ---
[INFO] Building jar: C:\sandbox\practice\groovy-first\target\groovy-first-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.953s
[INFO] Finished at: Thu Sep 02 08:50:57 MDT 2010
[INFO] Final Memory: 9M/16M
[INFO] ------------------------------------------------------------------------