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);
}
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);
}
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();
}
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);
}
}
});
}
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);
}
Aggregations