EmailServiceImpl(Environment env, TextService text) { this.env = requireNonNull(env); this.text = requireNonNull(text); this.mailgun = new RestTemplate(); this.mailgun.setMessageConverters(asList( new FormHttpMessageConverter(), new StringHttpMessageConverter(), new MappingJackson2HttpMessageConverter() )); this.mailgun.getInterceptors().add(new BasicAuthorizationInterceptor( "api", env.getProperty("mailgun.apiKey") )); }
@Override public BotStatus getBotStatus(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + STATUS_RESOURCE_PATH; LOG.info(() -> "Fetching BotStatus from: " + endpointUrl); final BotStatus botStatus = restTemplate.getForObject(endpointUrl, BotStatus.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + botStatus); return botStatus; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public List<StrategyConfig> findAll(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + STRATEGY_RESOURCE_PATH; LOG.info(() -> "Fetching all StrategyConfig from: " + endpointUrl); @SuppressWarnings("unchecked") final List<StrategyConfig> allTheStrategyConfig = restTemplate.getForObject(endpointUrl, List.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + allTheStrategyConfig); return allTheStrategyConfig; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return new ArrayList<>(); } }
@Override public StrategyConfig findById(BotConfig botConfig, String strategyId) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + STRATEGY_RESOURCE_PATH + '/' + strategyId; LOG.info(() -> "Fetching StrategyConfig from: " + endpointUrl); @SuppressWarnings("unchecked") final StrategyConfig strategyConfig = restTemplate.getForObject(endpointUrl, StrategyConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + strategyConfig); return strategyConfig; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public StrategyConfig save(BotConfig botConfig, StrategyConfig strategyConfig) { LOG.info(() -> "Saving StrategyConfig: " + strategyConfig + " for botId: " + botConfig.getId()); try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + STRATEGY_RESOURCE_PATH; LOG.info(() -> "Sending StrategyConfig to: " + endpointUrl); final HttpEntity<StrategyConfig> requestUpdate = new HttpEntity<>(strategyConfig); final ResponseEntity<StrategyConfig> savedConfig = restTemplate.exchange( endpointUrl, HttpMethod.PUT, requestUpdate, StrategyConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + savedConfig.getBody()); return savedConfig.getBody(); } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public boolean delete(BotConfig botConfig, String strategyId) { LOG.info(() -> "Deleting StrategyConfig for strategyId: " + strategyId + " for botId: " + botConfig.getId()); try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + STRATEGY_RESOURCE_PATH + '/' + strategyId; LOG.info(() -> "Deleting StrategyConfig from: " + endpointUrl); restTemplate.delete(endpointUrl); return true; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return false; } }
@Override public List<MarketConfig> findAll(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + MARKET_RESOURCE_PATH; LOG.info(() -> "Fetching all MarketConfig from: " + endpointUrl); @SuppressWarnings("unchecked") final List<MarketConfig> allTheMarketConfig = restTemplate.getForObject(endpointUrl, List.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + allTheMarketConfig); return allTheMarketConfig; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return new ArrayList<>(); } }
@Override public MarketConfig findById(BotConfig botConfig, String marketId) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + MARKET_RESOURCE_PATH + '/' + marketId; LOG.info(() -> "Fetching MarketConfig from: " + endpointUrl); @SuppressWarnings("unchecked") final MarketConfig marketConfig = restTemplate.getForObject(endpointUrl, MarketConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + marketConfig); return marketConfig; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public MarketConfig save(BotConfig botConfig, MarketConfig marketConfig) { LOG.info(() -> "Saving MarketConfig: " + marketConfig + " for botId: " + botConfig.getId()); try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + MARKET_RESOURCE_PATH; LOG.info(() -> "Sending MarketConfig to: " + endpointUrl); final HttpEntity<MarketConfig> requestUpdate = new HttpEntity<>(marketConfig); final ResponseEntity<MarketConfig> savedConfig = restTemplate.exchange( endpointUrl, HttpMethod.PUT, requestUpdate, MarketConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + savedConfig.getBody()); return savedConfig.getBody(); } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public boolean delete(BotConfig botConfig, String marketId) { LOG.info(() -> "Deleting MarketConfig for marketId: " + marketId + " for botId: " + botConfig.getId()); try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + MARKET_RESOURCE_PATH + '/' + marketId; LOG.info(() -> "Deleting MarketConfig from: " + endpointUrl); restTemplate.delete(endpointUrl); return true; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return false; } }
@Override public ExchangeConfig get(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + EXCHANGE_RESOURCE_PATH; LOG.info(() -> "Fetching ExchangeConfig from: " + endpointUrl); final ExchangeConfig config = restTemplate.getForObject(endpointUrl, ExchangeConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + config); return config; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public ExchangeConfig save(BotConfig botConfig, ExchangeConfig exchangeConfig) { try { LOG.info(() -> "About to save ExchangeConfig: " + exchangeConfig); restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + EXCHANGE_RESOURCE_PATH; LOG.info(() -> "Sending ExchangeConfig to: " + endpointUrl); final HttpEntity<ExchangeConfig> requestUpdate = new HttpEntity<>(exchangeConfig); final ResponseEntity<ExchangeConfig> savedConfig = restTemplate.exchange( endpointUrl, HttpMethod.PUT, requestUpdate, ExchangeConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + savedConfig); return savedConfig.getBody(); } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public EmailAlertsConfig get(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + EMAIL_ALERTS_RESOURCE_PATH; LOG.info(() -> "Fetching EmailAlertsConfig from: " + endpointUrl); final EmailAlertsConfig config = restTemplate.getForObject(endpointUrl, EmailAlertsConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + config); config.setId(botConfig.getId()); return config; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public EmailAlertsConfig save(BotConfig botConfig, EmailAlertsConfig emailAlertsConfig) { try { LOG.info(() -> "About to save EmailAlertsConfig: " + emailAlertsConfig); restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + EMAIL_ALERTS_RESOURCE_PATH; LOG.info(() -> "Sending EmailAlertsConfig to: " + endpointUrl); final HttpEntity<EmailAlertsConfig> requestUpdate = new HttpEntity<>(emailAlertsConfig); final ResponseEntity<EmailAlertsConfig> savedConfig = restTemplate.exchange( endpointUrl, HttpMethod.PUT, requestUpdate, EmailAlertsConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + savedConfig); final EmailAlertsConfig savedConfigBody = savedConfig.getBody(); savedConfigBody.setId(botConfig.getId()); return savedConfigBody; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
@Override public EngineConfig get(BotConfig botConfig) { try { restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + ENGINE_RESOURCE_PATH; LOG.info(() -> "Fetching EngineConfig from: " + endpointUrl); final EngineConfig config = restTemplate.getForObject(endpointUrl, EngineConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + config); config.setId(botConfig.getId()); return config; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
/** * register specific RestTemplate in Spring Application Context if needed */ private Consumer<Application> createRestTemplateBasicAuth(ConfigurableListableBeanFactory registry) { return app -> { // Create rest template instance RestTemplate restTemplateBasicAuth = defaultRestTemplateConfig.restTemplate(requestFactory, Arrays.asList(defaultRestTemplateConfig.getDefaultAcceptHeader(), MediaType.ALL)); // Configure it with BASIC auth restTemplateBasicAuth.getInterceptors().add(new BasicAuthorizationInterceptor(app.getActuatorUsername(), app.getActuatorPassword())); LOGGER.info("Registered RestTemplate with BASIC auth for application with id {}", app.getId()); // Add bean in Spring application context registry.registerSingleton(getRestTemplateBeanName(app), restTemplateBasicAuth); }; }
private RestTemplateBuilder(boolean detectRequestFactory, String rootUri, Set<HttpMessageConverter<?>> messageConverters, ClientHttpRequestFactory requestFactory, UriTemplateHandler uriTemplateHandler, ResponseErrorHandler errorHandler, BasicAuthorizationInterceptor basicAuthorization, Set<RestTemplateCustomizer> restTemplateCustomizers, Set<RequestFactoryCustomizer> requestFactoryCustomizers) { super(); this.detectRequestFactory = detectRequestFactory; this.rootUri = rootUri; this.messageConverters = messageConverters; this.requestFactory = requestFactory; this.uriTemplateHandler = uriTemplateHandler; this.errorHandler = errorHandler; this.basicAuthorization = basicAuthorization; this.restTemplateCustomizers = restTemplateCustomizers; this.requestFactoryCustomizers = requestFactoryCustomizers; }
@Bean public CommandLineRunner run(RestTemplate restTemplate) { return args -> { restTemplate.getInterceptors().add( new BasicAuthorizationInterceptor(alfredUsername, alfredPassword)); // oldSchool(restTemplate); newSchool(restTemplate); }; }
AlfredServiceImpl(final RestTemplateBuilder restTemplateBuilder, final NodeReferenceBuilder nodeReferenceBuilder, final String url, final String username, final String password) { this.nodeReferenceBuilder = nodeReferenceBuilder; this.url = url; restTemplate = restTemplateBuilder.build(); // restTemplate.getMessageConverters().add(new MappingJacksonHttpMessageConverter()); restTemplate.getMessageConverters().add(new StringHttpMessageConverter()); restTemplate.getInterceptors().add( new BasicAuthorizationInterceptor(username, password != null ? password : "")); logger.debug("Creation of AlfredServiceImpl for URL {}", url); }
@Override public EngineConfig save(BotConfig botConfig, EngineConfig engineConfig) { try { LOG.info(() -> "About to save EngineConfig: " + engineConfig); restTemplate.getInterceptors().clear(); restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor( botConfig.getUsername(), botConfig.getPassword())); final String endpointUrl = botConfig.getBaseUrl() + ENGINE_RESOURCE_PATH; LOG.info(() -> "Sending EngineConfig to: " + endpointUrl); final HttpEntity<EngineConfig> requestUpdate = new HttpEntity<>(engineConfig); final ResponseEntity<EngineConfig> savedConfig = restTemplate.exchange( endpointUrl, HttpMethod.PUT, requestUpdate, EngineConfig.class); LOG.info(() -> REMOTE_RESPONSE_RECEIVED_LOG_MSG + savedConfig); final EngineConfig savedConfigBody = savedConfig.getBody(); savedConfigBody.setId(botConfig.getId()); return savedConfigBody; } catch (RestClientException e) { LOG.error(FAILED_TO_INVOKE_REMOTE_BOT_LOG_MSG + e.getMessage(), e); return null; } }
private RestTemplate getRawRestTemplate() { RestTemplate restTemplate = new RestTemplate(); restTemplate.getMessageConverters().add( new ByteArrayHttpMessageConverter()); restTemplate.setRequestFactory( new InterceptingClientHttpRequestFactory( restTemplate.getRequestFactory(), Collections.singletonList( new BasicAuthorizationInterceptor("1", "1")))); return restTemplate; }
private RestOperations createRestOperations(String username, String password) { RestTemplate restTemplate = new RestTemplate(); ClientHttpRequestInterceptor basicAuthInterceptor = new BasicAuthorizationInterceptor(username, password); restTemplate.getInterceptors().add(basicAuthInterceptor); return restTemplate; }
@Bean(MIRRORGATE_REST_TEMPLATE) public RestTemplate getRestTemplate() { RestTemplate restTemplate = new RestTemplate(); MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter(); restTemplate.getMessageConverters().add(jsonHttpMessageConverter); if(mirrorGateUserName.isPresent() && !mirrorGateUserName.get().isEmpty()) { restTemplate.getInterceptors().add( new BasicAuthorizationInterceptor(mirrorGateUserName.get(), mirrorGatePassword.get())); } return restTemplate; }
@Bean(JIRA_REST_TEMPLATE) public RestTemplate getJiraRestTemplate() { RestTemplate restTemplate = new RestTemplate(); MappingJackson2HttpMessageConverter jsonHttpMessageConverter = new MappingJackson2HttpMessageConverter(); restTemplate.getMessageConverters().add(jsonHttpMessageConverter); restTemplate.getInterceptors().add( new BasicAuthorizationInterceptor(jiraUserName, jiraPassword)); return restTemplate; }
@Test public void basicAuthorizationShouldApply() throws Exception { RestTemplate template = this.builder.basicAuthorization("spring", "boot").build(); ClientHttpRequestInterceptor interceptor = template.getInterceptors().get(0); assertThat(interceptor).isInstanceOf(BasicAuthorizationInterceptor.class); assertThat(interceptor).extracting("username").containsExactly("spring"); assertThat(interceptor).extracting("password").containsExactly("boot"); }
private void addAuthentication(RestTemplate restTemplate, String username, String password) { if (username == null) { return; } List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList( new BasicAuthorizationInterceptor(username, password)); restTemplate.setRequestFactory(new InterceptingClientHttpRequestFactory( restTemplate.getRequestFactory(), interceptors)); }
private void addAuthentication(String username, String password) { if (username == null) { return; } List<ClientHttpRequestInterceptor> interceptors = Collections .<ClientHttpRequestInterceptor>singletonList( new BasicAuthorizationInterceptor(username, password)); setRequestFactory(new InterceptingClientHttpRequestFactory(getRequestFactory(), interceptors)); }
/** * Add HTTP basic authentication to requests. See * {@link BasicAuthorizationInterceptor} for details. * @param username the user name * @param password the password * @return a new builder instance */ public RestTemplateBuilder basicAuthorization(String username, String password) { return new RestTemplateBuilder(this.detectRequestFactory, this.rootUri, this.messageConverters, this.requestFactory, this.uriTemplateHandler, this.errorHandler, new BasicAuthorizationInterceptor(username, password), this.restTemplateCustomizers, this.requestFactoryCustomizers); }