/** * Get the records from the particular shard * * @param streamName Name of the stream from where the records to be accessed * @param recordsLimit Number of records to return from shard * @param shId Shard Id of the shard * @param iteratorType Shard iterator type * @param seqNo Record sequence number * @return the list of records from the given shard * @throws AmazonClientException */ public List<Record> getRecords( String streamName, Integer recordsLimit, String shId, ShardIteratorType iteratorType, String seqNo) throws AmazonClientException { assert client != null : "Illegal client"; try { // Create the GetShardIteratorRequest instance and sets streamName, shardId and iteratorType // to it GetShardIteratorRequest iteratorRequest = new GetShardIteratorRequest(); iteratorRequest.setStreamName(streamName); iteratorRequest.setShardId(shId); iteratorRequest.setShardIteratorType(iteratorType); // If the iteratorType is AFTER_SEQUENCE_NUMBER, set the sequence No to the iteratorRequest if (ShardIteratorType.AFTER_SEQUENCE_NUMBER.equals(iteratorType) || ShardIteratorType.AT_SEQUENCE_NUMBER.equals(iteratorType)) iteratorRequest.setStartingSequenceNumber(seqNo); // Get the Response from the getShardIterator service method & get the shardIterator from that // response GetShardIteratorResult iteratorResponse = client.getShardIterator(iteratorRequest); // getShardIterator() specifies the position in the shard String iterator = iteratorResponse.getShardIterator(); // Create the GetRecordsRequest instance and set the recordsLimit and iterator GetRecordsRequest getRequest = new GetRecordsRequest(); getRequest.setLimit(recordsLimit); getRequest.setShardIterator(iterator); // Get the Response from the getRecords service method and get the data records from that // response. GetRecordsResult getResponse = client.getRecords(getRequest); return getResponse.getRecords(); } catch (AmazonClientException e) { throw new RuntimeException(e); } }
/** * Exports a <code>GetRecordsResponse</code> instance to a <code>GetRecordsResponseDocument</code> * . * * @param response * @return DOM representation of the <code>GetRecordsResponse</code> * @throws XMLException if XML template could not be loaded */ public static GetRecordsResultDocument export(GetRecordsResult response) throws XMLException { // 'version'-attribute String version = response.getRequest().getVersion(); if (version == null || "".equals(version.trim())) { version = "2.0.0"; } GetRecordsResultDocument responseDocument = new GetRecordsResultDocument(version); try { Element rootElement = responseDocument.getRootElement(); Document doc = rootElement.getOwnerDocument(); // set required namespaces Element recordRespRoot = response.getSearchResults().getRecords().getOwnerDocument().getDocumentElement(); NamedNodeMap nnm = recordRespRoot.getAttributes(); for (int i = 0; i < nnm.getLength(); i++) { Node node = nnm.item(i); if (node instanceof Attr) { rootElement.setAttribute(node.getNodeName(), node.getNodeValue()); } } rootElement.setAttribute("version", version); String namespace = (version.equals("2.0.2") ? CSW202NS.toString() : CSWNS.toString()); // 'RequestId'-element (optional) if (response.getRequest().getId() != null) { Element requestIdElement = doc.createElementNS(namespace, "csw:RequestId"); requestIdElement.appendChild(doc.createTextNode(response.getRequest().getId())); rootElement.appendChild(requestIdElement); } // 'SearchStatus'-element (required) Element searchStatusElement = doc.createElementNS(namespace, "csw:SearchStatus"); // 'status'-attribute (required) if (!version.equals("2.0.2")) { searchStatusElement.setAttribute("status", response.getSearchStatus().getStatus()); } // 'timestamp'-attribute (optional) if (response.getSearchStatus().getTimestamp() != null) { Date date = response.getSearchStatus().getTimestamp(); String time = TimeTools.getISOFormattedTime(date); searchStatusElement.setAttribute("timestamp", time); } rootElement.appendChild(searchStatusElement); // 'SeachResults'-element (required) Element searchResultsElement = doc.createElementNS(namespace, "csw:SearchResults"); SearchResults results = response.getSearchResults(); // 'resultSetId'-attribute (optional) if (results.getResultSetId() != null) { searchResultsElement.setAttribute("resultSetId", results.getResultSetId().toString()); } // 'elementSet'-attribute (optional) if (results.getElementSet() != null) { searchResultsElement.setAttribute("elementSet", results.getElementSet().toString()); } // 'recordSchema'-attribute (optional) if (results.getRecordSchema() != null) { searchResultsElement.setAttribute("recordSchema", results.getRecordSchema().toString()); } // 'numberOfRecordsMatched'-attribute (required) searchResultsElement.setAttribute( "numberOfRecordsMatched", "" + results.getNumberOfRecordsMatched()); // 'numberOfRecordsReturned'-attribute (required) searchResultsElement.setAttribute( "numberOfRecordsReturned", "" + results.getNumberOfRecordsReturned()); // 'nextRecord'-attribute (required) searchResultsElement.setAttribute("nextRecord", "" + results.getNextRecord()); // 'expires'-attribute (optional) if (results.getExpires() != null) { Date date = results.getExpires(); String time = TimeTools.getISOFormattedTime(date); searchResultsElement.setAttribute("expires", time); } // append all children of the records container node NodeList nl = results.getRecords().getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node copy = doc.importNode(nl.item(i), true); searchResultsElement.appendChild(copy); } rootElement.appendChild(searchResultsElement); } catch (Exception e) { LOG.logError(e.getMessage(), e); throw new XMLException(e.getMessage()); } return responseDocument; }