Created
April 19, 2013 16:30
-
-
Save gsmet/5421471 to your computer and use it in GitHub Desktop.
Generate Wicket URL from a batch context
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private String buildUrl(String applicationName, IRequestHandler requestHandler) { | |
Args.notNull(applicationName, "applicationName"); | |
Args.notNull(requestHandler, "requestHandler"); | |
RequestCycle requestCycle = getRequestCycle(applicationName); | |
String url = requestCycle.getUrlRenderer().renderFullUrl(Url.parse(requestCycle.urlFor(requestHandler))); | |
if (ThreadContext.getSession() == null) { | |
ThreadContext.detach(); | |
} | |
return url; | |
} | |
private RequestCycle getRequestCycle(String applicationName) { | |
if (RequestCycle.get() == null) { | |
WebApplication application; | |
if (WebApplication.exists()) { | |
application = WebApplication.get(); | |
} else { | |
application = (WebApplication) WebApplication.get(applicationName); | |
} | |
RequestCycle requestCycle = createDummyRequestCycle(application); | |
ThreadContext.setRequestCycle(requestCycle); | |
} | |
return RequestCycle.get(); | |
} | |
private RequestCycle createDummyRequestCycle(WebApplication application) { | |
final ServletContext context = application.getServletContext(); | |
final HttpSession newHttpSession = new MockHttpSession(context); | |
final MockHttpServletRequest servletRequest = new MockHttpServletRequest(application, newHttpSession, context) { | |
@Override | |
public String getServerName() { | |
return configurer.getSchedulerUrlBuilderServerName(); | |
} | |
@Override | |
public int getServerPort() { | |
return configurer.getSchedulerUrlBuilderServerPort(); | |
} | |
@Override | |
public String getScheme() { | |
return configurer.getSchedulerUrlBuilderScheme(); | |
} | |
@Override | |
public String getContextPath() { | |
return context.getContextPath(); | |
} | |
@Override | |
public String getServletPath() { | |
return "/"; | |
} | |
}; | |
final MockHttpServletResponse servletResponse = new MockHttpServletResponse(servletRequest); | |
servletRequest.initialize(); | |
servletResponse.initialize(); | |
final ServletWebRequest webRequest = new ServletWebRequest(servletRequest, servletRequest.getFilterPrefix()); | |
final WebResponse webResponse = new BufferedWebResponse(new ServletWebResponse(webRequest, servletResponse)); | |
return application.createRequestCycle(webRequest, webResponse); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment