use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method addCommentToAPI.
@Override
public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBodyDTO, String replyTo, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = new Comment();
comment.setText(postRequestBodyDTO.getContent());
comment.setCategory(postRequestBodyDTO.getCategory());
comment.setParentCommentID(replyTo);
comment.setEntryPoint("DEVPORTAL");
comment.setUser(username);
comment.setApiId(apiId);
String createdCommentId = apiConsumer.addComment(apiId, comment, username);
Comment createdComment = apiConsumer.getComment(apiTypeWrapper, createdCommentId, 0, 0);
CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + createdCommentId;
URI uri = new URI(uriString);
return Response.created(uri).entity(commentDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to add comment to the API " + apiId, e, log);
}
} catch (URISyntaxException e) {
throw new APIManagementException("Error while retrieving comment content location for API " + apiId);
}
return null;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getCommentOfAPI.
@Override
public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, replyLimit, replyOffset);
if (comment != null) {
CommentDTO commentDTO;
if (includeCommenterInfo) {
Map<String, Map<String, String>> userClaimsMap = CommentMappingUtil.retrieveUserClaims(comment.getUser(), new HashMap<>());
commentDTO = CommentMappingUtil.fromCommentToDTOWithUserInfo(comment, userClaimsMap);
} else {
commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
}
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
URI uri = new URI(uriString);
return Response.ok(uri).entity(commentDTO).build();
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} 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 {
String errorMessage = "Error while retrieving comment for API : " + apiId + "with comment ID " + commentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comment content location : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteComment.
@Override
public Response deleteComment(String commentId, String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, 0, 0);
if (comment != null) {
String[] tokenScopes = (String[]) PhaseInterceptorChain.getCurrentMessage().getExchange().get(RestApiConstants.USER_REST_API_SCOPES);
if (Arrays.asList(tokenScopes).contains("apim:admin") || comment.getUser().equals(username)) {
if (apiConsumer.deleteComment(apiTypeWrapper, commentId)) {
JSONObject obj = new JSONObject();
obj.put("id", commentId);
obj.put("message", "The comment has been deleted");
return Response.ok(obj).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(405, "Method Not Allowed").type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(403, "Forbidden").type(MediaType.APPLICATION_JSON).build();
}
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} 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 {
String errorMessage = "Error while deleting comment " + commentId + "for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method editCommentOfAPI.
@Override
public Response editCommentOfAPI(String commentId, String apiId, PatchRequestBodyDTO patchRequestBodyDTO, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
String requestedTenantDomain = RestApiCommonUtil.getLoggedInUserTenantDomain();
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ApiTypeWrapper apiTypeWrapper = apiProvider.getAPIorAPIProductByUUID(apiId, requestedTenantDomain);
Comment comment = apiProvider.getComment(apiTypeWrapper, commentId, 0, 0);
if (comment != null) {
if (comment.getUser().equals(username)) {
boolean commentEdited = false;
if (patchRequestBodyDTO.getCategory() != null && !(patchRequestBodyDTO.getCategory().equals(comment.getCategory()))) {
comment.setCategory(patchRequestBodyDTO.getCategory());
commentEdited = true;
}
if (patchRequestBodyDTO.getContent() != null && !(patchRequestBodyDTO.getContent().equals(comment.getText()))) {
comment.setText(patchRequestBodyDTO.getContent());
commentEdited = true;
}
if (commentEdited) {
if (apiProvider.editComment(apiTypeWrapper, commentId, comment)) {
Comment editedComment = apiProvider.getComment(apiTypeWrapper, commentId, 0, 0);
CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(editedComment);
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
URI uri = new URI(uriString);
return Response.ok(uri).entity(commentDTO).build();
}
} else {
return Response.ok().build();
}
} else {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comment content location for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApiDAOImpl method getCommentByUUID.
@Override
public Comment getCommentByUUID(String commentId, String apiId) throws APIMgtDAOException {
final String query = "SELECT UUID, COMMENT_TEXT, USER_IDENTIFIER, API_ID, " + "CREATED_BY, CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME " + "FROM AM_API_COMMENTS WHERE UUID = ? AND API_ID = ?";
try (Connection connection = DAOUtil.getConnection();
PreparedStatement statement = connection.prepareStatement(query)) {
try {
statement.setString(1, commentId);
statement.setString(2, apiId);
statement.execute();
try (ResultSet rs = statement.getResultSet()) {
if (rs.next()) {
return constructCommentFromResultSet(rs);
}
}
} catch (SQLException e) {
String errorMessage = "getting comment for API: " + apiId + ", Comment: " + commentId;
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
}
} catch (SQLException e) {
String errorMessage = "getting comment for API: " + apiId + ", Comment: " + commentId;
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
}
return null;
}
Aggregations