Search in sources :

Example 46 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project spring-data-commons by spring-projects.

the class HateoasPageableHandlerMethodArgumentResolverUnitTests method assertUriStringFor.

protected void assertUriStringFor(Pageable pageable, String expected) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/");
    MethodParameter parameter = getParameterOfMethod("supportedMethod");
    getResolver().enhance(builder, parameter, pageable);
    assertThat(builder.build().toUriString()).endsWith(expected);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MethodParameter(org.springframework.core.MethodParameter)

Example 47 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project spring-data-commons by spring-projects.

the class HateoasSortHandlerMethodArgumentResolverUnitTests method assertUriStringFor.

private void assertUriStringFor(Sort sort, String expected, String baseUri) throws Exception {
    UriComponentsBuilder builder = UriComponentsBuilder.fromUri(new URI(baseUri));
    MethodParameter parameter = getParameterOfMethod("supportedMethod");
    new HateoasSortHandlerMethodArgumentResolver().enhance(builder, parameter, sort);
    assertThat(builder.build().toUriString()).endsWith(expected);
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) MethodParameter(org.springframework.core.MethodParameter) URI(java.net.URI)

Example 48 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project ma-modules-public by infiniteautomation.

the class DataPointRestController method bulkDataPointOperation.

@ApiOperation(value = "Bulk get/create/update/delete data points", notes = "User must have read/edit permission for the data point")
@RequestMapping(method = RequestMethod.POST, value = "/bulk")
public ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>> bulkDataPointOperation(@RequestBody DataPointBulkRequest requestBody, @AuthenticationPrincipal User user, UriComponentsBuilder builder) {
    VoAction defaultAction = requestBody.getAction();
    DataPointModel defaultBody = requestBody.getBody();
    List<DataPointIndividualRequest> requests = requestBody.getRequests();
    if (requests == null) {
        throw new BadRequestException(new TranslatableMessage("rest.error.mustNotBeNull", "requests"));
    }
    String resourceId = requestBody.getId();
    Long expiration = requestBody.getExpiration();
    Long timeout = requestBody.getTimeout();
    TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception> responseBody = bulkResourceManager.newTemporaryResource(RESOURCE_TYPE_BULK_DATA_POINT, resourceId, user.getId(), expiration, timeout, (resource) -> {
        DataPointBulkResponse bulkResponse = new DataPointBulkResponse();
        int i = 0;
        resource.progress(bulkResponse, i++, requests.size());
        for (DataPointIndividualRequest request : requests) {
            UriComponentsBuilder reqBuilder = UriComponentsBuilder.newInstance();
            DataPointIndividualResponse individualResponse = doIndividualRequest(request, defaultAction, defaultBody, user, reqBuilder);
            bulkResponse.addResponse(individualResponse);
            resource.progressOrSuccess(bulkResponse, i++, requests.size());
        }
    });
    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(builder.path("/v2/data-points/bulk/{id}").buildAndExpand(responseBody.getId()).toUri());
    return new ResponseEntity<TemporaryResource<DataPointBulkResponse, AbstractRestV2Exception>>(responseBody, headers, HttpStatus.CREATED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) VoAction(com.infiniteautomation.mango.rest.v2.bulk.VoAction) DataPointModel(com.infiniteautomation.mango.rest.v2.model.dataPoint.DataPointModel) AbstractRestV2Exception(com.infiniteautomation.mango.rest.v2.exception.AbstractRestV2Exception) ResponseEntity(org.springframework.http.ResponseEntity) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) BadRequestException(com.infiniteautomation.mango.rest.v2.exception.BadRequestException) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project molgenis by molgenis.

the class RestControllerV2 method createEntityCollectionResponse.

private EntityCollectionResponseV2 createEntityCollectionResponse(String entityTypeId, EntityCollectionRequestV2 request, HttpServletRequest httpRequest, boolean includeCategories) {
    EntityType meta = dataService.getEntityType(entityTypeId);
    Query<Entity> q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl<>();
    q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
    Fetch fetch = AttributeFilterToFetchConverter.convert(request.getAttrs(), meta, LocaleContextHolder.getLocale().getLanguage());
    if (fetch != null) {
        q.fetch(fetch);
    }
    if (request.getAggs() != null) {
        // return aggregates for aggregate query
        AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
        Attribute xAttr = aggsQ.getAttributeX();
        Attribute yAttr = aggsQ.getAttributeY();
        if (xAttr == null && yAttr == null) {
            throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
        }
        AggregateResult aggs = dataService.aggregate(entityTypeId, aggsQ);
        AttributeResponseV2 xAttrResponse = xAttr != null ? new AttributeResponseV2(entityTypeId, meta, xAttr, fetch, permissionService, dataService) : null;
        AttributeResponseV2 yAttrResponse = yAttr != null ? new AttributeResponseV2(entityTypeId, meta, yAttr, fetch, permissionService, dataService) : null;
        return new EntityAggregatesResponse(aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityTypeId);
    } else {
        Long count = dataService.count(entityTypeId, new QueryImpl<>(q).setOffset(0).setPageSize(0));
        Iterable<Entity> it;
        if (count > 0 && q.getPageSize() > 0) {
            it = () -> dataService.findAll(entityTypeId, q).iterator();
        } else {
            it = Collections.emptyList();
        }
        EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
        List<Map<String, Object>> entities = new ArrayList<>();
        for (Entity entity : it) {
            Map<String, Object> responseData = new LinkedHashMap<>();
            createEntityValuesResponse(entity, fetch, responseData);
            entities.add(responseData);
        }
        UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));
        String prevHref = null;
        if (pager.getPrevStart() != null) {
            builder.replaceQueryParam("start", pager.getPrevStart());
            prevHref = builder.build(false).toUriString();
        }
        String nextHref = null;
        if (pager.getNextStart() != null) {
            builder.replaceQueryParam("start", pager.getNextStart());
            nextHref = builder.build(false).toUriString();
        }
        return new EntityCollectionResponseV2(pager, entities, fetch, BASE_URI + '/' + entityTypeId, meta, permissionService, dataService, prevHref, nextHref, includeCategories);
    }
}
Also used : Attribute(org.molgenis.data.meta.model.Attribute) AggregateQuery(org.molgenis.data.aggregation.AggregateQuery) EntityType(org.molgenis.data.meta.model.EntityType) AttributeFilterToFetchConverter.createDefaultAttributeFetch(org.molgenis.data.rest.v2.AttributeFilterToFetchConverter.createDefaultAttributeFetch) EntityPager(org.molgenis.data.rest.EntityPager) AggregateResult(org.molgenis.data.aggregation.AggregateResult) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Example 50 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project spring-security by spring-projects.

the class Saml2LogoutRequestFilter method doRedirect.

private void doRedirect(HttpServletRequest request, HttpServletResponse response, Saml2LogoutResponse logoutResponse) throws IOException {
    String location = logoutResponse.getResponseLocation();
    UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(location);
    addParameter(Saml2ParameterNames.SAML_RESPONSE, logoutResponse::getParameter, uriBuilder);
    addParameter(Saml2ParameterNames.RELAY_STATE, logoutResponse::getParameter, uriBuilder);
    addParameter(Saml2ParameterNames.SIG_ALG, logoutResponse::getParameter, uriBuilder);
    addParameter(Saml2ParameterNames.SIGNATURE, logoutResponse::getParameter, uriBuilder);
    this.redirectStrategy.sendRedirect(request, response, uriBuilder.build(true).toUriString());
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder)

Aggregations

UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)131 Test (org.junit.Test)34 UriComponents (org.springframework.web.util.UriComponents)23 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)12 URI (java.net.URI)9 Test (org.junit.jupiter.api.Test)9 SearchRequest (org.nzbhydra.searching.searchrequests.SearchRequest)8 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)8 ArrayList (java.util.ArrayList)7 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)7 List (java.util.List)6 User (com.serotonin.m2m2.vo.User)5 RestProcessResult (com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 HashMap (java.util.HashMap)5 HttpEntity (org.springframework.http.HttpEntity)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)5 IOException (java.io.IOException)4 Map (java.util.Map)4