📄 cswsearchrequest.java
字号:
package com.esri.solutions.jitk.datasources.ogc.csw;
import org.xml.sax.SAXException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
/**
* CswSearchRequest class.
*
* @remark CswSearchRequest class is used to submit CSW search queries and to
* return CSW search results. Before submiting a request, you need to specify a
* catalog and provide search criteria.
* @version 1.0
* @created 25-Jul-2007 1:44:57 PM
*/
public class CswSearchRequest {
private CswCatalog catalog;
private CswSearchCriteria criteria;
private CswClient cswClient;
private CswSearchResponse response;
public CswSearchRequest() {
this(null, null);
}
public CswSearchRequest(CswCatalog catalog, String searchText) {
this.catalog = catalog;
// Initialize the neccessary objects
// create search criteria
this.criteria = new CswSearchCriteria();
this.criteria.setSearchText(searchText);
// create csw client
this.cswClient = new CswClient();
this.response = new CswSearchResponse();
}
/**
* Accessor methods
*/
public CswCatalog getCatalog() {
return this.catalog;
}
public void setCatalog(CswCatalog catalog) {
this.catalog = catalog;
}
public CswSearchCriteria getCriteria() {
return this.criteria;
}
public void setCriteria(CswSearchCriteria criteria) {
this.criteria = criteria;
}
public CswClient getCswClient() {
return this.cswClient;
}
public void setCswClient(CswClient cswClient) {
this.cswClient = cswClient;
}
public CswSearchResponse getCswSearchResponse() {
return this.response;
}
public void setCswSearchResponse(CswSearchResponse response) {
this.response = response;
}
/**
* Retrieve metadata from CSW service by its ID
*
* @param DocID Metadata document ID
*/
public void getMetadataByID(String DocID)
throws NullReferenceException, IOException, InvalidOperationException,
TransformerException, SAXException, ParserConfigurationException {
if ((DocID == null) || (DocID.length() == 0)) {
throw new NullReferenceException("No DocID specified");
}
if (catalog == null) {
throw new NullReferenceException("Catalog not specified.");
}
if (catalog.getProfile() == null) {
throw new NullReferenceException("Catalog profile not specified.");
}
if (!catalog.IsConnected()) {
catalog.connect();
}
if ((catalog.getCapabilities().get_getRecordByIDGetURL() == null) ||
(catalog.getCapabilities().get_getRecordByIDGetURL().length() == 0)) {
throw new NullReferenceException(
"GetRecordByID URL not specified for the catalog capabilities.");
}
CswProfile profile = catalog.getProfile();
// generate request url
String getRecordByIDBaseUrl = catalog.getCapabilities()
.get_getRecordByIDGetURL();
String requestUrl = profile.generateCSWGetMetadataByIDRequestURL(getRecordByIDBaseUrl,
DocID);
if (cswClient == null) {
cswClient = new CswClient();
}
InputStream responseStream = cswClient.submitHttpRequest("GET",
requestUrl, "");
BufferedReader reader = new BufferedReader(new InputStreamReader(
responseStream));
String responseStr = "";
String tmp = null;
while ((tmp = reader.readLine()) != null) {
responseStr += tmp;
}
response.setResponseXML(responseStr);
responseStream.close();
CswRecord record = new CswRecord(DocID);
profile.readCSWGetMetadataByIDResponse(responseStr, record);
if (record == null) {
throw new NullReferenceException("Record not populated.");
}
// check if full metadata or resourceURL has been returned
boolean hasFullMetadata = !((record.getFullMetadata() == null) ||
(record.getFullMetadata() == ""));
boolean hasResourceUrl = !((record.getMetadataResourceURL() == null) ||
(record.getMetadataResourceURL() == ""));
if (!hasFullMetadata && !hasResourceUrl) {
throw new InvalidOperationException(
"Neither full metadata nor metadata resource URL was found for the CSW record.");
} else if (!hasFullMetadata) {
// need to load metadata from resource URL
responseStream = cswClient.submitHttpRequest("GET",
record.getMetadataResourceURL(), "", "", "");
reader = new BufferedReader(new InputStreamReader(responseStream));
responseStr = "";
tmp = null;
while ((tmp = reader.readLine()) != null) {
responseStr += tmp;
}
record.setFullMetadata(responseStr);
}
// add record to the response
CswRecords records = new CswRecords();
if (record != null) {
records.addRecord(record);
}
response.setRecords(records);
}
public void getRepositoryItem(String docId)
throws NullReferenceException, IOException, InvalidOperationException,
TransformerException, SAXException, ParserConfigurationException {
CswProfile profile = null;
String getRepositoryItemBaseUrl = null;
String requestUrl = null;
InputStream responseStream = null;
BufferedReader reader = null;
String responseStr = null;
CswRecord record = null;
CswRecords records = null;
if ((docId == null) || (docId.length() == 0)) {
throw new NullReferenceException("No docId specified");
}
if (catalog == null) {
throw new NullReferenceException("Catalog not specified.");
}
if (catalog.getProfile() == null) {
throw new NullReferenceException("Catalog profile not specified.");
}
if (!catalog.IsConnected()) {
catalog.connect();
}
if (!catalog.getCapabilities().supportsGetRepositoryItemOperation()) {
throw new InvalidOperationException(
"GetRepositoryItem operation is not supported by this service.");
}
profile = catalog.getProfile();
// generate request url
getRepositoryItemBaseUrl = catalog.getCapabilities()
.get_getRepositoryItemURL();
requestUrl = profile.generateCSWGetRepositoryItemRequestURL(getRepositoryItemBaseUrl,
docId);
if (cswClient == null) {
cswClient = new CswClient();
}
responseStream = cswClient.submitHttpRequest("GET", requestUrl, "");
reader = new BufferedReader(new InputStreamReader(responseStream));
responseStr = "";
String tmp = null;
while ((tmp = reader.readLine()) != null) {
responseStr += tmp;
}
response.setResponseXML(responseStr);
responseStream.close();
record = new CswRecord(docId);
record.setFullMetadata(response.getResponseXML());
if ((record.getFullMetadata() == null) ||
(record.getFullMetadata().length() <= 0)) {
throw new InvalidOperationException(
"Actual metadata was not found for the CSW record using the GetRepositoryItem operation.");
}
// add record to the response
records = new CswRecords();
if (record != null) {
records.addRecord(record);
}
response.setRecords(records);
}
/**
* Get the CSW search response of a CSW search request
*
* @remark Get the CSW search response of a CSW search request
* @returns a CswSearchResponse object.
*/
public CswSearchResponse getResponse() {
return this.response;
}
/**
* Search CSW catalog using the provided criteria. Search result can be accessed
* by calling GetResponse().
* @throws IOException
* @throws SAXException
* @throws ParserConfigurationException
* @throws XPathExpressionException
* @throws TransformerException
*/
public void search()
throws NullReferenceException, XPathExpressionException,
ParserConfigurationException, SAXException, IOException,
TransformerException {
// Check the necessary info. to search csw
if (criteria == null) {
throw new NullReferenceException("No specified Criteria");
}
if ((catalog.getUrl() == null) || (catalog.getUrl().length() == 0)) {
throw new NullReferenceException("No specified url");
}
if (catalog.getProfile() == null) {
throw new NullReferenceException("No specified profile");
}
// Generate getRecords query
CswProfile profile = catalog.getProfile();
catalog.connect();
CswCatalogCapabilities capabilities = catalog.getCapabilities();
String requestUrl = capabilities.get_getRecordsPostURL();
String requestQuery = profile.generateCSWGetRecordsRequest(criteria);
// Submit search query and get response as an InputStream object
InputStream responseStream = cswClient.submitHttpRequest("POST",
requestUrl, requestQuery);
// Transform the response stream to String
BufferedReader reader = new BufferedReader(new InputStreamReader(
responseStream));
String responseStr = "";
String tmp;
while ((tmp = reader.readLine()) != null) {
responseStr += tmp;
}
// Transform input xml to output xml with Profile
CswRecords records = new CswRecords();
profile.readCSWGetRecordsResponse(responseStr, records);
response.setRecords(records);
response.setResponseXML(responseStr);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -