Search in sources :

Example 41 with Comment

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;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Example 42 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 43 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) HashMap(java.util.HashMap) URI(java.net.URI)

Example 44 with Comment

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 45 with Comment

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;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

Comment (org.wso2.carbon.apimgt.core.models.Comment)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)23 Test (org.testng.annotations.Test)22 SQLException (java.sql.SQLException)18 BeforeTest (org.testng.annotations.BeforeTest)18 API (org.wso2.carbon.apimgt.core.models.API)17 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 Comment (org.wso2.carbon.apimgt.api.model.Comment)16 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)15 PreparedStatement (java.sql.PreparedStatement)13 Connection (java.sql.Connection)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 ArrayList (java.util.ArrayList)10 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)8 Test (org.junit.Test)8