Client implements IClient { @Override public String readMetadata(String id) throws Exception { LOG.debug(String.format("Executing readMetadata(id=%s)", id)); loadCapabilities(); String getRecordByIdUrl = createGetMetadataByIdUrl(capabilites.get_getRecordByIDGetURL(), URLEncoder.encode(id, "UTF-8")); HttpGet get = new HttpGet(getRecordByIdUrl); get.setConfig(DEFAULT_REQUEST_CONFIG); get.setHeader("User-Agent", HttpConstants.getUserAgent()); try (CloseableHttpResponse httpResponse = httpClient.execute(get); InputStream responseStream = httpResponse.getEntity().getContent();) { if (httpResponse.getStatusLine().getStatusCode()>=400) { throw new HttpResponseException(httpResponse.getStatusLine().getStatusCode(), httpResponse.getStatusLine().getReasonPhrase()); } String response = IOUtils.toString(responseStream, "UTF-8"); if (CONFIG_FOLDER_PATH.equals(profile.getMetadataxslt())) { return response; } Templates template = TemplatesManager.getInstance().getTemplate(profile.getMetadataxslt()); Transformer transformer = template.newTransformer(); try (ByteArrayInputStream contentStream = new ByteArrayInputStream(response.getBytes("UTF-8"));) { StringWriter writer = new StringWriter(); transformer.transform(new StreamSource(contentStream), new StreamResult(writer)); String intermediateResult = writer.toString(); DcList lstDctReferences = new DcList(intermediateResult); String xmlUrl = lstDctReferences.stream() .filter(v -> v.getValue().toLowerCase().endsWith(".xml") || v.getScheme().equals(SCHEME_METADATA_DOCUMENT)) .map(v -> v.getValue()) .findFirst() .orElse(""); if (!xmlUrl.isEmpty()) { HttpGet getRequest = new HttpGet(xmlUrl); try (CloseableHttpResponse httpResp = httpClient.execute(getRequest); InputStream metadataStream = httpResp.getEntity().getContent();) { return IOUtils.toString(metadataStream, "UTF-8"); } } if (!intermediateResult.isEmpty()) { try { TransformerFactory trFactory = TransformerFactory.newInstance(); trFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); trFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, ""); trFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, ""); Transformer tr = trFactory.newTransformer(); tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tr.setOutputProperty(OutputKeys.INDENT, "yes"); ByteArrayInputStream intermediateStream = new ByteArrayInputStream(intermediateResult.getBytes("UTF-8")); StringWriter intermediateBuffer = new StringWriter(); tr.transform(new StreamSource(intermediateStream), new StreamResult(intermediateBuffer)); return intermediateBuffer.toString(); } catch (Exception ex) { return makeResourceFromCswResponse(response, id); } } return response; } } } Client(CloseableHttpClient httpClient, URL baseUrl, IProfile profile, SimpleCredentials cred); @Override IRecords findRecords(int start, int max, Date from, Date to); @Override IRecords findRecords(int start, int max, Date from, Date to, String searchText); @Override String readMetadata(String id); @Override String toString();  }
@Test public void testReadMetadata() throws Exception { Profiles profiles = new ProfilesLoader().load(); IProfile defaultProfile = profiles.getDefaultProfile(); assertNotNull("No default profile", defaultProfile); Bots bots = BotsUtils.readBots("http: BotsHttpClient httpClient = new BotsHttpClient(bots); Client cswClient = new Client(httpClient, new URL("http: String metadata = cswClient.readMetadata("{093CBDB1-9D7A-4602-9937-2EC89359E633}"); assertNotNull("No metadata", metadata); assertNotEquals("No metadata", metadata.length(), 0); }