From e4bc33aa55e015c2d301f4a3713c0e6a7263787a Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Fri, 24 Nov 2023 08:23:17 -0500 Subject: [PATCH 1/6] :wrench: chore(migration): Create TIP parameters table --- .../V2_6_0_20231124_0756__CreateTipTable.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_0756__CreateTipTable.sql diff --git a/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_0756__CreateTipTable.sql b/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_0756__CreateTipTable.sql new file mode 100644 index 0000000000..4a8c38df11 --- /dev/null +++ b/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_0756__CreateTipTable.sql @@ -0,0 +1,12 @@ +CREATE TABLE tip_parameters ( + id bigint(20) auto_increment NOT NULL, + tip_token_service text NULL, + tip_login_service text NULL, + tip_status_service text NULL, + token_value text NULL, + token_due_date TIMESTAMP NULL, + CONSTRAINT `PRIMARY` PRIMARY KEY (id) +) +ENGINE=InnoDB +DEFAULT CHARSET=utf8 +COLLATE=utf8_general_ci; \ No newline at end of file From 21b4e94e7d5fabf503e552136fd2f31be3bf3788 Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Fri, 24 Nov 2023 08:54:11 -0500 Subject: [PATCH 2/6] :wrench: chore(TIP parameters): Create tip parameters DAOs and models --- .../marlo/data/dao/TipParametersDAO.java | 67 ++++++++++ .../data/dao/mysql/TipParametersMySQLDAO.java | 84 ++++++++++++ .../data/manager/TipParametersManager.java | 74 +++++++++++ .../impl/TipParametersManagerImpl.java | 77 +++++++++++ .../ccafs/marlo/data/model/TipParameters.java | 121 ++++++++++++++++++ .../src/main/resources/hibernate.cfg.xml | 1 + .../main/resources/xmls/TipParameters.hbm.xml | 27 ++++ 7 files changed, 451 insertions(+) create mode 100644 marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/TipParametersDAO.java create mode 100644 marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/mysql/TipParametersMySQLDAO.java create mode 100644 marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/TipParametersManager.java create mode 100644 marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/impl/TipParametersManagerImpl.java create mode 100644 marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java create mode 100644 marlo-data/src/main/resources/xmls/TipParameters.hbm.xml diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/TipParametersDAO.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/TipParametersDAO.java new file mode 100644 index 0000000000..b2960e29d3 --- /dev/null +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/TipParametersDAO.java @@ -0,0 +1,67 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ + + +package org.cgiar.ccafs.marlo.data.dao; + +import org.cgiar.ccafs.marlo.data.model.TipParameters; + +import java.util.List; + + +public interface TipParametersDAO { + + /** + * This method removes a specific tipParameters value from the database. + * + * @param tipParametersId is the tipParameters identifier. + * @return true if the tipParameters was successfully deleted, false otherwise. + */ + public void deleteTipParameters(long tipParametersId); + + /** + * This method validate if the tipParameters identify with the given id exists in the system. + * + * @param tipParametersID is a tipParameters identifier. + * @return true if the tipParameters exists, false otherwise. + */ + public boolean existTipParameters(long tipParametersID); + + /** + * This method gets a tipParameters object by a given tipParameters identifier. + * + * @param tipParametersID is the tipParameters identifier. + * @return a TipParameters object. + */ + public TipParameters find(long id); + + /** + * This method gets a list of tipParameters that are active + * + * @return a list from TipParameters null if no exist records + */ + public List findAll(); + + + /** + * This method saves the information of the given tipParameters + * + * @param tipParameters - is the tipParameters object with the new information to be added/updated. + * @return a number greater than 0 representing the new ID assigned by the database, 0 if the tipParameters was + * updated + * or -1 is some error occurred. + */ + public TipParameters save(TipParameters tipParameters); +} diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/mysql/TipParametersMySQLDAO.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/mysql/TipParametersMySQLDAO.java new file mode 100644 index 0000000000..11dcdd303f --- /dev/null +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/dao/mysql/TipParametersMySQLDAO.java @@ -0,0 +1,84 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ + + +package org.cgiar.ccafs.marlo.data.dao.mysql; + +import org.cgiar.ccafs.marlo.data.dao.TipParametersDAO; +import org.cgiar.ccafs.marlo.data.model.TipParameters; + +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; + +import org.hibernate.SessionFactory; + +@Named +public class TipParametersMySQLDAO extends AbstractMarloDAO implements TipParametersDAO { + + + @Inject + public TipParametersMySQLDAO(SessionFactory sessionFactory) { + super(sessionFactory); + } + + @Override + public void deleteTipParameters(long tipParametersId) { + TipParameters tipParameters = this.find(tipParametersId); + this.delete(tipParameters); + } + + @Override + public boolean existTipParameters(long tipParametersID) { + TipParameters tipParameters = this.find(tipParametersID); + if (tipParameters == null) { + return false; + } + return true; + + } + + @Override + public TipParameters find(long id) { + return super.find(TipParameters.class, id); + + } + + @Override + public List findAll() { + String query = "from " + TipParameters.class.getName(); + List list = super.findAll(query); + if (!list.isEmpty()) { + return list; + } + return null; + + } + + @Override + public TipParameters save(TipParameters tipParameters) { + if (tipParameters.getId() == null) { + super.saveEntity(tipParameters); + } else { + tipParameters = super.update(tipParameters); + } + + + return tipParameters; + } + + +} \ No newline at end of file diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/TipParametersManager.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/TipParametersManager.java new file mode 100644 index 0000000000..fd658fed8a --- /dev/null +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/TipParametersManager.java @@ -0,0 +1,74 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ +package org.cgiar.ccafs.marlo.data.manager; + +import org.cgiar.ccafs.marlo.data.model.TipParameters; + +import java.util.List; + + +/** + * @author CCAFS + */ + +public interface TipParametersManager { + + + /** + * This method removes a specific tipParameters value from the database. + * + * @param tipParametersId is the tipParameters identifier. + * @return true if the tipParameters was successfully deleted, false otherwise. + */ + public void deleteTipParameters(long tipParametersId); + + + /** + * This method validate if the tipParameters identify with the given id exists in the system. + * + * @param tipParametersID is a tipParameters identifier. + * @return true if the tipParameters exists, false otherwise. + */ + public boolean existTipParameters(long tipParametersID); + + + /** + * This method gets a list of tipParameters that are active + * + * @return a list from TipParameters null if no exist records + */ + public List findAll(); + + + /** + * This method gets a tipParameters object by a given tipParameters identifier. + * + * @param tipParametersID is the tipParameters identifier. + * @return a TipParameters object. + */ + public TipParameters getTipParametersById(long tipParametersID); + + /** + * This method saves the information of the given tipParameters + * + * @param tipParameters - is the tipParameters object with the new information to be added/updated. + * @return a number greater than 0 representing the new ID assigned by the database, 0 if the tipParameters was + * updated + * or -1 is some error occurred. + */ + public TipParameters saveTipParameters(TipParameters tipParameters); + + +} diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/impl/TipParametersManagerImpl.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/impl/TipParametersManagerImpl.java new file mode 100644 index 0000000000..7912fae5e5 --- /dev/null +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/manager/impl/TipParametersManagerImpl.java @@ -0,0 +1,77 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ +package org.cgiar.ccafs.marlo.data.manager.impl; + + +import org.cgiar.ccafs.marlo.data.dao.TipParametersDAO; +import org.cgiar.ccafs.marlo.data.manager.TipParametersManager; +import org.cgiar.ccafs.marlo.data.model.TipParameters; + +import java.util.List; + +import javax.inject.Inject; +import javax.inject.Named; + +/** + * @author CCAFS + */ +@Named +public class TipParametersManagerImpl implements TipParametersManager { + + + private TipParametersDAO tipParametersDAO; + // Managers + + + @Inject + public TipParametersManagerImpl(TipParametersDAO tipParametersDAO) { + this.tipParametersDAO = tipParametersDAO; + + + } + + @Override + public void deleteTipParameters(long tipParametersId) { + + tipParametersDAO.deleteTipParameters(tipParametersId); + } + + @Override + public boolean existTipParameters(long tipParametersID) { + + return tipParametersDAO.existTipParameters(tipParametersID); + } + + @Override + public List findAll() { + + return tipParametersDAO.findAll(); + + } + + @Override + public TipParameters getTipParametersById(long tipParametersID) { + + return tipParametersDAO.find(tipParametersID); + } + + @Override + public TipParameters saveTipParameters(TipParameters tipParameters) { + + return tipParametersDAO.save(tipParameters); + } + + +} diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java new file mode 100644 index 0000000000..9cf1dae65a --- /dev/null +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java @@ -0,0 +1,121 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ + +package org.cgiar.ccafs.marlo.data.model; + +import org.cgiar.ccafs.marlo.data.IAuditLog; + +import java.util.Date; + +import com.google.gson.annotations.Expose; + +public class TipParameters extends MarloBaseEntity implements java.io.Serializable, IAuditLog { + + + private static final long serialVersionUID = -963914989396761020L; + + @Expose + private String tipTokenService; + @Expose + private String tipLoginService; + @Expose + private String tipStatusService; + @Expose + private String tokenValue; + @Expose + private Date tokenDueDate; + + + @Override + public String getLogDeatil() { + // TODO Auto-generated method stub + return null; + } + + @Override + public String getModificationJustification() { + // TODO Auto-generated method stub + return null; + } + + @Override + public User getModifiedBy() { + // TODO Auto-generated method stub + return null; + } + + + public String getTipLoginService() { + return tipLoginService; + } + + public String getTipStatusService() { + return tipStatusService; + } + + + public String getTipTokenService() { + return tipTokenService; + } + + + public Date getTokenDueDate() { + return tokenDueDate; + } + + + public String getTokenValue() { + return tokenValue; + } + + + @Override + public boolean isActive() { + // TODO Auto-generated method stub + return false; + } + + + @Override + public void setModifiedBy(User modifiedBy) { + // TODO Auto-generated method stub + } + + + public void setTipLoginService(String tipLoginService) { + this.tipLoginService = tipLoginService; + } + + + public void setTipStatusService(String tipStatusService) { + this.tipStatusService = tipStatusService; + } + + + public void setTipTokenService(String tipTokenService) { + this.tipTokenService = tipTokenService; + } + + + public void setTokenDueDate(Date tokenDueDate) { + this.tokenDueDate = tokenDueDate; + } + + + public void setTokenValue(String tokenValue) { + this.tokenValue = tokenValue; + } +} + diff --git a/marlo-data/src/main/resources/hibernate.cfg.xml b/marlo-data/src/main/resources/hibernate.cfg.xml index 958b3e3732..a4113699c1 100644 --- a/marlo-data/src/main/resources/hibernate.cfg.xml +++ b/marlo-data/src/main/resources/hibernate.cfg.xml @@ -429,6 +429,7 @@ + diff --git a/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml b/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml new file mode 100644 index 0000000000..82a952f583 --- /dev/null +++ b/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 4d6f76b892bb6eb4398bf60b9779b45da878d516 Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Fri, 24 Nov 2023 16:48:22 -0500 Subject: [PATCH 3/6] :wrench: chore(migration): Add private key field to TIP parameters table --- .../migrations/V2_6_0_20231124_1643__CreateKeyFieldTipTable.sql | 1 + 1 file changed, 1 insertion(+) create mode 100644 marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_1643__CreateKeyFieldTipTable.sql diff --git a/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_1643__CreateKeyFieldTipTable.sql b/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_1643__CreateKeyFieldTipTable.sql new file mode 100644 index 0000000000..ee3e8cd9b9 --- /dev/null +++ b/marlo-web/src/main/resources/database/migrations/V2_6_0_20231124_1643__CreateKeyFieldTipTable.sql @@ -0,0 +1 @@ +ALTER TABLE tip_parameters ADD private_key text NULL; From 70032812307d12aa55274dad1c0577719d0dcf95 Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Fri, 24 Nov 2023 16:51:33 -0500 Subject: [PATCH 4/6] :wrench: chore(TIP Integration): Create service to get TIP token and update java object model --- .../ccafs/marlo/data/model/TipParameters.java | 21 +-- .../json/tip/TipGetTokenServiceAction.java | 154 ++++++++++++++++++ marlo-web/src/main/resources/struts-json.xml | 8 + 3 files changed, 171 insertions(+), 12 deletions(-) create mode 100644 marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java diff --git a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java index 9cf1dae65a..e609848dbe 100644 --- a/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java +++ b/marlo-data/src/main/java/org/cgiar/ccafs/marlo/data/model/TipParameters.java @@ -35,9 +35,10 @@ public class TipParameters extends MarloBaseEntity implements java.io.Serializab @Expose private String tokenValue; @Expose + private String privateKey; + @Expose private Date tokenDueDate; - @Override public String getLogDeatil() { // TODO Auto-generated method stub @@ -56,6 +57,9 @@ public User getModifiedBy() { return null; } + public String getPrivateKey() { + return privateKey; + } public String getTipLoginService() { return tipLoginService; @@ -65,57 +69,50 @@ public String getTipStatusService() { return tipStatusService; } - public String getTipTokenService() { return tipTokenService; } - public Date getTokenDueDate() { return tokenDueDate; } - public String getTokenValue() { return tokenValue; } - @Override public boolean isActive() { // TODO Auto-generated method stub return false; } - @Override public void setModifiedBy(User modifiedBy) { // TODO Auto-generated method stub } + public void setPrivateKey(String privateKey) { + this.privateKey = privateKey; + } public void setTipLoginService(String tipLoginService) { this.tipLoginService = tipLoginService; } - public void setTipStatusService(String tipStatusService) { this.tipStatusService = tipStatusService; } - public void setTipTokenService(String tipTokenService) { this.tipTokenService = tipTokenService; } - public void setTokenDueDate(Date tokenDueDate) { this.tokenDueDate = tokenDueDate; } - public void setTokenValue(String tokenValue) { this.tokenValue = tokenValue; } -} - +} \ No newline at end of file diff --git a/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java new file mode 100644 index 0000000000..d842acb20f --- /dev/null +++ b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java @@ -0,0 +1,154 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ + +package org.cgiar.ccafs.marlo.action.json.tip; + +import org.cgiar.ccafs.marlo.action.BaseAction; +import org.cgiar.ccafs.marlo.data.manager.TipParametersManager; +import org.cgiar.ccafs.marlo.data.model.TipParameters; +import org.cgiar.ccafs.marlo.utils.APConfig; + +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import javax.inject.Inject; + +import com.ibm.icu.text.SimpleDateFormat; +import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TipGetTokenServiceAction extends BaseAction { + + private static final long serialVersionUID = 3229622720197560976L; + private static final Logger LOG = LoggerFactory.getLogger(TipGetTokenServiceAction.class); + + // Managers + TipParametersManager tipParametersManager; + + // Paramters + String token; + + // Front-end + private String url; + + @Inject + public TipGetTokenServiceAction(APConfig config, TipParametersManager tipParametersManager) { + super(config); + this.tipParametersManager = tipParametersManager; + } + + @Override + public String execute() throws Exception { + + try { + TipParameters tipParameters = new TipParameters(); + + // URL of the first service + List tipParametesList = new ArrayList<>(); + tipParametesList = tipParametersManager.findAll(); + String serviceUrl = null; + if (tipParametesList != null && !tipParametesList.isEmpty() && tipParametesList.get(0) != null + && tipParametesList.get(0).getTipTokenService() != null) { + + tipParameters = tipParametesList.get(0); + serviceUrl = tipParameters.getTipTokenService(); + } + + URL url = new URL(serviceUrl); + + // Create connection + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); + + String requestBody = "{\"private_key\":1234}"; + + // Get the output stream to send data + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes("utf-8"); + os.write(input, 0, input.length); + } + + // Get response + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); + } + reader.close(); + + // Process JSON + JSONObject jsonResponse = new JSONObject(response.toString()); + int status = jsonResponse.getInt("status"); + if (status == 200) { + token = jsonResponse.getString("token"); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Date dueDate = dateFormat.parse(jsonResponse.getString("due_date")); + + // Save token in DB + if (token != null && !token.isEmpty()) { + tipParameters.setTokenValue(token); + } + if (dueDate != null) { + tipParameters.setTokenDueDate(dueDate); + } + tipParametersManager.saveTipParameters(tipParameters); + + return SUCCESS; + } else { + System.out.println("Error: Unexpected status"); + return ERROR; + } + } catch (Exception e) { + e.printStackTrace(); + return ERROR; + } + } + + public String getToken() { + return token; + } + + @Override + public String getUrl() { + return url; + } + + @Override + public void prepare() throws Exception { + } + + + public void setToken(String token) { + this.token = token; + } + + + @Override + public void setUrl(String url) { + this.url = url; + } +} diff --git a/marlo-web/src/main/resources/struts-json.xml b/marlo-web/src/main/resources/struts-json.xml index 1c4ac690fc..61e35e6191 100644 --- a/marlo-web/src/main/resources/struts-json.xml +++ b/marlo-web/src/main/resources/struts-json.xml @@ -698,6 +698,14 @@ + + + true + true + + + From 8c62c1d815941c06d2446c9515b1411382d0c2d6 Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Mon, 27 Nov 2023 08:58:53 -0500 Subject: [PATCH 5/6] :wrench: chore(TIP service): Add parametrization for TIP secret key --- .../main/resources/xmls/TipParameters.hbm.xml | 3 + .../json/tip/TipGetTokenServiceAction.java | 89 ++++++++++--------- 2 files changed, 50 insertions(+), 42 deletions(-) diff --git a/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml b/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml index 82a952f583..086e30205f 100644 --- a/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml +++ b/marlo-data/src/main/resources/xmls/TipParameters.hbm.xml @@ -23,5 +23,8 @@ + + + \ No newline at end of file diff --git a/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java index d842acb20f..201a4129b3 100644 --- a/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java +++ b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipGetTokenServiceAction.java @@ -25,6 +25,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -66,60 +67,65 @@ public String execute() throws Exception { List tipParametesList = new ArrayList<>(); tipParametesList = tipParametersManager.findAll(); String serviceUrl = null; + String privateKey = null; if (tipParametesList != null && !tipParametesList.isEmpty() && tipParametesList.get(0) != null - && tipParametesList.get(0).getTipTokenService() != null) { + && tipParametesList.get(0).getTipTokenService() != null && tipParametesList.get(0).getPrivateKey() != null) { tipParameters = tipParametesList.get(0); serviceUrl = tipParameters.getTipTokenService(); - } - - URL url = new URL(serviceUrl); - - // Create connection - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("POST"); - connection.setRequestProperty("Content-Type", "application/json"); - connection.setRequestProperty("Accept", "application/json"); - connection.setDoOutput(true); - - String requestBody = "{\"private_key\":1234}"; + privateKey = tipParametesList.get(0).getPrivateKey(); - // Get the output stream to send data - try (OutputStream os = connection.getOutputStream()) { - byte[] input = requestBody.getBytes("utf-8"); - os.write(input, 0, input.length); - } + URL url = new URL(serviceUrl); - // Get response - BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); - StringBuilder response = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - response.append(line); - } - reader.close(); + // Create connection + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("POST"); + connection.setRequestProperty("Content-Type", "application/json"); + connection.setRequestProperty("Accept", "application/json"); + connection.setDoOutput(true); - // Process JSON - JSONObject jsonResponse = new JSONObject(response.toString()); - int status = jsonResponse.getInt("status"); - if (status == 200) { - token = jsonResponse.getString("token"); - SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + String requestBody = "{\"private_key\":" + privateKey + "}"; - Date dueDate = dateFormat.parse(jsonResponse.getString("due_date")); + // Get the output stream to send data + try (OutputStream os = connection.getOutputStream()) { + byte[] input = requestBody.getBytes(StandardCharsets.UTF_8); + os.write(input, 0, input.length); + } - // Save token in DB - if (token != null && !token.isEmpty()) { - tipParameters.setTokenValue(token); + // Get response + BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); + StringBuilder response = new StringBuilder(); + String line; + while ((line = reader.readLine()) != null) { + response.append(line); } - if (dueDate != null) { - tipParameters.setTokenDueDate(dueDate); + reader.close(); + + // Process JSON + JSONObject jsonResponse = new JSONObject(response.toString()); + int status = jsonResponse.getInt("status"); + if (status == 200) { + token = jsonResponse.getString("token"); + SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); + + Date dueDate = dateFormat.parse(jsonResponse.getString("due_date")); + + // Save token in DB + if (token != null && !token.isEmpty()) { + tipParameters.setTokenValue(token); + } + if (dueDate != null) { + tipParameters.setTokenDueDate(dueDate); + } + tipParametersManager.saveTipParameters(tipParameters); + + return SUCCESS; + } else { + System.out.println("Error: Unexpected status"); + return ERROR; } - tipParametersManager.saveTipParameters(tipParameters); - return SUCCESS; } else { - System.out.println("Error: Unexpected status"); return ERROR; } } catch (Exception e) { @@ -146,7 +152,6 @@ public void setToken(String token) { this.token = token; } - @Override public void setUrl(String url) { this.url = url; From ad47703a4340d570639b7c5016f8b1cac2b55981 Mon Sep 17 00:00:00 2001 From: Kenji Tanaka Date: Tue, 28 Nov 2023 09:18:36 -0500 Subject: [PATCH 6/6] :wrench: chore(TIP Integration): Create service to generate dinamic URL and update TIP Js files --- .../tip/TipDinamicUrlGenerationAction.java | 91 +++++++++++++++++++ marlo-web/src/main/resources/struts-json.xml | 8 ++ .../WEB-INF/crp/views/tip/tipEmbedded.ftl | 2 +- .../src/main/webapp/crp/js/tip/tipEmbedded.js | 10 +- 4 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipDinamicUrlGenerationAction.java diff --git a/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipDinamicUrlGenerationAction.java b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipDinamicUrlGenerationAction.java new file mode 100644 index 0000000000..f1de6f813b --- /dev/null +++ b/marlo-web/src/main/java/org/cgiar/ccafs/marlo/action/json/tip/TipDinamicUrlGenerationAction.java @@ -0,0 +1,91 @@ +/***************************************************************** + * This file is part of Managing Agricultural Research for Learning & + * Outcomes Platform (MARLO). + * MARLO is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * MARLO is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * You should have received a copy of the GNU General Public License + * along with MARLO. If not, see . + *****************************************************************/ + +package org.cgiar.ccafs.marlo.action.json.tip; + +import org.cgiar.ccafs.marlo.action.BaseAction; +import org.cgiar.ccafs.marlo.data.manager.TipParametersManager; +import org.cgiar.ccafs.marlo.data.model.TipParameters; + +import java.util.List; + +import javax.inject.Inject; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class TipDinamicUrlGenerationAction extends BaseAction { + + private static final long serialVersionUID = 3229622720197560976L; + private static final Logger LOG = LoggerFactory.getLogger(TipDinamicUrlGenerationAction.class); + + // Front-end + private String dinamicTipURL; + + @Inject + private TipParametersManager tipParametersManager; + + @Inject + public TipDinamicUrlGenerationAction() { + + } + + public String createDinamicURL() { + String tipURL = null; + try { + List tipParameters = tipParametersManager.findAll(); + + String userEmail = "", token = "", loginService = ""; + if (this.getCurrentUser() != null && this.getCurrentUser().getEmail() != null) { + userEmail = this.getCurrentUser().getEmail(); + } + if (tipParameters != null && !tipParameters.isEmpty() && tipParameters.get(0) != null) { + if (tipParameters.get(0).getTipTokenService() != null) { + token = tipParameters.get(0).getTokenValue(); + } + if (tipParameters.get(0).getTipLoginService() != null) { + loginService = tipParameters.get(0).getTipLoginService(); + } + } + tipURL = loginService + "/" + token + "/staff/" + userEmail; + } catch (NumberFormatException e) { + LOG.error("Error getting tip URL: " + e); + } + + return tipURL; + } + + @Override + public String execute() throws Exception { + + if (this.createDinamicURL() != null) { + this.dinamicTipURL = this.createDinamicURL(); + return SUCCESS; + } + return ERROR; + } + + public String getDinamicTipURL() { + return dinamicTipURL; + } + + @Override + public void prepare() throws Exception { + } + + public void setDinamicTipURL(String dinamicTipURL) { + this.dinamicTipURL = dinamicTipURL; + } +} \ No newline at end of file diff --git a/marlo-web/src/main/resources/struts-json.xml b/marlo-web/src/main/resources/struts-json.xml index 61e35e6191..f9185d2681 100644 --- a/marlo-web/src/main/resources/struts-json.xml +++ b/marlo-web/src/main/resources/struts-json.xml @@ -1206,6 +1206,14 @@ true + + + + true + true + + diff --git a/marlo-web/src/main/webapp/WEB-INF/crp/views/tip/tipEmbedded.ftl b/marlo-web/src/main/webapp/WEB-INF/crp/views/tip/tipEmbedded.ftl index 00b6e0cf45..eddfbba6cb 100644 --- a/marlo-web/src/main/webapp/WEB-INF/crp/views/tip/tipEmbedded.ftl +++ b/marlo-web/src/main/webapp/WEB-INF/crp/views/tip/tipEmbedded.ftl @@ -1,7 +1,7 @@ [#ftl] [#assign title = "TIP" /] [#assign currentSectionString = "${actionName?replace('/','-')}-phase-${(actualPhase.id)!}" /] -[#assign customJS = ["${baseUrlMedia}/js/tip/tipEmbedded.js?20231121a"] /] +[#assign customJS = ["${baseUrlMedia}/js/tip/tipEmbedded.js?20231128a"] /] [#assign customCSS = [ "${baseUrl}/crp/css/tip/tipEmbedded.css?20231121a", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" ] diff --git a/marlo-web/src/main/webapp/crp/js/tip/tipEmbedded.js b/marlo-web/src/main/webapp/crp/js/tip/tipEmbedded.js index 641b298f38..0d49acec65 100644 --- a/marlo-web/src/main/webapp/crp/js/tip/tipEmbedded.js +++ b/marlo-web/src/main/webapp/crp/js/tip/tipEmbedded.js @@ -6,15 +6,21 @@ $(document).ready(function() { var fullscreenButton = null; $.ajax({ - url: baseURL + '/tipUrlAction.do', + url: baseURL + '/tipGenerateUrlAction.do', type: 'GET', dataType: 'json', success: function(response) { - var embeddedPageUrl = response.tipURL; + + var embeddedPageUrl = response.dinamicTipURL; + console.log("embeddedPageUrl " + embeddedPageUrl); iframe = document.createElement('iframe'); iframe.src = embeddedPageUrl; iframe.style.width = iframeInitialWidth; iframe.style.height = iframeInitialHeight; + + /****************/ + iframe.setAttribute('allow', 'autoplay'); + iframe.setAttribute('sandbox', 'allow-scripts allow-forms allow-same-origin allow-top-navigation'); // Add iframe to the element with ID 'embeddedPage' document.getElementById('embeddedPage').appendChild(iframe);