Posts

Using a NavigiationHandler to do HTTP/HTTPS switching

In a project I was working on, we wanted the log in page and anything to do with payment to be secure using HTTPS, whilst the majority of the site would be unsecure using HTTP. JSF 1.2 doesn't really give you anyway of doing this other than manually specifying the full url for every single link and button on the site. This removes a lot of the flexibility and simplicity of JSF. I came up with a pretty good solution. For any link or action that you know will switch between secure and unsecure, which isn't that hard to know (if you design the site with that in mind), then you configure it to be a redirect. Then you create your own navigation handler which applies rules you create yourself, to determine which parts should be secure or not. This is a very simple solution that when a redirect occurs, it checks the path of the destination page (its viewId). If it starts with "/secure" it gives a full HTTPS url and for all others, it gives a full HTTP url. ...

Updating the nextval of all sequences in PostgreSQL

If you've been doing SQL level inserts for testing purposes, then when you go to use the sequences then they will have a nextval that already exists in your table. So below is a script which updates the nextval for all fields that were created using bigserial/serial: /* Updates all the sequences to have a next value of max+1 excluding the list passed */ CREATE OR REPLACE FUNCTION fn_fixsequences(excludes text) RETURNS integer AS $BODY$ DECLARE themax BIGINT; mytables RECORD; num integer; BEGIN num := 0; FOR mytables IN select relname, ns.nspname, a.attname, pg_get_serial_sequence(c.relname, a.attname) as seq FROM pg_catalog.pg_attribute a INNER JOIN pg_catalog.pg_class c ON c.oid=a.attrelid inner join pg_catalog.pg_attrdef d on d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef LEFT JOIN pg_catalog.pg_namespace ns ON ns.oid = c.relnamespace WHERE pg_catalog.pg_table_is_visible(c.oid) and a.attnum > 0 AND NOT a.attisdropped and a...

Getting JBoss Scheduler working

When using the @Service annotation together with the scheduler has given me no end of trouble so I'd like to share my working configuration. It took me a while to get the objectName right. If your not using @Service(objectName='name') but instead using @Service(name='name') then the final object name is not the same as what they imply at EJB Extensions Help . Instead I'd recommend going into the JMX console of the running server and searching for it manually, listed under 'jboss.j2ee' for the name you've specified AND type=ManagementInterface . As well as defining the @Service, you'll also need to define the @Management. @Management(SampleServiceMBean.class) @Service(name = SampleServiceMBean.SERVICE_NAME) public class SampleServiceMBean implements SampleService { public final static String SERVICE_NAME = "SampleServiceMBean"; public void process(ObjectName name, Date date) { log.info("process {} {}", name, da...

Disabling RichFaces FacesBeanValidator

If you use the rich:ajaxValidator, for example: <rich:ajaxValidator event="onblur" /> It automatically adds a FacesBeanValidator to the components list of validators. If you don't have javax.validation or Hibernate Validator setup then you'll get the following exception: 2010-01-07 00:29:19,889 [http-8443-1] WARN org.richfaces.validator.ObjectValidator - Hibernate Validator could not be instantiated, use stub instead javax.validation.ValidationException: Unable to find a default provider at javax.validation.Validation$GenericBootstrapImpl.configure(Validation.java:248) at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:115) at org.richfaces.validator.BeanValidator. (BeanValidator.java:34) at org.richfaces.validator.ObjectValidator.createInstance(ObjectValidator.java:54) at org.richfaces.validator.ObjectValidator.getInstance(ObjectValidator.java:85) at org.richfaces.validator.FacesBeanValidator.validate(FacesBeanValidator.java:10...

Friendly paths to access dynamic JSF content

JSF 1 is HTTP POST based and, as such, makes it hard to generate friendly/RESTful URLs that are meaningful to the user. Try to serve something like a content management system (CMS) using JSF and you immediately run into problems. However if you write a custom ViewHandler , you can translate the virtual url, such as /page/about/contactUs , into a physical page (view), e.g. /dynamicContent.xhtml . All cases where the ViewHandler references the viewId (which is just the full filename of the page its executing/referencing), need to be mapped correctly so that JSF opens the page that exists, while the user sees the content specified by the URL that maps to a database for example. Below is the view handler that wraps around the Facelets view handler to provide the described functionality.: /** * View Handler that wraps around first Facelets then the default handler, to translate the virtual address of the content page to the physical xhtml * page that will serve it. * * @author Ch...

Undeploying Google Guice jar file locked

When trying to undeploy an webapp that uses Google Guice from an webapp server like Tomcat, you may notice that it doesn't undeploy properly. The problem seems to be related to com.google.inject.internal.Finalizer , which is a Thread created to clean up after Guice is finished. However the thread doesn't stop properly. My solution is to modify this class (by just adding that individual source file to the project as I don't want to rebuild the whole project) adding a stop method that can be call from a ServletContextListener.contextDestroyed() . My modified Finializer: private boolean stop = false; /** * Loops continuously, pulling references off the queue and cleaning them up. */ // @SuppressWarnings("InfiniteLoopStatement") @Override public void run() { try { while (!stop) { try { cleanUp(queue.remove()); } catch (InterruptedException e) { /* ignore */ } } } catch (ShutDown shutDown) { /* ignore */ } } public void stopThr...

Actions not being called when using 2 view tags

I had a problem where a command button's action was not being called on the managed bean. When using tiles inside JSF, you may have it set up so that you have a template.jsp and a body.jsp. The template.jsp, using tiles inserts body.jsp. template.jsp defines a <f:view> tag as per normal. Inside body.jsp, whilst you don't technically need a <f:view> tag, you may get some component (namely f:phaseListener ) complaining about not having a view root, so you does it asks and defines the <f:view> at the start of the file. You load up the page, everything renders correctly and you click a submit button. The form validation errors appear as normal, so you fill in some values and click submit again. This time, NOTHING. So you try turning on all the logging and still nothing seems wrong. This happened to me. I wasted quiet a few hours on this one: working out how to log things correctly using eclipse/tomcat and get Java Util Logging working etc. After a ton of breakp...