Search in sources :

Example 1 with JsonVal

use of org.opencastproject.util.JsonVal in project opencast by opencast.

the class OaiPmhRepositoryTest method validate.

private static boolean validate(String verb, XmlGen xmlgen, OaiPmhResponseStatus status) {
    logger.info("--- TALKING TO EXTERNAL OAI-PMH VALIDATION SERVICE ---");
    logger.info("--- " + VALIDATOR_SERVICE);
    final CloseableHttpClient client = HttpClients.createDefault();
    final String xml = xmlgen.generateAsString();
    final HttpPost post = HttpUtil.post(VALIDATOR_SERVICE, HttpUtil.param("xml", xml));
    logger.info("--- REQUEST ---");
    logger.info(xml);
    try {
        final HttpResponse res = client.execute(post);
        final String json = withResource(res.getEntity().getContent(), IoSupport.readToString);
        logger.info("--- RESPONSE ---");
        logger.info(json);
        boolean ok = true;
        for (JsonVal message : JsonObj.jsonObj(json).obj("json").arr("messages")) {
            if (message.isObj()) {
                final JsonObj messageObj = message.as(JsonVal.asJsonObj);
                if (messageObj.has("className")) {
                    final String className = messageObj.val("className").as(JsonVal.asString).trim();
                    final String text = messageObj.val("text").as(JsonVal.asString).trim();
                    logger.info(format("[%s] %s", className, text));
                    ok = ok && (eq(className, "correct") || // since the validator does not validate everything correctly here are some exclusions
                    (status == IsError && eq(text, "Could not find a valid OAI-PMH command."))) || (eq(verb, OaiPmhConstants.VERB_IDENTIFY) && eq(text, "Invalid OAI-PMH protocol version .")) || (eq(verb, OaiPmhConstants.VERB_IDENTIFY) && eq(text, "Invalid adminEmail ."));
                }
            }
        }
        return ok;
    } catch (Exception e) {
        return (Boolean) chuck(e);
    } finally {
        IoSupport.closeQuietly(client);
    }
}
Also used : JsonVal(org.opencastproject.util.JsonVal) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) JsonObj(org.opencastproject.util.JsonObj) HttpResponse(org.apache.http.HttpResponse) XpathReturnType.returningAString(org.xmlmatchers.xpath.XpathReturnType.returningAString) OaiPmhDatabaseException(org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException) NotFoundException(org.opencastproject.util.NotFoundException) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 OaiPmhDatabaseException (org.opencastproject.oaipmh.persistence.OaiPmhDatabaseException)1 JsonObj (org.opencastproject.util.JsonObj)1 JsonVal (org.opencastproject.util.JsonVal)1 NotFoundException (org.opencastproject.util.NotFoundException)1 XpathReturnType.returningAString (org.xmlmatchers.xpath.XpathReturnType.returningAString)1