List of usage examples for org.springframework.web.util UriComponentsBuilder fromUriString
public static UriComponentsBuilder fromUriString(String uri)
From source file:org.opentestsystem.authoring.testspecbank.client.TestSpecBankClient.java
@Override public Optional<ValidationError> deleteTestSpecification(final String testSpecificationKey) { Optional<ValidationError> maybeValidationError = Optional.empty(); final URI uri = UriComponentsBuilder .fromUriString(String.format("%stestSpecification/%s", baseUri, testSpecificationKey)).build() .toUri();/*from w w w .j a v a 2 s .com*/ try { tsbOauthRestTemplate.delete(uri); } catch (final HttpClientErrorException hce) { LOGGER.error("Error deleting {0} test specification: ", hce); if (hce.getStatusCode() == HttpStatus.UNPROCESSABLE_ENTITY) { try { // The NoContentResponseResource contains an array of ValidationErrors. If we got to this point, // the TestSpecificationController#deleteTestSpecification endpoint will have returned a // NoContentResponseResource with a single ValidationError describing what went wrong. final NoContentResponseResource response = objectMapper.readValue(hce.getResponseBodyAsString(), NoContentResponseResource.class); maybeValidationError = Optional.of(response.getErrors()[0]); } catch (final Exception mapEx) { LOGGER.error(String.format("Error mapping response %s to ValidationError: ", hce.getResponseBodyAsString()), mapEx); final String errorMessage = mapEx.getMessage() == null ? mapEx.getClass().getName() : mapEx.getMessage(); maybeValidationError = Optional.of(new ValidationError("mapping exception", errorMessage)); } } else { maybeValidationError = Optional.of(new ValidationError("client exception", hce.getMessage())); } } catch (final Exception e) { LOGGER.error("Error deleting {0} test specification: ", e); maybeValidationError = Optional.of(new ValidationError("server exception", e.getMessage())); } return maybeValidationError; }
From source file:org.opentestsystem.delivery.logging.LoggingConfigRefresher.java
public void refreshLoggerConfig() { final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory(); if (StringUtils.isBlank(configUrl) || StringUtils.isBlank(appName)) { addProfiles(context);// w ww. ja va 2 s . c o m return; } try { final UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(configUrl); builder.pathSegment(appName, profiles, configLabel, String.format("logback-%s.xml", appName)); final URL springConfigURL = builder.build().toUri().toURL(); final JoranConfigurator configurator = new JoranConfigurator(); configurator.setContext(context); // Call context.reset() to clear any previous configuration, e.g. default // configuration. For multi-step configuration, omit calling context.reset(). context.reset(); configurator.doConfigure(springConfigURL); addProfiles(context); } catch (JoranException je) { logger.warn("Exception occurred while configuring logback", je); } catch (MalformedURLException e) { logger.warn("Exception occurred while building url to spring-boot config server", e); } }
From source file:org.springframework.cloud.gateway.filter.headers.NonStandardHeadersInResponseTests.java
@Test public void nonStandardHeadersInResponse() { URI uri = UriComponentsBuilder.fromUriString(this.baseUri + "/get-image").build(true).toUri(); String contentType = WebClient.builder().baseUrl(baseUri).build().get().uri(uri).exchange() .map(clientResponse -> clientResponse.headers().asHttpHeaders().getFirst(HttpHeaders.CONTENT_TYPE)) .block();/*from w ww . j a va2 s . com*/ assertThat(contentType).isEqualTo(CONTENT_TYPE_IMAGE); }
From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitBindingCleaner.java
private Map<String, List<String>> doClean(String adminUri, String user, String pw, String vhost, String binderPrefix, String entity, boolean isJob) { RestTemplate restTemplate = RabbitManagementUtils.buildRestTemplate(adminUri, user, pw); List<String> removedQueues = isJob ? null : findStreamQueues(adminUri, vhost, binderPrefix, entity, restTemplate); List<String> removedExchanges = findExchanges(adminUri, vhost, binderPrefix, entity, restTemplate); // Delete the queues in reverse order to enable re-running after a partial success. // The queue search above starts with 0 and terminates on a not found. for (int i = removedQueues.size() - 1; i >= 0; i--) { String queueName = removedQueues.get(i); URI uri = UriComponentsBuilder.fromUriString(adminUri + "/api") .pathSegment("queues", "{vhost}", "{stream}").buildAndExpand(vhost, queueName).encode().toUri(); restTemplate.delete(uri);/*from www . j av a2s. co m*/ if (logger.isDebugEnabled()) { logger.debug("deleted queue: " + queueName); } } Map<String, List<String>> results = new HashMap<>(); if (removedQueues.size() > 0) { results.put("queues", removedQueues); } // Fanout exchanges for taps for (String exchange : removedExchanges) { URI uri = UriComponentsBuilder.fromUriString(adminUri + "/api") .pathSegment("exchanges", "{vhost}", "{name}").buildAndExpand(vhost, exchange).encode().toUri(); restTemplate.delete(uri); if (logger.isDebugEnabled()) { logger.debug("deleted exchange: " + exchange); } } if (removedExchanges.size() > 0) { results.put("exchanges", removedExchanges); } return results; }
From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitBindingCleaner.java
private List<Map<String, Object>> listAllQueues(String adminUri, String vhost, RestTemplate restTemplate) { URI uri = UriComponentsBuilder.fromUriString(adminUri + "/api").pathSegment("queues", "{vhost}") .buildAndExpand(vhost).encode().toUri(); @SuppressWarnings("unchecked") List<Map<String, Object>> queues = restTemplate.getForObject(uri, List.class); return queues; }
From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitBindingCleaner.java
@SuppressWarnings("unchecked") private List<String> findExchanges(String adminUri, String vhost, String binderPrefix, String entity, RestTemplate restTemplate) {//from www . j a v a 2s .c om List<String> removedExchanges = new ArrayList<>(); URI uri = UriComponentsBuilder.fromUriString(adminUri + "/api").pathSegment("exchanges", "{vhost}") .buildAndExpand(vhost).encode().toUri(); List<Map<String, Object>> exchanges = restTemplate.getForObject(uri, List.class); String exchangeNamePrefix = adjustPrefix(AbstractBinder.applyPrefix(binderPrefix, entity)); for (Map<String, Object> exchange : exchanges) { String exchangeName = (String) exchange.get("name"); if (exchangeName.startsWith(exchangeNamePrefix)) { uri = UriComponentsBuilder.fromUriString(adminUri + "/api") .pathSegment("exchanges", "{vhost}", "{name}", "bindings", "source") .buildAndExpand(vhost, exchangeName).encode().toUri(); List<Map<String, Object>> bindings = restTemplate.getForObject(uri, List.class); if (hasNoForeignBindings(bindings, exchangeNamePrefix)) { uri = UriComponentsBuilder.fromUriString(adminUri + "/api") .pathSegment("exchanges", "{vhost}", "{name}", "bindings", "destination") .buildAndExpand(vhost, exchangeName).encode().toUri(); bindings = restTemplate.getForObject(uri, List.class); if (bindings.size() == 0) { removedExchanges.add((String) exchange.get("name")); } else { throw new RabbitAdminException( "Cannot delete exchange " + exchangeName + "; it is a destination: " + bindings); } } else { throw new RabbitAdminException( "Cannot delete exchange " + exchangeName + "; it has bindings: " + bindings); } } } return removedExchanges; }
From source file:org.springframework.cloud.stream.binder.rabbit.LocalizedQueueConnectionFactory.java
private ConnectionFactory determineConnectionFactory(String queue) { for (int i = 0; i < this.adminAdresses.length; i++) { String adminUri = this.adminAdresses[i]; RestTemplate template = createRestTemplate(adminUri); URI uri = UriComponentsBuilder.fromUriString(adminUri + "/api") .pathSegment("queues", "{vhost}", "{queue}").buildAndExpand(this.vhost, queue).encode().toUri(); try {//w w w . j a v a2s .c om @SuppressWarnings("unchecked") Map<String, Object> queueInfo = template.getForObject(uri, Map.class); if (queueInfo != null) { String node = (String) queueInfo.get("node"); if (node != null) { for (int j = 0; j < this.nodes.length; j++) { if (this.nodes[j].equals(node)) { return nodeConnectionFactory(queue, j); } } } } } catch (Exception e) { logger.error("Failed to determine queue location for: " + queue + " at: " + uri.toString(), e); } } logger.warn("Failed to determine queue location for: " + queue); return null; }
From source file:org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint.java
private String append(String base, Map<String, ?> query, Map<String, String> keys, boolean fragment) { UriComponentsBuilder template = UriComponentsBuilder.newInstance(); UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(base); URI redirectUri;/* w w w . j ava2 s . c o m*/ try { // assume it's encoded to start with (if it came in over the wire) redirectUri = builder.build(true).toUri(); } catch (Exception e) { // ... but allow client registrations to contain hard-coded non-encoded values redirectUri = builder.build().toUri(); builder = UriComponentsBuilder.fromUri(redirectUri); } template.scheme(redirectUri.getScheme()).port(redirectUri.getPort()).host(redirectUri.getHost()) .userInfo(redirectUri.getUserInfo()).path(redirectUri.getPath()); if (fragment) { StringBuilder values = new StringBuilder(); if (redirectUri.getFragment() != null) { String append = redirectUri.getFragment(); values.append(append); } for (String key : query.keySet()) { if (values.length() > 0) { values.append("&"); } String name = key; if (keys != null && keys.containsKey(key)) { name = keys.get(key); } values.append(name + "={" + key + "}"); } if (values.length() > 0) { template.fragment(values.toString()); } UriComponents encoded = template.build().expand(query).encode(); builder.fragment(encoded.getFragment()); } else { for (String key : query.keySet()) { String name = key; if (keys != null && keys.containsKey(key)) { name = keys.get(key); } template.queryParam(name, "{" + key + "}"); } template.fragment(redirectUri.getFragment()); UriComponents encoded = template.build().expand(query).encode(); builder.query(encoded.getQuery()); } return builder.build().toUriString(); }
From source file:org.springframework.web.socket.client.AbstractWebSocketClient.java
@Override public ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, String uriTemplate, Object... uriVars) {//from www . j av a 2 s. c o m Assert.notNull(uriTemplate, "'uriTemplate' must not be null"); URI uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVars).encode().toUri(); return doHandshake(webSocketHandler, null, uri); }
From source file:org.springframework.web.socket.client.ConnectionManagerSupport.java
public ConnectionManagerSupport(String uriTemplate, Object... uriVariables) { this.uri = UriComponentsBuilder.fromUriString(uriTemplate).buildAndExpand(uriVariables).encode().toUri(); }