Sorry, I am just starting with all this EJB, JSF and JAX-RS stuff and need your help now.
I have created a JAX-RS Resource class, which works very well and implements @GET, @PUT, etc. Methods.
In the same project I now created a JSF page with the according BackBean. This Backbean should talk to the REST interface. While testing, I hardcoded the URI of the REST Interface into the bean, but of course I would like to get the URI programmaticly. I tried with a @Produces method and injection, but I always get an IllegalStateException. I think this has to do with contexts, but I actually do not have the understandning to solve it.
My REST Resource:
@Path("task")
@ManagedBean
@RequestScoped
public class TaskResource {
@Context
private UriInfo context;
@Inject TaskLifecycle lc;
public TaskResource() {
}
@GET
@Path("{id}")
public Response getTask(@PathParam("id") String id) { ... etc.
My Backbean:
@ApplicationScoped
@LocalBean
@Named("tmmlWrapper")
public class TmmlTaskWrapperBean implements Serializable {
// Here another ManagedBean is injected, which works fine!
@Inject TaskLifecycle lc;
and finally my JSF Page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Tasklist</title>
</h:head>
<h:body>
<h:form>
<h:outputLabel ><h3>Tasklist:</h3></h:outputLabel>
<h:dataTable value="#{tmmlWrapper.taskList}" var="tl">
<h:column>
<f:facet name="header">ID</f:facet>
#{tl.id}
</h:column> ... and so on ... etc.
My question:
How can my BackBean get the URI of the REST resource (ex. “http://exampledomain:8080/as”)?
Any help is welcome!
Cheers,
Joern
You’ll first need to get access to the underlying servlet container (assumed to one, instead of a portlet container) produced HttpServletRequest object. Use the
FacesContextobject to access the HttpServletRequest object in the following manner:The
HttpServletRequestclass provides several utility methods to obtain a near representation of the original request:getRequestURL(), which provides the original request sans the query stringgetScheme,getServerName,getServerPort,getContextPath,getServletPath,getPathInfoandgetQueryStringall of whose outputs can be combined in sequence to obtain the original request. You may have to omit the latter invocations if you want a lesser fragment of the URL.