Configure the Advanced I/O Function

Let’s now configure the Advanced I/O function component. For the Advanced I/O function intialized Java, its directory, functions/NewsApp_AIO will contain:

  • The DatastoreFetch.java main function file
  • The catalyst-config.json configuration file
  • Java library files in the lib folder
  • .classpath and .project dependency files

You will be adding code in the DatastoreFetch.java.

The Advanced I/O function fetches the news item from its source table in the Data Store and forwards it to the client component as a JSON response.

Copy the code and paste it in the DatastoreFetch.java in the functions/NewsApp_AIO directory.

Note: Please go through the code in this section to make sure you fully understand it.
    
DatastoreFetch.java
copy
import java.util.logging.Logger;
import java.util.ArrayList;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.catalyst.advanced.CatalystAdvancedIOHandler;
import com.zc.component.object.ZCRowObject;
import com.zc.component.zcql.ZCQL;
import org.json.JSONArray;
import org.json.JSONObject;
public class DatastoreFetch implements CatalystAdvancedIOHandler {
private static final Logger LOGGER = Logger.getLogger(DatastoreFetch.class.getName());
@Override
public void runner(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
//Fetches the endpoint that the call was made to
String url = request.getRequestURI();
String method = request.getMethod();
if (url.equals("/fetchData") && method.equals("GET")) {
String tableName = request.getParameter("tablename");
//Queries the Data Store to fetch news from a particular table using ZCQL
String query = "select title,url from " + tableName;
ArrayList rowList = ZCQL.getInstance().executeQuery(query);
JSONObject data = new JSONObject();
JSONArray content = new JSONArray();
//Constructs the data from the Data Store as an JSON response
for (int i = 0; i < rowList.size(); i++) {
JSONObject rowData = new JSONObject();
JSONObject tableData = new JSONObject();
String urls = (String) rowList.get(i).get(tableName, "url");
Object title = rowList.get(i).get(tableName, "title");
rowData.put("title", title);
rowData.put("url", urls);
tableData.put(tableName, rowData);
content.put(tableData);
}
data.put("content", content);
//Sends the JSON response back to the client
response.setContentType("application/json");
response.getWriter().write(data.toString());
response.setStatus(200);
} else {
//The errors are logged. You can check them from Catalyst Logs.
LOGGER.log(Level.SEVERE, "Error. Invalid Request");
response.setStatus(404);
}
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Exception in DatastoreFetch", e);
response.setStatus(500);
}
}
}
View more

The Advanced I/O function is now configured. We will discuss the application’s architecture after you configure the client.

Last Updated 2023-12-15 18:54:08 +0530 +0530

ON THIS PAGE
ACCESS THIS PAGE