0

there's a RESTful service which is Producing text/plain and I want to add that content into my JSF page. Is that possible? Is there a tag for that?

coubeatczech
  • 5,892
  • 7
  • 43
  • 71

1 Answers1

0

If you're using JSP as view technology, you can use the JSTL <c:import> for this. It's as simple as

<c:import url="http://example.com/service" />

It however doesn't run in sync with JSF as you'd expect from the coding. Also, this is not supported in Facelets. So if you want to render/display it conditionally (and thus run in sync with JSF) or are using Facelets, then you'll have to do this in the managed bean. To start, you can use java.net.URL and consorts to get content from an URL.

InputStream input = new URL("http://example.com/service").openStream();
Reader reader = new InputStreamReader(input, "UTF-8"); // You may want to verify charset based on response headers.
StringBuilder builder = new StringBuilder();
for (int data; (data = reader.read()) > -1;) {
    builder.append((char) data);
}
String result = builder.toString();

Put this in the constructor or action method, depending on the conditions it needs to execute. You can however also wrap this all in flavor of a standalone custom/composite component or EL function.

Related questions:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452