/** {@inheritDoc} */ @Override public RESTEasyBindingData decompose(Exchange exchange, RESTEasyBindingData target) throws Exception { Object content = exchange.getMessage().getContent(); if (exchange.getState().equals(ExchangeState.FAULT)) { if (content instanceof HandlerException) { HandlerException he = (HandlerException) content; if (he.getCause() instanceof ItemNotFoundException) { throw (Exception) he.getCause(); } if (he.getMessage() != null && he.getMessage().startsWith("SWITCHYARD014014")) { UnauthorizedException ue = new UnauthorizedException("Unauthorized"); throw (Exception) ue; } } } target = super.decompose(exchange, target); if (target.getOperationName().equals("addItem") && (content != null) && (content instanceof Item)) { // Unwrap the parameters target.setParameters(new Object[] {((Item) content).getItemId(), ((Item) content).getName()}); } return target; }
protected static void assertCause(String message, Exchange exchange) { assertEquals(ExchangeState.FAULT, exchange.getState()); HandlerException exception = exchange.getMessage().getContent(HandlerException.class); assertTrue(exception.isWrapper()); assertNotNull("Cause should not be null", exception.getCause()); assertEquals(message, exception.getCause().getMessage()); }
protected static void assertNoCause(String message, Exchange exchange) { assertEquals(ExchangeState.FAULT, exchange.getState()); Exception exception = exchange.getMessage().getContent(Exception.class); assertNotNull("Exception should not be null", exception); assertNull("Cause should be null", exception.getCause()); assertEquals(message, exception.getMessage()); }
@Test public void testFaultWithNoHandler() throws Exception { final QName serviceName = new QName("testFaultWithNoHandler"); // Provide the service MockHandler provider = new MockHandler() { @Override public void handleMessage(Exchange exchange) throws HandlerException { throw new HandlerException("Fault With No Handler!"); } }; ServiceReference service = _domain.createInOnlyService(serviceName, provider); // Consume the service Exchange exchange = service.createExchange(); exchange.send(exchange.createMessage()); // Make sure the exchange is in fault status Assert.assertEquals(ExchangeState.FAULT, exchange.getState()); }
/** * Send a message hash to the specified operation. * * @param operationName The operation name. * @param rubyHash The message hash. * @return The response object if an IN_OUT operation was invoked, otherwise null. * @throws Throwable An exception occurred while invoking the target operation. */ public Object send(String operationName, RubyHash rubyHash) throws Throwable { if (operationName == null) { throw new IllegalArgumentException("null 'operationName' argument."); } if (rubyHash == null) { throw new IllegalArgumentException("null 'rubyHash' argument."); } ServiceOperation operation = _serviceReference.getInterface().getOperation(operationName); if (operation == null) { throw new IllegalArgumentException( "Unknown operation name '" + operationName + "' on Service '" + _serviceReference.getName() + "'."); } // Clone the RubyHash to convert it to a normal Map based graph. This makes it possible // to more safely transport the payload data out of the ruby app via a SwitchYard Exchange... Map<String, Object> payload = deepClone(rubyHash); // Create the exchange contract... BaseExchangeContract exchangeContract = new BaseExchangeContract(operation); // Set the input type... exchangeContract .getInvokerInvocationMetaData() .setInputType(JavaService.toMessageType(payload.getClass())); if (operation.getExchangePattern() == ExchangePattern.IN_OUT) { final BlockingQueue<Exchange> responseQueue = new ArrayBlockingQueue<Exchange>(1); AtomicReference<ExchangeHandler> responseExchangeHandler = new AtomicReference<ExchangeHandler>( new ExchangeHandler() { public void handleMessage(Exchange exchange) throws HandlerException { responseQueue.offer(exchange); } public void handleFault(Exchange exchange) { responseQueue.offer(exchange); } }); Exchange exchange = _serviceReference.createExchange(exchangeContract, responseExchangeHandler.get()); Message message = exchange.createMessage().setContent(payload); exchange.send(message); Exchange exchangeOut = null; try { exchangeOut = responseQueue.take(); } catch (InterruptedException e) { throw new SwitchYardException( "Operation '" + operationName + "' on Service '" + _serviceReference.getName() + "' interrupted.", e); } if (exchangeOut.getState() == ExchangeState.OK) { return exchangeOut.getMessage().getContent(); } else { Object failureObj = exchangeOut.getMessage().getContent(); if (failureObj instanceof Throwable) { if (failureObj instanceof InvocationTargetException) { throw ((Throwable) failureObj).getCause(); } else { throw (Throwable) failureObj; } } else { throw new SwitchYardException( "Service invocation failure. Service '" + _serviceReference.getName() + "', operation '" + operationName + "'. Non Throwable failure message payload: " + failureObj); } } } else { Exchange exchange = _serviceReference.createExchange(exchangeContract); Message message = exchange.createMessage().setContent(payload); exchange.send(message); } return null; }