Search in sources :

Example 86 with UriComponentsBuilder

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

the class RealTimeDataRestController method getAll.

/**
 * Get all of the Users Real Time Data
 * @param request
 * @param limit
 * @return
 */
@ApiOperation(value = "List realtime values", notes = "Check the status member to ensure the point is OK not DISABLED or UNRELIABLE")
@RequestMapping(method = RequestMethod.GET, value = "/list", produces = { "application/json" })
public ResponseEntity<List<RealTimeModel>> getAll(HttpServletRequest request, @ApiParam(value = "Limit the number of results", required = false) @RequestParam(value = "limit", required = false, defaultValue = "100") int limit) {
    RestProcessResult<List<RealTimeModel>> result = new RestProcessResult<List<RealTimeModel>>(HttpStatus.OK);
    User user = this.checkUser(request, result);
    if (result.isOk()) {
        List<RealTimeDataPointValue> values = RealTimeDataPointValueCache.instance.getUserView(user);
        ASTNode root = new ASTNode("limit", limit);
        values = root.accept(new RQLToObjectListQuery<RealTimeDataPointValue>(), values);
        List<RealTimeModel> models = new ArrayList<RealTimeModel>();
        UriComponentsBuilder imageServletBuilder = UriComponentsBuilder.fromPath("/imageValue/{ts}_{id}.jpg");
        for (RealTimeDataPointValue value : values) {
            if (value.getDataTypeId() == DataTypes.IMAGE) {
                models.add(new RealTimeModel(value, imageServletBuilder.buildAndExpand(value.getTimestamp(), value.getDataPointId()).toUri()));
            } else {
                models.add(new RealTimeModel(value, value.getValue()));
            }
        }
        return result.createResponseEntity(models);
    }
    return result.createResponseEntity();
}
Also used : RestProcessResult(com.serotonin.m2m2.web.mvc.rest.v1.message.RestProcessResult) User(com.serotonin.m2m2.vo.User) RealTimeDataPointValue(com.serotonin.m2m2.rt.dataImage.RealTimeDataPointValue) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ASTNode(net.jazdw.rql.parser.ASTNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) RealTimeModel(com.serotonin.m2m2.web.mvc.rest.v1.model.RealTimeModel) RQLToObjectListQuery(com.infiniteautomation.mango.db.query.pojo.RQLToObjectListQuery) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 87 with UriComponentsBuilder

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

the class OAuth2ClientContextFilter method redirectUser.

/**
 * Redirect the user according to the specified exception.
 *
 * @param e
 *            The user redirect exception.
 * @param request
 *            The request.
 * @param response
 *            The response.
 */
protected void redirectUser(UserRedirectRequiredException e, HttpServletRequest request, HttpServletResponse response) throws IOException {
    String redirectUri = e.getRedirectUri();
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(redirectUri);
    Map<String, String> requestParams = e.getRequestParams();
    for (Map.Entry<String, String> param : requestParams.entrySet()) {
        builder.queryParam(param.getKey(), param.getValue());
    }
    if (e.getStateKey() != null) {
        builder.queryParam("state", e.getStateKey());
    }
    this.redirectStrategy.sendRedirect(request, response, builder.build().encode().toUriString());
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder) Map(java.util.Map)

Example 88 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project wombat by PLOS.

the class Link method fillVariables.

private static String fillVariables(String path, final Map<String, ?> variables) {
    UriComponentsBuilder builder = ServletUriComponentsBuilder.fromPath(path);
    UriComponents.UriTemplateVariables uriVariables = (String name) -> {
        Object value = variables.get(name);
        if (value == null) {
            throw new IllegalArgumentException("Missing required parameter " + name);
        }
        return value;
    };
    return builder.build().expand(uriVariables).encode().toString();
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) ServletUriComponentsBuilder(org.springframework.web.servlet.support.ServletUriComponentsBuilder)

Example 89 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project mots by motech-implementations.

the class IvrService method sendVotoRequest.

private <T> T sendVotoRequest(String url, MultiValueMap<String, String> params, ParameterizedTypeReference<T> responseType, HttpMethod method) throws IvrException {
    params.add(API_KEY, ivrApiKey);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
    HttpEntity<?> request = new HttpEntity<>(headers);
    try {
        ResponseEntity<T> responseEntity = restTemplate.exchange(builder.build().toString(), method, request, responseType);
        return responseEntity.getBody();
    } catch (RestClientResponseException ex) {
        String responseBodyJson = ex.getResponseBodyAsString();
        String responseMessage = responseBodyJson;
        String clearVotoInfo = "";
        try {
            VotoResponseDto<String> votoResponse = mapper.readValue(responseBodyJson, new TypeReference<VotoResponseDto<String>>() {
            });
            if (votoResponse.getMessage() != null) {
                responseMessage = votoResponse.getMessage();
                clearVotoInfo = "Invalid IVR service response: " + responseMessage;
            }
        } catch (IOException e) {
            responseMessage = responseBodyJson;
        }
        String message = "Invalid IVR service response: " + ex.getRawStatusCode() + " " + ex.getStatusText() + ", Response body: " + responseMessage;
        throw new IvrException(message, ex, clearVotoInfo);
    } catch (RestClientException ex) {
        String message = "Error occurred when sending request to IVR service: " + ex.getMessage();
        throw new IvrException(message, ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) IvrException(org.motechproject.mots.exception.IvrException) HttpEntity(org.springframework.http.HttpEntity) VotoResponseDto(org.motechproject.mots.dto.VotoResponseDto) IOException(java.io.IOException) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RestClientException(org.springframework.web.client.RestClientException) RestClientResponseException(org.springframework.web.client.RestClientResponseException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 90 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project elastest-torm by elastest.

the class EsmServiceClient method provisionServiceInstance.

public String provisionServiceInstance(SupportServiceInstance serviceInstance, String instanceId, String accept_incomplete) {
    String serviceInstanceData = "";
    logger.info("Request a service instance.");
    HttpEntity<String> entity = new HttpEntity<String>(utilTools.convertJsonString(serviceInstance, ProvisionView.class), headers);
    Map<String, String> params = new HashMap<>();
    params.put("instance_id", instanceId);
    UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(URL_ESM_PROVISION_INSTANCE).queryParam("accept_incomplete", accept_incomplete);
    try {
        httpClient.exchange(builder.buildAndExpand(params).toUri(), HttpMethod.PUT, entity, String.class);
        logger.info("Registered service.");
    } catch (Exception e) {
        throw new RuntimeException("Exception provisioning service \"" + serviceInstance.getService_id() + "\" with instanceId \"" + instanceId + "\"", e);
    }
    return serviceInstanceData;
}
Also used : ProvisionView(io.elastest.etm.model.SupportServiceInstance.ProvisionView) HttpEntity(org.springframework.http.HttpEntity) HashMap(java.util.HashMap) 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