Search in sources :

Example 21 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project service-proxy by membrane.

the class GenericJsonParserTest method testTextContent.

@Test
public void testTextContent() {
    String want = "println(\"Hello, World!\")";
    JSONObject spec = new JSONObject(new HashMap<String, Object>() {

        {
            put("src", want);
        }
    });
    GroovyInterceptor gi = GenericJsonParser.parse(GroovyInterceptor.class, spec);
    assertEquals(want, gi.getSrc());
}
Also used : JSONObject(org.jose4j.json.internal.json_simple.JSONObject) GroovyInterceptor(com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Test(org.junit.Test)

Example 22 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class GetAllTodosIT method testGetAllTodos.

@Test
public void testGetAllTodos() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "gettodos");
    map.put("version", "0.1.0");
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest)

Example 23 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class DeleteTodoIT method testDeleteTodo.

@Test
public void testDeleteTodo() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "delete");
    map.put("version", "0.1.0");
    Map<String, Object> data = new HashMap<>();
    data.put("id", "101010");
    map.put("data", data);
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 24 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project light-example-4j by networknt.

the class UpdateTodoIT method testUpdateTodo.

@Test
public void testUpdateTodo() throws ClientException, ApiException {
    final Http2Client client = Http2Client.getInstance();
    final CountDownLatch latch = new CountDownLatch(1);
    final ClientConnection connection;
    try {
        connection = client.connect(new URI(url), Http2Client.WORKER, Http2Client.SSL, Http2Client.BUFFER_POOL, enableHttp2 ? OptionMap.create(UndertowOptions.ENABLE_HTTP2, true) : OptionMap.EMPTY).get();
    } catch (Exception e) {
        throw new ClientException(e);
    }
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("host", "lightapi.net");
    map.put("service", "todo");
    map.put("action", "update");
    map.put("version", "0.1.0");
    Map<String, Object> data = new HashMap<>();
    data.put("id", "101010");
    data.put("title", "create todo from hybrid service unit test");
    data.put("completed", false);
    data.put("order", 1);
    map.put("data", data);
    JSONObject json = new JSONObject();
    json.putAll(map);
    System.out.printf("JSON: %s", json.toString());
    final AtomicReference<ClientResponse> reference = new AtomicReference<>();
    try {
        ClientRequest request = new ClientRequest().setPath("/api/json").setMethod(Methods.POST);
        request.getRequestHeaders().put(Headers.CONTENT_TYPE, "application/json");
        request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
        connection.sendRequest(request, client.createClientCallback(reference, latch, json.toString()));
        latch.await();
    } catch (Exception e) {
        logger.error("Exception: ", e);
        throw new ClientException(e);
    } finally {
        IoUtils.safeClose(connection);
    }
    int statusCode = reference.get().getResponseCode();
    String body = reference.get().getAttachment(Http2Client.RESPONSE_BODY);
    Assert.assertEquals(200, statusCode);
    Assert.assertNotNull(body);
}
Also used : ClientResponse(io.undertow.client.ClientResponse) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ClientException(com.networknt.exception.ClientException) ApiException(com.networknt.exception.ApiException) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) ClientConnection(io.undertow.client.ClientConnection) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) Http2Client(com.networknt.client.Http2Client) ClientException(com.networknt.exception.ClientException) ClientRequest(io.undertow.client.ClientRequest) Test(org.junit.Test)

Example 25 with JSONObject

use of org.jose4j.json.internal.json_simple.JSONObject in project open-ecard by ecsec.

the class VersionUpdateLoader method loadVersionUpdateList.

public VersionUpdateList loadVersionUpdateList() throws IllegalArgumentException {
    try {
        // load proxy if one is available
        // make sure it is initialized
        ProxySettings.getDefault();
        List<Proxy> proxies = ProxySelector.getDefault().select(updateUrl.toURI());
        Proxy p = Proxy.NO_PROXY;
        for (Proxy next : proxies) {
            if (next.type() != Proxy.Type.DIRECT) {
                LOG.debug("Found a proxy for the update connection.");
                p = next;
                break;
            }
        }
        LOG.info("Trying to load version list.");
        URLConnection con = updateUrl.openConnection(p);
        con.connect();
        InputStream in = con.getInputStream();
        Reader r = new InputStreamReader(in, StandardCharsets.UTF_8);
        JSONObject rootObj = (JSONObject) new JSONParser().parse(r);
        // get package specific download page
        String downloadPageString = (String) rootObj.get(pkgType + "_download_page");
        // access package specific list
        JSONArray updatesRaw = (JSONArray) rootObj.get(pkgType);
        ArrayList<VersionUpdate> updates = new ArrayList<>();
        for (Object ur : updatesRaw) {
            try {
                VersionUpdate next = VersionUpdate.fromJson((JSONObject) ur);
                updates.add(next);
            } catch (InvalidUpdateDefinition ex) {
                LOG.warn("Invalid version info contained in update list.", ex);
                throw new IllegalArgumentException("Invalid version info contained in update list.", ex);
            }
        }
        // make sure the versions are in the correct order
        Collections.sort(updates);
        VersionUpdateList list = new VersionUpdateList(updates, new URL(downloadPageString));
        LOG.info("Successfully retrieved version update list.");
        return list;
    } catch (IOException ex) {
        LOG.error("Failed to retrieve update list from server.", ex);
        throw new IllegalArgumentException("Failed to retrieve update list from server.", ex);
    } catch (NullPointerException ex) {
        LOG.warn("Package type {} not supported in update list.", pkgType);
        throw new IllegalArgumentException("Package type " + pkgType + " not supported in update list.", ex);
    } catch (URISyntaxException ex) {
        String msg = "Failed to convert Update URL to a URI.";
        LOG.error(msg, ex);
        throw new IllegalArgumentException(msg, ex);
    } catch (ParseException ex) {
        String msg = "Failed to deserialize JSON data.";
        LOG.error(msg, ex);
        throw new IllegalArgumentException(msg, ex);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) JSONArray(org.jose4j.json.internal.json_simple.JSONArray) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URLConnection(java.net.URLConnection) URL(java.net.URL) Proxy(java.net.Proxy) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) JSONParser(org.jose4j.json.internal.json_simple.parser.JSONParser) JSONObject(org.jose4j.json.internal.json_simple.JSONObject) InvalidUpdateDefinition(org.openecard.common.util.InvalidUpdateDefinition) ParseException(org.jose4j.json.internal.json_simple.parser.ParseException)

Aggregations

JSONObject (org.jose4j.json.internal.json_simple.JSONObject)29 Test (org.junit.Test)14 HashMap (java.util.HashMap)12 Test (org.testng.annotations.Test)8 AbstractServiceProxy (com.predic8.membrane.core.rules.AbstractServiceProxy)7 ServiceProxy (com.predic8.membrane.core.rules.ServiceProxy)7 URI (java.net.URI)6 InvalidUpdateDefinition (org.openecard.common.util.InvalidUpdateDefinition)6 Http2Client (com.networknt.client.Http2Client)5 ApiException (com.networknt.exception.ApiException)5 ClientException (com.networknt.exception.ClientException)5 ClientConnection (io.undertow.client.ClientConnection)5 ClientRequest (io.undertow.client.ClientRequest)5 ClientResponse (io.undertow.client.ClientResponse)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 URL (java.net.URL)4 GroovyInterceptor (com.predic8.membrane.core.interceptor.groovy.GroovyInterceptor)3 IOException (java.io.IOException)2 KeyStore (java.security.KeyStore)2