use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class TestOpenSearchSource method testQueryBySearchPhraseContentTypeSet.
@Test
public void testQueryBySearchPhraseContentTypeSet() throws UnsupportedQueryException, URISyntaxException, IOException {
WebClient client = mock(WebClient.class);
Response clientResponse = mock(Response.class);
when(client.get()).thenReturn(clientResponse);
when(clientResponse.getStatus()).thenReturn(Response.Status.OK.getStatusCode());
when(clientResponse.getEntity()).thenReturn(getSampleAtomStream());
SecureCxfClientFactory factory = getMockFactory(client);
OverriddenOpenSearchSource source = new OverriddenOpenSearchSource(FILTER_ADAPTER, encryptionService);
InputTransformer inputTransformer = mock(InputTransformer.class);
MetacardImpl generatedMetacard = new MetacardImpl();
generatedMetacard.setMetadata(getSample());
generatedMetacard.setId(SAMPLE_ID);
generatedMetacard.setContentTypeName("myType");
try {
when(inputTransformer.transform(isA(InputStream.class))).thenReturn(generatedMetacard);
when(inputTransformer.transform(isA(InputStream.class), isA(String.class))).thenReturn(generatedMetacard);
} catch (IOException e) {
fail();
} catch (CatalogTransformerException e) {
fail();
}
source.setInputTransformer(inputTransformer);
source.setEndpointUrl("http://localhost:8181/services/catalog/query");
source.init();
source.setParameters(DEFAULT_PARAMETERS);
source.factory = factory;
Filter filter = filterBuilder.attribute(Metacard.METADATA).like().text(SAMPLE_SEARCH_PHRASE);
SourceResponse response = source.query(new QueryRequestImpl(new QueryImpl(filter)));
Assert.assertEquals(1, response.getHits());
List<Result> results = response.getResults();
Assert.assertTrue(results.size() == 1);
Result result = results.get(0);
Metacard metacard = result.getMetacard();
Assert.assertNotNull(metacard);
Assert.assertEquals("myType", metacard.getContentTypeName());
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class TestOpenSearchSiteUtil method populateNullTemporal.
/**
* Verify that passing in null will still remove the parameters from the URL.
*/
@Test
public void populateNullTemporal() {
StringBuilder resultStr = new StringBuilder(url);
WebClient webClient = WebClient.create(url.toString());
OpenSearchSiteUtil.populateTemporal(webClient, null, Arrays.asList("q,src,mr,start,count,mt,dn,lat,lon,radius,bbox,polygon,dtstart,dtend,dateName,filter,sort".split(",")));
String urlStr = webClient.getCurrentURI().toString();
assertTrue(urlStr.indexOf(OpenSearchSiteUtil.TIME_START) == -1);
assertTrue(urlStr.indexOf(OpenSearchSiteUtil.TIME_END) == -1);
assertTrue(urlStr.indexOf(OpenSearchSiteUtil.TIME_NAME) == -1);
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class TestOpenSearchSiteUtil method populateContextual.
@Test
public void populateContextual() {
String searchPhrase = "TestQuery123";
assertTrue(url.indexOf(OpenSearchSiteUtil.SEARCH_TERMS) != -1);
WebClient webClient = WebClient.create(url.toString());
OpenSearchSiteUtil.populateContextual(webClient, searchPhrase, Arrays.asList("q,src,mr,start,count,mt,dn,lat,lon,radius,bbox,polygon,dtstart,dtend,dateName,filter,sort".split(",")));
String urlStr = webClient.getCurrentURI().toString();
assertTrue(urlStr.indexOf(searchPhrase) != -1);
assertTrue(urlStr.indexOf("?" + OpenSearchSiteUtil.SEARCH_TERMS) != -1);
try {
new URL(urlStr.toString());
} catch (MalformedURLException mue) {
fail("URL is not valid: " + mue.getMessage());
}
LOGGER.info("URL after contextual population: {}", urlStr.toString());
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class URLResourceReader method retrieveHttpProduct.
private ResourceResponse retrieveHttpProduct(URI resourceURI, String productName, String bytesToSkip, Map<String, Serializable> properties) throws ResourceNotFoundException {
try {
LOGGER.debug("Opening connection to: {}", resourceURI.toString());
WebClient client = getWebClient(resourceURI.toString(), properties);
Object subjectObj = properties.get(SecurityConstants.SECURITY_SUBJECT);
if (subjectObj != null) {
Subject subject = (Subject) subjectObj;
LOGGER.debug("Setting Subject on webclient: {}", subject);
RestSecurity.setSubjectOnClient(subject, client);
}
Response response = client.get();
MultivaluedMap<String, Object> headers = response.getHeaders();
List<Object> cdHeaders = headers.get(HttpHeaders.CONTENT_DISPOSITION);
if (cdHeaders != null && !cdHeaders.isEmpty()) {
String contentHeader = (String) cdHeaders.get(0);
productName = StringUtils.defaultIfBlank(handleContentDispositionHeader(contentHeader), productName);
}
String mimeType = getMimeType(resourceURI, productName);
Response clientResponse = client.get();
InputStream is = null;
Object entityObj = clientResponse.getEntity();
if (entityObj instanceof InputStream) {
is = (InputStream) entityObj;
if (Response.Status.OK.getStatusCode() != clientResponse.getStatus() && Response.Status.PARTIAL_CONTENT.getStatusCode() != clientResponse.getStatus()) {
String error = null;
try {
if (is != null) {
error = IOUtils.toString(is);
}
} catch (IOException ioe) {
LOGGER.debug("Could not convert error message to a string for output.", ioe);
}
String errorMsg = "Received error code while retrieving resource (status " + clientResponse.getStatus() + "): " + error;
throw new ResourceNotFoundException(errorMsg);
}
} else {
throw new ResourceNotFoundException("Received null response while retrieving resource.");
}
long responseBytesSkipped = 0L;
if (headers.getFirst(HttpHeaders.CONTENT_RANGE) != null) {
String contentRangeHeader = String.valueOf(headers.getFirst(HttpHeaders.CONTENT_RANGE));
responseBytesSkipped = Long.parseLong(StringUtils.substringBetween(contentRangeHeader.toLowerCase(), "bytes ", "-"));
}
alignStream(is, Long.parseLong(bytesToSkip), responseBytesSkipped);
return new ResourceResponseImpl(new ResourceImpl(new BufferedInputStream(is), mimeType, FilenameUtils.getName(productName)));
} catch (MimeTypeResolutionException | IOException | WebApplicationException e) {
LOGGER.info("Error retrieving resource", e);
throw new ResourceNotFoundException("Unable to retrieve resource at: " + resourceURI.toString(), e);
}
}
use of org.apache.cxf.jaxrs.client.WebClient in project ddf by codice.
the class ResourceReaderTest method testSetRedirect.
@Test
public void testSetRedirect() throws Exception {
URLResourceReader resourceReader = new URLResourceReader(mimeTypeMapper);
WebClient client = resourceReader.getWebClient(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + JPEG_FILE_NAME_1, new HashMap<>());
assertFalse(WebClient.getConfig(client).getHttpConduit().getClient().isAutoRedirect());
resourceReader.setFollowRedirects(true);
client = resourceReader.getWebClient(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + JPEG_FILE_NAME_1, new HashMap<>());
assertTrue(WebClient.getConfig(client).getHttpConduit().getClient().isAutoRedirect());
resourceReader.setFollowRedirects(false);
client = resourceReader.getWebClient(HTTP_SCHEME_PLUS_SEP + HOST + TEST_PATH + JPEG_FILE_NAME_1, new HashMap<>());
assertFalse(WebClient.getConfig(client).getHttpConduit().getClient().isAutoRedirect());
}
Aggregations