Commit 7b3330ce authored by Joel Widmer's avatar Joel Widmer 💬

Cache the api response, while the bean is alive

parent efd9cdf7
......@@ -4,7 +4,6 @@ import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
......@@ -25,7 +24,7 @@ public class ApiControlBean {
private Client client;
private Gson gson = new Gson();
private List<EntryBean> entries = new ArrayList<EntryBean>();
private List<EntryBean> entries = null;
public ApiControlBean(){
ClientConfig config = new ClientConfig().connectorProvider(new ApacheConnectorProvider());
......@@ -33,12 +32,15 @@ public class ApiControlBean {
}
public List<EntryBean> getEntries() {
System.out.println("Requesting data");
Response response = client.target(BASE_URL).path(ALL_USERS).request(MediaType.APPLICATION_JSON_TYPE).get();
response.bufferEntity();
System.out.println("Parsing response");
InputStreamReader inputStreamReader = new InputStreamReader((InputStream) response.getEntity());
this.entries = gson.fromJson(inputStreamReader, new TypeToken<ArrayList<EntryBean>>(){}.getType());
if(entries == null) {
System.out.println("Requesting data");
Response response = client.target(BASE_URL).path(ALL_USERS).request(MediaType.APPLICATION_JSON_TYPE).get();
response.bufferEntity();
System.out.println("Parsing response");
InputStreamReader inputStreamReader = new InputStreamReader((InputStream) response.getEntity());
this.entries = gson.fromJson(inputStreamReader, new TypeToken<ArrayList<EntryBean>>() {
}.getType());
}
return entries;
}
......
package beans;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
@ManagedBean
public class EntryBean {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment