Search in sources :

Example 1 with GetRecordsRequest

use of org.geotoolkit.csw.GetRecordsRequest 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);
}
Also used : GetRecordByIdResponse(org.geotoolkit.csw.xml.GetRecordByIdResponse) GetCapabilitiesRequest(org.geotoolkit.csw.GetCapabilitiesRequest) InputStream(java.io.InputStream) ExceptionReport(org.geotoolkit.ows.xml.v100.ExceptionReport) AbstractRecord(org.geotoolkit.csw.xml.AbstractRecord) Metadata(org.opengis.metadata.Metadata) CSWMarshallerPool(org.geotoolkit.csw.xml.CSWMarshallerPool) MarshallerPool(org.apache.sis.xml.MarshallerPool) GetRecordByIdRequest(org.geotoolkit.csw.GetRecordByIdRequest) URL(java.net.URL) GetRecordsRequest(org.geotoolkit.csw.GetRecordsRequest) CatalogServicesClient(org.geotoolkit.csw.CatalogServicesClient) Capabilities(org.geotoolkit.csw.xml.v202.Capabilities) GetRecordsResponseType(org.geotoolkit.csw.xml.v202.GetRecordsResponseType) Unmarshaller(javax.xml.bind.Unmarshaller)

Aggregations

InputStream (java.io.InputStream)1 URL (java.net.URL)1 Unmarshaller (javax.xml.bind.Unmarshaller)1 MarshallerPool (org.apache.sis.xml.MarshallerPool)1 CatalogServicesClient (org.geotoolkit.csw.CatalogServicesClient)1 GetCapabilitiesRequest (org.geotoolkit.csw.GetCapabilitiesRequest)1 GetRecordByIdRequest (org.geotoolkit.csw.GetRecordByIdRequest)1 GetRecordsRequest (org.geotoolkit.csw.GetRecordsRequest)1 AbstractRecord (org.geotoolkit.csw.xml.AbstractRecord)1 CSWMarshallerPool (org.geotoolkit.csw.xml.CSWMarshallerPool)1 GetRecordByIdResponse (org.geotoolkit.csw.xml.GetRecordByIdResponse)1 Capabilities (org.geotoolkit.csw.xml.v202.Capabilities)1 GetRecordsResponseType (org.geotoolkit.csw.xml.v202.GetRecordsResponseType)1 ExceptionReport (org.geotoolkit.ows.xml.v100.ExceptionReport)1 Metadata (org.opengis.metadata.Metadata)1