Skip to content

Instantly share code, notes, and snippets.

@edm00se
Last active February 9, 2016 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edm00se/15249dba8ff3fea38312 to your computer and use it in GitHub Desktop.
Save edm00se/15249dba8ff3fea38312 to your computer and use it in GitHub Desktop.
My proof of concept that Java on the server, along with RESTful APIs, can be an impressive and easy way of managing business applications (this uses a standard Domino Java Code element with an XPage as the front-end). Package is defined with a single class and single (static) method, to return the data from a simple REST call, fully on the serve…
package com.eric.restful;
import java.net.URL;
import java.net.URLConnection;
import java.io.BufferedReader;
import com.google.gson.*;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.net.MalformedURLException;
import org.apache.commons.validator.routines.*;
/**
* Class with a single, public, static method to provide a REST consumer
* which returns data as a JsonObject.
*
* @author Eric McCormick, @edm00se
*
*/
public class CustRestConsumer {
/**
* Method for receiving HTTP JSON GET request against a RESTful URL data source.
*
* @param myUrlStr the URL of the REST endpoint
* @return JsonObject containing the data from the REST response.
* @throws IOException
* @throws MalformedURLException
* @throws ParseException
*/
public static JsonObject GetMyRestData( String myUrlStr ) throws IOException, MalformedURLException {
JsonObject myRestData = new JsonObject();
try{
UrlValidator defaultValidator = new UrlValidator();
if(defaultValidator.isValid(myUrlStr)){
URL myUrl = new URL(myUrlStr);
URLConnection urlCon = myUrl.openConnection();
urlCon.setConnectTimeout(5000);
InputStream is = urlCon.getInputStream();
InputStreamReader isR = new InputStreamReader(is);
BufferedReader reader = new BufferedReader(isR);
StringBuffer buffer = new StringBuffer();
String line = "";
while( (line = reader.readLine()) != null ){
buffer.append(line);
}
reader.close();
JsonParser parser = new JsonParser();
myRestData = (JsonObject) parser.parse(buffer.toString());
return myRestData;
}else{
myRestData.addProperty("error", "URL failed validation by Apache Commmons URL Validator");
return myRestData;
}
}catch( MalformedURLException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}catch( IOException e ){
e.printStackTrace();
myRestData.addProperty("error", e.toString());
return myRestData;
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<xp:view
xmlns:xp="http://www.ibm.com/xsp/core">
<xp:this.beforePageLoad><![CDATA[#{javascript://import Java package
importPackage(com.eric.restful);
var urlStr = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&q=";
urlStr += "http://www.npr.org/rss/rss.php?id=1001";
var myStuff:JsonObject = CustRestConsumer.GetMyRestData(urlStr);
viewScope.myResp = fromJson(myStuff);}]]></xp:this.beforePageLoad>
<xp:dataTable
id="dataTable1"
rows="30"
var="rowData"
indexVar="rowCount">
<xp:this.value><![CDATA[#{javascript:var obj = viewScope.get("myResp");
return obj.responseData.feed.entries;}]]></xp:this.value>
<xp:column
id="column1">
<xp:this.facets>
<xp:panel
xp:key="header"
tagName="h1">
<xp:link
escape="true"
id="link1">
<xp:this.text><![CDATA[#{javascript:var obj = viewScope.get("myResp");
return obj.title;}]]></xp:this.text>
<xp:this.value><![CDATA[#{javascript:var obj = viewScope.get("myResp");
return obj.link;}]]></xp:this.value>
</xp:link>
</xp:panel>
</xp:this.facets>
</xp:column>
<xp:column
id="column2">
<xp:panel
tagName="h3">
<xp:link
escape="true"
id="link2"
text="#{javascript:rowData.title}"
value="#{javascript:rowData.link}">
</xp:link>
</xp:panel>
<xp:br></xp:br>
<xp:text
escape="false"
id="computedField2"
value="#{javascript:rowData.content}">
</xp:text>
<xp:br></xp:br>
</xp:column>
<xp:column
id="column3">
<xp:text
escape="true"
id="computedField3"
value="#{javascript:rowData.publishedDate}">
</xp:text>
</xp:column>
</xp:dataTable>
</xp:view>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment