Search in sources :

Example 66 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class ParameterValidatorsTest method validate_withInvalidStringParam_shouldFail.

@Test
public void validate_withInvalidStringParam_shouldFail() {
    Status status = parameterValidators.validate("", stringParam());
    Assert.assertNotNull(status);
    // request parameter missing
    Assert.assertEquals("ERR11001", status.getCode());
}
Also used : Status(com.networknt.status.Status) Test(org.junit.Test)

Example 67 with Status

use of com.networknt.status.Status in project light-rest-4j by networknt.

the class SchemaValidator method doValidate.

private Status doValidate(final Object value, final Object schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        final JsonNode schemaObject = Json.mapper().readTree(Json.pretty(schema));
        if (api != null) {
            if (this.definitions == null) {
                this.definitions = Json.mapper().readTree(Json.pretty(api.getDefinitions()));
            }
            ((ObjectNode) schemaObject).set(DEFINITIONS_FIELD, this.definitions);
        }
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schemaObject);
        final JsonNode content = Json.mapper().valueToTree(value);
        processingReport = jsonSchema.validate(content);
    } catch (JsonParseException e) {
        return new Status(VALIDATOR_SCHEMA_INVALID_JSON, e.getMessage());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (processingReport != null && processingReport.size() > 0) {
        ValidationMessage vm = processingReport.iterator().next();
        status = new Status(VALIDATOR_SCHEMA, vm.getMessage());
    }
    return status;
}
Also used : Status(com.networknt.status.Status) ValidationMessage(com.networknt.schema.ValidationMessage) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JsonSchema(com.networknt.schema.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 68 with Status

use of com.networknt.status.Status in project light-4j by networknt.

the class ZooKeeperRegistry method doRegister.

@Override
protected void doRegister(URL url) {
    try {
        serverLock.lock();
        removeNode(url, ZkNodeType.AVAILABLE_SERVER);
        removeNode(url, ZkNodeType.UNAVAILABLE_SERVER);
        createNode(url, ZkNodeType.UNAVAILABLE_SERVER);
    } catch (Throwable e) {
        throw new FrameworkException(new Status(REGISTER_ZOOKEEPER_ERROR, url, getUrl(), e.getMessage()), e);
    } finally {
        serverLock.unlock();
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException)

Example 69 with Status

use of com.networknt.status.Status in project light-4j by networknt.

the class ZooKeeperRegistry method discoverService.

@Override
protected List<URL> discoverService(URL url) {
    try {
        String parentPath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        List<String> currentChilds = new ArrayList<String>();
        if (client.exists(parentPath)) {
            currentChilds = client.getChildren(parentPath);
        }
        return nodeChildsToUrls(parentPath, currentChilds);
    } catch (Throwable e) {
        throw new FrameworkException(new Status(DISCOVER_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) ArrayList(java.util.ArrayList)

Example 70 with Status

use of com.networknt.status.Status in project light-4j by networknt.

the class ZooKeeperRegistry method subscribeService.

@Override
protected void subscribeService(final URL url, final ServiceListener serviceListener) {
    try {
        clientLock.lock();
        ConcurrentHashMap<ServiceListener, IZkChildListener> childChangeListeners = serviceListeners.get(url);
        if (childChangeListeners == null) {
            serviceListeners.putIfAbsent(url, new ConcurrentHashMap<ServiceListener, IZkChildListener>());
            childChangeListeners = serviceListeners.get(url);
        }
        IZkChildListener zkChildListener = childChangeListeners.get(serviceListener);
        if (zkChildListener == null) {
            childChangeListeners.putIfAbsent(serviceListener, new IZkChildListener() {

                @Override
                public void handleChildChange(String parentPath, List<String> currentChilds) {
                    serviceListener.notifyService(url, getUrl(), nodeChildsToUrls(parentPath, currentChilds));
                    if (logger.isInfoEnabled())
                        logger.info(String.format("[ZooKeeperRegistry] service list change: path=%s, currentChilds=%s", parentPath, currentChilds.toString()));
                }
            });
            zkChildListener = childChangeListeners.get(serviceListener);
        }
        // prevent old node unregistered
        removeNode(url, ZkNodeType.CLIENT);
        createNode(url, ZkNodeType.CLIENT);
        String serverTypePath = ZkUtils.toNodeTypePath(url, ZkNodeType.AVAILABLE_SERVER);
        client.subscribeChildChanges(serverTypePath, zkChildListener);
        if (logger.isInfoEnabled())
            logger.info(String.format("[ZooKeeperRegistry] subscribe service: path=%s, info=%s", ZkUtils.toNodePath(url, ZkNodeType.AVAILABLE_SERVER), url.toFullStr()));
    } catch (Throwable e) {
        throw new FrameworkException(new Status(SUBSCRIBE_ZOOKEEPER_SERVICE_ERROR, url, getUrl(), e.getMessage()), e);
    } finally {
        clientLock.unlock();
    }
}
Also used : Status(com.networknt.status.Status) ServiceListener(com.networknt.registry.support.command.ServiceListener) FrameworkException(com.networknt.exception.FrameworkException) IZkChildListener(org.I0Itec.zkclient.IZkChildListener)

Aggregations

Status (com.networknt.status.Status)71 Test (org.junit.Test)45 Http2Client (com.networknt.client.Http2Client)19 ClientException (com.networknt.exception.ClientException)19 URI (java.net.URI)19 CountDownLatch (java.util.concurrent.CountDownLatch)19 AtomicReference (java.util.concurrent.atomic.AtomicReference)19 ClientConnection (io.undertow.client.ClientConnection)17 ClientRequest (io.undertow.client.ClientRequest)17 ClientResponse (io.undertow.client.ClientResponse)17 IOException (java.io.IOException)12 FrameworkException (com.networknt.exception.FrameworkException)9 HttpString (io.undertow.util.HttpString)7 IntegerProperty (io.swagger.models.properties.IntegerProperty)4 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 JsonSchema (com.networknt.schema.JsonSchema)3 ValidationMessage (com.networknt.schema.ValidationMessage)3