Configure the Advanced I/O Function

The Advanced I/O function directory (functions/NewsApp_AIO) contains the following files:

  • 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 file.

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.

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 {
            // Fetch the endpoint and HTTP method
            String url = request.getRequestURI();
            String method = request.getMethod();
            if (url.equals("/fetchData") && method.equals("GET")) {
                String tableName = request.getParameter("tablename");
                // Query the Data Store using ZCQL
                String query = "select title, url from " + tableName;
                ArrayList<ZCRowObject> rowList = ZCQL.getInstance().executeQuery(query);
                JSONObject data = new JSONObject();
                JSONArray content = new JSONArray();
                // Construct JSON response
                for (int i = 0; i < rowList.size(); i++) {
                    JSONObject rowData = new JSONObject();
                    JSONObject tableData = new JSONObject();
                    ZCRowObject row = rowList.get(i);
                    String title = (String) row.get("title");
                    String urlVal = (String) row.get("url");
                    rowData.put("title", title);
                    rowData.put("url", urlVal);
                    tableData.put(tableName, rowData);
                    content.put(tableData);
                }
                data.put("content", content);
                // Send response back to client
                response.setContentType("application/json");
                response.getWriter().write(data.toString());
                response.setStatus(200);
            } else {
                // Invalid request
                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 we configure the client.

Last Updated 2025-10-21 12:04:01 +0530 IST