Search in sources :

Example 1 with NavajoResponseHandler

use of com.dexels.navajo.client.NavajoResponseHandler in project navajo by Dexels.

the class ITAsyncClient method testAsync.

@Test
@Ignore
public void testAsync() throws Exception {
    final ManualAsyncClient ac = new AsyncClientImpl();
    String service = "club/InitUpdateClub";
    System.err.println(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setServer(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setUsername(TestConfig.NAVAJO_TEST_USER.getValue());
    ac.setPassword(TestConfig.NAVAJO_TEST_PASS.getValue());
    Navajo input = NavajoFactory.getInstance().createNavajo();
    final NavajoResponseHandler showOutput = new NavajoResponseHandler() {

        @Override
        public void onResponse(Navajo n) {
            logger.info("Navajo finished!");
            try {
                StringWriter sw = new StringWriter();
                n.write(sw);
                logger.info("Response2 : {}", sw);
            } catch (NavajoException e) {
                logger.error("Error: ", e);
            }
        }

        @Override
        public void onFail(Throwable t) {
            logger.error("whoops: ", t);
        }

        @Override
        public Throwable getCaughtException() {
            return null;
        }
    };
    for (int i = 0; i < 10; i++) {
        ac.callService(input, service, showOutput);
        logger.info("Exchange sent");
    }
    Thread.sleep(10000);
}
Also used : ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) StringWriter(java.io.StringWriter) NavajoException(com.dexels.navajo.document.NavajoException) NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Navajo(com.dexels.navajo.document.Navajo) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with NavajoResponseHandler

use of com.dexels.navajo.client.NavajoResponseHandler in project navajo by Dexels.

the class AsyncClientImpl method callService.

// Only used from Rhino
@Override
public void callService(Access inputAccess, Navajo input, String service, TmlRunnable onSuccess, TmlRunnable onFail, NavajoResponseCallback navajoResponseCallback) throws IOException {
    final Access currentAccess = inputAccess.cloneWithoutNavajos();
    if (input == null) {
        input = NavajoFactory.getInstance().createNavajo();
    }
    currentAccess.setInDoc(input);
    Header header = input.getHeader();
    if (header == null) {
        header = NavajoFactory.getInstance().createHeader(input, service, currentAccess.rpcUser, currentAccess.rpcUser, -1);
        input.addHeader(header);
    }
    header.setRPCName(service);
    header.setRPCUser(currentAccess.rpcUser);
    header.setRPCPassword(currentAccess.rpcPwd);
    NavajoResponseHandler nrh = new NavajoResponseHandler() {

        Throwable caughtException = null;

        @Override
        public void onResponse(Navajo n) {
            setActualCalls(getActualCalls() - 1);
            currentAccess.setOutputDoc(n);
            if (onSuccess != null) {
                onSuccess.setResponseNavajo(n);
                if (navajoResponseCallback != null) {
                    navajoResponseCallback.responseReceived(n);
                }
                setActualCalls(getActualCalls() - 1);
                SchedulerRegistry.submit(onSuccess, false);
            }
        }

        @Override
        public synchronized void onFail(Throwable t) throws IOException {
            caughtException = t;
            logger.warn("Error: ", caughtException);
            setActualCalls(getActualCalls() - 1);
            try {
                if (onFail != null) {
                    SchedulerRegistry.submit(onFail, false);
                }
            } finally {
                setActualCalls(getActualCalls() - 1);
            }
        }

        @Override
        public synchronized Throwable getCaughtException() {
            return caughtException;
        }
    };
    setActualCalls(getActualCalls() + 1);
    callService(currentAccess.getRequestUrl(), input, nrh, null);
}
Also used : Header(com.dexels.navajo.document.Header) Access(com.dexels.navajo.script.api.Access) NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Navajo(com.dexels.navajo.document.Navajo)

Example 3 with NavajoResponseHandler

use of com.dexels.navajo.client.NavajoResponseHandler in project navajo by Dexels.

the class AsyncClientImpl method callService.

@Override
public Navajo callService(Navajo input, String service) throws IOException {
    final Object semaphore = new Object();
    final Set<Navajo> result = new HashSet<>();
    NavajoResponseHandler nrh = new NavajoResponseHandler() {

        Throwable caughtException = null;

        @Override
        public Throwable getCaughtException() {
            synchronized (semaphore) {
                return caughtException;
            }
        }

        @Override
        public void onResponse(Navajo n) {
            result.add(n);
            synchronized (semaphore) {
                semaphore.notify();
            }
        }

        @Override
        public void onFail(Throwable t) throws IOException {
            logger.error("Problem calling navajo: ", t);
            synchronized (semaphore) {
                caughtException = t;
                semaphore.notify();
            }
        }
    };
    callService(input, service, nrh);
    synchronized (semaphore) {
        try {
            while (result.isEmpty() && nrh.getCaughtException() == null) {
                semaphore.wait();
            }
        } catch (InterruptedException e) {
            logger.debug("Error: ", e);
        }
    }
    if (nrh.getCaughtException() != null) {
        throw new IOException("Error calling remote navajo: " + server, nrh.getCaughtException());
    }
    return result.iterator().next();
}
Also used : NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Navajo(com.dexels.navajo.document.Navajo) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 4 with NavajoResponseHandler

use of com.dexels.navajo.client.NavajoResponseHandler in project navajo by Dexels.

the class AsyncClientImpl method callService.

private void callService(final String url, Navajo input, NavajoResponseHandler continuation, Integer timeout) throws IOException {
    logger.info("Calling service: {} at {} ", input.getHeader().getRPCName(), url);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    input.write(baos);
    final byte[] byteArray = baos.toByteArray();
    httpClient.newRequest(url).method(HttpMethod.POST).header("Accept-Encoding", null).timeout(MAX_TIMEOUT, TimeUnit.SECONDS).idleTimeout(MAX_IDLE_TIMEOUT, TimeUnit.SECONDS).content(new BytesContentProvider(byteArray), "text/xml; charset=utf-8").onRequestFailure((request, throwable) -> {
        logger.error("Request failed: HTTP call to: " + url + " failed: {}", throwable);
        if (continuation != null) {
            try {
                continuation.onFail(throwable);
            } catch (IOException exc) {
                logger.error("Error: ", exc);
            }
        }
        if (closeAfterUse) {
            close();
        }
    }).onResponseFailure((response, throwable) -> {
        logger.error("Response failed: HTTP call to: " + url + " failed: {}", throwable);
        if (continuation != null) {
            try {
                continuation.onFail(throwable);
            } catch (IOException exc) {
                logger.error("Error: ", exc);
            }
        }
        if (closeAfterUse) {
            close();
        }
    }).send(new BufferingResponseListener(MAX_RESULT_SIZE) {

        @Override
        public void onComplete(Result result) {
            try {
                Navajo response = NavajoFactory.getInstance().createNavajo(getContentAsInputStream());
                if (continuation != null) {
                    continuation.onResponse(response);
                }
            } catch (UnsupportedOperationException exc) {
                logger.error("Error: ", exc);
            } finally {
                if (closeAfterUse) {
                    close();
                }
                setActualCalls(getActualCalls() - 1);
            }
        }
    });
}
Also used : Result(org.eclipse.jetty.client.api.Result) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) LoggerFactory(org.slf4j.LoggerFactory) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) HashSet(java.util.HashSet) HttpClient(org.eclipse.jetty.client.HttpClient) NavajoResponseCallback(com.dexels.navajo.script.api.NavajoResponseCallback) Map(java.util.Map) HttpProxy(org.eclipse.jetty.client.HttpProxy) ProxyConfiguration(org.eclipse.jetty.client.ProxyConfiguration) Navajo(com.dexels.navajo.document.Navajo) SchedulerRegistry(com.dexels.navajo.script.api.SchedulerRegistry) NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Logger(org.slf4j.Logger) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Access(com.dexels.navajo.script.api.Access) Set(java.util.Set) IOException(java.io.IOException) TimeUnit(java.util.concurrent.TimeUnit) NavajoFactory(com.dexels.navajo.document.NavajoFactory) HttpMethod(org.eclipse.jetty.http.HttpMethod) ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) AsyncClientFactory(com.dexels.navajo.client.async.AsyncClientFactory) TmlRunnable(com.dexels.navajo.script.api.TmlRunnable) Header(com.dexels.navajo.document.Header) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Navajo(com.dexels.navajo.document.Navajo) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) BufferingResponseListener(org.eclipse.jetty.client.util.BufferingResponseListener) Result(org.eclipse.jetty.client.api.Result)

Example 5 with NavajoResponseHandler

use of com.dexels.navajo.client.NavajoResponseHandler in project navajo by Dexels.

the class ITAsyncClient method testPost.

@Test
public void testPost() throws Exception {
    final ManualAsyncClient ac = new AsyncClientImpl();
    String service = "ProcessPrintGenericBirt";
    System.err.println(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setServer(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setUsername(TestConfig.NAVAJO_TEST_USER.getValue());
    ac.setPassword(TestConfig.NAVAJO_TEST_PASS.getValue());
    Navajo input = NavajoFactory.getInstance().createNavajo(getClass().getResourceAsStream("test.xml"));
    final NavajoResponseHandler showOutput = new NavajoResponseHandler() {

        @Override
        public void onResponse(Navajo n) {
            logger.info("Navajo finished!");
            try {
                StringWriter sw = new StringWriter();
                n.write(sw);
                Binary b = (Binary) n.getMessage("Result").getProperty("Data").getTypedValue();
                BinaryOpenerFactory.getInstance().open(b);
                logger.info("Response2 : {}", sw);
            } catch (NavajoException e) {
                logger.error("Error: ", e);
            }
        }

        @Override
        public void onFail(Throwable t) {
            logger.error("whoops: ", t);
        }

        @Override
        public Throwable getCaughtException() {
            return null;
        }
    };
    ac.callService(input, service, showOutput);
    logger.info("Exchange sent");
    Thread.sleep(10000);
}
Also used : ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) StringWriter(java.io.StringWriter) NavajoException(com.dexels.navajo.document.NavajoException) NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Navajo(com.dexels.navajo.document.Navajo) Binary(com.dexels.navajo.document.types.Binary) Test(org.junit.Test)

Aggregations

NavajoResponseHandler (com.dexels.navajo.client.NavajoResponseHandler)5 Navajo (com.dexels.navajo.document.Navajo)5 ManualAsyncClient (com.dexels.navajo.client.async.ManualAsyncClient)3 Header (com.dexels.navajo.document.Header)2 NavajoException (com.dexels.navajo.document.NavajoException)2 Access (com.dexels.navajo.script.api.Access)2 IOException (java.io.IOException)2 StringWriter (java.io.StringWriter)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 AsyncClientFactory (com.dexels.navajo.client.async.AsyncClientFactory)1 NavajoFactory (com.dexels.navajo.document.NavajoFactory)1 Binary (com.dexels.navajo.document.types.Binary)1 NavajoResponseCallback (com.dexels.navajo.script.api.NavajoResponseCallback)1 SchedulerRegistry (com.dexels.navajo.script.api.SchedulerRegistry)1 TmlRunnable (com.dexels.navajo.script.api.TmlRunnable)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 Set (java.util.Set)1