Search in sources :

Example 1 with ReportWithResultImpl

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

the class OpenSearchSourceUtils method getOpenSearchConfig.

/**
     * Attempts to create an OpenSearch configuration with the provided URL and credentials. If a configuration
     * is not found or created, the {@link ReportWithResultImpl}'s will have errors.
     *
     * @param urlField The URL to probe for OpenSearch capabilities
     * @param creds    optional credentials to send with Basic Auth header
     * @return a {@link ReportWithResultImpl} containing the {@link SourceConfigUnionField} or containing {@link org.codice.ddf.admin.common.report.message.ErrorMessage}s on failure.
     */
public ReportWithResultImpl<SourceConfigUnionField> getOpenSearchConfig(UrlField urlField, CredentialsField creds) {
    ReportWithResultImpl<SourceConfigUnionField> configResult = new ReportWithResultImpl<>();
    configResult.addMessages(verifyOpenSearchCapabilities(urlField, creds));
    if (configResult.containsErrorMsgs()) {
        return configResult;
    }
    OpenSearchSourceConfigurationField config = new OpenSearchSourceConfigurationField();
    config.endpointUrl(urlField.getValue()).credentials().username(creds.username()).password(FLAG_PASSWORD);
    configResult.result(config);
    return configResult;
}
Also used : ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) OpenSearchSourceConfigurationField(org.codice.ddf.admin.sources.fields.type.OpenSearchSourceConfigurationField) SourceConfigUnionField(org.codice.ddf.admin.sources.fields.type.SourceConfigUnionField)

Example 2 with ReportWithResultImpl

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

the class WfsSourceUtils method getPreferredWfsConfig.

/**
     * Attempts to create a WFS 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 WFS URL to probe for a configuration
     * @param creds    optional username to add to Basic Auth header
     * @return a {@link ReportWithResultImpl} containing the preferred {@link SourceConfigUnionField}, or containing {@link org.codice.ddf.admin.common.report.message.ErrorMessage}s on failure.
     */
public ReportWithResultImpl<SourceConfigUnionField> getPreferredWfsConfig(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 read response from WFS endpoint.");
        configResult.addArgumentMessage(unknownEndpointError(urlField.path()));
        return configResult;
    }
    WfsSourceConfigurationField preferredConfig = new WfsSourceConfigurationField();
    preferredConfig.endpointUrl(urlField.getValue()).credentials().username(creds.username()).password(FLAG_PASSWORD);
    XPath xpath = XPathFactory.newInstance().newXPath();
    xpath.setNamespaceContext(SOURCES_NAMESPACE_CONTEXT);
    String wfsVersion;
    try {
        wfsVersion = xpath.compile(WFS_VERSION_EXP).evaluate(capabilitiesXml);
    } catch (XPathExpressionException e) {
        LOGGER.debug("Failed to parse XML response.");
        configResult.addArgumentMessage(unknownEndpointError(urlField.path()));
        return configResult;
    }
    try {
        configResult.result(preferredConfig.wfsVersion(wfsVersion));
    } catch (IllegalArgumentException e) {
        configResult.addArgumentMessage(unknownEndpointError(urlField.path()));
    }
    return configResult;
}
Also used : XPath(javax.xml.xpath.XPath) ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) XPathExpressionException(javax.xml.xpath.XPathExpressionException) 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) WfsSourceConfigurationField(org.codice.ddf.admin.sources.fields.type.WfsSourceConfigurationField) XPathExpressionException(javax.xml.xpath.XPathExpressionException)

Example 3 with ReportWithResultImpl

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

the class RequestUtils method sendPostRequest.

/**
     * Sends a POST request to the specified url.
     *
     * Possible Error Codes to be returned
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#CANNOT_CONNECT}
     *
     * @param urlField    URL to send Post request to
     * @param creds       optional credentials consisting of a username and password
     * @param contentType Mime type of the post body
     * @param content     Body of the post request
     * @return a {@link ReportWithResultImpl} containing the POST request response body or an {@link org.codice.ddf.admin.common.report.message.ErrorMessage} on failure.
     */
public ReportWithResultImpl<String> sendPostRequest(UrlField urlField, CredentialsField creds, String contentType, String content) {
    WebClient client = generateClient(urlField.getValue(), creds, Collections.emptyMap());
    Response response = client.type(contentType).post(content);
    ReportWithResultImpl<String> responseBodyResult = new ReportWithResultImpl<>();
    if (response.getStatus() != HTTP_OK || response.readEntity(String.class).equals("")) {
        LOGGER.debug("Bad or empty response received from sending POST to {}.", urlField.getValue());
        responseBodyResult.addArgumentMessage(cannotConnectError(urlField.path()));
        return responseBodyResult;
    }
    responseBodyResult.result(response.readEntity(String.class));
    return responseBodyResult;
}
Also used : Response(javax.ws.rs.core.Response) ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) WebClient(org.apache.cxf.jaxrs.client.WebClient)

Example 4 with ReportWithResultImpl

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

the class RequestUtils method discoverUrlFromHost.

/**
     * Takes a list of url formats, for example "https://%s:%d/wfs", formats them together with the
     * hostField name and port, then sends GET requests to those URLs. If an HTTP 200 and response body is returned on one the the formatted
     * URLs, then a {@link UrlField} whose value is the formatted URL is returned. The {@code UrlField} returned
     * will have the same path and field name as the hostField passed in.
     *
     * Possible Error Codes to be returned
     * - {@link org.codice.ddf.admin.common.report.message.DefaultMessages#CANNOT_CONNECT}
     *
     * @param hostField host field containing the host name and port
     * @param urlFormats list of url formats to format with the hostField
     * @param creds credentials for basic authentication
     * @param queryParams additional query params
     * @return a {@code ReportWithResult} containing a {@code UrlField} on success, or {@link org.codice.ddf.admin.common.report.message.ErrorMessage}s on failure
     */
public ReportWithResultImpl<UrlField> discoverUrlFromHost(HostField hostField, List<String> urlFormats, CredentialsField creds, Map<String, String> queryParams) {
    ReportWithResultImpl<UrlField> responseBody = new ReportWithResultImpl<>();
    for (String formatUrl : urlFormats) {
        UrlField clientUrl = new UrlField();
        clientUrl.fieldName(hostField.fieldName());
        // copy this hostField's path so that if it's used to make errors they have the path of the hostField
        clientUrl.updatePath(hostField.path().subList(0, hostField.path().size() - 1));
        clientUrl.setValue(String.format(formatUrl, hostField.name(), hostField.port()));
        ReportWithResultImpl<String> body = sendGetRequest(clientUrl, creds, queryParams);
        if (!body.containsErrorMsgs()) {
            responseBody.result(clientUrl);
            return responseBody;
        }
    }
    return responseBody.addResultMessage(cannotConnectError());
}
Also used : ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl) UrlField(org.codice.ddf.admin.common.fields.common.UrlField)

Example 5 with ReportWithResultImpl

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

the class RequestUtils method sendGetRequest.

/**
     * Creates a secure CXF {@code WebClient} and sends a GET request to the URL given by the clientUrl and
     * optional queryParams.
     *
     * 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 clientUrl url to send GET request to
     * @param creds optional credentials for basic authentication
     * @param queryParams optional query parameters
     * @return {@link ReportWithResultImpl} containing the body of the response on success, or containing an {@link org.codice.ddf.admin.common.report.message.ErrorMessage}
     */
public ReportWithResultImpl<String> sendGetRequest(UrlField clientUrl, CredentialsField creds, Map<String, String> queryParams) {
    ReportWithResultImpl<String> body = new ReportWithResultImpl<>();
    ReportWithResultImpl<Response> responseResult = executeGetRequest(clientUrl, creds, queryParams);
    if (responseResult.containsErrorMsgs()) {
        body.addMessages(responseResult);
        return body;
    }
    Response response = responseResult.result();
    String responseString = response.readEntity(String.class);
    if (response.getStatus() == HTTP_OK && !responseString.equals("")) {
        body.result(responseString);
    } else if (response.getStatus() == HTTP_UNAUTHORIZED) {
        body.addArgumentMessage(unauthorizedError(creds.path()));
    } else {
        body.addArgumentMessage(unknownEndpointError(clientUrl.path()));
    }
    return body;
}
Also used : Response(javax.ws.rs.core.Response) ReportWithResultImpl(org.codice.ddf.admin.common.report.ReportWithResultImpl)

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