Can PrettyFaces be used in Quartz job?

Splash Forums PrettyFaces Users Can PrettyFaces be used in Quartz job?

This topic contains 3 replies, has 2 voices, and was last updated by  Christian Kaltepoth 2 years, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #24779

    Extak
    Participant

    I have JavaEE application with Quartz job. The job sometimes sends some emails and i need to generate some URLs to put them into this emails. Quite common issue I suppose.

    Is there way to generate URLs in Quartz job without any HttpRequest?

    #24780

    That do you mean with “generate URLs”? What’s the exact use case? The typical way to generate pretty URLs from non-pretty URLs is to use HttpServletResponse.encodeURL(), but this won’t be possible from outside of a request.

    #24792

    Extak
    Participant

    I’ve solved it 🙂

    I’m using something like this:

    
    // request -> HttpServletRequest
    PrettyContext context = PrettyContext.getCurrentInstance(request);
    UrlMapping mapping = context.getConfig().getMappingById(mappingId);
    PrettyURLBuilder builder = new PrettyURLBuilder();
    String targetURL = builder.build(mapping, false, params);
    

    I can construct this way URL anywhere in webapp. I’m using Spring and when a job is a bean, i can use this:

    
    public class SomethingJob extends QuartzJobBean{
        @Inject
        private ServletContext servletContext;
    
        @Override
        protected void doExecute(ApplicationContext ctx, JobExecutionContext jobContext){
            ...
            MockHttpServletRequest request = new MockHttpServletRequest();
            MockHttpServletResponse response = new MockHttpServletResponse();     
    
            URL url = ... absolute URL of my application
     
            request.setRemoteAddr(url.getHost());
            request.setServerName(url.getHost());
            request.setRemotePort(url.getPort());
            request.setServerPort(url.getPort());
    
            PrettyFilter prettyFilter = new PrettyFilter();
            try {
                prettyFilter.init(new MockFilterConfig(servletContext));
                prettyFilter.doFilter(request, response, new MockFilterChain());            
            } catch (Exception ex) {
                ...
            }
    
            // Construct URL by PrettyFaces with code mentioned above
    
            // Place generated URL to email body and send it
        }
    }
    

    This workaround is functional, but is there a better way?

    #24793

    I think the PrettyConfig is available as a servlet context attribute. So something like this could also work:

    
    ServletContext context = ...;
    PrettyConfig config = context.getAttribute(PrettyContext.CONFIG_KEY);
    UrlMapping mapping = config.getMappingById(mappingId);
    PrettyURLBuilder builder = new PrettyURLBuilder();
    String targetURL = builder.build(mapping, false, params);
    

    Perhaps a bit simpler. But I’m not sure if this also works. 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

Comments are closed.