UrlSourceJob extends SourceJob { @Override protected void executeInternal( ApplicationContext context, JobKey jobKey, Map<String, Object> configuration) { String url = configuration.get(PARAM_URL).toString(); boolean enableSslVerify = isSslVerificationEnabled(configuration); CredentialsProvider credentialsProvider = configureBasicAuthentication(configuration); HttpClientContext clientContext = configureContext(credentialsProvider, url); HashMap<String, Object> data = new HashMap<>(); List<String> errors = new ArrayList<>(); try (CloseableHttpClient httpClient = configureHttpClient(enableSslVerify)) { HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response; if (clientContext != null) { response = httpClient.execute(httpGet, clientContext); } else { response = httpClient.execute(httpGet); } data.put(PARAM_STATUS, response.getStatusLine().getStatusCode()); if (response.getEntity() != null) { data.put(PARAM_CONTENT, IOUtils.toString(response.getEntity().getContent())); } else { data.put(PARAM_CONTENT, response.getStatusLine().getReasonPhrase()); } } catch (SSLHandshakeException e) { String msg = logConnectionError( "Could not establish SSL connection to '%s'. '%s' is set to '%s'. Cause: '%s'", url, enableSslVerify, e); errors.add(msg); } catch (HttpHostConnectException e) { String msg = logConnectionError( "Could not establish connection to '%s'. '%s' is set to '%s'. Cause: '%s'", url, enableSslVerify, e); errors.add(msg); } catch (UnknownHostException e) { String msg = String.format("Could not establish connection to unknown host '%s'", e.getMessage()); errors.add(msg); LOG.error(msg); } catch (IOException e) { String msg = String.format("could not fetch url '%s'", url); errors.add(msg); LOG.error(msg, e); } if (!errors.isEmpty()) { data.put(PARAM_ERRORS, errors); } upsertResults(context, jobKey, data); }    }
@Test public void executeInternalReturnsContent() throws IOException { TestUrlSourceJob job = new TestUrlSourceJob(prepareHttpClientMock(true)); job.executeInternal(applicationContext, new JobKey(SOURCE_ID, WIDGET_ID), createConfigMap()); testExecuteInternalReturnsContent(); }
@Test public void executeInternalReturnsContentWithSslVerificationDisabled() throws IOException { TestUrlSourceJob job = new TestUrlSourceJob(prepareHttpClientMock(true)); HashMap<String, Object> configMap = createConfigMap(); configMap.put(PARAM_ENABLE_SSL_VERIFY, false); job.executeInternal(applicationContext, new JobKey(SOURCE_ID, WIDGET_ID), configMap); testExecuteInternalReturnsContent(); }
@Test public void executeInternalReturnsReasonIfThereIsNoContent() throws IOException { TestUrlSourceJob job = new TestUrlSourceJob(prepareHttpClientMock(false)); job.executeInternal(applicationContext, new JobKey(SOURCE_ID, WIDGET_ID), createConfigMap()); HashMap<String, Object> expectedData = new HashMap<>(); expectedData.put(UrlSourceJob.PARAM_STATUS, HttpStatus.SC_OK); expectedData.put(UrlSourceJob.PARAM_CONTENT, REASON_OK); verify(repository).save(refEq(new SourceData(WIDGET_ID, SOURCE_ID, expectedData))); }
@Test public void executeInternalHandlesSslHandshakeExceptionWhenSslIsDisabled() throws IOException { CloseableHttpClient httpClient = mock(CloseableHttpClient.class); when(httpClient.execute(any(HttpGet.class))).thenThrow(SSLHandshakeException.class); HashMap<String, Object> configMap = createConfigMap(); configMap.put(PARAM_ENABLE_SSL_VERIFY, false); TestUrlSourceJob job = new TestUrlSourceJob(httpClient); job.executeInternal(applicationContext, new JobKey(SOURCE_ID, WIDGET_ID), configMap); }