Add application search by package name #343

Merged
tcdlpds merged 2 commits from pahansith/device-mgt-core:app-restricion into master 9 months ago

@ -2657,7 +2657,12 @@ public interface DeviceManagementService {
name = "appName", name = "appName",
value = "App name to be searched") value = "App name to be searched")
@QueryParam("appName") @QueryParam("appName")
String appName); String appName,
@ApiParam(
name = "packageName",
value = "App package name to be searched")
@QueryParam("packageName")
String packageName);
@GET @GET
@Path("/application/{packageName}/versions") @Path("/application/{packageName}/versions")

@ -27,6 +27,7 @@ import io.entgra.device.mgt.core.application.mgt.common.services.ApplicationMana
import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager; import io.entgra.device.mgt.core.application.mgt.common.services.SubscriptionManager;
import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil; import io.entgra.device.mgt.core.application.mgt.core.util.HelperUtil;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.service.impl.util.DisenrollRequest;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.util.DeviceMgtUtil;
import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -1564,11 +1565,13 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@QueryParam("offset") int offset, @QueryParam("offset") int offset,
@DefaultValue("10") @DefaultValue("10")
@QueryParam("limit") int limit, @QueryParam("limit") int limit,
@QueryParam("appName") String appName) { @QueryParam("appName") String appName,
@QueryParam("packageName") String packageName) {
PaginationRequest request = new PaginationRequest(offset, limit); PaginationRequest request = new PaginationRequest(offset, limit);
ApplicationList applicationList = new ApplicationList(); ApplicationList applicationList = new ApplicationList();
request.setDeviceType(deviceType); request.setDeviceType(deviceType);
request.setFilter(appName); request.setFilter(DeviceMgtUtil.buildAppSearchFilter(appName, packageName));
try { try {
PaginationResult paginationResult = DeviceMgtAPIUtils PaginationResult paginationResult = DeviceMgtAPIUtils
.getDeviceManagementService() .getDeviceManagementService()

@ -24,7 +24,9 @@ import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ErrorResponse;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ProfileFeature; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.beans.ProfileFeature;
import io.entgra.device.mgt.core.device.mgt.api.jaxrs.exception.BadRequestException; import io.entgra.device.mgt.core.device.mgt.api.jaxrs.exception.BadRequestException;
import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Profile; import io.entgra.device.mgt.core.device.mgt.common.policy.mgt.Profile;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationFilter;
import com.google.gson.Gson;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -141,4 +143,12 @@ public class DeviceMgtUtil {
errorResponse.setErrorItems(errorListItems); errorResponse.setErrorItems(errorListItems);
return errorResponse; return errorResponse;
} }
public static String buildAppSearchFilter(String appName, String packageName) {
//Create search filter as a Json string to attach into the filter in PaginationRequest
ApplicationFilter applicationFilter = new ApplicationFilter();
applicationFilter.setAppName(appName);
applicationFilter.setPackageName(packageName);
return new Gson().toJson(applicationFilter);
}
} }

@ -0,0 +1,53 @@
/*
* Copyright (c) 2018 - 2024, Entgra (Pvt) Ltd. (http://www.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.device.mgt.core.device.mgt.common.app.mgt;
public class ApplicationFilter {
public static final class FilterProperties {
public static final String REGEX_WHITESPACE = ".*\\s.*";
public static final String REGEX_WHITESPACE_REPLACER = "\\s+";
public static final String URL_ENCODE_SPACE = "%20";
}
private String appName;
private String packageName;
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
if (this.appName != null && !this.appName.isEmpty() &&
//Check if the filter contains spaces and replace URL encode them
this.appName.matches(FilterProperties.REGEX_WHITESPACE)) {
this.appName = this.appName
.replaceAll(FilterProperties.REGEX_WHITESPACE_REPLACER,
FilterProperties.URL_ENCODE_SPACE);
}
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}

@ -17,6 +17,8 @@
*/ */
package io.entgra.device.mgt.core.device.mgt.core.dao.impl; package io.entgra.device.mgt.core.device.mgt.core.dao.impl;
import com.google.gson.Gson;
import io.entgra.device.mgt.core.device.mgt.common.app.mgt.ApplicationFilter;
import io.entgra.device.mgt.core.device.mgt.core.common.Constants; import io.entgra.device.mgt.core.device.mgt.core.common.Constants;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -296,10 +298,21 @@ public class ApplicationDAOImpl implements ApplicationDAO {
"PLATFORM = ? AND TENANT_ID = ?) "; "PLATFORM = ? AND TENANT_ID = ?) ";
try { try {
String filter = request.getFilter(); ApplicationFilter applicationFilter = new Gson().fromJson(request.getFilter(), ApplicationFilter.class);
if (filter != null) { boolean isAppNameFilterProvided = false;
if (null != applicationFilter.getAppName()) {
sql = sql + "AND NAME LIKE ? "; sql = sql + "AND NAME LIKE ? ";
filter = Constants.QUERY_WILDCARD.concat(filter).concat(Constants.QUERY_WILDCARD); applicationFilter.setAppName(Constants.QUERY_WILDCARD.concat(applicationFilter.getAppName())
.concat(Constants.QUERY_WILDCARD));
isAppNameFilterProvided = true;
}
boolean isPackageFilterProvided = false;
if (null != applicationFilter.getPackageName()) {
sql = sql + "AND APP_IDENTIFIER LIKE ? ";
applicationFilter.setPackageName(Constants.QUERY_WILDCARD.concat(applicationFilter.getPackageName())
.concat(Constants.QUERY_WILDCARD));
isPackageFilterProvided = true;
} }
boolean isLimitPresent = false; boolean isLimitPresent = false;
@ -314,8 +327,11 @@ public class ApplicationDAOImpl implements ApplicationDAO {
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
stmt.setString(paramIdx++, request.getDeviceType()); stmt.setString(paramIdx++, request.getDeviceType());
stmt.setInt(paramIdx++, tenantId); stmt.setInt(paramIdx++, tenantId);
if (filter != null){ if (isAppNameFilterProvided){
stmt.setString(paramIdx++, filter); stmt.setString(paramIdx++, applicationFilter.getAppName());
}
if (isPackageFilterProvided){
stmt.setString(paramIdx++, applicationFilter.getPackageName());
} }
if (isLimitPresent) { if (isLimitPresent) {
stmt.setInt(paramIdx++, request.getRowCount()); stmt.setInt(paramIdx++, request.getRowCount());

Loading…
Cancel
Save