Search in sources :

Example 31 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project open-kilda by telstra.

the class SwitchIntegrationService method getIslLinkPortsInfo.

/**
 * Gets the isl links port info.
 *
 * @return the isl links port info
 */
public List<IslLink> getIslLinkPortsInfo(final LinkProps keys) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(applicationProperties.getNbBaseUrl() + IConstants.NorthBoundUrl.GET_LINKS);
    builder = setLinkProps(keys, builder);
    String fullUri = builder.build().toUriString();
    HttpResponse response = restClientManager.invoke(fullUri, HttpMethod.GET, "", "", applicationService.getAuthHeader());
    if (RestClientManager.isValidResponse(response)) {
        List<IslLink> links = restClientManager.getResponseList(response, IslLink.class);
        return links;
    }
    return null;
}
Also used : IslLink(org.openkilda.integration.model.response.IslLink) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) HttpResponse(org.apache.http.HttpResponse)

Example 32 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project connectors-workspace-one by vmware.

the class SalesforceControllerTest method expectSalesforceRequest.

private ResponseActions expectSalesforceRequest(String soqlQuery) throws IOException {
    UriComponentsBuilder builder = fromHttpUrl(SERVER_URL).path(ACCOUNT_SEARCH_PATH).queryParam("q", soqlQuery);
    URI tmp = builder.build().toUri();
    return mockSF.expect(requestTo(tmp)).andExpect(method(HttpMethod.GET)).andExpect(MockRestRequestMatchers.header(HttpHeaders.AUTHORIZATION, "Bearer abc"));
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) URI(java.net.URI)

Example 33 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project connectors-workspace-one by vmware.

the class SalesforceControllerTest method testAddConversations.

@Test
void testAddConversations() throws Exception {
    UriComponentsBuilder addTaskReqBuilder = fromHttpUrl(SERVER_URL).path(LINK_OPPORTUNITY_TASK_PATH);
    UriComponentsBuilder addAttachmentReqBuilder = fromHttpUrl(SERVER_URL).path(LINK_ATTACHMENT_TASK_PATH);
    Map<String, String> userContactDetailMap = getUserContactDetails("/salesforce/request/conversations.txt");
    expectSalesforceRequest(String.format(QUERY_FMT_CONTACT_ID, userContactDetailMap.get("contact_email"), userContactDetailMap.get("user_email"))).andRespond(withSuccess(sfExistingContactId, APPLICATION_JSON));
    mockSF.expect(requestTo(addTaskReqBuilder.build().toUri())).andExpect(method(HttpMethod.POST)).andExpect(MockRestRequestMatchers.header(HttpHeaders.AUTHORIZATION, "Bearer abc")).andRespond(withSuccess(sfResponseTaskCreated, APPLICATION_JSON));
    mockSF.expect(requestTo(addAttachmentReqBuilder.build().toUri())).andExpect(method(HttpMethod.POST)).andExpect(MockRestRequestMatchers.header(HttpHeaders.AUTHORIZATION, "Bearer abc")).andRespond(withSuccess(sfResponseAttachmentCreated, APPLICATION_JSON));
    perform(requestAddConversationAsAttcahment("abc", "/salesforce/request/conversations.txt")).andExpect(status().isOk());
    mockSF.verify();
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with UriComponentsBuilder

use of org.springframework.web.util.UriComponentsBuilder in project tutorials by eugenp.

the class ApiClient method invokeAPI.

/**
 * Invoke API by sending HTTP request with the given options.
 *
 * @param <T> the return type to use
 * @param path The sub-path of the HTTP URL
 * @param method The request method
 * @param queryParams The query parameters
 * @param body The request body object
 * @param headerParams The header parameters
 * @param formParams The form parameters
 * @param accept The request's Accept header
 * @param contentType The request's Content-Type header
 * @param authNames The authentications to apply
 * @param returnType The return type into which to deserialize the response
 * @return The response body in chosen type
 */
public <T> T invokeAPI(String path, HttpMethod method, MultiValueMap<String, String> queryParams, Object body, HttpHeaders headerParams, MultiValueMap<String, Object> formParams, List<MediaType> accept, MediaType contentType, String[] authNames, ParameterizedTypeReference<T> returnType) throws RestClientException {
    updateParamsForAuth(authNames, queryParams, headerParams);
    final UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(basePath).path(path);
    if (queryParams != null) {
        builder.queryParams(queryParams);
    }
    final BodyBuilder requestBuilder = RequestEntity.method(method, builder.build().toUri());
    if (accept != null) {
        requestBuilder.accept(accept.toArray(new MediaType[accept.size()]));
    }
    if (contentType != null) {
        requestBuilder.contentType(contentType);
    }
    addHeadersToRequest(headerParams, requestBuilder);
    addHeadersToRequest(defaultHeaders, requestBuilder);
    RequestEntity<Object> requestEntity = requestBuilder.body(selectBody(body, formParams, contentType));
    ResponseEntity<T> responseEntity = restTemplate.exchange(requestEntity, returnType);
    statusCode = responseEntity.getStatusCode();
    responseHeaders = responseEntity.getHeaders();
    if (responseEntity.getStatusCode() == HttpStatus.NO_CONTENT) {
        return null;
    } else if (responseEntity.getStatusCode().is2xxSuccessful()) {
        if (returnType == null) {
            return null;
        }
        return responseEntity.getBody();
    } else {
        // The error handler built into the RestTemplate should handle 400 and 500 series errors.
        throw new RestClientException("API returned " + statusCode + " and it wasn't handled by the RestTemplate error handler");
    }
}
Also used : UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RestClientException(org.springframework.web.client.RestClientException) MediaType(org.springframework.http.MediaType) BodyBuilder(org.springframework.http.RequestEntity.BodyBuilder)

Example 35 with UriComponentsBuilder

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

the class AbstractHttpRequestExecutingMessageHandler method generateUri.

private URI generateUri(Message<?> requestMessage) {
    Object uri = this.uriExpression.getValue(this.evaluationContext, requestMessage);
    Assert.state(uri instanceof String || uri instanceof URI, "'uriExpression' evaluation must result in a 'String' or 'URI' instance, not: " + (uri == null ? "null" : uri.getClass()));
    Map<String, ?> uriVariables = determineUriVariables(requestMessage);
    UriComponentsBuilder uriComponentsBuilder = uri instanceof String ? UriComponentsBuilder.fromUriString((String) uri) : UriComponentsBuilder.fromUri((URI) uri);
    UriComponents uriComponents = uriComponentsBuilder.buildAndExpand(uriVariables);
    try {
        return this.encodeUri ? uriComponents.toUri() : new URI(uriComponents.toUriString());
    } catch (URISyntaxException e) {
        throw new MessageHandlingException(requestMessage, "Invalid URI [" + uri + "]", e);
    }
}
Also used : UriComponents(org.springframework.web.util.UriComponents) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Aggregations

UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)162 Test (org.junit.Test)34 UriComponents (org.springframework.web.util.UriComponents)24 HttpEntity (org.springframework.http.HttpEntity)18 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)13 URI (java.net.URI)10 Test (org.junit.jupiter.api.Test)9 HttpHeaders (org.springframework.http.HttpHeaders)9 MvcUriComponentsBuilder (org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder)9 ArrayList (java.util.ArrayList)8 SearchRequest (org.nzbhydra.searching.searchrequests.SearchRequest)8 List (java.util.List)7 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)7 HttpClientErrorException (org.springframework.web.client.HttpClientErrorException)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 Map (java.util.Map)5 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)5