forked from community/device-mgt-core
commit
8d38c37d53
@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.common;
|
||||
|
||||
public class IllegalTransactionStateException extends RuntimeException {
|
||||
|
||||
private static final long serialVersionUID = -3151279331929070297L;
|
||||
|
||||
private String errorMessage;
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public IllegalTransactionStateException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public IllegalTransactionStateException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
setErrorMessage(message);
|
||||
}
|
||||
|
||||
public IllegalTransactionStateException(String msg) {
|
||||
super(msg);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public IllegalTransactionStateException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public IllegalTransactionStateException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,93 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.common.notification.mgt;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
|
||||
/**
|
||||
* DTO of Notification object which is used to communicate Operation notifications to MDM core.
|
||||
*/
|
||||
public class Notification {
|
||||
|
||||
public enum Status{
|
||||
NEW, CHECKED
|
||||
}
|
||||
|
||||
public enum Type{
|
||||
ALERT,
|
||||
}
|
||||
|
||||
private int notificationId;
|
||||
private DeviceIdentifier deviceIdentifier;
|
||||
private String description;
|
||||
private int operationId;
|
||||
private Status status;
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(String status) {
|
||||
this.status = Status.valueOf(status);
|
||||
}
|
||||
|
||||
public int getNotificationId() {
|
||||
return notificationId;
|
||||
}
|
||||
|
||||
public void setNotificationId(int notificationId) {
|
||||
this.notificationId = notificationId;
|
||||
}
|
||||
|
||||
public DeviceIdentifier getDeviceIdentifier() {
|
||||
return deviceIdentifier;
|
||||
}
|
||||
|
||||
public void setDeviceIdentifier(DeviceIdentifier deviceIdentifier) {
|
||||
this.deviceIdentifier = deviceIdentifier;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setDescription(String description) {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public int getOperationId() {
|
||||
return operationId;
|
||||
}
|
||||
|
||||
public void setOperationId(int operationId) {
|
||||
this.operationId = operationId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Notification{" +
|
||||
"notificationId='" + notificationId + '\'' +
|
||||
", deviceId=" + deviceIdentifier.getId() +
|
||||
", deviceType=" + deviceIdentifier.getType() +
|
||||
", status=" + status +
|
||||
", description='" + description + '\'' +
|
||||
", operationId='" + operationId + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.common.notification.mgt;
|
||||
|
||||
/**
|
||||
* Custom exception class to be used in NotificationMgmt related functionalities.
|
||||
*/
|
||||
public class NotificationManagementException extends Exception {
|
||||
|
||||
private static final long serialVersionUID = -8933146283800122660L;
|
||||
private String errorMessage;
|
||||
|
||||
public String getErrorMessage() {
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
public void setErrorMessage(String errorMessage) {
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
|
||||
public NotificationManagementException(String msg, Exception nestedEx) {
|
||||
super(msg, nestedEx);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public NotificationManagementException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
setErrorMessage(message);
|
||||
}
|
||||
|
||||
public NotificationManagementException(String msg) {
|
||||
super(msg);
|
||||
setErrorMessage(msg);
|
||||
}
|
||||
|
||||
public NotificationManagementException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public NotificationManagementException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.common.notification.mgt;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Defines the contract of NotificationManagementService.
|
||||
*/
|
||||
public interface NotificationManagementService {
|
||||
|
||||
public boolean addNotification(Notification notification) throws NotificationManagementException;
|
||||
public boolean updateNotification(Notification notification) throws NotificationManagementException;
|
||||
public boolean updateNotificationStatus(int notificationId, Notification.Status status) throws
|
||||
NotificationManagementException;
|
||||
public List<Notification> getAllNotifications() throws NotificationManagementException;
|
||||
public List<Notification> getNotificationsByStatus(Notification.Status status) throws
|
||||
NotificationManagementException;
|
||||
|
||||
}
|
@ -0,0 +1,172 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.core.notification.mgt;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.Device;
|
||||
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class implements the NotificationManagementService.
|
||||
*/
|
||||
public class NotificationManagementServiceImpl implements NotificationManagementService {
|
||||
|
||||
private static final Log log = LogFactory.getLog(NotificationManagementServiceImpl.class);
|
||||
|
||||
private NotificationDAO notificationDAO;
|
||||
private DeviceDAO deviceDAO;
|
||||
|
||||
public NotificationManagementServiceImpl() {
|
||||
this.notificationDAO = NotificationManagementDAOFactory.getNotificationDAO();
|
||||
this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addNotification(Notification notification) throws NotificationManagementException {
|
||||
boolean status = false;
|
||||
int deviceId, tenantId;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Adding a Notification : [" + notification.toString() + "]");
|
||||
}
|
||||
try {
|
||||
tenantId = NotificationDAOUtil.getTenantId();
|
||||
DeviceManagementDAOFactory.openConnection();
|
||||
Device device = deviceDAO.getDevice(notification.getDeviceIdentifier(), tenantId);
|
||||
deviceId = device.getId();
|
||||
} catch (SQLException e) {
|
||||
throw new NotificationManagementException("Error occurred while opening a connection to" +
|
||||
" the data source", e);
|
||||
} catch (DeviceManagementDAOException e) {
|
||||
throw new NotificationManagementException("Error occurred while retriving device data for " +
|
||||
" adding notification", e);
|
||||
} finally {
|
||||
DeviceManagementDAOFactory.closeConnection();
|
||||
}
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
int notificationId = notificationDAO.addNotification(deviceId, tenantId, notification);
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Notification id : " + notificationId +" was added to the table.");
|
||||
}
|
||||
if(notificationId > 0) {
|
||||
status = true;
|
||||
}
|
||||
} catch (TransactionManagementException e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while adding notification", e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateNotification(Notification notification) throws NotificationManagementException {
|
||||
boolean status = false;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Updating Notification : [" + notification.toString() + "]");
|
||||
}
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
if(notificationDAO.updateNotification(notification) > 0 ) {
|
||||
status = true;
|
||||
}
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Notification id : " + notification.getNotificationId() +
|
||||
" has updated successfully.");
|
||||
}
|
||||
} catch (TransactionManagementException e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while updating notification ", e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean updateNotificationStatus(int notificationId, Notification.Status status)
|
||||
throws NotificationManagementException {
|
||||
boolean operationStatus = false;
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Updating Notification id : " + notificationId);
|
||||
}
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
if(notificationDAO.updateNotificationStatus(notificationId, status) > 0 ) {
|
||||
operationStatus = true;
|
||||
}
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Notification id : " + notificationId +" has updated successfully.");
|
||||
}
|
||||
} catch (TransactionManagementException e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while updating notification", e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return operationStatus;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notification> getAllNotifications() throws NotificationManagementException{
|
||||
try {
|
||||
NotificationManagementDAOFactory.openConnection();
|
||||
return notificationDAO.getAllNotifications(NotificationDAOUtil.getTenantId());
|
||||
} catch (SQLException e) {
|
||||
throw new NotificationManagementException("Error occurred while opening a connection to" +
|
||||
" the data source", e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notification> getNotificationsByStatus(Notification.Status status)
|
||||
throws NotificationManagementException{
|
||||
try {
|
||||
NotificationManagementDAOFactory.openConnection();
|
||||
return notificationDAO.getNotificationsByStatus(status, NotificationDAOUtil.getTenantId());
|
||||
} catch (SQLException e) {
|
||||
throw new NotificationManagementException("Error occurred while opening a connection " +
|
||||
"to the data source", e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.core.notification.mgt.dao;
|
||||
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* This class defines the methods to be implemented by NotificationDAO layer.
|
||||
*/
|
||||
public interface NotificationDAO {
|
||||
|
||||
/**
|
||||
* This method is used to add a notification.
|
||||
*
|
||||
* @param deviceId device id.
|
||||
* @param tenantId tenant id.
|
||||
* @param notification Notification object.
|
||||
* @return returns the id of the persisted Notification record.
|
||||
* @throws NotificationManagementException
|
||||
*/
|
||||
int addNotification(int deviceId, int tenantId, Notification notification) throws
|
||||
NotificationManagementException;
|
||||
|
||||
/**
|
||||
* This method is used to update a notification.
|
||||
*
|
||||
* @param notification Notification object.
|
||||
* @return returns the no of updated records.
|
||||
* @throws NotificationManagementException
|
||||
*/
|
||||
int updateNotification(Notification notification) throws NotificationManagementException;
|
||||
|
||||
/**
|
||||
* This method is used to update a notification status.
|
||||
*
|
||||
* @param notificationId notification id.
|
||||
* @param status Notification.Status.
|
||||
* @return returns the no of updated records.
|
||||
* @throws NotificationManagementException
|
||||
*/
|
||||
int updateNotificationStatus(int notificationId, Notification.Status status)
|
||||
throws NotificationManagementException;
|
||||
|
||||
/**
|
||||
* This method is used to get all notifications based on tenant-id.
|
||||
*
|
||||
* @param tenantId tenant id.
|
||||
* @return returns the matching notifications.
|
||||
* @throws NotificationManagementException
|
||||
*/
|
||||
List<Notification> getAllNotifications(int tenantId) throws NotificationManagementException;
|
||||
|
||||
/**
|
||||
* This method is used to get all notifications based on notification-status.
|
||||
*
|
||||
* @param status Notification.Status.
|
||||
* @param tenantId tenant id.
|
||||
* @return returns the matching notifications.
|
||||
* @throws NotificationManagementException
|
||||
*/
|
||||
List<Notification> getNotificationsByStatus(Notification.Status status, int tenantId) throws
|
||||
NotificationManagementException;
|
||||
|
||||
}
|
@ -0,0 +1,171 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.core.notification.mgt.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException;
|
||||
import org.wso2.carbon.device.mgt.common.TransactionManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
|
||||
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl.NotificationDAOImpl;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* DAO factory class to be used in NotificationManagement related functionalities.
|
||||
*/
|
||||
public class NotificationManagementDAOFactory {
|
||||
|
||||
private static DataSource dataSource;
|
||||
private static final Log log = LogFactory.getLog(NotificationManagementDAOFactory.class);
|
||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||
|
||||
public static NotificationDAO getNotificationDAO() {
|
||||
return new NotificationDAOImpl();
|
||||
}
|
||||
|
||||
public static void init(DataSourceConfig config) {
|
||||
dataSource = resolveDataSource(config);
|
||||
}
|
||||
|
||||
public static void init(DataSource dtSource) {
|
||||
dataSource = dtSource;
|
||||
}
|
||||
|
||||
public static void beginTransaction() throws TransactionManagementException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn != null) {
|
||||
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
|
||||
"transaction is already active is a sign of improper transaction handling");
|
||||
}
|
||||
try {
|
||||
conn = dataSource.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
currentConnection.set(conn);
|
||||
} catch (SQLException e) {
|
||||
throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void openConnection() throws SQLException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn != null) {
|
||||
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||
"this particular thread. Therefore, calling 'beginTransaction/openConnection' while another " +
|
||||
"transaction is already active is a sign of improper transaction handling");
|
||||
}
|
||||
conn = dataSource.getConnection();
|
||||
currentConnection.set(conn);
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws SQLException {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||
"'beginTransaction'/'openConnection' methods");
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public static void commitTransaction() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||
"'beginTransaction'/'openConnection' methods");
|
||||
}
|
||||
try {
|
||||
conn.commit();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while committing the transaction", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void rollbackTransaction() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||
"'beginTransaction'/'openConnection' methods");
|
||||
}
|
||||
try {
|
||||
conn.rollback();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() {
|
||||
Connection conn = currentConnection.get();
|
||||
if (conn == null) {
|
||||
throw new IllegalTransactionStateException("No connection is associated with the current transaction. " +
|
||||
"This might have ideally been caused by not properly initiating the transaction via " +
|
||||
"'beginTransaction'/'openConnection' methods");
|
||||
}
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while close the connection");
|
||||
}
|
||||
currentConnection.remove();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resolve data source from the data source definition
|
||||
*
|
||||
* @param config data source configuration
|
||||
* @return data source resolved from the data source definition
|
||||
*/
|
||||
private static DataSource resolveDataSource(DataSourceConfig config) {
|
||||
DataSource dataSource = null;
|
||||
if (config == null) {
|
||||
throw new RuntimeException(
|
||||
"Device Management Repository data source configuration " + "is null and " +
|
||||
"thus, is not initialized");
|
||||
}
|
||||
JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition();
|
||||
if (jndiConfig != null) {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Initializing Device Management Repository data source using the JNDI " +
|
||||
"Lookup Definition");
|
||||
}
|
||||
List<JNDILookupDefinition.JNDIProperty> jndiPropertyList =
|
||||
jndiConfig.getJndiProperties();
|
||||
if (jndiPropertyList != null) {
|
||||
Hashtable<Object, Object> jndiProperties = new Hashtable<Object, Object>();
|
||||
for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) {
|
||||
jndiProperties.put(prop.getName(), prop.getValue());
|
||||
}
|
||||
dataSource = NotificationDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties);
|
||||
} else {
|
||||
dataSource = NotificationDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null);
|
||||
}
|
||||
}
|
||||
return dataSource;
|
||||
}
|
||||
}
|
@ -0,0 +1,214 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.core.notification.mgt.dao.impl;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.Notification;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil;
|
||||
|
||||
import java.sql.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Implementation of NotificationDAO which includes the methods to do CRUD operations on notification.
|
||||
*/
|
||||
public class NotificationDAOImpl implements NotificationDAO {
|
||||
|
||||
private static final Log log = LogFactory.getLog(NotificationDAOImpl.class);
|
||||
|
||||
@Override
|
||||
public int addNotification(int deviceId, int tenantId, Notification notification) throws
|
||||
NotificationManagementException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt;
|
||||
ResultSet rs;
|
||||
int notificationId = -1;
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
conn = NotificationManagementDAOFactory.getConnection();
|
||||
String sql =
|
||||
"INSERT INTO DM_NOTIFICATION(DEVICE_ID, OPERATION_ID, STATUS, DESCRIPTION, TENANT_ID) " +
|
||||
"VALUES (?, ?, ?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
|
||||
stmt.setInt(1, deviceId);
|
||||
stmt.setInt(2, notification.getOperationId());
|
||||
stmt.setString(3, notification.getStatus().toString());
|
||||
stmt.setString(4, notification.getDescription());
|
||||
stmt.setInt(5, tenantId);
|
||||
stmt.execute();
|
||||
rs = stmt.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
notificationId = rs.getInt(1);
|
||||
}
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
} catch (Exception e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while adding the " +
|
||||
"Notification for device id : " + deviceId,
|
||||
e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return notificationId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateNotification(Notification notification)
|
||||
throws NotificationManagementException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt;
|
||||
int rows;
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
conn = NotificationManagementDAOFactory.getConnection();
|
||||
String sql = "UPDATE DM_NOTIFICATION SET OPERATION_ID = ?, STATUS = ?, DESCRIPTION = ? " +
|
||||
"WHERE NOTIFICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, notification.getOperationId());
|
||||
stmt.setString(2, notification.getStatus().toString());
|
||||
stmt.setString(3, notification.getDescription());
|
||||
stmt.setInt(4, notification.getNotificationId());
|
||||
rows = stmt.executeUpdate();
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
} catch (Exception e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while updating the " +
|
||||
"Notification id : " + notification.getNotificationId(), e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int updateNotificationStatus(int notificationId, Notification.Status status)
|
||||
throws NotificationManagementException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt;
|
||||
int rows;
|
||||
try {
|
||||
NotificationManagementDAOFactory.beginTransaction();
|
||||
conn = NotificationManagementDAOFactory.getConnection();
|
||||
String sql = "UPDATE DM_NOTIFICATION SET STATUS = ? WHERE NOTIFICATION_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, status.toString());
|
||||
stmt.setInt(2, notificationId);
|
||||
rows = stmt.executeUpdate();
|
||||
NotificationManagementDAOFactory.commitTransaction();
|
||||
} catch (Exception e) {
|
||||
NotificationManagementDAOFactory.rollbackTransaction();
|
||||
throw new NotificationManagementException("Error occurred while updating the status of " +
|
||||
"Notification id : " + notificationId, e);
|
||||
} finally {
|
||||
NotificationManagementDAOFactory.closeConnection();
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notification> getAllNotifications(int tenantId)
|
||||
throws NotificationManagementException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
List<Notification> notifications = null;
|
||||
try {
|
||||
conn = NotificationManagementDAOFactory.getConnection();
|
||||
String sql =
|
||||
"SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS," +
|
||||
" n1.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
|
||||
"DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT NOTIFICATION_ID, DEVICE_ID, " +
|
||||
"OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
||||
"TENANT_ID = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID " +
|
||||
"AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setInt(2, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
notifications = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
notifications.add(this.getNotification(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new NotificationManagementException(
|
||||
"Error occurred while retrieving information of all " +
|
||||
"notifications", e);
|
||||
} finally {
|
||||
NotificationDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return notifications;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Notification> getNotificationsByStatus(Notification.Status status, int tenantId)
|
||||
throws NotificationManagementException {
|
||||
Connection conn;
|
||||
PreparedStatement stmt = null;
|
||||
ResultSet rs = null;
|
||||
List<Notification> notifications = null;
|
||||
try {
|
||||
conn = NotificationManagementDAOFactory.getConnection();
|
||||
String sql = "SELECT n1.NOTIFICATION_ID, n1.DEVICE_ID, n1.OPERATION_ID, n1.STATUS," +
|
||||
" n1.DESCRIPTION, d.DEVICE_IDENTIFICATION, t.NAME AS DEVICE_TYPE FROM " +
|
||||
"DM_DEVICE d, DM_DEVICE_TYPE t, (SELECT NOTIFICATION_ID, DEVICE_ID, " +
|
||||
"OPERATION_ID, STATUS, DESCRIPTION FROM DM_NOTIFICATION WHERE " +
|
||||
"TENANT_ID = ? AND STATUS = ?) n1 WHERE n1.DEVICE_ID = d.ID AND d.DEVICE_TYPE_ID=t.ID " +
|
||||
"AND TENANT_ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, tenantId);
|
||||
stmt.setString(2, status.toString());
|
||||
stmt.setInt(3, tenantId);
|
||||
rs = stmt.executeQuery();
|
||||
notifications = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
notifications.add(this.getNotification(rs));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new NotificationManagementException(
|
||||
"Error occurred while retrieving information of all " +
|
||||
"notifications by status : " + status, e);
|
||||
} finally {
|
||||
NotificationDAOUtil.cleanupResources(stmt, rs);
|
||||
}
|
||||
return notifications;
|
||||
}
|
||||
|
||||
private Notification getNotification(ResultSet rs) throws SQLException {
|
||||
Notification notification = new Notification();
|
||||
notification.setNotificationId(rs.getInt("NOTIFICATION_ID"));
|
||||
notification.setDeviceIdentifier(this.getDeviceIdentifier(rs));
|
||||
notification.setOperationId(rs.getInt("OPERATION_ID"));
|
||||
notification.setDescription(rs.getString("DESCRIPTION"));
|
||||
notification.setStatus(rs.getString("STATUS"));
|
||||
return notification;
|
||||
}
|
||||
|
||||
private DeviceIdentifier getDeviceIdentifier(ResultSet rs) throws SQLException {
|
||||
DeviceIdentifier identifier = new DeviceIdentifier();
|
||||
identifier.setId(rs.getString("DEVICE_IDENTIFICATION"));
|
||||
identifier.setType(rs.getString("DEVICE_TYPE"));
|
||||
return identifier;
|
||||
}
|
||||
}
|
@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.device.mgt.core.notification.mgt.dao.util;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
import org.wso2.carbon.user.core.tenant.TenantManager;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* This class includes the utility methods required by NotificationMgmt functionalities.
|
||||
*/
|
||||
public class NotificationDAOUtil {
|
||||
|
||||
private static final Log log = LogFactory.getLog(NotificationDAOUtil.class);
|
||||
|
||||
public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
if (conn != null) {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing database connection", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void cleanupResources(PreparedStatement stmt, ResultSet rs) {
|
||||
if (rs != null) {
|
||||
try {
|
||||
rs.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing result set", e);
|
||||
}
|
||||
}
|
||||
if (stmt != null) {
|
||||
try {
|
||||
stmt.close();
|
||||
} catch (SQLException e) {
|
||||
log.warn("Error occurred while closing prepared statement", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get id of the current tenant.
|
||||
*
|
||||
* @return tenant id
|
||||
* @throws org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException if an error is observed when getting tenant id
|
||||
*/
|
||||
public static int getTenantId() throws NotificationManagementException {
|
||||
CarbonContext context = CarbonContext.getThreadLocalCarbonContext();
|
||||
int tenantId = context.getTenantId();
|
||||
if (tenantId != MultitenantConstants.INVALID_TENANT_ID) {
|
||||
return tenantId;
|
||||
}
|
||||
String tenantDomain = context.getTenantDomain();
|
||||
if (tenantDomain == null) {
|
||||
String msg = "Tenant domain is not properly set and thus, is null";
|
||||
throw new NotificationManagementException(msg);
|
||||
}
|
||||
TenantManager tenantManager = DeviceManagementDataHolder.getInstance().getTenantManager();
|
||||
try {
|
||||
tenantId = tenantManager.getTenantId(tenantDomain);
|
||||
} catch (UserStoreException e) {
|
||||
String msg =
|
||||
"Error occurred while retrieving id from the domain of tenant " + tenantDomain;
|
||||
throw new NotificationManagementException(msg);
|
||||
}
|
||||
return tenantId;
|
||||
}
|
||||
|
||||
public static DataSource lookupDataSource(String dataSourceName,
|
||||
final Hashtable<Object, Object> jndiProperties) {
|
||||
try {
|
||||
if (jndiProperties == null || jndiProperties.isEmpty()) {
|
||||
return (DataSource) InitialContext.doLookup(dataSourceName);
|
||||
}
|
||||
final InitialContext context = new InitialContext(jndiProperties);
|
||||
return (DataSource) context.lookup(dataSourceName);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.policy.mgt.common.monitor;
|
||||
|
||||
public class PolicyDeviceWrapper {
|
||||
|
||||
private int policyId;
|
||||
private int deviceId;
|
||||
private int enrolmentId;
|
||||
|
||||
public int getPolicyId() {
|
||||
return policyId;
|
||||
}
|
||||
|
||||
public void setPolicyId(int policyId) {
|
||||
this.policyId = policyId;
|
||||
}
|
||||
|
||||
public int getDeviceId() {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(int deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public int getEnrolmentId() {
|
||||
return enrolmentId;
|
||||
}
|
||||
|
||||
public void setEnrolmentId(int enrolmentId) {
|
||||
this.enrolmentId = enrolmentId;
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.webapp.authenticator.framework;
|
||||
|
||||
public class APIInfo {
|
||||
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.webapp.authenticator.framework;
|
||||
|
||||
import org.apache.catalina.Lifecycle;
|
||||
import org.apache.catalina.LifecycleEvent;
|
||||
import org.apache.catalina.LifecycleListener;
|
||||
|
||||
public class APIMapperContextListener implements LifecycleListener {
|
||||
|
||||
@Override
|
||||
public void lifecycleEvent(LifecycleEvent lifecycleEvent) {
|
||||
if (Lifecycle.AFTER_INIT_EVENT.equals(lifecycleEvent.getType())) {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.webapp.authenticator.framework;
|
||||
|
||||
import org.apache.catalina.connector.Request;
|
||||
import org.apache.catalina.connector.Response;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.tomcat.ext.valves.CarbonTomcatValve;
|
||||
import org.wso2.carbon.tomcat.ext.valves.CompositeValve;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.authenticator.WebappAuthenticator;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
public class WebappAuthenticationHandler extends CarbonTomcatValve {
|
||||
|
||||
private static final Log log = LogFactory.getLog(WebappAuthenticationHandler.class);
|
||||
|
||||
@Override
|
||||
public void invoke(Request request, Response response, CompositeValve compositeValve) {
|
||||
if (this.isContextSkipped(request) || (this.isNonAdminService(request) && this.skipAuthentication(request))) {
|
||||
this.getNext().invoke(request, response, compositeValve);
|
||||
return;
|
||||
}
|
||||
WebappAuthenticator authenticator = WebappAuthenticatorFactory.getAuthenticator(request);
|
||||
if (authenticator == null) {
|
||||
String msg = "Failed to load an appropriate authenticator to authenticate the request";
|
||||
AuthenticationFrameworkUtil.handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED, msg);
|
||||
return;
|
||||
}
|
||||
WebappAuthenticator.Status status = authenticator.authenticate(request, response);
|
||||
this.processResponse(request, response, compositeValve, status);
|
||||
}
|
||||
|
||||
private boolean isNonAdminService(Request request) {
|
||||
String param = request.getContext().findParameter("isAdminService");
|
||||
return !(param != null && Boolean.parseBoolean(param));
|
||||
}
|
||||
|
||||
private boolean skipAuthentication(Request request) {
|
||||
String param = request.getContext().findParameter("doAuthentication");
|
||||
return (param == null || !Boolean.parseBoolean(param));
|
||||
}
|
||||
|
||||
private boolean isContextSkipped(Request request) {
|
||||
String ctx = request.getContext().getPath();
|
||||
if (ctx == null || "".equals(ctx)) {
|
||||
ctx = request.getContextPath();
|
||||
if (ctx == null || "".equals(ctx)) {
|
||||
String requestUri = request.getRequestURI();
|
||||
if ("/".equals(requestUri)) {
|
||||
return true;
|
||||
}
|
||||
StringTokenizer tokenizer = new StringTokenizer(request.getRequestURI(), "/");
|
||||
if (!tokenizer.hasMoreTokens()) {
|
||||
return false;
|
||||
}
|
||||
ctx = tokenizer.nextToken();
|
||||
}
|
||||
}
|
||||
return (ctx.equalsIgnoreCase("carbon") || ctx.equalsIgnoreCase("services"));
|
||||
}
|
||||
|
||||
private void processResponse(Request request, Response response, CompositeValve compositeValve,
|
||||
WebappAuthenticator.Status status) {
|
||||
switch (status) {
|
||||
case SUCCESS:
|
||||
case CONTINUE:
|
||||
this.getNext().invoke(request, response, compositeValve);
|
||||
break;
|
||||
case FAILURE:
|
||||
String msg = "Failed to authorize incoming request";
|
||||
log.error(msg);
|
||||
AuthenticationFrameworkUtil.handleResponse(request, response, HttpServletResponse.SC_UNAUTHORIZED, msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.webapp.authenticator.framework.authenticator;
|
||||
|
||||
import com.nimbusds.jose.JOSEException;
|
||||
import com.nimbusds.jose.JWSVerifier;
|
||||
import com.nimbusds.jose.crypto.RSASSAVerifier;
|
||||
import com.nimbusds.jwt.SignedJWT;
|
||||
import org.apache.axiom.util.base64.Base64Utils;
|
||||
import org.apache.axis2.transport.http.HTTPConstants;
|
||||
import org.apache.catalina.connector.Request;
|
||||
import org.apache.catalina.connector.Response;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
||||
import org.wso2.carbon.core.util.KeyStoreManager;
|
||||
import org.wso2.carbon.user.api.TenantManager;
|
||||
import org.wso2.carbon.user.api.UserStoreException;
|
||||
import org.wso2.carbon.user.api.UserStoreManager;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
|
||||
import org.wso2.carbon.utils.multitenancy.MultitenantUtils;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.DataHolder;
|
||||
|
||||
import java.security.interfaces.RSAPublicKey;
|
||||
import java.text.ParseException;
|
||||
import java.util.StringTokenizer;
|
||||
|
||||
/**
|
||||
* This authenticator authenticates HTTP requests using JWT header.
|
||||
*/
|
||||
public class JWTAuthenticator implements WebappAuthenticator {
|
||||
|
||||
private static final Log log = LogFactory.getLog(JWTAuthenticator.class);
|
||||
public static final String SIGNED_JWT_AUTH_USERNAME = "Username";
|
||||
private static final String JWT_AUTHENTICATOR = "JWT";
|
||||
|
||||
@Override
|
||||
public boolean canHandle(Request request) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status authenticate(Request request, Response response) {
|
||||
String requestUri = request.getRequestURI();
|
||||
if (requestUri == null || "".equals(requestUri)) {
|
||||
return Status.CONTINUE;
|
||||
}
|
||||
StringTokenizer tokenizer = new StringTokenizer(requestUri, "/");
|
||||
String context = tokenizer.nextToken();
|
||||
if (context == null || "".equals(context)) {
|
||||
return Status.CONTINUE;
|
||||
}
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Authenticating using JWT header.");
|
||||
}
|
||||
|
||||
//Get the filesystem keystore default primary certificate
|
||||
KeyStoreManager keyStoreManager = KeyStoreManager.getInstance(
|
||||
MultitenantConstants.SUPER_TENANT_ID);
|
||||
try {
|
||||
keyStoreManager.getDefaultPrimaryCertificate();
|
||||
String authorizationHeader = request.getHeader(HTTPConstants.HEADER_AUTHORIZATION);
|
||||
String headerData = decodeAuthorizationHeader(authorizationHeader);
|
||||
JWSVerifier verifier =
|
||||
new RSASSAVerifier((RSAPublicKey) keyStoreManager.getDefaultPublicKey());
|
||||
SignedJWT jwsObject = SignedJWT.parse(headerData);
|
||||
if (jwsObject.verify(verifier)) {
|
||||
String username = jwsObject.getJWTClaimsSet().getStringClaim(SIGNED_JWT_AUTH_USERNAME);
|
||||
String tenantDomain = MultitenantUtils.getTenantDomain(username);
|
||||
username = MultitenantUtils.getTenantAwareUsername(username);
|
||||
TenantManager tenantManager = DataHolder.getInstance().getRealmService().getTenantManager();
|
||||
int tenantId = tenantManager.getTenantId(tenantDomain);
|
||||
|
||||
if (tenantId == -1) {
|
||||
log.error("tenantDomain is not valid. username : " + username + ", tenantDomain " +
|
||||
": " + tenantDomain);
|
||||
return Status.FAILURE;
|
||||
}
|
||||
|
||||
UserStoreManager userStore = DataHolder.getInstance().getRealmService().
|
||||
getTenantUserRealm(tenantId).getUserStoreManager();
|
||||
if (userStore.isExistingUser(username)) {
|
||||
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
||||
ctx.setTenantId(tenantId);
|
||||
ctx.setUsername(username);
|
||||
return Status.SUCCESS;
|
||||
}
|
||||
}
|
||||
} catch (UserStoreException e) {
|
||||
log.error("Error occurred while obtaining the user.", e);
|
||||
return Status.FAILURE;
|
||||
} catch (ParseException e) {
|
||||
log.error("Error occurred while parsing the JWT header.", e);
|
||||
return Status.FAILURE;
|
||||
} catch (JOSEException e) {
|
||||
log.error("Error occurred while verifying the JWT header.", e);
|
||||
return Status.FAILURE;
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while verifying the JWT header.", e);
|
||||
return Status.FAILURE;
|
||||
}
|
||||
return Status.CONTINUE;
|
||||
}
|
||||
|
||||
private String decodeAuthorizationHeader(String authorizationHeader) {
|
||||
String[] splitValues = authorizationHeader.trim().split(" ");
|
||||
byte[] decodedBytes = Base64Utils.decode(splitValues[1].trim());
|
||||
if (decodedBytes != null) {
|
||||
return new String(decodedBytes);
|
||||
} else {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Error decoding authorization header.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return JWTAuthenticator.JWT_AUTHENTICATOR;
|
||||
}
|
||||
}
|
4
components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticator.java → components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/WebappAuthenticator.java
4
components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticator.java → components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/WebappAuthenticator.java
@ -1,59 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2015, 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 org.wso2.carbon.webapp.authenticator.framework.authorizer;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.context.CarbonContext;
|
||||
import org.wso2.carbon.context.RegistryType;
|
||||
import org.wso2.carbon.registry.api.Collection;
|
||||
import org.wso2.carbon.registry.api.Registry;
|
||||
import org.wso2.carbon.registry.api.RegistryException;
|
||||
import org.wso2.carbon.webapp.authenticator.framework.authorizer.config.Permission;
|
||||
|
||||
public class PermissionAuthorizerUtil {
|
||||
|
||||
private static Registry registry = CarbonContext.getThreadLocalCarbonContext().
|
||||
getRegistry(RegistryType.SYSTEM_GOVERNANCE);
|
||||
|
||||
private static final String PROPERTY_NAME = "name";
|
||||
private static final String PATH_PERMISSION = "/permission";
|
||||
private static final Log log = LogFactory.getLog(PermissionAuthorizerUtil.class);
|
||||
|
||||
public static void addPermission(Permission permission) {
|
||||
|
||||
if (registry == null) {
|
||||
throw new IllegalArgumentException("Registry instance retrieved is null");
|
||||
}
|
||||
|
||||
if (permission == null) {
|
||||
throw new IllegalArgumentException("Permission argument is null");
|
||||
}
|
||||
try {
|
||||
Collection collection = registry.newCollection();
|
||||
collection.setProperty(PROPERTY_NAME, permission.getName());
|
||||
registry.put(PATH_PERMISSION + permission.getPath(), collection);
|
||||
|
||||
} catch (RegistryException e) {
|
||||
String errorMsg = "Error occured while adding permission '" + permission.getName() +
|
||||
"' to registry. ";
|
||||
log.error(errorMsg + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
48
components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkBundleActivator.java → components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java
48
components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkBundleActivator.java → components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/internal/WebappAuthenticatorFrameworkServiceComponent.java
Binary file not shown.
@ -1,2 +1,3 @@
|
||||
instructions.configure = \
|
||||
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.certificate.mgt.server_${feature.version}/conf/wso2certs.jks,target:${installFolder}/../../resources/security/wso2certs.jks,overwrite:true);\
|
||||
org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.certificate.mgt.server_${feature.version}/conf/certificate-config.xml,target:${installFolder}/../../conf/certificate-config.xml,overwrite:true);\
|
Loading…
Reference in new issue