2

I am building a java EE application that implements JSF as the frontend framework. The application needs to consume a RESTFul Service (which I own but hosted as a separate application).

I have found some decent tutorials about implementing Restlet, however I am still missing the final leg. The user should be able to click a button and a POST is submitted by a local REST Client to the server and the response evaluated. I'd appreciate advice about how I can achieve this.

  • I personally would develop a service interface to access the REST functions you're interested in. Then, implement it using a REST client to access the service. Then, develop your JSF bean the common way and provide it access to the service. BTW, how do you want to manage authorizations? – Aritz Sep 03 '15 at 20:02
  • *"A **local** REST client"* Huh? Where exactly? The way how you formulated the question gives the impression that you want to use JavaScript in client side for that, not Java in server side. In any case .. This is food for read and likely a duplicate: http://stackoverflow.com/questions/29982657/how-to-implement-jax-rs-restful-service-in-jsf-framework – BalusC Sep 03 '15 at 21:36

2 Answers2

2

Use Apache HttpClient from JSF Managed Beans.

@ManagedBean(eager = true, name = "restAuthBean")
@SessionScoped
public class RESTAuthBean implements Serializable {
 private CloseableHttpClient CLIENT = HttpClients.createDefault();

 public UserModel saveUser(UserModel model) {
        try {
             CLIENT = HttpClients.createDefault();
            LOGGER.debug("RESTAuthBean: save user called ");
            HttpPost request = new HttpPost(AUTH_SERVICE_PATH + "user/register");
            JSONObject json = new JSONObject();
            json.put("firstname", model.getFirstname());
            json.put("lastname", model.getLastname());
            json.put("ip", model.getIp());
            json.put("email", model.getEmail());
            json.put("password", PasswordUtil.hashPassword(model.getPassword()));
            StringEntity params = new StringEntity(json.toString(), "UTF-8");
            request.addHeader("content-type", "application/json;charset=UTF-8");
            request.addHeader("charset", "UTF-8");
            request.setEntity(params);
            HttpResponse response = (HttpResponse) CLIENT.execute(request);
            HttpEntity entity = response.getEntity();
            ObjectMapper mapper = new ObjectMapper();
            model = mapper.readValue((EntityUtils.toString(entity)), UserModel.class);
        } catch (IOException | ParseException ex) {
            LOGGER.debug("RESTAuthBean: save user error " + ex.getLocalizedMessage());
        }
        return model;
    }
Armen Arzumanyan
  • 1,939
  • 3
  • 30
  • 56
0

Thank you for the input. I was struggling with deciding where/how to implement the REST client in application-A (JSF) that integrates to application-B (REST, no GUI).

My decision was to implement it in the service layer via EJB as suggested by Xtreme Biker, and expose my controller methods using JAXB for other apps that are not based on JSF as in How to implement JAX-RS RESTful service in JSF framework

To clarify further authorizations are also quite involved but it will be a combination of 3rd party api and java security framework.

Community
  • 1
  • 1