forked from community/device-mgt-core
parent
d7ccaa559b
commit
5e247c6e6b
@ -0,0 +1,23 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
public class Base64File {
|
||||
|
||||
private String name;
|
||||
private String base64String;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getBase64String() {
|
||||
return base64String;
|
||||
}
|
||||
|
||||
public void setBase64String(String base64String) {
|
||||
this.base64String = base64String;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class FileDataHolder {
|
||||
|
||||
private String name;
|
||||
private InputStream file;
|
||||
|
||||
public FileDataHolder(String name, InputStream file) {
|
||||
this.name = name;
|
||||
this.file = file;
|
||||
}
|
||||
public FileDataHolder() {
|
||||
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public InputStream getFile() {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void setFile(InputStream file) {
|
||||
this.file = file;
|
||||
}
|
||||
}
|
@ -0,0 +1,77 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
|
||||
public class IdentityServer {
|
||||
private int id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String url;
|
||||
private String spAppsUri;
|
||||
private String spAppsApi;
|
||||
private String userName;
|
||||
private String password;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getSpAppsUri() {
|
||||
return spAppsUri;
|
||||
}
|
||||
|
||||
public void setSpAppsURI(String spAppsUri) {
|
||||
this.spAppsUri = spAppsUri;
|
||||
}
|
||||
|
||||
public String getSpAppsApi() {
|
||||
return spAppsApi;
|
||||
}
|
||||
|
||||
public void setSpAppsApi(String spAppsApi) {
|
||||
this.spAppsApi = spAppsApi;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public String getUserName() {
|
||||
return userName;
|
||||
}
|
||||
|
||||
public void setUserName(String userName) {
|
||||
this.userName = userName;
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class IdentityServerList {
|
||||
private List<IdentityServer> identityServers;
|
||||
|
||||
public List<IdentityServer> getIdentityServers() {
|
||||
return identityServers;
|
||||
}
|
||||
|
||||
public void setIdentityServers(List<IdentityServer> identityServers) {
|
||||
this.identityServers = identityServers;
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
|
||||
import io.entgra.application.mgt.common.response.Application;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SPApplication {
|
||||
private String id;
|
||||
private String name;
|
||||
private String description;
|
||||
private String image;
|
||||
private String accessUrl;
|
||||
private String access;
|
||||
private String self;
|
||||
private List<Application> existingApplications;
|
||||
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
|
||||
public String getAccessUrl() {
|
||||
return accessUrl;
|
||||
}
|
||||
|
||||
public void setAccessUrl(String accessUrl) {
|
||||
this.accessUrl = accessUrl;
|
||||
}
|
||||
|
||||
public String getAccess() {
|
||||
return access;
|
||||
}
|
||||
|
||||
public void setAccess(String access) {
|
||||
this.access = access;
|
||||
}
|
||||
|
||||
public String getSelf() {
|
||||
return self;
|
||||
}
|
||||
|
||||
public void setSelf(String self) {
|
||||
this.self = self;
|
||||
}
|
||||
|
||||
public List<Application> getExistingApplications() {
|
||||
return existingApplications;
|
||||
}
|
||||
|
||||
public void setExistingApplications(List<Application> existingApplications) {
|
||||
this.existingApplications = existingApplications;
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package io.entgra.application.mgt.common;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class SPApplicationListResponse {
|
||||
private int totalResults;
|
||||
private int startIndex;
|
||||
private int count;
|
||||
private List<SPApplication> applications;
|
||||
|
||||
public int getTotalResults() {
|
||||
return totalResults;
|
||||
}
|
||||
|
||||
public void setTotalResults(int totalResults) {
|
||||
this.totalResults = totalResults;
|
||||
}
|
||||
|
||||
public int getStartIndex() {
|
||||
return startIndex;
|
||||
}
|
||||
|
||||
public void setStartIndex(int startIndex) {
|
||||
this.startIndex = startIndex;
|
||||
}
|
||||
|
||||
public int getCount() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public List<SPApplication> getApplications() {
|
||||
return applications;
|
||||
}
|
||||
|
||||
public void setApplications(List<SPApplication> applications) {
|
||||
this.applications = applications;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package io.entgra.application.mgt.common.services;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.IdentityServerList;
|
||||
import io.entgra.application.mgt.common.SPApplication;
|
||||
import io.entgra.application.mgt.common.exception.ApplicationManagementException;
|
||||
import io.entgra.application.mgt.common.exception.RequestValidatingException;
|
||||
import io.entgra.application.mgt.common.response.Application;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SPApplicationManager {
|
||||
|
||||
|
||||
void addExistingApps(int identityServerId, List<SPApplication> applications) throws ApplicationManagementException;
|
||||
|
||||
void detachSPApplications(int identityServerId, String spUID, List<Integer> appIds) throws ApplicationManagementException;
|
||||
|
||||
void attachSPApplications(int identityServerId, String spUID, List<Integer> appIds) throws ApplicationManagementException;
|
||||
|
||||
IdentityServer getIdentityServer(int identityServerId) throws ApplicationManagementException;
|
||||
|
||||
IdentityServerList getIdentityServers() throws ApplicationManagementException;
|
||||
|
||||
List<Application> getSPApplications(int identityServerId, String spUID) throws ApplicationManagementException;
|
||||
|
||||
<T> Application createSPApplication(T app, int identityServerId, String spId) throws ApplicationManagementException, RequestValidatingException;
|
||||
|
||||
void validateAttachAppsRequest(int identityServerId, List<Integer> appIds) throws ApplicationManagementException;
|
||||
|
||||
void validateDetachAppsRequest(int identityServerId, String spId, List<Integer> appIds) throws ApplicationManagementException;
|
||||
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package io.entgra.application.mgt.core.dao;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface SPApplicationDAO {
|
||||
/**
|
||||
*
|
||||
* @param tenantId
|
||||
* @return the application with the provided installer location
|
||||
* @throws ApplicationManagementDAOException
|
||||
*/
|
||||
List<ApplicationDTO> getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tenantId
|
||||
* @return the application with the provided installer location
|
||||
* @throws ApplicationManagementDAOException
|
||||
*/
|
||||
int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tenantId
|
||||
* @return the application with the provided installer location
|
||||
* @throws ApplicationManagementDAOException
|
||||
*/
|
||||
void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
List<IdentityServer> getIdentityServers(int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
/**
|
||||
* Verify whether application exist for given application name and device type. Because a name and device type is
|
||||
* unique for an application.
|
||||
*
|
||||
* @param appId id of the application.
|
||||
* @param spUID UID of the service provider.
|
||||
* @param tenantId ID of the tenant.
|
||||
* @return ID of the ApplicationDTO.
|
||||
* @throws ApplicationManagementDAOException Application Management DAO Exception.
|
||||
*/
|
||||
boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException;
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,290 @@
|
||||
package io.entgra.application.mgt.core.dao.impl.application.spapplication;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.common.exception.DBConnectionException;
|
||||
import io.entgra.application.mgt.core.dao.SPApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.impl.AbstractDAOImpl;
|
||||
import io.entgra.application.mgt.core.dao.impl.application.GenericApplicationDAOImpl;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException;
|
||||
import io.entgra.application.mgt.core.util.DAOUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class GenericSPApplicationDAOImpl extends AbstractDAOImpl implements SPApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(GenericApplicationDAOImpl.class);
|
||||
@Override
|
||||
public List<IdentityServer> getIdentityServers(int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServers(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ? AND "
|
||||
+ "ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, id);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServer(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (UnexpectedServerErrorException e) {
|
||||
String msg = "Found more than one identity server for id: " + id;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApplicationDTO> getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting identity server applications from the database");
|
||||
}
|
||||
String sql = "SELECT "
|
||||
+ "AP_APP.ID AS APP_ID, "
|
||||
+ "AP_APP.NAME AS APP_NAME, "
|
||||
+ "AP_APP.DESCRIPTION AS APP_DESCRIPTION, "
|
||||
+ "AP_APP.TYPE AS APP_TYPE, "
|
||||
+ "AP_APP.STATUS AS APP_STATUS, "
|
||||
+ "AP_APP.SUB_TYPE AS APP_SUB_TYPE, "
|
||||
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
|
||||
+ "AP_APP.RATING AS APP_RATING, "
|
||||
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
|
||||
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
|
||||
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
|
||||
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
|
||||
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "
|
||||
+ "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, "
|
||||
+ "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, "
|
||||
+ "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, "
|
||||
+ "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, "
|
||||
+ "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, "
|
||||
+ "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, "
|
||||
+ "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, "
|
||||
+ "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, "
|
||||
+ "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, "
|
||||
+ "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, "
|
||||
+ "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, "
|
||||
+ "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, "
|
||||
+ "AP_APP_RELEASE.RATING AS RELEASE_RATING, "
|
||||
+ "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, "
|
||||
+ "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT "
|
||||
+ "FROM AP_APP "
|
||||
+ "LEFT JOIN AP_APP_RELEASE ON "
|
||||
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND "
|
||||
+ "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID "
|
||||
+ "INNER JOIN AP_IS_SP_APP_MAPPING as SP_APPS on SP_APPS.AP_APP_ID = AP_APP.ID "
|
||||
+ "WHERE "
|
||||
+ "SP_APPS.SP_UID = ? "
|
||||
+ "AND SP_APPS.IS_ID = ? "
|
||||
+ "AND AP_APP.TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, identityServerId);
|
||||
stmt.setInt(3, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the identity server applications");
|
||||
}
|
||||
return DAOUtil.loadApplications(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to get identity server applications for application release";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while getting identity server applications while executing query. Executed query: " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT AP_APP_ID AS ID "
|
||||
+ "FROM AP_IS_SP_APP_MAPPING SP_MAPPING "
|
||||
+ "WHERE "
|
||||
+ "SP_UID = ? AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? AND "
|
||||
+ "TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
return rs.next();
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to check whether the existence of "
|
||||
+ "sp application with id " + appId + " for service provider which has UID " + spUID;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to check whether the existence of service provider application " +
|
||||
"with id " + appId + " for service provider which has UID " + spUID + ". executed query " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING "
|
||||
+ "WHERE SP_UID = ? "
|
||||
+ "AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? "
|
||||
+ "AND TENANT_ID = ? ";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "INSERT INTO AP_IS_SP_APP_MAPPING "
|
||||
+ "(SP_UID, "
|
||||
+ "AP_APP_ID, "
|
||||
+ "IS_ID, TENANT_ID) "
|
||||
+ "VALUES (?, ?, ?, ?)";
|
||||
int mappingId = -1;
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to delete application with the id: " + applicationId + " from service providers");
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING WHERE AP_APP_ID = ? AND TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setInt(1, applicationId);
|
||||
stmt.setInt(2, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,289 @@
|
||||
package io.entgra.application.mgt.core.dao.impl.application.spapplication;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.common.exception.DBConnectionException;
|
||||
import io.entgra.application.mgt.core.dao.SPApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.impl.AbstractDAOImpl;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException;
|
||||
import io.entgra.application.mgt.core.util.DAOUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class OracleSPApplicationDAOImpl extends AbstractDAOImpl implements SPApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(OracleSPApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<IdentityServer> getIdentityServers(int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServers(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ? AND "
|
||||
+ "ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, id);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServer(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (UnexpectedServerErrorException e) {
|
||||
String msg = "Found more than one identity server for id: " + id;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApplicationDTO> getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting identity server applications from the database");
|
||||
}
|
||||
String sql = "SELECT "
|
||||
+ "AP_APP.ID AS APP_ID, "
|
||||
+ "AP_APP.NAME AS APP_NAME, "
|
||||
+ "AP_APP.DESCRIPTION AS APP_DESCRIPTION, "
|
||||
+ "AP_APP.TYPE AS APP_TYPE, "
|
||||
+ "AP_APP.STATUS AS APP_STATUS, "
|
||||
+ "AP_APP.SUB_TYPE AS APP_SUB_TYPE, "
|
||||
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
|
||||
+ "AP_APP.RATING AS APP_RATING, "
|
||||
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
|
||||
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
|
||||
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
|
||||
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
|
||||
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "
|
||||
+ "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, "
|
||||
+ "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, "
|
||||
+ "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, "
|
||||
+ "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, "
|
||||
+ "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, "
|
||||
+ "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, "
|
||||
+ "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, "
|
||||
+ "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, "
|
||||
+ "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, "
|
||||
+ "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, "
|
||||
+ "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, "
|
||||
+ "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, "
|
||||
+ "AP_APP_RELEASE.RATING AS RELEASE_RATING, "
|
||||
+ "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, "
|
||||
+ "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT "
|
||||
+ "FROM AP_APP "
|
||||
+ "LEFT JOIN AP_APP_RELEASE ON "
|
||||
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND "
|
||||
+ "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID "
|
||||
+ "INNER JOIN AP_IS_SP_APP_MAPPING as SP_APPS on SP_APPS.AP_APP_ID = AP_APP.ID "
|
||||
+ "WHERE "
|
||||
+ "SP_APPS.SP_UID = ? "
|
||||
+ "AND SP_APPS.IS_ID = ? "
|
||||
+ "AND AP_APP.TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, identityServerId);
|
||||
stmt.setInt(3, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the identity server applications");
|
||||
}
|
||||
return DAOUtil.loadApplications(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to get identity server applications for application release";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while getting identity server applications while executing query. Executed query: " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT AP_APP_ID AS ID "
|
||||
+ "FROM AP_IS_SP_APP_MAPPING SP_MAPPING "
|
||||
+ "WHERE "
|
||||
+ "SP_UID = ? AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? AND "
|
||||
+ "TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
return rs.next();
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to check whether the existence of "
|
||||
+ "sp application with id " + appId + " for service provider which has UID " + spUID;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to check whether the existence of service provider application " +
|
||||
"with id " + appId + " for service provider which has UID " + spUID + ". executed query " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING "
|
||||
+ "WHERE SP_UID = ? "
|
||||
+ "AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? "
|
||||
+ "AND TENANT_ID = ? ";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "INSERT INTO AP_IS_SP_APP_MAPPING "
|
||||
+ "(SP_UID, "
|
||||
+ "AP_APP_ID, "
|
||||
+ "IS_ID, TENANT_ID) "
|
||||
+ "VALUES (?, ?, ?, ?)";
|
||||
int mappingId = -1;
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to delete application with the id: " + applicationId + " from service providers");
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING WHERE AP_APP_ID = ? AND TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setInt(1, applicationId);
|
||||
stmt.setInt(2, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,289 @@
|
||||
package io.entgra.application.mgt.core.dao.impl.application.spapplication;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.common.exception.DBConnectionException;
|
||||
import io.entgra.application.mgt.core.dao.SPApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.impl.AbstractDAOImpl;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException;
|
||||
import io.entgra.application.mgt.core.util.DAOUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class PostgreSQLSPApplicationDAOImpl extends AbstractDAOImpl implements SPApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(PostgreSQLSPApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<IdentityServer> getIdentityServers(int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServers(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ? AND "
|
||||
+ "ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, id);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServer(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (UnexpectedServerErrorException e) {
|
||||
String msg = "Found more than one identity server for id: " + id;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApplicationDTO> getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting identity server applications from the database");
|
||||
}
|
||||
String sql = "SELECT "
|
||||
+ "AP_APP.ID AS APP_ID, "
|
||||
+ "AP_APP.NAME AS APP_NAME, "
|
||||
+ "AP_APP.DESCRIPTION AS APP_DESCRIPTION, "
|
||||
+ "AP_APP.TYPE AS APP_TYPE, "
|
||||
+ "AP_APP.STATUS AS APP_STATUS, "
|
||||
+ "AP_APP.SUB_TYPE AS APP_SUB_TYPE, "
|
||||
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
|
||||
+ "AP_APP.RATING AS APP_RATING, "
|
||||
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
|
||||
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
|
||||
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
|
||||
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
|
||||
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "
|
||||
+ "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, "
|
||||
+ "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, "
|
||||
+ "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, "
|
||||
+ "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, "
|
||||
+ "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, "
|
||||
+ "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, "
|
||||
+ "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, "
|
||||
+ "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, "
|
||||
+ "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, "
|
||||
+ "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, "
|
||||
+ "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, "
|
||||
+ "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, "
|
||||
+ "AP_APP_RELEASE.RATING AS RELEASE_RATING, "
|
||||
+ "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, "
|
||||
+ "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT "
|
||||
+ "FROM AP_APP "
|
||||
+ "LEFT JOIN AP_APP_RELEASE ON "
|
||||
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND "
|
||||
+ "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID "
|
||||
+ "INNER JOIN AP_IS_SP_APP_MAPPING as SP_APPS on SP_APPS.AP_APP_ID = AP_APP.ID "
|
||||
+ "WHERE "
|
||||
+ "SP_APPS.SP_UID = ? "
|
||||
+ "AND SP_APPS.IS_ID = ? "
|
||||
+ "AND AP_APP.TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, identityServerId);
|
||||
stmt.setInt(3, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the identity server applications");
|
||||
}
|
||||
return DAOUtil.loadApplications(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to get identity server applications for application release";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while getting identity server applications while executing query. Executed query: " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT AP_APP_ID AS ID "
|
||||
+ "FROM AP_IS_SP_APP_MAPPING SP_MAPPING "
|
||||
+ "WHERE "
|
||||
+ "SP_UID = ? AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? AND "
|
||||
+ "TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
return rs.next();
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to check whether the existence of "
|
||||
+ "sp application with id " + appId + " for service provider which has UID " + spUID;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to check whether the existence of service provider application " +
|
||||
"with id " + appId + " for service provider which has UID " + spUID + ". executed query " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING "
|
||||
+ "WHERE SP_UID = ? "
|
||||
+ "AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? "
|
||||
+ "AND TENANT_ID = ? ";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "INSERT INTO AP_IS_SP_APP_MAPPING "
|
||||
+ "(SP_UID, "
|
||||
+ "AP_APP_ID, "
|
||||
+ "IS_ID, TENANT_ID) "
|
||||
+ "VALUES (?, ?, ?, ?)";
|
||||
int mappingId = -1;
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to delete application with the id: " + applicationId + " from service providers");
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING WHERE AP_APP_ID = ? AND TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setInt(1, applicationId);
|
||||
stmt.setInt(2, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,289 @@
|
||||
package io.entgra.application.mgt.core.dao.impl.application.spapplication;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.common.exception.DBConnectionException;
|
||||
import io.entgra.application.mgt.core.dao.SPApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.impl.AbstractDAOImpl;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import io.entgra.application.mgt.core.exception.UnexpectedServerErrorException;
|
||||
import io.entgra.application.mgt.core.util.DAOUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
|
||||
public class SQLServerSPApplicationDAOImpl extends AbstractDAOImpl implements SPApplicationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SQLServerSPApplicationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public List<IdentityServer> getIdentityServers(int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServers(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServer getIdentityServerById(int id, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT * "
|
||||
+ "FROM AP_IDENTITY_SERVER "
|
||||
+ "WHERE TENANT_ID = ? AND "
|
||||
+ "ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, id);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved available identity servers" );
|
||||
}
|
||||
return DAOUtil.loadIdentityServer(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to retrieve available identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (UnexpectedServerErrorException e) {
|
||||
String msg = "Found more than one identity server for id: " + id;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ApplicationDTO> getSPApplications(int identityServerId, String spUID, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Getting identity server applications from the database");
|
||||
}
|
||||
String sql = "SELECT "
|
||||
+ "AP_APP.ID AS APP_ID, "
|
||||
+ "AP_APP.NAME AS APP_NAME, "
|
||||
+ "AP_APP.DESCRIPTION AS APP_DESCRIPTION, "
|
||||
+ "AP_APP.TYPE AS APP_TYPE, "
|
||||
+ "AP_APP.STATUS AS APP_STATUS, "
|
||||
+ "AP_APP.SUB_TYPE AS APP_SUB_TYPE, "
|
||||
+ "AP_APP.CURRENCY AS APP_CURRENCY, "
|
||||
+ "AP_APP.RATING AS APP_RATING, "
|
||||
+ "AP_APP.DEVICE_TYPE_ID AS APP_DEVICE_TYPE_ID, "
|
||||
+ "AP_APP_RELEASE.ID AS RELEASE_ID, "
|
||||
+ "AP_APP_RELEASE.DESCRIPTION AS RELEASE_DESCRIPTION, "
|
||||
+ "AP_APP_RELEASE.VERSION AS RELEASE_VERSION, "
|
||||
+ "AP_APP_RELEASE.UUID AS RELEASE_UUID, "
|
||||
+ "AP_APP_RELEASE.RELEASE_TYPE AS RELEASE_TYPE, "
|
||||
+ "AP_APP_RELEASE.INSTALLER_LOCATION AS AP_RELEASE_STORED_LOC, "
|
||||
+ "AP_APP_RELEASE.ICON_LOCATION AS AP_RELEASE_ICON_LOC, "
|
||||
+ "AP_APP_RELEASE.BANNER_LOCATION AS AP_RELEASE_BANNER_LOC, "
|
||||
+ "AP_APP_RELEASE.SC_1_LOCATION AS AP_RELEASE_SC1, "
|
||||
+ "AP_APP_RELEASE.SC_2_LOCATION AS AP_RELEASE_SC2, "
|
||||
+ "AP_APP_RELEASE.SC_3_LOCATION AS AP_RELEASE_SC3, "
|
||||
+ "AP_APP_RELEASE.APP_HASH_VALUE AS RELEASE_HASH_VALUE, "
|
||||
+ "AP_APP_RELEASE.APP_PRICE AS RELEASE_PRICE, "
|
||||
+ "AP_APP_RELEASE.APP_META_INFO AS RELEASE_META_INFO, "
|
||||
+ "AP_APP_RELEASE.PACKAGE_NAME AS PACKAGE_NAME, "
|
||||
+ "AP_APP_RELEASE.SUPPORTED_OS_VERSIONS AS RELEASE_SUP_OS_VERSIONS, "
|
||||
+ "AP_APP_RELEASE.RATING AS RELEASE_RATING, "
|
||||
+ "AP_APP_RELEASE.CURRENT_STATE AS RELEASE_CURRENT_STATE, "
|
||||
+ "AP_APP_RELEASE.RATED_USERS AS RATED_USER_COUNT "
|
||||
+ "FROM AP_APP "
|
||||
+ "LEFT JOIN AP_APP_RELEASE ON "
|
||||
+ "AP_APP.ID = AP_APP_RELEASE.AP_APP_ID AND "
|
||||
+ "AP_APP.TENANT_ID = AP_APP_RELEASE.TENANT_ID "
|
||||
+ "INNER JOIN AP_IS_SP_APP_MAPPING as SP_APPS on SP_APPS.AP_APP_ID = AP_APP.ID "
|
||||
+ "WHERE "
|
||||
+ "SP_APPS.SP_UID = ? "
|
||||
+ "AND SP_APPS.IS_ID = ? "
|
||||
+ "AND AP_APP.TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, identityServerId);
|
||||
stmt.setInt(3, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Successfully retrieved basic details of the identity server applications");
|
||||
}
|
||||
return DAOUtil.loadApplications(rs);
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to get identity server applications for application release";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while getting identity server applications while executing query. Executed query: " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSPApplicationExist(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
String sql = "SELECT AP_APP_ID AS ID "
|
||||
+ "FROM AP_IS_SP_APP_MAPPING SP_MAPPING "
|
||||
+ "WHERE "
|
||||
+ "SP_UID = ? AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? AND "
|
||||
+ "TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql)){
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
try (ResultSet rs = stmt.executeQuery()){
|
||||
return rs.next();
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to check whether the existence of "
|
||||
+ "sp application with id " + appId + " for service provider which has UID " + spUID;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing query to check whether the existence of service provider application " +
|
||||
"with id " + appId + " for service provider which has UID " + spUID + ". executed query " + sql;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void detachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING "
|
||||
+ "WHERE SP_UID = ? "
|
||||
+ "AND AP_APP_ID = ? "
|
||||
+ "AND IS_ID = ? "
|
||||
+ "AND TENANT_ID = ? ";
|
||||
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int attachSPApplication(int identityServerId, String spUID, int appId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to map an application wit identity server:service provider");
|
||||
log.debug("Application Details : ");
|
||||
log.debug("App ID : " + appId + " SP UID : " + spUID + " IS ID : " + identityServerId);
|
||||
}
|
||||
String sql = "INSERT INTO AP_IS_SP_APP_MAPPING "
|
||||
+ "(SP_UID, "
|
||||
+ "AP_APP_ID, "
|
||||
+ "IS_ID, TENANT_ID) "
|
||||
+ "VALUES (?, ?, ?, ?)";
|
||||
int mappingId = -1;
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setString(1, spUID);
|
||||
stmt.setInt(2, appId);
|
||||
stmt.setInt(3, identityServerId);
|
||||
stmt.setInt(4, tenantId);
|
||||
stmt.executeUpdate();
|
||||
try (ResultSet rs = stmt.getGeneratedKeys()) {
|
||||
if (rs.next()) {
|
||||
mappingId = rs.getInt(1);
|
||||
}
|
||||
return mappingId;
|
||||
}
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteApplicationFromServiceProviders(int applicationId, int tenantId) throws ApplicationManagementDAOException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Request received in DAO Layer to delete application with the id: " + applicationId + " from service providers");
|
||||
}
|
||||
String sql = "DELETE FROM AP_IS_SP_APP_MAPPING WHERE AP_APP_ID = ? AND TENANT_ID = ?";
|
||||
try {
|
||||
Connection conn = this.getDBConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
|
||||
stmt.setInt(1, applicationId);
|
||||
stmt.setInt(2, tenantId);
|
||||
stmt.executeUpdate();
|
||||
}
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while obtaining the DB connection to create an sp application mapping which has "
|
||||
+ "application id " + applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error occurred while executing SQL to create an application which has application id "
|
||||
+ applicationId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementDAOException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,347 @@
|
||||
package io.entgra.application.mgt.core.impl;
|
||||
|
||||
import io.entgra.application.mgt.common.ApplicationArtifact;
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.IdentityServerList;
|
||||
import io.entgra.application.mgt.common.SPApplication;
|
||||
import io.entgra.application.mgt.common.dto.ApplicationDTO;
|
||||
import io.entgra.application.mgt.common.exception.ApplicationManagementException;
|
||||
import io.entgra.application.mgt.common.exception.DBConnectionException;
|
||||
import io.entgra.application.mgt.common.exception.RequestValidatingException;
|
||||
import io.entgra.application.mgt.common.exception.TransactionManagementException;
|
||||
import io.entgra.application.mgt.common.response.Application;
|
||||
import io.entgra.application.mgt.common.services.ApplicationManager;
|
||||
import io.entgra.application.mgt.common.services.SPApplicationManager;
|
||||
import io.entgra.application.mgt.common.wrapper.ApplicationWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.CustomAppReleaseWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.CustomAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.EntAppReleaseWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.PublicAppReleaseWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.PublicAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.WebAppReleaseWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.WebAppWrapper;
|
||||
import io.entgra.application.mgt.core.dao.ApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.SPApplicationDAO;
|
||||
import io.entgra.application.mgt.core.dao.VisibilityDAO;
|
||||
import io.entgra.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
|
||||
import io.entgra.application.mgt.core.exception.ApplicationManagementDAOException;
|
||||
import io.entgra.application.mgt.core.exception.BadRequestException;
|
||||
import io.entgra.application.mgt.core.internal.DataHolder;
|
||||
import io.entgra.application.mgt.core.lifecycle.LifecycleStateManager;
|
||||
import io.entgra.application.mgt.core.util.APIUtil;
|
||||
import io.entgra.application.mgt.core.util.ApplicationManagementUtil;
|
||||
import io.entgra.application.mgt.core.util.ConnectionManagerUtil;
|
||||
import io.entgra.application.mgt.core.util.Constants;
|
||||
import io.entgra.application.mgt.core.util.SPApplicationManagementUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SPApplicationManagerImpl implements SPApplicationManager {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SPApplicationManagerImpl.class);
|
||||
private ApplicationDAO applicationDAO;
|
||||
private SPApplicationDAO spApplicationDAO;
|
||||
private VisibilityDAO visibilityDAO;
|
||||
private final LifecycleStateManager lifecycleStateManager;
|
||||
|
||||
public SPApplicationManagerImpl() {
|
||||
initDataAccessObjects();
|
||||
lifecycleStateManager = DataHolder.getInstance().getLifecycleStateManager();
|
||||
}
|
||||
|
||||
private void initDataAccessObjects() {
|
||||
this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
|
||||
this.visibilityDAO = ApplicationManagementDAOFactory.getVisibilityDAO();
|
||||
this.spApplicationDAO = ApplicationManagementDAOFactory.getSPApplicationDAO();
|
||||
}
|
||||
|
||||
public void addExistingApps(int identityServerId, List<SPApplication> applications) throws ApplicationManagementException {
|
||||
for (SPApplication application : applications) {
|
||||
List<Application> existingApplications = getSPApplications(identityServerId, application.getId());
|
||||
application.setExistingApplications(existingApplications);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServer getIdentityServer(int identityServerId) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
return spApplicationDAO.getIdentityServerById(identityServerId, tenantId);
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred when getting database connection to get identity server with the id: " + identityServerId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg =
|
||||
"DAO exception while getting identity server with the id " + identityServerId ;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IdentityServerList getIdentityServers() throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
IdentityServerList identityServerList = new IdentityServerList();
|
||||
identityServerList.setIdentityServers(spApplicationDAO.getIdentityServers(tenantId));
|
||||
return identityServerList;
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred when getting database connection to get identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg =
|
||||
"DAO exception while getting identity servers";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Application> getSPApplications(int identityServerId, String spUID) throws
|
||||
ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
ApplicationManager applicationManager = APIUtil.getApplicationManager();
|
||||
List<Application> applications = new ArrayList<>();
|
||||
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
List<ApplicationDTO> appDTOs = spApplicationDAO.getSPApplications(identityServerId, spUID, tenantId);
|
||||
for (ApplicationDTO applicationDTO : appDTOs) {
|
||||
if (lifecycleStateManager.getEndState().equals(applicationDTO.getStatus())) {
|
||||
continue;
|
||||
}
|
||||
boolean isHideableApp = applicationManager.isHideableApp(applicationDTO.getApplicationReleaseDTOs());
|
||||
boolean isDeletableApp = applicationManager.isDeletableApp(applicationDTO.getApplicationReleaseDTOs());
|
||||
|
||||
//Set application categories, tags and unrestricted roles to the application DTO.
|
||||
applicationDTO
|
||||
.setUnrestrictedRoles(visibilityDAO.getUnrestrictedRoles(applicationDTO.getId(), tenantId));
|
||||
applicationDTO.setAppCategories(applicationDAO.getAppCategories(applicationDTO.getId(), tenantId));
|
||||
applicationDTO.setTags(applicationDAO.getAppTags(applicationDTO.getId(), tenantId));
|
||||
|
||||
applicationDTO.setApplicationReleaseDTOs(applicationDTO.getApplicationReleaseDTOs());
|
||||
Application application = APIUtil.appDtoToAppResponse(applicationDTO);
|
||||
application.setDeletableApp(isDeletableApp);
|
||||
application.setHideableApp(isHideableApp);
|
||||
applications.add(application);
|
||||
}
|
||||
|
||||
return applications;
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred when getting database connection to get applications by filtering from "
|
||||
+ "requested filter.";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg =
|
||||
"DAO exception while getting applications of tenant " + tenantId ;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void validateAttachAppsRequest(int identityServerId, List<Integer> appIds) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
for (int appId : appIds) {
|
||||
try {
|
||||
ApplicationDTO appDTO = applicationDAO.getApplication(appId, tenantId);
|
||||
if (appDTO == null) {
|
||||
String msg = "Payload contains invalid an app id. " + "No app exist with the appId: " + appId + ".";
|
||||
throw new BadRequestException(msg);
|
||||
}
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg = "Error occurred while trying to retrieve application with the id:" + appId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void validateDetachAppsRequest(int identityServerId, String spId, List<Integer> appIds) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
|
||||
try {
|
||||
ConnectionManagerUtil.openDBConnection();
|
||||
for (int id : appIds) {
|
||||
try {
|
||||
boolean isSPAppExist = spApplicationDAO.isSPApplicationExist(identityServerId, spId, id, tenantId);
|
||||
if (!isSPAppExist) {
|
||||
String msg = "No service provider app exist with the appId: " + id + " for service provider with the " +
|
||||
"UID " + spId;
|
||||
throw new ApplicationManagementException(msg);
|
||||
}
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg = "Error occurred while checking if application exists with the id:" + id;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public void attachSPApplications(int identityServerId, String spUID, List<Integer> appIds) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
for (int appId : appIds) {
|
||||
spApplicationDAO.attachSPApplication(identityServerId, spUID, appId, tenantId);
|
||||
}
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
} catch (ApplicationManagementDAOException e){
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg =
|
||||
"DAO exception while getting applications of tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void detachSPApplications(int identityServerId, String spUID, List<Integer> appIds) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
for (int id : appIds) {
|
||||
spApplicationDAO.detachSPApplication(identityServerId, spUID, id, tenantId);
|
||||
}
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
} catch (ApplicationManagementDAOException e){
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg =
|
||||
"DAO exception while getting applications of tenant " + tenantId;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> Application createSPApplication(T app, int identityServerId, String spId) throws ApplicationManagementException,
|
||||
RequestValidatingException {
|
||||
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
|
||||
applicationManager.validateAppCreatingRequest(app, false);
|
||||
ApplicationDTO applicationDTO = constructApplicationDTO(app);
|
||||
try {
|
||||
ConnectionManagerUtil.beginDBTransaction();
|
||||
Application createdApp = createSPApplication(applicationDTO);
|
||||
attachCreatedSPApplication(createdApp, identityServerId, spId);
|
||||
ConnectionManagerUtil.commitDBTransaction();
|
||||
return createdApp;
|
||||
} catch (DBConnectionException e) {
|
||||
String msg = "Error occurred while getting database connection.";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} catch (TransactionManagementException e) {
|
||||
String msg = "Error occurred while disabling AutoCommit.";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} catch (ApplicationManagementException e) {
|
||||
ConnectionManagerUtil.rollbackDBTransaction();
|
||||
String msg = "Error occurred while creating and attaching application with the name " + applicationDTO.getName() ;
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
ConnectionManagerUtil.closeDBConnection();
|
||||
}
|
||||
}
|
||||
|
||||
private <T> ApplicationDTO constructApplicationDTO(T app) throws ApplicationManagementException,
|
||||
RequestValidatingException {
|
||||
if (ApplicationManagementUtil.isReleaseAvailable(app)) {
|
||||
return uploadReleaseArtifactAndConstructApplicationDTO(app);
|
||||
}
|
||||
return APIUtil.convertToAppDTO(app);
|
||||
}
|
||||
|
||||
public Application createSPApplication(ApplicationDTO applicationDTO) throws ApplicationManagementException {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Application release create request is received. Application name: " + applicationDTO.getName()
|
||||
+ " Device type ID: " + applicationDTO.getDeviceTypeId());
|
||||
}
|
||||
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
|
||||
return applicationManager.executeApplicationPersistenceTransaction(applicationDTO);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void attachCreatedSPApplication(Application createdApp, int identityServerId, String spUID) throws ApplicationManagementException {
|
||||
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
try {
|
||||
spApplicationDAO.attachSPApplication(identityServerId, spUID, createdApp.getId(), tenantId);
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
String msg = "Error occurred while attaching application with the id " + createdApp.getId();
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> ApplicationDTO uploadReleaseArtifactAndConstructApplicationDTO(T app) throws ApplicationManagementException,
|
||||
RequestValidatingException {
|
||||
ApplicationArtifact artifact;
|
||||
ApplicationDTO applicationDTO;
|
||||
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManagerInstance();
|
||||
if (app instanceof ApplicationWrapper) {
|
||||
ApplicationWrapper wrapper = (ApplicationWrapper) app;
|
||||
EntAppReleaseWrapper releaseWrapper = wrapper.getEntAppReleaseWrappers().get(0);
|
||||
applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType());
|
||||
applicationManager.validateBinaryArtifact(releaseWrapper.getBinaryFile());
|
||||
applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
|
||||
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
|
||||
releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), null);
|
||||
applicationDTO = applicationManager.uploadEntAppReleaseArtifacts(wrapper, artifact);
|
||||
} else if (app instanceof PublicAppWrapper) {
|
||||
PublicAppWrapper wrapper = (PublicAppWrapper) app;
|
||||
PublicAppReleaseWrapper releaseWrapper = wrapper.getPublicAppReleaseWrappers().get(0);
|
||||
applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType());
|
||||
applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
|
||||
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
|
||||
releaseWrapper.getScreenshots(), null, null);
|
||||
applicationDTO = applicationManager.uploadPublicAppReleaseArtifacts(wrapper, artifact);
|
||||
} else if (app instanceof WebAppWrapper) {
|
||||
WebAppWrapper wrapper = (WebAppWrapper) app;
|
||||
WebAppReleaseWrapper releaseWrapper = wrapper.getWebAppReleaseWrappers().get(0);
|
||||
applicationManager.validateReleaseCreatingRequest(releaseWrapper, Constants.ANY);
|
||||
applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
|
||||
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
|
||||
releaseWrapper.getScreenshots(), null, null);
|
||||
applicationDTO = applicationManager.uploadWebAppReleaseArtifacts(wrapper, artifact);
|
||||
} else if (app instanceof CustomAppWrapper) {
|
||||
CustomAppWrapper wrapper = (CustomAppWrapper) app;
|
||||
CustomAppReleaseWrapper releaseWrapper = wrapper.getCustomAppReleaseWrappers().get(0);
|
||||
applicationManager.validateReleaseCreatingRequest(releaseWrapper, wrapper.getDeviceType());
|
||||
applicationManager.validateBinaryArtifact(releaseWrapper.getBinaryFile());
|
||||
applicationManager.validateImageArtifacts(releaseWrapper.getIcon(), releaseWrapper.getScreenshots());
|
||||
artifact = ApplicationManagementUtil.constructApplicationArtifact(releaseWrapper.getIcon(),
|
||||
releaseWrapper.getScreenshots(), releaseWrapper.getBinaryFile(), null);
|
||||
applicationDTO = applicationManager.uploadCustomAppReleaseArtifactsAndConstructAppDTO(wrapper, artifact);
|
||||
} else {
|
||||
String msg = "Invalid payload found with the request. Hence verify the request payload object.";
|
||||
log.error(msg);
|
||||
throw new ApplicationManagementException(msg);
|
||||
}
|
||||
return applicationDTO;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.entgra.application.mgt.core.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This DAOUtil class is responsible for making sure single instance of each Extension Manager is used throughout for
|
||||
* all the tasks.
|
||||
*/
|
||||
public class SPApplicationManagementUtil {
|
||||
|
||||
private static Log log = LogFactory.getLog(SPApplicationManagementUtil.class);
|
||||
|
||||
private static List<String> getDefaultSPAppCategories() {
|
||||
List<String> categories = new ArrayList<>();
|
||||
categories.add(Constants.SP_APP_CATEGORY);
|
||||
return categories;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,280 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.application.mgt.publisher.api.services;
|
||||
|
||||
import io.entgra.application.mgt.common.wrapper.ApplicationWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.CustomAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.PublicAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.WebAppWrapper;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.swagger.annotations.Extension;
|
||||
import io.swagger.annotations.ExtensionProperty;
|
||||
import io.swagger.annotations.Info;
|
||||
import io.swagger.annotations.SwaggerDefinition;
|
||||
import io.swagger.annotations.Tag;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scope;
|
||||
import org.wso2.carbon.apimgt.annotations.api.Scopes;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import javax.ws.rs.core.UriInfo;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This is the application registration service that exposed for apimApplicationRegistration
|
||||
*/
|
||||
|
||||
@SwaggerDefinition(
|
||||
info = @Info(
|
||||
version = "1.0.0",
|
||||
title = "Service Provider Application Management Publisher Service",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = "name", value = "SPApplicationService"),
|
||||
@ExtensionProperty(name = "context", value = "/api/application-mgt-publisher/v1.0/identity-server-applications"),
|
||||
})
|
||||
}
|
||||
),
|
||||
tags = {
|
||||
@Tag(name = "application_management, device_management", description = "App publisher related APIs")
|
||||
}
|
||||
)
|
||||
@Scopes(
|
||||
scopes = {
|
||||
@Scope(
|
||||
name = "view a service provider applications",
|
||||
description = "Get application details",
|
||||
key = "perm:app:publisher:service-provider:view",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/app-mgt/publisher/service-provider/application/view"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Create a service provider application",
|
||||
description = "Update an application",
|
||||
key = "perm:app:publisher:service-provider:create",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/app-mgt/publisher/service-provider/application/create"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Attach a service provider application",
|
||||
description = "Update an application",
|
||||
key = "perm:app:publisher:service-provider:attach",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/app-mgt/publisher/service-provider/application/attach"}
|
||||
),
|
||||
@Scope(
|
||||
name = "Detach a service provider application",
|
||||
description = "Update an application",
|
||||
key = "perm:app:publisher:service-provider:detach",
|
||||
roles = {"Internal/devicemgt-user"},
|
||||
permissions = {"/app-mgt/publisher/service-provider/application/detach"}
|
||||
)
|
||||
}
|
||||
)
|
||||
@Path("/identity-server-applications")
|
||||
@Api(value = "SPApplication Management")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public interface SPApplicationService {
|
||||
|
||||
String SCOPE = "scope";
|
||||
|
||||
/**
|
||||
* This method is used to register an APIM application for tenant domain.
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/identity-servers")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response getIdentityServers();
|
||||
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/identity-servers/{id}")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response getIdentityServer(@PathParam("id") int id);
|
||||
|
||||
/**
|
||||
* This method is used to register an APIM application for tenant domain.
|
||||
*/
|
||||
@GET
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("{identity-server-id}/service-providers")
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "GET",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:view")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response getServiceProviders(@QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset,
|
||||
@PathParam("identity-server-id") int identityServerId);
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/attach")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:attach")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response attachApps(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, List<Integer> appIds);
|
||||
|
||||
/**
|
||||
* This method is used to register an APIM application for tenant domain.
|
||||
*/
|
||||
@Path("/{identity-server-id}/{service-provider-id}/detach")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:detach")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response detachApps(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, List<Integer> appIds);
|
||||
|
||||
/**
|
||||
* This method is used to register an APIM application for tenant domain.
|
||||
*/
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/ent-app")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response createEntApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, ApplicationWrapper app);
|
||||
|
||||
|
||||
/**
|
||||
* This method is used to register an APIM application for tenant domain.
|
||||
*/
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/public-app")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response createPubApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, PublicAppWrapper app);
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/web-app")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response createWebApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, WebAppWrapper app);
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/custom-app")
|
||||
@POST
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@ApiOperation(
|
||||
produces = MediaType.APPLICATION_JSON,
|
||||
httpMethod = "POST",
|
||||
value = "get the application of requesting application id and state",
|
||||
notes = "This will get the application identified by the application id and state, if exists",
|
||||
tags = "ApplicationDTO Management",
|
||||
extensions = {
|
||||
@Extension(properties = {
|
||||
@ExtensionProperty(name = SCOPE, value = "perm:app:publisher:service-provider:create")
|
||||
})
|
||||
}
|
||||
)
|
||||
Response createCustomApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, CustomAppWrapper app);
|
||||
}
|
@ -0,0 +1,211 @@
|
||||
/*
|
||||
* Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
*
|
||||
* WSO2 Inc. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package io.entgra.application.mgt.publisher.api.services.impl;
|
||||
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.IdentityServerList;
|
||||
import io.entgra.application.mgt.common.SPApplicationListResponse;
|
||||
import io.entgra.application.mgt.common.exception.ApplicationManagementException;
|
||||
import io.entgra.application.mgt.common.exception.RequestValidatingException;
|
||||
import io.entgra.application.mgt.common.response.Application;
|
||||
import io.entgra.application.mgt.common.services.SPApplicationManager;
|
||||
import io.entgra.application.mgt.common.wrapper.ApplicationWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.CustomAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.PublicAppWrapper;
|
||||
import io.entgra.application.mgt.common.wrapper.WebAppWrapper;
|
||||
import io.entgra.application.mgt.core.exception.BadRequestException;
|
||||
import io.entgra.application.mgt.core.util.APIUtil;
|
||||
import io.entgra.application.mgt.publisher.api.services.SPApplicationService;
|
||||
import io.entgra.application.mgt.publisher.api.services.util.SPAppRequestHandlerUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.POST;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.PathParam;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.QueryParam;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
import javax.ws.rs.core.Response;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Path("/identity-server-applications")
|
||||
public class SPApplicationServiceImpl implements SPApplicationService {
|
||||
private static final Log log = LogFactory.getLog(SPApplicationServiceImpl.class);
|
||||
|
||||
@Path("/identity-servers")
|
||||
@GET
|
||||
@Override
|
||||
public Response getIdentityServers() {
|
||||
try {
|
||||
SPApplicationManager spAppManager = APIUtil.getSPApplicationManager();
|
||||
IdentityServerList identityServers = spAppManager.getIdentityServers();
|
||||
return Response.status(Response.Status.OK).entity(identityServers).build();
|
||||
} catch (ApplicationManagementException e) {
|
||||
String errMsg = "Error occurred while trying to merge identity server apps with existing apps";
|
||||
log.error(errMsg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/identity-servers/{id}")
|
||||
@GET
|
||||
@Override
|
||||
public Response getIdentityServer(@PathParam("id") int id) {
|
||||
try {
|
||||
SPApplicationManager spAppManager = APIUtil.getSPApplicationManager();
|
||||
IdentityServer identityServer = spAppManager.getIdentityServer(id);
|
||||
return Response.status(Response.Status.OK).entity(identityServer).build();
|
||||
} catch (ApplicationManagementException e) {
|
||||
String errMsg = "Error occurred while trying to merge identity server apps with existing apps";
|
||||
log.error(errMsg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/service-providers")
|
||||
@GET
|
||||
@Override
|
||||
public Response getServiceProviders(@QueryParam("limit") Integer limit, @QueryParam("offset") Integer offset,
|
||||
@PathParam("identity-server-id") int identityServerId) {
|
||||
try {
|
||||
SPApplicationManager spAppManager = APIUtil.getSPApplicationManager();
|
||||
SPApplicationListResponse applications = SPAppRequestHandlerUtil.
|
||||
getServiceProvidersFromIdentityServer(identityServerId, limit, offset);
|
||||
spAppManager.addExistingApps(identityServerId, applications.getApplications());
|
||||
return Response.status(Response.Status.OK).entity(applications).build();
|
||||
} catch (ApplicationManagementException e) {
|
||||
String errMsg = "Error occurred while trying to merge identity server apps with existing apps";
|
||||
log.error(errMsg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errMsg).build();
|
||||
}
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/attach")
|
||||
@POST
|
||||
@Override
|
||||
public Response attachApps(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, List<Integer> appIds) {
|
||||
SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager();
|
||||
try {
|
||||
validateServiceProviderUID(identityServerId, serviceProviderId);
|
||||
spApplicationManager.validateAttachAppsRequest(identityServerId, appIds);
|
||||
spApplicationManager.attachSPApplications(identityServerId, serviceProviderId, appIds);
|
||||
} catch (ApplicationManagementException e) {
|
||||
String msg = "Error occurred while attaching apps to service provider with the id" + serviceProviderId;
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/detach")
|
||||
@POST
|
||||
@Override
|
||||
public Response detachApps(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, List<Integer> appIds) {
|
||||
SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager();
|
||||
try {
|
||||
validateServiceProviderUID(identityServerId, serviceProviderId);
|
||||
spApplicationManager.validateDetachAppsRequest(identityServerId, serviceProviderId, appIds);
|
||||
spApplicationManager.detachSPApplications(identityServerId, serviceProviderId, appIds);
|
||||
} catch (ApplicationManagementException e) {
|
||||
String msg = "Error occurred while attaching apps to service provider with the id" + serviceProviderId;
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
return Response.status(Response.Status.OK).build();
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/ent-app")
|
||||
@POST
|
||||
@Override
|
||||
public Response createEntApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, ApplicationWrapper app) {
|
||||
return createSPApplication(identityServerId, serviceProviderId, app);
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/public-app")
|
||||
@POST
|
||||
@Override
|
||||
public Response createPubApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, PublicAppWrapper app) {
|
||||
return createSPApplication(identityServerId, serviceProviderId, app);
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/web-app")
|
||||
@POST
|
||||
@Override
|
||||
public Response createWebApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, WebAppWrapper app) {
|
||||
return createSPApplication(identityServerId, serviceProviderId, app);
|
||||
}
|
||||
|
||||
@Path("/{identity-server-id}/{service-provider-id}/create/custom-app")
|
||||
@POST
|
||||
@Override
|
||||
public Response createCustomApp(@PathParam("identity-server-id") int identityServerId,
|
||||
@PathParam("service-provider-id") String serviceProviderId, CustomAppWrapper app) {
|
||||
return createSPApplication(identityServerId, serviceProviderId, app);
|
||||
}
|
||||
|
||||
private <T> Response createSPApplication(int identityServerId, String serviceProviderId, T appWrapper) {
|
||||
try {
|
||||
validateServiceProviderUID(identityServerId, serviceProviderId);
|
||||
SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager();
|
||||
Application createdApp = spApplicationManager.createSPApplication(appWrapper, identityServerId, serviceProviderId);
|
||||
return Response.status(Response.Status.CREATED).entity(createdApp).build();
|
||||
} catch (BadRequestException e) {
|
||||
String msg = "Found incompatible payload with create service provider app request.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
} catch (RequestValidatingException e) {
|
||||
String msg = "Found invalid release payload with create service provider app request.";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.BAD_REQUEST).entity(msg).build();
|
||||
}
|
||||
catch (ApplicationManagementException e) {
|
||||
String msg = "Error occurred while creating service provider app";
|
||||
log.error(msg, e);
|
||||
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
|
||||
}
|
||||
}
|
||||
|
||||
private void validateServiceProviderUID(int identityServerId, String spUID) throws
|
||||
ApplicationManagementException {
|
||||
try {
|
||||
boolean isSPAppExists = SPAppRequestHandlerUtil.
|
||||
isSPApplicationExist(identityServerId, spUID);
|
||||
if (!isSPAppExists) {
|
||||
String errMsg = "Service provider with the uid " + spUID + " does not exist.";
|
||||
log.error(errMsg);
|
||||
throw new BadRequestException(errMsg);
|
||||
}
|
||||
} catch (ApplicationManagementException e) {
|
||||
String errMsg = "Error occurred while trying to validate service provider uid";
|
||||
log.error(errMsg, e);
|
||||
throw new ApplicationManagementException(errMsg, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
package io.entgra.application.mgt.publisher.api.services.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import io.entgra.application.mgt.common.IdentityServer;
|
||||
import io.entgra.application.mgt.common.SPApplication;
|
||||
import io.entgra.application.mgt.common.SPApplicationListResponse;
|
||||
import io.entgra.application.mgt.common.exception.ApplicationManagementException;
|
||||
import io.entgra.application.mgt.common.services.SPApplicationManager;
|
||||
import io.entgra.application.mgt.core.util.APIUtil;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.HttpClient;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpRequestBase;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.impl.client.HttpClients;
|
||||
import org.wso2.carbon.device.mgt.core.common.util.HttpUtil;
|
||||
|
||||
import javax.ws.rs.core.HttpHeaders;
|
||||
import javax.ws.rs.core.UriBuilder;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
|
||||
public class SPAppRequestHandlerUtil {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SPAppRequestHandlerUtil.class);
|
||||
|
||||
public static boolean isSPApplicationExist(int identityServerId, String spAppId) throws ApplicationManagementException {
|
||||
SPApplication application = retrieveSPApplication(identityServerId, spAppId);
|
||||
if (application == null) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static SPApplicationListResponse getServiceProvidersFromIdentityServer(int identityServerId, Integer limit, Integer offSet)
|
||||
throws ApplicationManagementException {
|
||||
return retrieveSPApplications(identityServerId, limit, offSet);
|
||||
}
|
||||
|
||||
public static SPApplication retrieveSPApplication(int identityServerId, String spAppId)
|
||||
throws ApplicationManagementException {
|
||||
IdentityServer identityServer = getIdentityServer(identityServerId);
|
||||
HttpGet req = new HttpGet();
|
||||
URI uri = HttpUtil.createURI(getSPApplicationsAPI(identityServer));
|
||||
uri = UriBuilder.fromUri(uri).path(spAppId).build();
|
||||
req.setURI(uri);
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
try {
|
||||
HttpResponse response = invokeISAPI(identityServer, client, req);
|
||||
String responseBody = HttpUtil.getResponseString(response);
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
return new Gson().fromJson(responseBody,
|
||||
SPApplication.class);
|
||||
}
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND) {
|
||||
return null;
|
||||
}
|
||||
String msg = "Error occurred while calling SP Applications API";
|
||||
log.error(msg);
|
||||
throw new ApplicationManagementException(msg);
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while calling SP Applications API";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred while closing http connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static SPApplicationListResponse retrieveSPApplications(int identityServerId, Integer limit, Integer offset)
|
||||
throws ApplicationManagementException {
|
||||
IdentityServer identityServer = getIdentityServer(identityServerId);
|
||||
HttpGet req = new HttpGet();
|
||||
URI uri = HttpUtil.createURI(getSPApplicationsAPI(identityServer));
|
||||
UriBuilder uriBuilder = UriBuilder.fromUri(uri);
|
||||
if (limit != null) {
|
||||
uriBuilder = uriBuilder.queryParam(io.entgra.application.mgt.core.util.Constants.LIMIT_QUERY_PARAM, limit);
|
||||
}
|
||||
if (offset != null) {
|
||||
uriBuilder = uriBuilder.queryParam(io.entgra.application.mgt.core.util.Constants.OFFSET_QUERY_PARAM, offset);
|
||||
}
|
||||
uri = uriBuilder.build();
|
||||
req.setURI(uri);
|
||||
CloseableHttpClient client = HttpClients.createDefault();
|
||||
try {
|
||||
HttpResponse response = invokeISAPI(identityServer, client, req);
|
||||
String responseBody = HttpUtil.getResponseString(response);
|
||||
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
|
||||
return new Gson().fromJson(responseBody,
|
||||
SPApplicationListResponse.class);
|
||||
}
|
||||
String msg = "Error occurred while calling SP Applications API";
|
||||
log.error(msg);
|
||||
throw new ApplicationManagementException(msg);
|
||||
} catch (IOException e) {
|
||||
String msg = "Error occurred while calling SP Applications API";
|
||||
log.error(msg, e);
|
||||
throw new ApplicationManagementException(msg, e);
|
||||
} finally {
|
||||
try {
|
||||
client.close();
|
||||
} catch (IOException e) {
|
||||
log.error("Error occurred while closing http connection");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static IdentityServer getIdentityServer(int identityServerId) throws ApplicationManagementException {
|
||||
SPApplicationManager spApplicationManager = APIUtil.getSPApplicationManager();
|
||||
return spApplicationManager.getIdentityServer(identityServerId);
|
||||
}
|
||||
|
||||
public static HttpResponse invokeISAPI(IdentityServer identityServer, HttpClient client, HttpRequestBase request) throws IOException {
|
||||
setBasicAuthHeader(identityServer, request);
|
||||
return client.execute(request);
|
||||
}
|
||||
|
||||
public static void setBasicAuthHeader(IdentityServer identityServer, HttpRequestBase request) {
|
||||
String basicAuthHeader = HttpUtil.getBasicAuthBase64Header(identityServer.getUserName(),
|
||||
identityServer.getPassword());
|
||||
request.setHeader(HttpHeaders.AUTHORIZATION, basicAuthHeader);
|
||||
}
|
||||
|
||||
private static String getSPApplicationsAPI(IdentityServer identityServer) {
|
||||
String api = identityServer.getSpAppsApi();
|
||||
return api;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package org.wso2.carbon.device.mgt.core.common;
|
||||
|
||||
public class Constants {
|
||||
public static final String SCHEME_SEPARATOR = "://";
|
||||
public static final String COLON = ":";
|
||||
public static final String URI_QUERY_SEPARATOR = "?";
|
||||
public static final String URI_SEPARATOR = "/";
|
||||
public static final String BASIC_AUTH_HEADER_PREFIX = "Basic ";
|
||||
public static final String BEARER = "Bearer ";
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package org.wso2.carbon.device.mgt.core.common.util;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.Base64;
|
||||
|
||||
public class FileUtil {
|
||||
|
||||
public static String removePathSeparatorFromBase64String(String base64String) {
|
||||
String partSeparator = ",";
|
||||
if (base64String.contains(partSeparator)) {
|
||||
return base64String.split(partSeparator)[1];
|
||||
}
|
||||
return base64String;
|
||||
}
|
||||
|
||||
public static InputStream base64ToInputStream(String base64) {
|
||||
base64 = FileUtil.removePathSeparatorFromBase64String(base64);
|
||||
byte[] base64Bytes = Base64.getDecoder().decode(base64);
|
||||
return new ByteArrayInputStream(base64Bytes);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
/*
|
||||
* Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved.
|
||||
*
|
||||
* Entgra (pvt) Ltd. licenses this file to you under the Apache License,
|
||||
* Version 2.0 (the "License"); you may not use this file except
|
||||
* in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.core.common.util;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.wso2.carbon.device.mgt.core.common.Constants;
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class HttpUtil {
|
||||
|
||||
public static URI createURI(String uriString) {
|
||||
uriString = uriString.replace(" ", "%20");
|
||||
return URI.create(uriString);
|
||||
}
|
||||
|
||||
public static String getBasicAuthBase64Header(String userName, String password) {
|
||||
return Constants.BASIC_AUTH_HEADER_PREFIX + getBase64Encode(userName, password);
|
||||
}
|
||||
|
||||
public static String getBase64Encode(String key, String value) {
|
||||
return new String(Base64.encodeBase64((key + ":" + value).getBytes()));
|
||||
}
|
||||
|
||||
public static String getRequestSubPathFromEnd(URI requestUri, int position) {
|
||||
if (requestUri.getPath() != null) {
|
||||
String[] pathList = requestUri.getPath().split("/");
|
||||
if (pathList.length - 1 >= position) {
|
||||
return pathList[pathList.length - 1 - position];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getRequestSubPath(String fullPath, int position) {
|
||||
String[] pathList = fullPath.split("/");
|
||||
if (pathList.length - 1 > position) {
|
||||
String path = pathList[position + 1];
|
||||
if(path.contains(Constants.URI_QUERY_SEPARATOR)) {
|
||||
path = path.substring(0, path.indexOf(Constants.URI_QUERY_SEPARATOR));
|
||||
}
|
||||
return path;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getResponseString(HttpResponse response) throws IOException {
|
||||
return EntityUtils.toString(response.getEntity());
|
||||
}
|
||||
|
||||
public static boolean isQueryParamExist(String param, URI request) {
|
||||
Map<String, List<String>> queryMap = getQueryMap(request);
|
||||
return queryMap.containsKey(param);
|
||||
}
|
||||
public static String getFirstQueryValue(String param, Map<String, List<String>> queryMap) {
|
||||
List<String> valueList = queryMap.get(param);
|
||||
String firstValue = null;
|
||||
if(valueList != null) {
|
||||
firstValue = valueList.get(0);
|
||||
}
|
||||
return firstValue;
|
||||
}
|
||||
public static Map<String, List<String>> getQueryMap(String uri) {
|
||||
String query = getQueryFromURIPath(uri);
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
if (query != null) {
|
||||
String[] params = query.split("&");
|
||||
for (String param : params) {
|
||||
String[] paramArr = param.split("=");
|
||||
if (paramArr.length == 2) {
|
||||
String name = paramArr[0];
|
||||
String value = paramArr[1];
|
||||
if (!map.containsKey(name)) {
|
||||
List<String> valueList = new ArrayList<>();
|
||||
map.put(name, valueList);
|
||||
}
|
||||
map.get(name).add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
public static Map<String, List<String>> getQueryMap(URI request) {
|
||||
String query = request.getQuery();
|
||||
Map<String, List<String>> map = new HashMap<>();
|
||||
if (query != null) {
|
||||
String[] params = query.split("&");
|
||||
for (String param : params) {
|
||||
String[] paramArr = param.split("=");
|
||||
if (paramArr.length == 2) {
|
||||
String name = paramArr[0];
|
||||
String value = paramArr[1];
|
||||
if (!map.containsKey(name)) {
|
||||
List<String> valueList = new ArrayList<>();
|
||||
map.put(name, valueList);
|
||||
}
|
||||
map.get(name).add(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
public static String getQueryFromURIPath(String uri) {
|
||||
String query = null;
|
||||
if (uri.length() > "?".length() && uri.contains("?")) {
|
||||
query = uri.substring(uri.lastIndexOf("?") + "?".length());
|
||||
}
|
||||
if (StringUtils.isEmpty(query)) {
|
||||
query = null;
|
||||
}
|
||||
return query;
|
||||
}
|
||||
|
||||
public static String getContentType(HttpResponse response) {
|
||||
ContentType contentType = ContentType.getOrDefault(response.getEntity());
|
||||
return contentType.getMimeType();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue