use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdUserRatingPut.
/**
* Add or update raating to an API
*
* @param apiId APIID
* @param body RatingDTO object
* @param request msf4j request
* @return 201 response if successful
* @throws NotFoundException if failed to find method implementation
*/
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
String ratingId;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
if (existingRating != null) {
String existingRatingUUID = existingRating.getUuid();
apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
return Response.ok().entity(updatedRatingDTO).build();
} else {
ratingId = apiStore.addRating(apiId, ratingFromPayload);
Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding/updating rating for user " + username + " for given API " + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for comment";
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSdksLanguageGet.
/**
* Generates an SDK for a API with provided ID and language
*
* @param apiId API ID
* @param language SDK language
* @param request msf4j request object
* @return ZIP file for the generated SDK
* @throws NotFoundException if failed to find method implementation
*/
@Override
public Response apisApiIdSdksLanguageGet(String apiId, String language, Request request) throws NotFoundException {
if (StringUtils.isBlank(apiId) || StringUtils.isBlank(language)) {
String errorMessage = "API ID or language is not valid";
log.error(errorMessage);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
String userName = RestApiUtil.getLoggedInUsername(request);
ApiStoreSdkGenerationManager sdkGenerationManager = new ApiStoreSdkGenerationManager();
if (!sdkGenerationManager.getSdkGenLanguages().containsKey(language)) {
String errorMessage = "Specified language parameter doesn't exist";
log.error(errorMessage);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 400L, errorMessage);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
String tempZipFilePath;
try {
tempZipFilePath = sdkGenerationManager.generateSdkForApi(apiId, language, userName);
} catch (ApiStoreSdkGenerationException e) {
String errorMessage = "Error while generating SDK for requested language";
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while retrieving API for SDK generation " + apiId;
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
File sdkZipFile = new File(tempZipFilePath);
return Response.ok().entity(sdkZipFile).header("Content-Disposition", "attachment; filename=\"" + sdkZipFile.getName() + "\"").build();
}
use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.
the class SubscriptionsApiServiceImplTestCase method getRequest.
// Sample request to be used by tests
private Request getRequest() throws Exception {
HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
Request request = new Request(carbonMessage);
return request;
}
use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.
the class EnvironmentsApiServiceImplTestCase method getRequest.
// Sample request to be used by tests
private Request getRequest() throws Exception {
HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
Request request = new Request(carbonMessage);
return request;
}
use of org.wso2.msf4j.Request in project carbon-apimgt by wso2.
the class ExternalResourcesApiServiceImplTestCase method getRequest.
// Sample request to be used by tests
private Request getRequest() throws Exception {
HTTPCarbonMessage carbonMessage = Mockito.mock(HTTPCarbonMessage.class);
Mockito.when(carbonMessage.getProperty("LOGGED_IN_USER")).thenReturn(USER);
Request request = new Request(carbonMessage);
return request;
}
Aggregations