Search in sources :

Example 46 with Status

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

the class JwtVerifyHandlerTest method testUnmatchedScopeInIdToken.

@Test
public void testUnmatchedScopeInIdToken() throws Exception {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI("http://localhost:8080"), Http2Client.WORKER, Http2Client.SSL, Http2Client.POOL, OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/v1/pets/111").setMethod(Methods.GET);
        request.getRequestHeaders().put(Headers.AUTHORIZATION, "Bearer eyJraWQiOiIxMDAiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1cm46Y29tOm5ldHdvcmtudDpvYXV0aDI6djEiLCJhdWQiOiJ1cm46Y29tLm5ldHdvcmtudCIsImV4cCI6MTgwNTEzNjU1MSwianRpIjoiTVJiZHdlQ295eG13a2ZUM3lVWGloQSIsImlhdCI6MTQ4OTc3NjU1MSwibmJmIjoxNDg5Nzc2NDMxLCJ2ZXJzaW9uIjoiMS4wIiwidXNlcl9pZCI6ImVyaWMiLCJ1c2VyX3R5cGUiOiJFTVBMT1lFRSIsImNsaWVudF9pZCI6ImY3ZDQyMzQ4LWM2NDctNGVmYi1hNTJkLTRjNTc4NzQyMWU3MiIsInNjb3BlIjpbIkFUTVAxMDAwLnciLCJBVE1QMTAwMC5yIl19.VOEggO6UIMHNJLrxShGivCh7sGyHiz7h9FqDjlKwywGP9xKbVTTODy2-FitUaS1Y2vjiHlJ0TNyxmj1SO11YwYnJlW1zn-6vfKWKI70DyvRwsvSX_8Z2fj0jPUiBqezwKRtLCHSsmiEpMrW6YQHYw0qzZ9kkMhiH2uFpZNCekOQWL1piRn1xVQkUmeFiTDvJQESHadFzw-9x0klO7-SxgKeHHDroxnpbLv2j795oMTB1gM_wJP6HO_M-gK6N1Uh6zssfnbyFReRNWkhZFOp3Y8DvwpfKhqXIVGUc_5WsO9M-y66icClVNl5zwLSmjsrNtqZkmeBCwQ6skBnRLfMocQ");
        connection.sendRequest(request, client.createClientCallback(reference, latch));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    Assert.assertEquals(403, statusCode);
    if (statusCode == 403) {
        Status status = Config.getInstance().getMapper().readValue(reference.get().getAttachment(Http2Client.RESPONSE_BODY), Status.class);
        Assert.assertNotNull(status);
        Assert.assertEquals("ERR10005", status.getCode());
    }
}
Also used : ClientResponse(io.undertow.client.ClientResponse) Status(com.networknt.status.Status) ClientConnection(io.undertow.client.ClientConnection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 47 with Status

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

the class RequestValidator method validateRequest.

/**
 * Validate the request against the given API operation
 * @param requestPath normalised path
 * @param exchange The HttpServerExchange to validate
 * @param openApiOperation OpenAPI operation
 * @return A validation report containing validation errors
 */
public Status validateRequest(final NormalisedPath requestPath, HttpServerExchange exchange, OpenApiOperation openApiOperation) {
    requireNonNull(requestPath, "A request path is required");
    requireNonNull(exchange, "An exchange is required");
    requireNonNull(openApiOperation, "An OpenAPI operation is required");
    Status status = validatePathParameters(requestPath, openApiOperation);
    if (status != null)
        return status;
    status = validateQueryParameters(exchange, openApiOperation);
    if (status != null)
        return status;
    status = validateHeader(exchange, openApiOperation);
    if (status != null)
        return status;
    Object body = exchange.getAttachment(BodyHandler.REQUEST_BODY);
    status = validateRequestBody(body, openApiOperation);
    return status;
}
Also used : Status(com.networknt.status.Status)

Example 48 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 JsonNode schema) {
    requireNonNull(schema, "A schema is required");
    Status status = null;
    Set<ValidationMessage> processingReport = null;
    try {
        JsonSchema jsonSchema = JsonSchemaFactory.getInstance().getSchema(schema);
        final JsonNode content = Config.getInstance().getMapper().valueToTree(value);
        processingReport = jsonSchema.validate(content);
    } 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) JsonSchema(com.networknt.schema.JsonSchema) JsonNode(com.fasterxml.jackson.databind.JsonNode) JsonParseException(com.fasterxml.jackson.core.JsonParseException)

Example 49 with Status

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

the class FailbackRegistry method register.

@Override
public void register(URL url) {
    failedRegistered.remove(url);
    failedUnregistered.remove(url);
    try {
        super.register(url);
    } catch (Exception e) {
        if (isCheckingUrls(getUrl(), url)) {
            throw new FrameworkException(new Status(REGISTER_ERROR, registryClassName, url, getUrl()), e);
        }
        failedRegistered.add(url);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) FrameworkException(com.networknt.exception.FrameworkException)

Example 50 with Status

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

the class CommandServiceManager method notifyService.

@Override
public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls) {
    if (registry == null) {
        throw new FrameworkException(new Status(REGISTRY_IS_NULL));
    }
    URL urlCopy = serviceUrl.createCopy();
    String groupName = urlCopy.getParameter(URLParamType.group.getName(), URLParamType.group.getValue());
    groupServiceCache.put(groupName, urls);
    List<URL> finalResult = new ArrayList<URL>();
    if (logger.isInfoEnabled())
        logger.info("command cache is null. service:" + serviceUrl.toSimpleString());
    // if no command cache, return group
    finalResult.addAll(discoverOneGroup(refUrl));
    for (NotifyListener notifyListener : notifySet) {
        notifyListener.notify(registry.getUrl(), finalResult);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) NotifyListener(com.networknt.registry.NotifyListener)

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