Skip to content

Commit

Permalink
ApiApiServiceImpl added to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
shnrndk committed Dec 4, 2023
1 parent 2e46e19 commit 7de9b63
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18067,7 +18067,7 @@ private void updateAPIServiceMapping(int apiId, String serviceKey, String md5, i
}

public void updateApiProvider(String apiUuid, String providerName)
throws APIManagementException, SQLException {
throws APIManagementException {
try (Connection connection = APIMgtDBUtil.getConnection()) {
PreparedStatement statement = connection.prepareStatement(SQLConstants.UPDATE_API_PROVIDER_SQL);
statement.setString(1, providerName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package org.wso2.carbon.apimgt.rest.api.admin.v1.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.ext.MessageContext;
import org.wso2.carbon.apimgt.api.APIManagementException;
import org.wso2.carbon.apimgt.api.APIProvider;
import org.wso2.carbon.apimgt.api.ExceptionCodes;
import org.wso2.carbon.apimgt.api.model.API;
import org.wso2.carbon.apimgt.impl.APIConstants;
import org.wso2.carbon.apimgt.impl.GZIPUtils;
import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO;
import org.wso2.carbon.apimgt.impl.factory.PersistenceFactory;
import org.wso2.carbon.apimgt.impl.utils.APIUtil;
import org.wso2.carbon.apimgt.persistence.APIPersistence;
import org.wso2.carbon.apimgt.persistence.exceptions.APIPersistenceException;
import org.wso2.carbon.apimgt.rest.api.admin.v1.ApisApiService;
import org.wso2.carbon.apimgt.rest.api.admin.v1.utils.mappings.APIInfoMappingUtil;
import org.wso2.carbon.apimgt.rest.api.common.RestApiCommonUtil;
import org.wso2.carbon.apimgt.rest.api.common.RestApiConstants;
import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ws.rs.core.Response;

public class ApisApiServiceImpl implements ApisApiService {

private static final Log log = LogFactory.getLog(ApisApiServiceImpl.class);

public Response getAllAPIs(Integer limit, Integer offset, String sortBy, String sortOrder, String xWSO2Tenant,
String query, String ifNoneMatch, String accept, MessageContext messageContext) {
List<API> allMatchedApis = new ArrayList<>();

Check warning on line 36 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L36

Added line #L36 was not covered by tests
Object apiListDTO;

//pre-processing
//setting default limit and offset values if they are not set
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
query = query == null ? "" : query;
sortBy = sortBy != null ? sortBy : RestApiConstants.DEFAULT_SORT_CRITERION;
sortOrder = sortOrder != null ? sortOrder : RestApiConstants.DESCENDING_SORT_ORDER;
try {

//revert content search back to normal search by name to avoid doc result complexity and
// to comply with REST api practices
if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":")) {
query = query
.replace(APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":",

Check warning on line 52 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L51-L52

Added lines #L51 - L52 were not covered by tests
APIConstants.NAME_TYPE_PREFIX + ":");
}

APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();

Check warning on line 56 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L56

Added line #L56 was not covered by tests

String organization = RestApiUtil.getValidatedOrganization(messageContext);

Check warning on line 58 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L58

Added line #L58 was not covered by tests

Map<String, Object> result;

result = apiProvider.searchPaginatedAPIs(query, organization, offset, limit, sortBy, sortOrder);

Check warning on line 62 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L62

Added line #L62 was not covered by tests

Set<API> apis = (Set<API>) result.get("apis");
allMatchedApis.addAll(apis);

Check warning on line 65 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L64-L65

Added lines #L64 - L65 were not covered by tests

apiListDTO = APIInfoMappingUtil.fromAPIListToInfoDTO(allMatchedApis);

Check warning on line 67 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L67

Added line #L67 was not covered by tests

//Add pagination section in the response
Object totalLength = result.get("length");
Integer length = 0;

Check warning on line 71 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L70-L71

Added lines #L70 - L71 were not covered by tests
if (totalLength != null) {
length = (Integer) totalLength;

Check warning on line 73 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L73

Added line #L73 was not covered by tests
}
APIInfoMappingUtil.setPaginationParams(apiListDTO, query, offset, limit, length);

Check warning on line 75 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L75

Added line #L75 was not covered by tests
if (APIConstants.APPLICATION_GZIP.equals(accept)) {
try {
File zippedResponse = GZIPUtils.constructZippedResponse(apiListDTO);
return Response.ok().entity(zippedResponse)
.header("Content-Disposition", "attachment").
header("Content-Encoding", "gzip").build();
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError(e.getMessage(), e, log);
}

Check warning on line 84 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L78-L84

Added lines #L78 - L84 were not covered by tests
} else {
return Response.ok().entity(apiListDTO).build();

Check warning on line 86 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L86

Added line #L86 was not covered by tests
}
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving APIs";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;

Check warning on line 92 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L88-L92

Added lines #L88 - L92 were not covered by tests
}

public Response providerProviderNameApisApiIdPut(String providerName, String apiId, MessageContext messageContext)
throws APIManagementException {
String tenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
String organization = null;

Check warning on line 98 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L97-L98

Added lines #L97 - L98 were not covered by tests
if (tenantDomain != null) {
organization = tenantDomain;

Check warning on line 100 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L100

Added line #L100 was not covered by tests
}
try {
if (!APIUtil.isUserExist(providerName)) {
throw new APIManagementException("User Not Found. Username :" + providerName + ",",

Check warning on line 104 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L104

Added line #L104 was not covered by tests
ExceptionCodes.USER_NOT_FOUND);
}
APIPersistence apiPersistenceInstance;
apiPersistenceInstance = PersistenceFactory.getAPIPersistenceInstance();
ApiMgtDAO.getInstance().updateApiProvider(apiId, providerName);
apiPersistenceInstance.changeApiProvider(providerName, apiId, organization);
}catch (APIManagementException | APIPersistenceException e) {
throw new APIManagementException("Error While Changing the Api Provider", e);
}
return Response.ok().build();

Check warning on line 114 in components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.admin.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/admin/v1/impl/ApisApiServiceImpl.java#L108-L114

Added lines #L108 - L114 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3234,7 +3234,7 @@ paths:
security:
- OAuth2Security:
- apim:admin
#- apim:api_provider_change
- apim:api_provider_change
x-code-samples:
- lang: Curl
source: 'curl -k -X PUT -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8"
Expand Down Expand Up @@ -3314,7 +3314,7 @@ paths:
security:
- OAuth2Security:
- apim:admin
#- apim:api_provider_change
- apim:api_provider_change
x-code-samples:
- lang: Curl
source: 'curl -k -H "Authorization: Bearer ae4eae22-3f65-387b-a171-d37eaa366fa8"
Expand Down

0 comments on commit 7de9b63

Please sign in to comment.