IntroductionThe pocogese-java project contains the com.philemonworks.pocogese.Servlet class which is a JEE Servlet 2.4 compliant Http Request Handler that can dispatch Command and Selection invocations. DetailsIn order to implement your Controllers for a Java Webapplication based Service, you need to: - implement your Controllers
- implement XML serialization
- add a servlet declaration in the Web Descriptor (web.xml)
- add a servlet mapping
- create an implementation of the com.philemonworks.pocogese.IServletConfigurator interface and register your Controllers for the Servlet
Implement Controllerspackage com.s3browse.web.admin.controller;
import com.philemonworks.pocogese.Controller;
import com.s3browse.domain.admin.UploadLogDao;
import com.s3browse.domain.core.UserAccountDao;
public class UserAccountController extends Controller {
private UserAccountDao userAccountDao;
private UploadLogDao uploadLogDao;
// http://localhost:8088/admin/useraccounts/service/findAll
// Selection
public List findAll() {
List accounts;
// ... fetch accounts using userAccountDao
return accounts;
}
// Command
public Reply saveAccount(UserAccount newAccount) {
// ... save new account using userAccountDao
return Reply.ok;
}
}Implement XML Serialization public class UserAccount {
public static UserAccount fromXML(String xmlString) {
// ... perform conversion, use whatever lib you like
}
public String toXML() {
// ... perform serialization, use whatever lib you like
}
}Suggestions for XML marshalling: - Jibx
- XStream
- com.philemonworks.pocogese.util.XMLWriter (writing only)
Alternatively, you can implement the controller Selection methods by returning the XML representation directly. This is useful for domain classes that cannot be extended with XML serialization behavior. In such cases, some helper class can be introduced such as UserAccountXMLIO in the example below. public String findByEmail(String email) {
Long id = userAccountDao.findIdWithEmail(email);
if (id == null) {
throw new RuntimeException("No useraccount for email:" + email);
}
UserAccount account = userAccountDao.findWithId(id);
// UserAccount is in a different project/package/domain
// we do serialization here instead of implementing toXML for UserAccount
return new UserAccountXmlIO().toXML(account);
}Servlet declaration<!-- Pocogese - Servlet -->
<servlet>
<servlet-name>pocogese</servlet-name>
<servlet-class>com.philemonworks.pocogese.Servlet</servlet-class>
<init-param>
<param-name>configurator</param-name>
<param-value>com.s3browse.web.admin.config.PocogeseConfigurator</param-value>
</init-param>
</servlet> Servlet mapping<!-- Pocogese - Mapping -->
<servlet-mapping>
<servlet-name>pocogese</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping> Implement IServletConfigurator (Pojo version)package com.s3browse.web.admin.config;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import com.philemonworks.pocogese.Controller;
import com.philemonworks.pocogese.IServletConfigurator;
import com.philemonworks.pocogese.Servlet;
import com.s3browse.admin.controllers.UserAccountController;
public class PocogeseConfigurator implements IServletConfigurator{
public void configure(Servlet servlet, ServletConfig config) throws ServletException {
servlet.configure("useraccounts", new UserAccountController() );
}
}Implement IServletConfigurator (Spring version)The following example uses Spring to wire the DAO beans into the Controller. Note that Spring is not required for Pocogese. package com.s3browse.web.admin.config;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.philemonworks.pocogese.Controller;
import com.philemonworks.pocogese.IServletConfigurator;
import com.philemonworks.pocogese.Servlet;
public class PocogeseConfigurator implements IServletConfigurator{
public void configure(Servlet servlet, ServletConfig config) throws ServletException {
ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(config.getServletContext());
servlet.configure("useraccounts", (Controller) ctx.getBean("userAccountController"));
}
}
|