Search in sources :

Example 6 with ReportWithResultImpl

use of org.codice.ddf.admin.common.report.ReportWithResultImpl in project admin-console-beta by connexta.

the class RequestUtils method executeGetRequest.

/**
     * Executes a request by creating a Secure CXF Client from the provided url, credentials, and query params.
     *
     * Possible Error Codes to return
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#CANNOT_CONNECT}
     *
     * @param clientUrl url to send GET request to
     * @param creds optional basic authentication
     * @param queryParams additional query parameters
     * @return {@link Response} of the request
     */
public ReportWithResultImpl<Response> executeGetRequest(UrlField clientUrl, CredentialsField creds, Map<String, String> queryParams) {
    WebClient client = generateClient(clientUrl.getValue(), creds, queryParams);
    ReportWithResultImpl<Response> report = new ReportWithResultImpl<>();
    Response response;
    try {
        response = client.get();
    } catch (ProcessingException e) {
        report.addResultMessage(cannotConnectError());
        return report;
    }
    report.result(response);
    return report;
}
Also used : Response(javax.ws.rs.core.Response) ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) WebClient(org.apache.cxf.jaxrs.client.WebClient) ProcessingException(javax.ws.rs.ProcessingException)

Example 7 with ReportWithResultImpl

use of org.codice.ddf.admin.common.report.ReportWithResultImpl in project admin-console-beta by connexta.

the class CswSourceUtils method getPreferredCswConfig.

/**
     * Attempts to create a CSW configuration from the given url.
     *
     * Possible Error Codes to be returned
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#CANNOT_CONNECT}
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#UNAUTHORIZED}
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#UNKNOWN_ENDPOINT}
     *
     * @param urlField A URL of an endpoint with CSW capabilities
     * @param creds    optional credentials for basic authentication
     * @return a {@link ReportWithResultImpl} containing the {@link SourceConfigUnionField} or an {@link org.codice.ddf.admin.common.report.message.ErrorMessage} on failure.
     */
public ReportWithResultImpl<SourceConfigUnionField> getPreferredCswConfig(UrlField urlField, CredentialsField creds) {
    ReportWithResultImpl<String> responseBodyResult = requestUtils.sendGetRequest(urlField, creds, GET_CAPABILITIES_PARAMS);
    ReportWithResultImpl<SourceConfigUnionField> configResult = new ReportWithResultImpl<>();
    if (responseBodyResult.containsErrorMsgs()) {
        configResult.addMessages(responseBodyResult);
        return configResult;
    }
    String requestBody = responseBodyResult.result();
    Document capabilitiesXml;
    try {
        capabilitiesXml = createDocument(requestBody);
    } catch (Exception e) {
        LOGGER.debug("Failed to create XML document from response.");
        configResult.addArgumentMessage(unknownEndpointError(urlField.path()));
        return configResult;
    }
    CswSourceConfigurationField preferred = new CswSourceConfigurationField();
    preferred.endpointUrl(urlField.getValue()).credentials().username(creds.username()).password(FLAG_PASSWORD);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(SOURCES_NAMESPACE_CONTEXT);
    try {
        if ((Boolean) xpath.compile(HAS_CATALOG_METACARD_EXP).evaluate(capabilitiesXml, XPathConstants.BOOLEAN)) {
            configResult.result(preferred.outputSchema(METACARD_OUTPUT_SCHEMA).cswProfile(CSW_FEDERATION_PROFILE_SOURCE));
            return configResult;
        }
    } catch (Exception e) {
        LOGGER.debug("Failed to compile DDF Profile CSW discovery XPath expression.");
    }
    try {
        if ((Boolean) xpath.compile(HAS_GMD_ISO_EXP).evaluate(capabilitiesXml, XPathConstants.BOOLEAN)) {
            configResult.result(preferred.outputSchema(GMD_OUTPUT_SCHEMA).cswProfile(GMD_CSW_ISO_FEDERATED_SOURCE));
            return configResult;
        }
    } catch (Exception e) {
        LOGGER.debug("Failed to compile GMD CSW discovery XPath expression.");
    }
    try {
        xpath.compile(GET_FIRST_OUTPUT_SCHEMA).evaluate(capabilitiesXml);
        configResult.result(preferred.outputSchema(CSW_2_0_2_OUTPUT_SCHEMA).cswProfile(CSW_SPEC_PROFILE_FEDERATED_SOURCE));
        return configResult;
    } catch (Exception e) {
        LOGGER.debug("Failed to compile generic CSW specification discovery XPath expression.");
    }
    LOGGER.debug("URL [{}] responded to GetCapabilities request, but response was not readable.", urlField.getValue());
    configResult.addArgumentMessage(unknownEndpointError(urlField.path()));
    return configResult;
}
Also used : XPath(javax.xml.xpath.XPath) ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) CswSourceConfigurationField(org.codice.ddf.admin.sources.fields.type.CswSourceConfigurationField) SourceConfigUnionField(org.codice.ddf.admin.sources.fields.type.SourceConfigUnionField) Document(org.w3c.dom.Document) SourceUtilCommons.createDocument(org.codice.ddf.admin.sources.commons.utils.SourceUtilCommons.createDocument)

Aggregations

ReportWithResultImpl (org.codice.ddf.admin.common.report.ReportWithResultImpl)7 Response (javax.ws.rs.core.Response)3 SourceConfigUnionField (org.codice.ddf.admin.sources.fields.type.SourceConfigUnionField)3 XPath (javax.xml.xpath.XPath)2 WebClient (org.apache.cxf.jaxrs.client.WebClient)2 SourceUtilCommons.createDocument (org.codice.ddf.admin.sources.commons.utils.SourceUtilCommons.createDocument)2 Document (org.w3c.dom.Document)2 ProcessingException (javax.ws.rs.ProcessingException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1 UrlField (org.codice.ddf.admin.common.fields.common.UrlField)1 CswSourceConfigurationField (org.codice.ddf.admin.sources.fields.type.CswSourceConfigurationField)1 OpenSearchSourceConfigurationField (org.codice.ddf.admin.sources.fields.type.OpenSearchSourceConfigurationField)1 WfsSourceConfigurationField (org.codice.ddf.admin.sources.fields.type.WfsSourceConfigurationField)1