D45Hub
4 years ago
5 changed files with 188 additions and 0 deletions
-
5pom.xml
-
17src/main/java/dev/dhbw/testproject/vaadintest/CatFact.java
-
24src/main/java/dev/dhbw/testproject/vaadintest/CatFactModel.java
-
2src/main/java/dev/dhbw/testproject/vaadintest/MainUI.java
-
140src/main/java/dev/dhbw/testproject/vaadintest/RestUI.java
@ -0,0 +1,17 @@ |
|||
package dev.dhbw.testproject.vaadintest; |
|||
|
|||
public class CatFact |
|||
{ |
|||
private String factString; |
|||
|
|||
public CatFact(String fact) |
|||
{ |
|||
this.factString = fact; |
|||
} |
|||
|
|||
public String getFactString() |
|||
{ |
|||
return factString; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,24 @@ |
|||
package dev.dhbw.testproject.vaadintest; |
|||
|
|||
import java.util.List; |
|||
|
|||
public class CatFactModel |
|||
{ |
|||
|
|||
private List<String> data; |
|||
|
|||
public CatFactModel() |
|||
{ |
|||
} |
|||
|
|||
public List<String> getData() |
|||
{ |
|||
return data; |
|||
} |
|||
|
|||
public void setData(List<String> data) |
|||
{ |
|||
this.data = data; |
|||
} |
|||
|
|||
} |
@ -0,0 +1,140 @@ |
|||
package dev.dhbw.testproject.vaadintest; |
|||
|
|||
import java.io.BufferedReader; |
|||
import java.io.InputStreamReader; |
|||
import java.net.HttpURLConnection; |
|||
import java.net.URL; |
|||
|
|||
import javax.servlet.annotation.WebServlet; |
|||
|
|||
import com.fasterxml.jackson.core.JsonProcessingException; |
|||
import com.fasterxml.jackson.databind.ObjectMapper; |
|||
import com.vaadin.annotations.Theme; |
|||
import com.vaadin.annotations.VaadinServletConfiguration; |
|||
import com.vaadin.server.Page; |
|||
import com.vaadin.server.VaadinRequest; |
|||
import com.vaadin.server.VaadinServlet; |
|||
import com.vaadin.shared.ui.ContentMode; |
|||
import com.vaadin.ui.Button; |
|||
import com.vaadin.ui.Label; |
|||
import com.vaadin.ui.UI; |
|||
import com.vaadin.ui.VerticalLayout; |
|||
import com.vaadin.ui.Button.ClickEvent; |
|||
import com.vaadin.ui.Button.ClickListener; |
|||
|
|||
|
|||
@Theme("mytheme") |
|||
public class RestUI extends UI |
|||
{ |
|||
|
|||
private static final long serialVersionUID = 2824052883319408801L; |
|||
|
|||
private final static ObjectMapper MAPPER = new ObjectMapper(); |
|||
|
|||
@Override |
|||
protected void init(VaadinRequest request) |
|||
{ |
|||
VerticalLayout factLayout = new VerticalLayout(); |
|||
factLayout.setId("factLayout"); |
|||
|
|||
CatFact initialCatFact = getCatFactFromService(); |
|||
|
|||
Label siteHeader = new Label("<h1>Get your dose of cat facts</h1>", ContentMode.HTML); |
|||
|
|||
Label catFactLabel = new Label("<p>" + initialCatFact.getFactString() + "</p>", ContentMode.HTML); |
|||
catFactLabel.setId("factLabel"); |
|||
|
|||
Button factRefreshButton = new Button("Get new cat fact."); |
|||
|
|||
Page.getCurrent().getStyles().add("#factLayout{padding: 30px 20%;} #factLabel{padding-bottom: 20px;}"); |
|||
factRefreshButton.addClickListener(new ClickListener() |
|||
{ |
|||
private static final long serialVersionUID = -6384557494236864977L; |
|||
|
|||
@Override |
|||
public void buttonClick(ClickEvent event) |
|||
{ |
|||
CatFact randomCatFact = getCatFactFromService(); |
|||
catFactLabel.setValue("<p>" + randomCatFact.getFactString() + "</p>"); |
|||
} |
|||
}); |
|||
|
|||
factLayout.addComponents(siteHeader, catFactLabel, factRefreshButton); |
|||
|
|||
setContent(factLayout); |
|||
} |
|||
|
|||
private CatFact getCatFactFromService() |
|||
{ |
|||
String jsonOutput = getResponseFromService(); |
|||
|
|||
CatFactModel model = parseCatModel(jsonOutput); |
|||
|
|||
CatFact output = convertToInternalModel(model); |
|||
|
|||
return output; |
|||
} |
|||
|
|||
private String getResponseFromService() |
|||
{ |
|||
String response = ""; |
|||
try |
|||
{ |
|||
|
|||
URL url = new URL("https://meowfacts.herokuapp.com"); |
|||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|||
conn.setRequestMethod("GET"); |
|||
conn.setRequestProperty("Accept", "application/json"); |
|||
if (conn.getResponseCode() != 200) |
|||
{ |
|||
throw new RuntimeException("Failed : HTTP Error code : " + conn.getResponseCode()); |
|||
} |
|||
InputStreamReader in = new InputStreamReader(conn.getInputStream()); |
|||
BufferedReader br = new BufferedReader(in); |
|||
|
|||
response = br.readLine(); |
|||
|
|||
conn.disconnect(); |
|||
|
|||
} |
|||
catch (Exception e) |
|||
{ |
|||
System.out.println("Exception in NetClientGet:- " + e); |
|||
} |
|||
|
|||
return response; |
|||
} |
|||
|
|||
private CatFactModel parseCatModel(String jsonString) |
|||
{ |
|||
CatFactModel model = null; |
|||
|
|||
try |
|||
{ |
|||
model = MAPPER.readValue(jsonString, CatFactModel.class); |
|||
} |
|||
catch (JsonProcessingException e) |
|||
{ |
|||
e.printStackTrace(); |
|||
} |
|||
|
|||
return model; |
|||
} |
|||
|
|||
private CatFact convertToInternalModel(CatFactModel model) |
|||
{ |
|||
return new CatFact(model.getData().get(0)); |
|||
} |
|||
|
|||
/** |
|||
* This is a basic Servlet which configures the URL where this page is available |
|||
* and actually deploys it to there. |
|||
*/ |
|||
@WebServlet("/rest/*") |
|||
@VaadinServletConfiguration(ui = RestUI.class, productionMode = false) |
|||
public static class RestServlet extends VaadinServlet |
|||
{ |
|||
private static final long serialVersionUID = -294459246930641751L; |
|||
|
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue