use of org.geotoolkit.csw.CatalogServicesClient in project geotoolkit by Geomatys.
the class CSWClientDemo method main.
public static void main(String[] args) throws MalformedURLException, URISyntaxException, IOException, JAXBException {
Demos.init();
final MarshallerPool pool = CSWMarshallerPool.getInstance();
final Unmarshaller um = pool.acquireUnmarshaller();
final MarshallWarnings warnings = new MarshallWarnings();
um.setProperty(XML.CONVERTER, warnings);
// build a new CSW client
final CatalogServicesClient cswServer = new CatalogServicesClient(new URL("http://catalog.data.gov/csw?"), "2.0.2");
/**
* make a getCapabilities request
*/
final GetCapabilitiesRequest getCapa = cswServer.createGetCapabilities();
InputStream is = getCapa.getResponseStream();
// unmarshall the response
Capabilities capabilities = (Capabilities) um.unmarshal(is);
// print the title of the server
System.out.println(capabilities.getServiceIdentification().getTitle());
/**
* make a getRecords request
*/
final GetRecordsRequest getRecords = cswServer.createGetRecords();
getRecords.setTypeNames("gmd:MD_Metadata");
getRecords.setConstraintLanguage("CQL");
getRecords.setConstraintLanguageVersion("1.1.0");
getRecords.setConstraint("apiso:Title like '%'");
getRecords.setElementSetName(ElementSetType.FULL);
is = getRecords.getResponseStream();
// unmarshall the response
Object obj = um.unmarshal(is);
GetRecordsResponseType response;
if (obj instanceof ExceptionReport) {
System.out.println("Error received:" + obj);
return;
} else {
response = ((JAXBElement<GetRecordsResponseType>) obj).getValue();
}
// print the number of result matching the request
System.out.println(response.getSearchResults().getNumberOfRecordsMatched());
/**
* retrieve results in dublin core
*/
getRecords.setResultType(ResultType.RESULTS);
is = getRecords.getResponseStream();
obj = um.unmarshal(is);
// unmarshall the response
if (obj instanceof ExceptionReport) {
System.out.println("Error received:" + obj);
return;
} else {
response = ((JAXBElement<GetRecordsResponseType>) obj).getValue();
}
// print the first result (Dublin core)
AbstractRecord record = (AbstractRecord) response.getSearchResults().getAny().get(0);
System.out.println(record);
/**
* retrieve results in ISO 19139
*/
getRecords.setOutputSchema("http://www.isotc211.org/2005/gmd");
is = getRecords.getResponseStream();
// unmarshall the response
obj = um.unmarshal(is);
// unmarshall the response
if (obj instanceof ExceptionReport) {
System.out.println("Error received:" + obj);
return;
} else {
response = ((JAXBElement<GetRecordsResponseType>) obj).getValue();
}
// print the first result (ISO 19139)
Metadata meta = (Metadata) response.getSearchResults().getAny().get(0);
System.out.println(meta);
final String identifier = meta.getFileIdentifier();
/**
* make a getRecordById request
*/
final GetRecordByIdRequest getRecordById = cswServer.createGetRecordById();
getRecordById.setOutputSchema("http://www.isotc211.org/2005/gmd");
getRecordById.setIds(identifier);
is = getRecordById.getResponseStream();
// unmarshall the response
obj = um.unmarshal(is);
// unmarshall the response
GetRecordByIdResponse responseBi;
if (obj instanceof ExceptionReport) {
System.out.println("Error received:" + obj);
return;
} else {
responseBi = ((JAXBElement<GetRecordByIdResponse>) obj).getValue();
}
// print the result (same as getRecords first result)
meta = (Metadata) responseBi.getAny().get(0);
System.out.println(meta);
pool.recycle(um);
}
Aggregations