Example usage for org.springframework.hateoas.client Traverson Traverson

List of usage examples for org.springframework.hateoas.client Traverson Traverson

Introduction

In this page you can find the example usage for org.springframework.hateoas.client Traverson Traverson.

Prototype

public Traverson(URI baseUri, List<MediaType> mediaTypes) 

Source Link

Document

Creates a new Traverson interacting with the given base URI and using the given MediaType s to interact with the service.

Usage

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

public String getRubricEvaluationObjectId(String associationId, String userId, String toolId) {
    try {/*w w  w .  j  a  v  a  2  s .c  o m*/
        URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
        Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);

        Traverson.TraversalBuilder builder = traverson.follow("evaluations", "search",
                "by-association-and-user");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", String.format("Bearer %s", generateJsonWebToken(toolId)));
        builder.withHeaders(headers);

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("associationId", associationId);
        parameters.put("userId", userId);

        String response = builder.withTemplateParameters(parameters).toObject(String.class);
        if (StringUtils.isNotEmpty(response)) {
            return response.replace("\"", "");
        }
    } catch (Exception e) {
        log.warn("Error {} while getting a rubric evaluation in assignment {} for user {}", e.getMessage(),
                associationId, userId);
    }
    return null;
}

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

/**
 * Delete all the rubric associations starting with itemId.
 * @param itemId The formatted item id./*from   w  w  w  .ja  va  2 s .co m*/
 */
public void deleteRubricAssociationsByItemIdPrefix(String itemId, String toolId) {
    try {
        TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>>() {
        };

        URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
        Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);

        Traverson.TraversalBuilder builder = traverson.follow("rubric-associations", "search",
                "by-item-id-prefix");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", String.format("Bearer %s", generateJsonWebToken(toolId)));
        builder.withHeaders(headers);

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("toolId", toolId);
        parameters.put("itemId", itemId);

        Resources<Resource<ToolItemRubricAssociation>> associationResources = builder
                .withTemplateParameters(parameters).toObject(resourceParameterizedTypeReference);

        for (Resource<ToolItemRubricAssociation> associationResource : associationResources) {
            String associationHref = associationResource.getLink(Link.REL_SELF).getHref();
            deleteRubricEvaluationsForAssociation(associationHref, toolId);
            deleteRubricResource(associationHref, toolId, null);
            hasAssociatedRubricCache.remove(toolId + "#" + itemId);
        }
    } catch (Exception e) {
        log.warn("Error deleting rubric association for id {} : {}", itemId, e.getMessage());
    }
}

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

protected Collection<Resource<Evaluation>> getRubricEvaluationsByAssociation(Long associationId)
        throws Exception {
    TypeReferences.ResourcesType<Resource<Evaluation>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resource<Evaluation>>() {
    };// ww  w .  j av  a 2 s.c  o  m

    URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
    Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);
    Traverson.TraversalBuilder builder = traverson.follow("evaluations", "search", "by-association-id");

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", String.format("Bearer %s", generateJsonWebToken(RubricsConstants.RBCS_TOOL)));
    builder.withHeaders(headers);

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("toolItemRubricAssociationId", associationId);
    Resources<Resource<Evaluation>> evaluationResources = builder.withTemplateParameters(parameters)
            .toObject(resourceParameterizedTypeReference);

    return evaluationResources.getContent();
}

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

@Override
public Map<String, String> transferCopyEntities(String fromContext, String toContext, List<String> ids,
        List<String> options) {

    Map<String, String> transversalMap = new HashMap<>();
    try {/*from w  ww.  ja v a  2 s  .  c  o m*/
        TypeReferences.ResourcesType<Resource<Rubric>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resource<Rubric>>() {
        };
        URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
        Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);
        Traverson.TraversalBuilder builder = traverson.follow("rubrics", "search", "rubrics-from-site");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization",
                String.format("Bearer %s", generateJsonWebToken(RubricsConstants.RBCS_TOOL, toContext)));
        builder.withHeaders(headers);

        Map<String, Object> parameters = new HashMap<>();
        parameters.put("siteId", fromContext);

        Resources<Resource<Rubric>> rubricResources = builder.withTemplateParameters(parameters)
                .toObject(resourceParameterizedTypeReference);
        for (Resource<Rubric> rubricResource : rubricResources) {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers2 = new HttpHeaders();
            headers2.setContentType(MediaType.APPLICATION_JSON);
            headers2.add("Authorization",
                    String.format("Bearer %s", generateJsonWebToken(RubricsConstants.RBCS_TOOL, toContext)));
            HttpEntity<?> requestEntity = new HttpEntity<>(headers2);
            ResponseEntity<Rubric> rubricEntity = restTemplate.exchange(
                    rubricResource.getLink(Link.REL_SELF).getHref() + "?projection=inlineRubric",
                    HttpMethod.GET, requestEntity, Rubric.class);
            Rubric rEntity = rubricEntity.getBody();
            String newId = cloneRubricToSite(String.valueOf(rEntity.getId()), toContext);
            String oldId = String.valueOf(rEntity.getId());
            transversalMap.put(RubricsConstants.RBCS_PREFIX + oldId, RubricsConstants.RBCS_PREFIX + newId);
        }
    } catch (Exception ex) {
        log.info("Exception on duplicateRubricsFromSite: " + ex.getMessage());
    }
    return transversalMap;
}

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

@Override
public Map<String, String> transferCopyEntities(String fromContext, String toContext, List<String> ids,
        List<String> options, boolean cleanup) {

    if (cleanup) {
        try {//w w w .  jav  a  2s  . co  m
            TypeReferences.ResourcesType<Resource<Rubric>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resource<Rubric>>() {
            };
            URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
            Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);
            Traverson.TraversalBuilder builder = traverson.follow("rubrics", "search", "rubrics-from-site");

            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization",
                    String.format("Bearer %s", generateJsonWebToken(RubricsConstants.RBCS_TOOL, toContext)));
            builder.withHeaders(headers);

            Map<String, Object> parameters = new HashMap<>();
            parameters.put("siteId", toContext);
            Resources<Resource<Rubric>> rubricResources = builder.withTemplateParameters(parameters)
                    .toObject(resourceParameterizedTypeReference);
            for (Resource<Rubric> rubricResource : rubricResources) {
                String[] rubricSplitted = rubricResource.getLink(Link.REL_SELF).getHref().split("/");
                Collection<Resource<ToolItemRubricAssociation>> assocs = getRubricAssociationByRubric(
                        rubricSplitted[rubricSplitted.length - 1], toContext);
                for (Resource<ToolItemRubricAssociation> associationResource : assocs) {
                    String associationHref = associationResource.getLink(Link.REL_SELF).getHref();
                    deleteRubricResource(associationHref, RubricsConstants.RBCS_TOOL, toContext);
                }
                deleteRubricResource(rubricResource.getLink(Link.REL_SELF).getHref(),
                        RubricsConstants.RBCS_TOOL, toContext);
            }
        } catch (Exception e) {
            log.error("Rubrics - transferCopyEntities: error trying to delete rubric -> {}", e.getMessage());
        }
    }
    return transferCopyEntities(fromContext, toContext, ids, null);
}

From source file:org.sakaiproject.rubrics.logic.RubricsServiceImpl.java

protected Collection<Resource<ToolItemRubricAssociation>> getRubricAssociationByRubric(String rubricId,
        String toSite) throws Exception {
    TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>> resourceParameterizedTypeReference = new TypeReferences.ResourcesType<Resource<ToolItemRubricAssociation>>() {
    };//ww w  .  java2 s  . c  o m

    URI apiBaseUrl = new URI(serverConfigurationService.getServerUrl() + RBCS_SERVICE_URL_PREFIX);
    Traverson traverson = new Traverson(apiBaseUrl, MediaTypes.HAL_JSON);
    Traverson.TraversalBuilder builder = traverson.follow("rubric-associations", "search", "by-rubric-id");

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization",
            String.format("Bearer %s", generateJsonWebToken(RubricsConstants.RBCS_TOOL, toSite)));
    builder.withHeaders(headers);

    Map<String, Object> parameters = new HashMap<>();
    parameters.put("rubricId", Long.valueOf(rubricId));
    Resources<Resource<ToolItemRubricAssociation>> associationResources = builder
            .withTemplateParameters(parameters).toObject(resourceParameterizedTypeReference);

    return associationResources.getContent();
}