Merge branch 'master' into 'master'

Improve APPM and Invoker sources

See merge request entgra/carbon-device-mgt!506
4.x.x
Dharmakeerthi Lasantha 5 years ago
commit b6d545e6db

@ -180,8 +180,7 @@ public class GenericApplicationDAOImpl extends AbstractDAOImpl implements Applic
if (filter.getLimit() != -1) {
sql += "LIMIT ? OFFSET ? ";
}
sql += ") AS app_data ON app_data.ID = AP_APP.ID " +
"WHERE AP_APP.TENANT_ID = ?";
sql += ") AS app_data ON app_data.ID = AP_APP.ID WHERE AP_APP.TENANT_ID = ?";
try {
Connection conn = this.getDBConnection();
try (PreparedStatement stmt = conn.prepareStatement(sql)) {

@ -73,7 +73,6 @@ import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO;
import org.wso2.carbon.device.application.mgt.core.dao.VisibilityDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.util.APIUtil;
import org.wso2.carbon.device.application.mgt.core.util.DAOUtil;
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.ForbiddenException;
@ -1057,7 +1056,8 @@ public class ApplicationManagerImpl implements ApplicationManager {
}
try {
DeviceManagementProviderService deviceManagementProviderService = DAOUtil.getDeviceManagementService();
DeviceManagementProviderService deviceManagementProviderService = DataHolder.getInstance()
.getDeviceManagementService();
return deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, lowestSupportingOsVersion)
== null || (highestSupportingOsVersion != null
&& deviceManagementProviderService.getDeviceTypeVersion(deviceTypeName, highestSupportingOsVersion)
@ -1565,8 +1565,7 @@ public class ApplicationManagerImpl implements ApplicationManager {
boolean isValidDeviceType = false;
List<DeviceType> deviceTypes;
try {
deviceTypes = DAOUtil.getDeviceManagementService().getDeviceTypes();
deviceTypes = DataHolder.getInstance().getDeviceManagementService().getDeviceTypes();
for (DeviceType dt : deviceTypes) {
if (dt.getName().equals(deviceType)) {
isValidDeviceType = true;

@ -43,6 +43,7 @@ import org.wso2.carbon.device.application.mgt.common.wrapper.WebAppWrapper;
import org.wso2.carbon.device.application.mgt.core.config.ConfigurationManager;
import org.wso2.carbon.device.application.mgt.core.exception.BadRequestException;
import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -197,7 +198,7 @@ public class APIUtil {
throws BadRequestException, UnexpectedServerErrorException {
List<DeviceType> deviceTypes;
try {
deviceTypes = DAOUtil.getDeviceManagementService().getDeviceTypes();
deviceTypes = DataHolder.getInstance().getDeviceManagementService().getDeviceTypes();
if (deviceTypeAttr instanceof String) {
for (DeviceType dt : deviceTypes) {
if (dt.getName().equals(deviceTypeAttr)) {

@ -32,8 +32,6 @@ import org.wso2.carbon.device.application.mgt.common.dto.ReviewDTO;
import org.wso2.carbon.device.application.mgt.common.dto.ScheduledSubscriptionDTO;
import org.wso2.carbon.device.application.mgt.core.exception.UnexpectedServerErrorException;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -306,18 +304,4 @@ public class DAOUtil {
}
}
}
public static DeviceManagementProviderService getDeviceManagementService() {
// PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
// DeviceManagementProviderService deviceManagementProviderService =
// (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null);
// if (deviceManagementProviderService == null) {
// String msg = "DeviceImpl Management provider service has not initialized.";
// log.error(msg);
// throw new IllegalStateException(msg);
// }
// return deviceManagementProviderService;
return new DeviceManagementProviderServiceImpl();
}
}

@ -38,6 +38,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO;
import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory;
import org.wso2.carbon.device.application.mgt.core.dto.ApplicationsDTO;
import org.wso2.carbon.device.application.mgt.core.impl.ApplicationManagerImpl;
import org.wso2.carbon.device.application.mgt.core.internal.DataHolder;
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.core.dto.DeviceType;
@ -60,6 +61,7 @@ public class ApplicationManagementTest extends BaseTestCase {
public void testAddApplication() throws Exception {
ApplicationDAO applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO();
DataHolder.getInstance().setDeviceManagementService(new DeviceManagementProviderServiceImpl());
ConnectionManagerUtil.beginDBTransaction();
applicationDAO.createApplication(ApplicationsDTO.getApp1(), -1234);
ConnectionManagerUtil.commitDBTransaction();

@ -36,6 +36,7 @@ import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpHead;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
@ -55,7 +56,6 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
@ -72,8 +72,7 @@ public class InvokerHandler extends HttpServlet {
private static final Log log = LogFactory.getLog(InvokerHandler.class);
private static final long serialVersionUID = -6508020875358160165L;
private static AuthData authData;
private static String serverUrl;
private static String platform;
private static String apiEndpoint;
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
@ -91,7 +90,7 @@ public class InvokerHandler extends HttpServlet {
}
}
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
log.error("Error occurred while invoking the API endpoint.");
log.error("Error occurred while invoking the POST API endpoint.");
HandlerUtil.handleError(resp, proxyResponse);
return;
}
@ -119,7 +118,7 @@ public class InvokerHandler extends HttpServlet {
}
}
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
log.error("Error occurred while invoking the API endpoint.");
log.error("Error occurred while invoking the GET API endpoint.");
HandlerUtil.handleError(resp, proxyResponse);
return;
}
@ -130,6 +129,32 @@ public class InvokerHandler extends HttpServlet {
}
}
@Override
protected void doHead(HttpServletRequest req, HttpServletResponse resp) {
try {
if (validateRequest(req, resp)) {
HttpHead headRequest = new HttpHead(generateBackendRequestURL(req));
copyRequestHeaders(req, headRequest, false);
headRequest.setHeader(HttpHeaders.AUTHORIZATION, HandlerConstants.BEARER + authData.getAccessToken());
ProxyResponse proxyResponse = HandlerUtil.execute(headRequest);
if (HandlerConstants.TOKEN_IS_EXPIRED.equals(proxyResponse.getExecutorResponse())) {
proxyResponse = retryRequestWithRefreshedToken(req, resp, headRequest);
if (proxyResponse == null) {
return;
}
}
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
log.error("Error occurred while invoking the HEAD API endpoint.");
HandlerUtil.handleError(resp, proxyResponse);
return;
}
HandlerUtil.handleSuccess(resp, proxyResponse);
}
} catch (IOException e) {
log.error("Error occurred when processing HEAD request.", e);
}
}
@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) {
try {
@ -146,7 +171,7 @@ public class InvokerHandler extends HttpServlet {
}
}
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
log.error("Error occurred while invoking the API endpoint.");
log.error("Error occurred while invoking the PUT API endpoint.");
HandlerUtil.handleError(resp, proxyResponse);
return;
}
@ -174,7 +199,7 @@ public class InvokerHandler extends HttpServlet {
}
}
if (proxyResponse.getExecutorResponse().contains(HandlerConstants.EXECUTOR_EXCEPTION_PREFIX)) {
log.error("Error occurred while invoking the API endpoint.");
log.error("Error occurred while invoking the DELETE API endpoint.");
HandlerUtil.handleError(resp, proxyResponse);
return;
}
@ -229,13 +254,7 @@ public class InvokerHandler extends HttpServlet {
*/
private String generateBackendRequestURL(HttpServletRequest req) {
StringBuilder urlBuilder = new StringBuilder();
String endpointUrl = Arrays.stream(HandlerConstants.SKIPPING_API_CONTEXT)
.anyMatch(contextPath -> contextPath.contains(req.getPathInfo())) ?
serverUrl :
req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host")
+ HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme());
urlBuilder.append(endpointUrl).append(HandlerConstants.API_COMMON_CONTEXT).append(req.getPathInfo());
urlBuilder.append(apiEndpoint).append(HandlerConstants.API_COMMON_CONTEXT).append(req.getPathInfo());
if (StringUtils.isNotEmpty(req.getQueryString())) {
urlBuilder.append("?").append(req.getQueryString());
}
@ -277,9 +296,19 @@ public class InvokerHandler extends HttpServlet {
*/
private static boolean validateRequest(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
serverUrl = req.getScheme() + "://" + req.getServerName() + ":" + System.getProperty("iot.gateway.https.port");
HttpSession session = req.getSession(false);
apiEndpoint = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host")
+ HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme());
if ("reports".equalsIgnoreCase(req.getHeader("appName"))){
apiEndpoint = System.getProperty("iot.reporting.event.host");
if (StringUtils.isBlank(apiEndpoint)){
log.error("Reporting Endpoint is not defined in the iot-server.sh properly.");
handleError(resp, HttpStatus.SC_INTERNAL_SERVER_ERROR);
return false;
}
}
HttpSession session = req.getSession(false);
if (session == null) {
log.error("Unauthorized, You are not logged in. Please log in to the portal");
handleError(resp, HttpStatus.SC_UNAUTHORIZED);
@ -287,7 +316,6 @@ public class InvokerHandler extends HttpServlet {
}
authData = (AuthData) session.getAttribute(HandlerConstants.SESSION_AUTH_DATA_KEY);
platform = (String) session.getAttribute(HandlerConstants.PLATFORM);
if (authData == null) {
log.error("Unauthorized, Access token not found in the current session");
handleError(resp, HttpStatus.SC_UNAUTHORIZED);
@ -340,7 +368,7 @@ public class InvokerHandler extends HttpServlet {
log.debug("refreshing the token");
}
HttpPost tokenEndpoint = new HttpPost(
serverUrl + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT);
apiEndpoint + HandlerConstants.API_COMMON_CONTEXT + HandlerConstants.TOKEN_ENDPOINT);
HttpSession session = req.getSession(false);
if (session == null) {
log.error("Couldn't find a session, hence it is required to login and proceed.");

@ -221,16 +221,16 @@ public class LoginHandler extends HttpServlet {
* Define username and password static parameters.
*/
private static void validateLoginRequest(HttpServletRequest req) throws LoginException {
String gatewayCarbonPort = System.getProperty("iot.core.https.port");
String iotsCorePort = System.getProperty("iot.core.https.port");
if (HandlerConstants.HTTP_PROTOCOL.equals(req.getScheme())) {
gatewayCarbonPort = System.getProperty("iot.core.http.port");
iotsCorePort = System.getProperty("iot.core.http.port");
}
username = req.getParameter("username");
password = req.getParameter("password");
gatewayUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.gateway.host")
+ HandlerConstants.COLON + HandlerUtil.getGatewayPort(req.getScheme());
uiConfigUrl = req.getScheme() + HandlerConstants.SCHEME_SEPARATOR + System.getProperty("iot.core.host")
+ HandlerConstants.COLON + gatewayCarbonPort + HandlerConstants.UI_CONFIG_ENDPOINT;
+ HandlerConstants.COLON + iotsCorePort + HandlerConstants.UI_CONFIG_ENDPOINT;
if (username == null || password == null) {
String msg = "Invalid login request. Username or Password is not received for login request.";
log.error(msg);

@ -42,8 +42,6 @@ public class HandlerConstants {
public static final String COLON = ":";
public static final String HTTP_PROTOCOL = "http";
public static final String[] SKIPPING_API_CONTEXT = {"artifact", "conf"};
public static final int INTERNAL_ERROR_CODE = 500;
public static final long TIMEOUT = 1200;
}

Loading…
Cancel
Save