use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromCommentToDTOWithUserInfo.
/**
* Converts a Comment object into corresponding REST API CommentDTO object with User Info
*
* @param comment comment object
* @return CommentDTO
*/
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
CommentDTO commentDTO = fromCommentToDTO(comment);
if (userClaimsMap.get(comment.getUser()) != null) {
Map userClaims = userClaimsMap.get(comment.getUser());
CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
commentDTO.setCommenterInfo(commenterInfoDTO);
}
return commentDTO;
}
use of org.wso2.carbon.apimgt.api.model.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.api.model.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.api.model.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.api.model.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