Configure the Advanced I/O Function

The functions directoryfunctions/file contains:

  • The 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

You can use any IDE to configure the function.

Note: Please go through the code in this section to make sure you fully understand it.

You can directly copy the code below and paste it in located in the functions/file directory and save the file.
import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.catalyst.advanced.CatalystAdvancedIOHandler; import com.zc.auth.connectors.ZCConnection; import com.zc.component.files.ZCFile; import com.zc.component.object.ZCObject; import com.zc.component.object.ZCRowObject; import com.zc.component.zcql.ZCQL; import org.json.JSONArray; import org.json.simple.JSONObject; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class Files implements CatalystAdvancedIOHandler { private static final Logger LOGGER = Logger.getLogger(Files.class.getName()); JSONObject responseData = new JSONObject(); static String GET = "GET"; static String DELETE = "DELETE"; static String WORKDRIVEFILEID = "WorkDriveFileID"; static Long FOLDER_ID = {{YOUR_FILESTORE_FOLDER_ID}}; //Enter your File Store Folder ID static Long TABLE_ID = {{YOUR_TABLE_ID}}; //Enter your Table ID @Override @SuppressWarnings("unchecked") public void runner(HttpServletRequest request, HttpServletResponse response) throws Exception { try { String url = request.getRequestURI(); String method = request.getMethod(); LOGGER.log(Level.SEVERE, url); if ((url.equals("/getFiles")) && method.equals(GET)) { String query = "SELECT * FROM WorkDriveFileID limit 1,100"; ArrayList rowList = ZCQL.getInstance().executeQuery(query); JSONArray jsonArray = new JSONArray(); for (int i = 0; i < rowList.size(); i++) { JSONObject formDetailsJson = new JSONObject(); formDetailsJson.put("FileID", rowList.get(i).get(WORKDRIVEFILEID, "FileID")); formDetailsJson.put("WorkDriveSync", rowList.get(i).get(WORKDRIVEFILEID, "WorkDriveSync")); formDetailsJson.put("FileName", rowList.get(i).get(WORKDRIVEFILEID, "FileName")); formDetailsJson.put("UploadedTime", rowList.get(i).get(WORKDRIVEFILEID, "UploadedTime")); formDetailsJson.put("FileSize", rowList.get(i).get(WORKDRIVEFILEID, "FileSize")); formDetailsJson.put("WorkDriveFileID", rowList.get(i).get(WORKDRIVEFILEID, "WorkDriveFileID")); formDetailsJson.put("ROWID", rowList.get(i).get(WORKDRIVEFILEID, "ROWID")); jsonArray.put(formDetailsJson); } response.setContentType("application/json"); response.getWriter().write(jsonArray.toString()); response.setStatus(200); } else if ((url.equals("/deleteFile")) && method.equals(DELETE)) { Long fileId = Long.parseLong(request.getParameter("fileID")); String query = "SELECT * FROM WorkDriveFileID where FileID=" + String.valueOf(fileId); ArrayList rowList = ZCQL.getInstance().executeQuery(query); String WorkDriveFileID = (String) rowList.get(0).get(WORKDRIVEFILEID, "WorkDriveFileID"); Response res = deleteWorkdriveFile(WorkDriveFileID); if (res.code() == 200) { ZCFile.getInstance().getFolderInstance(FOLDER_ID).deleteFile(fileId); String ROWID = (String) rowList.get(0).get(WORKDRIVEFILEID, "ROWID"); ZCObject.getInstance().getTable(TABLE_ID).deleteRow(Long.parseLong(ROWID)); responseData.put("message", "Deleted Successfully"); } else { responseData.put("message", "Workdrive API Error"); } response.setContentType("application/json"); response.getWriter().write(responseData.toString()); response.setStatus(200); } else { LOGGER.log(Level.SEVERE, "Error. Invalid Request"); responseData.put("error", "Request Endpoint not found"); response.setStatus(404); } } catch (Exception e) { LOGGER.log(Level.SEVERE, "Exception in Files", e); responseData.put("error", "Internal server error occurred. Please try again in some time."); response.getWriter().write(responseData.toString()); response.setStatus(500); } } @SuppressWarnings("unchecked") public String getAccessToken() throws Exception { JSONObject authJson = new JSONObject(); JSONObject connectorJson = new JSONObject(); authJson.put("client_id", "{{YOUR_CLIENT_ID}}"); //Enter your Client ID authJson.put("client_secret", "{{YOUR_CLIENT_SECRET}}"); //Enter your Client Secret authJson.put("auth_url", ""); authJson.put("refresh_url", ""); authJson.put("refresh_token", "{{YOUR_REFRESH_TOKEN}}"); //Enter your Refresh Token connectorJson.put("WorkDriveConnector", authJson); return ZCConnection.getInstance(connectorJson).getConnector("WorkDriveConnector").getAccessToken(); } @SuppressWarnings("unchecked") public Response deleteWorkdriveFile(String WorkDriveFileID) throws Exception { String accessToken = getAccessToken(); OkHttpClient client = new OkHttpClient().newBuilder().build(); JSONObject data = new JSONObject(); JSONObject attributes = new JSONObject(); attributes.put("status", "51"); data.put("attributes", attributes); data.put("type", "files"); JSONObject reqBody = new JSONObject(); reqBody.put("data", data); LOGGER.log(Level.SEVERE, reqBody.toString()); RequestBody body = RequestBody.create(null, reqBody.toJSONString()); Request req = new Request.Builder().url(""+WorkDriveFileID).method("PATCH", body).addHeader("Authorization", "Zoho-oauthtoken " + accessToken).addHeader("Accept", "application/vnd.api+json").build(); return client.newCall(req).execute(); }
View more
Note: After you copy and paste this code in your function file,ensure that you provide the following values in it, as indicated by the comments:
  • File Store Folder ID in line 29: You can obtain the ID of the folder you created in the File Store from the remote console. catalyst_workdrive_filestore_folderid

  • Table ID in line 30: You can obtain the ID of the table you created from the Data Store section, as mentioned in the previous step. catalyst_workdrive_tableid

  • Client ID in line 105

  • Client Secret in line 106

  • Refresh Token in line 107

The Advanced I/O function is now configured. We will discuss the function and client code, after you configure the client.

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