use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDocumentsDocumentIdGet.
@Override
public Response apisApiIdDocumentsDocumentIdGet(String apiId, String documentId, String xWSO2Tenant, String ifModifiedSince, MessageContext messageContext) {
Documentation documentation;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
if (!RestAPIStoreUtils.isUserAccessAllowedForAPIByUUID(apiId, organization)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
}
documentation = apiConsumer.getDocumentation(apiId, documentId, organization);
if (null != documentation) {
DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(documentation);
return Response.ok().entity(documentDTO).build();
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_DOCUMENTATION, documentId, log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdUserRatingPut.
@Override
public Response apisApiIdUserRatingPut(String id, RatingDTO body, String xWSO2Tenant, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
int rating = 0;
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
// this will fail if user doesn't have access to the API or the API does not exist
apiConsumer.checkAPIVisibility(id, organization);
if (body != null) {
rating = body.getRating();
}
switch(rating) {
// Below case 0[Rate 0] - is to remove ratings from a user
case 0:
{
apiConsumer.rateAPI(id, APIRating.RATING_ZERO, username);
break;
}
case 1:
{
apiConsumer.rateAPI(id, APIRating.RATING_ONE, username);
break;
}
case 2:
{
apiConsumer.rateAPI(id, APIRating.RATING_TWO, username);
break;
}
case 3:
{
apiConsumer.rateAPI(id, APIRating.RATING_THREE, username);
break;
}
case 4:
{
apiConsumer.rateAPI(id, APIRating.RATING_FOUR, username);
break;
}
case 5:
{
apiConsumer.rateAPI(id, APIRating.RATING_FIVE, username);
break;
}
default:
{
RestApiUtil.handleBadRequest("Provided API Rating is not in the range from 1 to 5", log);
}
}
JSONObject obj = apiConsumer.getUserRatingInfo(id, username);
RatingDTO ratingDTO = new RatingDTO();
if (obj != null && !obj.isEmpty()) {
ratingDTO = APIMappingUtil.fromJsonToRatingDTO(obj);
ratingDTO.setApiId(id);
}
return Response.ok().entity(ratingDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, id, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, id, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while adding/updating user rating for API " + id, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdUserRatingDelete.
@Override
public Response apisApiIdUserRatingDelete(String apiId, String xWSO2Tenant, String ifMatch, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
// this will fail if user doesn't have access to the API or the API does not exist
apiConsumer.checkAPIVisibility(apiId, organization);
apiConsumer.removeAPIRating(apiId, username);
return Response.ok().build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_RATING + " for " + RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while deleting user rating for API " + apiId, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdDocumentsGet.
@Override
public Response apisApiIdDocumentsGet(String apiId, Integer limit, Integer offset, String xWSO2Tenant, String ifNoneMatch, MessageContext messageContext) {
// 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;
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
// this will fail if user doesn't have access to the API or the API does not exist
// APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, organization);
// List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiIdentifier, username);
List<Documentation> documentationList = apiConsumer.getAllDocumentation(apiId, organization);
DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(documentationList, offset, limit);
// todo : set total count properly
DocumentationMappingUtil.setPaginationParams(documentListDTO, apiId, offset, limit, documentationList.size());
return Response.ok().entity(documentListDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Error while getting API " + apiId, e, log);
}
}
/*catch (UnsupportedEncodingException e) {
String errorMessage = "Error while Decoding apiId" + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}*/
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class SearchApiServiceImpl method searchGet.
@Override
public Response searchGet(Integer limit, Integer offset, String xWSO2Tenant, String query, String ifNoneMatch, MessageContext messageContext) {
SearchResultListDTO resultListDTO = new SearchResultListDTO();
List<SearchResultDTO> allmatchedResults = new ArrayList<>();
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
query = query == null ? "*" : query;
String organization = RestApiUtil.getOrganization(messageContext);
try {
if (!query.contains(":")) {
query = (APIConstants.CONTENT_SEARCH_TYPE_PREFIX + ":" + query);
}
String username = RestApiCommonUtil.getLoggedInUsername();
APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
Map<String, Object> result = null;
// Extracting search queries for the recommendation system
apiConsumer.publishSearchQuery(query, username);
if (query.startsWith(APIConstants.CONTENT_SEARCH_TYPE_PREFIX)) {
result = apiConsumer.searchPaginatedContent(query, organization, offset, limit);
} else {
result = apiConsumer.searchPaginatedAPIs(query, organization, offset, limit, null, null);
}
ArrayList<Object> apis;
/* Above searchPaginatedAPIs method underneath calls searchPaginatedAPIsByContent method,searchPaginatedAPIs
method and searchAPIDoc method in AbstractApiManager. And those methods respectively returns ArrayList,
TreeSet and a HashMap.
Hence the below logic.
*/
Object apiSearchResults = result.get("apis");
if (apiSearchResults instanceof List<?>) {
apis = (ArrayList<Object>) apiSearchResults;
} else if (apiSearchResults instanceof HashMap) {
Collection<String> values = ((HashMap) apiSearchResults).values();
apis = new ArrayList<Object>(values);
} else {
apis = new ArrayList<Object>();
apis.addAll((Collection<?>) apiSearchResults);
}
for (Object searchResult : apis) {
if (searchResult instanceof API) {
API api = (API) searchResult;
SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(api);
allmatchedResults.add(apiResult);
} else if (searchResult instanceof Map.Entry) {
Map.Entry pair = (Map.Entry) searchResult;
SearchResultDTO docResult = SearchResultMappingUtil.fromDocumentationToDocumentResultDTO((Documentation) pair.getKey(), (API) pair.getValue());
allmatchedResults.add(docResult);
} else if (searchResult instanceof APIProduct) {
APIProduct apiProduct = (APIProduct) searchResult;
SearchResultDTO apiResult = SearchResultMappingUtil.fromAPIToAPIResultDTO(apiProduct);
allmatchedResults.add(apiResult);
}
}
Object totalLength = result.get("length");
Integer length = 0;
if (totalLength != null) {
length = (Integer) totalLength;
}
List<Object> allmatchedObjectResults = new ArrayList<>(allmatchedResults);
resultListDTO.setList(allmatchedObjectResults);
resultListDTO.setCount(allmatchedResults.size());
SearchResultMappingUtil.setPaginationParams(resultListDTO, query, offset, limit, length);
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving search results";
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return Response.ok().entity(resultListDTO).build();
}
Aggregations