Last active
August 29, 2015 14:05
-
-
Save edm00se/ce4206cf3daff409b8f3 to your computer and use it in GitHub Desktop.
XAgent Java FacesContext response JSON data class.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.eric.test; | |
import javax.faces.context.ExternalContext; | |
import javax.faces.context.FacesContext; | |
import javax.faces.context.ResponseWriter; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.util.Map; | |
import lotus.domino.*; | |
import com.ibm.xsp.model.domino.DominoUtils; | |
import com.google.gson.JsonArray; | |
import com.google.gson.JsonObject; | |
/** | |
* Data provider Class with a single, public, static method | |
* to provide an XAgent micro-service, returning formatted | |
* data as application/json. | |
* | |
* @author Eric McCormick, @edm00se | |
* | |
*/ | |
public class DataProvider { | |
/** | |
* This method performs some sample actions against | |
* a Domino View's Documents, reads them into a | |
* JsonArray, attaches it to the JsonObject response | |
* and returns it as a data response via FacesContext. | |
* This should be invoked as part of an XAgent. | |
* | |
* @return JsonObject sample response | |
* @throws IOException | |
*/ | |
public static void GetMyDataAsJson() throws IOException{ | |
//initialize the main JsonObject for the response | |
JsonObject myData = new JsonObject(); | |
/* | |
* Here we're establishing our external context handle, | |
* where we get our response writer from. | |
*/ | |
FacesContext ctx = FacesContext.getCurrentInstance(); | |
ExternalContext exCon = ctx.getExternalContext(); | |
/* | |
* Using a response writer is one way of directly dumping into the response. | |
* Instead, I'm returning the JsonObject. | |
*/ | |
ResponseWriter writer = ctx.getResponseWriter(); | |
HttpServletResponse response = (HttpServletResponse) exCon.getResponse(); | |
//set my content type, use a robust character encoding, and don't cache my response | |
response.setContentType("application/json"); | |
response.setHeader("Cache-Control", "no-cache"); | |
response.setCharacterEncoding("utf-8"); | |
try { | |
/* | |
* This is how we can get a handle on and use any URL parameters | |
* instead of the Domino SSJS param handle. Note that I check | |
* for the existence of the the parameter of myKey before assigning | |
* it, via ternary operator. | |
*/ | |
Map<String,Object> exConP = exCon.getRequestParameterMap(); | |
String myParam = (exConP.containsKey("myKey")) ? exConP.get("myKey").toString() : null; | |
/* | |
* Using the Domino Session class, we can get a handle on our current | |
* session and interact with anything via the Java NotesDomino API. | |
*/ | |
Session s = DominoUtils.getCurrentSession(); | |
Database db = s.getCurrentDatabase(); | |
View vw = db.getView("GoTCharFlat"); | |
/* | |
* perform any necessary business logic with the data | |
*/ | |
//creating an array of objects | |
JsonArray dataAr = new JsonArray(); | |
/* | |
* This is an example only as there are easier ways to | |
* get a JSON response of a View; e.g.- Domino Data/Access Services. | |
*/ | |
Document first = vw.getFirstDocument(); | |
//simple View iteration of documents and adding of a given value | |
while(first!=null){ | |
//creates current object | |
JsonObject curOb = new JsonObject(); | |
String name = first.getItemValueString("CharFullName_FL"); | |
String title = first.getItemValueString("Title"); | |
curOb.addProperty("name", name); | |
curOb.addProperty("title", title); | |
//adds current object into JsonArray | |
dataAr.add(curOb); | |
//no OpenNTF Domino API implemented, ham fist away! | |
Document tmpDoc = vw.getNextDocument(first); | |
first.recycle(); | |
first = tmpDoc; | |
} | |
//wrap it up and add the JsonArray of JsonObjects to the main object | |
myData.add("data", dataAr); | |
/* | |
* Business logic done, setting error to false last, so | |
* if anything errors out, we'll catch it. | |
*/ | |
myData.addProperty("error", false); | |
}catch(Exception e){ | |
/* | |
* On error, sets a boolean error value of true | |
* and adds the message into the errorMessage | |
* property. | |
*/ | |
myData.addProperty("error", true); | |
myData.addProperty("errorMessage", e.toString()); | |
System.out.println("Error with data provision method:"); | |
System.out.println(e.toString()); | |
} | |
/* | |
* This will always return a fully formed JsonObject response. | |
* Meaning that if there's an error, we hear about it and can | |
* handle that on the client side for display while developing, | |
* or logging when in production. | |
* | |
* Note: since we're hijacking the FacesContext response, we're | |
* returning a string (not data object) into the ResponseWriter. | |
* This is why the method is void. Don't worry, it's application/json. | |
*/ | |
writer.write(myData.toString()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"data": [{ | |
"name": "Arya Stark", | |
"title": "Lady" | |
}, | |
{ | |
"name": "Bran Stark", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Bronn ", | |
"title": "Sellsword" | |
}, | |
{ | |
"name": "Catelyn Tully", | |
"title": "Lady" | |
}, | |
{ | |
"name": "Cersei Lannister", | |
"title": "Queen" | |
}, | |
{ | |
"name": "Daenerys Targaryen", | |
"title": "Princess" | |
}, | |
{ | |
"name": "Davos Seaworth", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Doreah ", | |
"title": "Servant" | |
}, | |
{ | |
"name": "Drogo ", | |
"title": "Khal" | |
}, | |
{ | |
"name": "Eddard Stark", | |
"title": "Warden of the North" | |
}, | |
{ | |
"name": "Gendry ", | |
"title": "Blacksmith" | |
}, | |
{ | |
"name": "Hodor ", | |
"title": "Hodor" | |
}, | |
{ | |
"name": "Ilyn Payne", | |
"title": "The King's Justice" | |
}, | |
{ | |
"name": "Irri ", | |
"title": "Servant" | |
}, | |
{ | |
"name": "Jaime Lannister", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Jeor Mormont", | |
"title": "Knight Commander" | |
}, | |
{ | |
"name": "Jhiqui ", | |
"title": "Servant" | |
}, | |
{ | |
"name": "Joffrey Baratheon", | |
"title": "Prince" | |
}, | |
{ | |
"name": "Jon Arryn", | |
"title": "Defender of the Vale" | |
}, | |
{ | |
"name": "Jon Snow", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Jorah Mormont", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Kevan Lannister", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Lancel Lannister", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Loras Tyrell", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Luwin ", | |
"title": "Maester" | |
}, | |
{ | |
"name": "Lysa Tully", | |
"title": "Lady of the Vale" | |
}, | |
{ | |
"name": "Margaery Tyrell", | |
"title": "Lady" | |
}, | |
{ | |
"name": "Melisandre ", | |
"title": "Red Priestess" | |
}, | |
{ | |
"name": "Meryn Trant", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Myrcella Baratheon", | |
"title": "Princess" | |
}, | |
{ | |
"name": "Osha ", | |
"title": "Spearwife" | |
}, | |
{ | |
"name": "Petyr Baelish", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Pycelle ", | |
"title": "Grand Maester" | |
}, | |
{ | |
"name": "Rakharo ", | |
"title": "" | |
}, | |
{ | |
"name": "Renly Baratheon", | |
"title": "King" | |
}, | |
{ | |
"name": "Rickon Stark", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Robb Stark", | |
"title": "King in the North" | |
}, | |
{ | |
"name": "Robert Baratheon", | |
"title": "King of the Seven Kingdoms" | |
}, | |
{ | |
"name": "Rodrik Cassel", | |
"title": "Ser" | |
}, | |
{ | |
"name": "Samwell Tarly", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Sandor Clegane", | |
"title": "Dog" | |
}, | |
{ | |
"name": "Sansa Stark", | |
"title": "Lady" | |
}, | |
{ | |
"name": "Shae ", | |
"title": "" | |
}, | |
{ | |
"name": "Stannis Baratheon", | |
"title": "King" | |
}, | |
{ | |
"name": "Talisa Maegyr", | |
"title": "" | |
}, | |
{ | |
"name": "Theon Greyjoy", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Timett ", | |
"title": "Ruler of the Burned Men" | |
}, | |
{ | |
"name": "Tommen Baratheon", | |
"title": "Prince" | |
}, | |
{ | |
"name": "Tyrion Lannister", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Tywin Lannister", | |
"title": "Lord" | |
}, | |
{ | |
"name": "Varys ", | |
"title": "Master of Whisperers" | |
}, | |
{ | |
"name": "Viserys Targaryen", | |
"title": "Beggar King" | |
}, | |
{ | |
"name": "Ygritte ", | |
"title": "Spearwife" | |
}, | |
{ | |
"name": "Yoren ", | |
"title": "" | |
}], | |
"error": false | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<xp:view | |
xmlns:xp="http://www.ibm.com/xsp/core" | |
rendered="false" | |
viewState="nostate"> | |
<xp:this.afterRenderResponse> | |
<![CDATA[#{javascript:com.eric.test.DataProvider.GetMyDataAsJson();}]]> | |
</xp:this.afterRenderResponse> | |
XAgent. This will not render as a page, but as application/json data. | |
</xp:view> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment