/** {@inheritDoc} */ @Override public Message compose(StreamableRecordBindingData source, Exchange exchange, boolean create) throws Exception { final org.switchyard.Message message = create ? exchange.createMessage() : exchange.getMessage(); getContextMapper().mapFrom(source, exchange.getContext(message)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); source.getRecord().write(baos); message.setContent(new ByteArrayInputStream(baos.toByteArray())); return message; }
/** {@inheritDoc} */ @Override public Message compose(RESTEasyBindingData source, Exchange exchange) throws Exception { final Message message = super.compose(source, exchange); if (message.getContent() instanceof BaseClientResponse) { BaseClientResponse<?> clientResponse = (BaseClientResponse<?>) message.getContent(); if (clientResponse.getResponseStatus() == Response.Status.NOT_FOUND) { throw new ItemNotFoundException("Item not found"); } } else if (source.getOperationName().equals("addItem") && (source.getParameters().length == 2)) { // Wrap the parameters Item item = new Item((Integer) source.getParameters()[0], (String) source.getParameters()[1]); message.setContent(item); } return message; }
@Override public void handleMessage(Exchange exchange) throws HandlerException { if (exchange.getPattern().equals(ExchangePattern.IN_OUT)) { Message message; Element request = exchange.getMessage().getContent(Element.class); Element name = XMLHelper.getFirstChildElementByName(request, "arg0"); String toWhom = ""; if (name != null) { toWhom = name.getTextContent(); } String response = null; if (toWhom.length() == 0) { message = MessageBuilder.newInstance(FaultMessage.class).buildMessage(); response = "<soap:fault xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" + " <faultcode>soap:Server.AppError</faultcode>" + " <faultstring>Invalid name</faultstring>" + " <detail>" + " <message>Looks like you did not specify a name!</message>" + " <errorcode>1000</errorcode>" + " </detail>" + "</soap:fault>"; } else { message = MessageBuilder.newInstance().buildMessage(); response = "<test:sayHelloResponse xmlns:test=\"http://test.ws/\">" + " <return>Hello " + toWhom + "</return>" + "</test:sayHelloResponse>"; } try { Document responseDom = SOAPUtil.parseAsDom(response); message.setContent(responseDom.getDocumentElement()); } catch (Exception e) { // Generate fault } exchange.send(message); } }
/** Make sure that the current message is set correctly when an exchange is sent. */ @Test public void testGetMessage() throws Exception { final QName serviceName = new QName("bleh"); final String inMsgContent = "in message"; final String outMsgContent = "out message"; // create a handler to test that the in and out content match // expected result from getMessage() ExchangeHandler provider = new BaseHandler() { public void handleMessage(Exchange exchange) { Assert.assertEquals(exchange.getMessage().getContent(), inMsgContent); Message outMsg = exchange.createMessage(); outMsg.setContent(outMsgContent); try { exchange.send(outMsg); } catch (Exception ex) { Assert.fail(ex.toString()); } } }; ExchangeHandler consumer = new BaseHandler() { public void handleMessage(Exchange exchange) { Assert.assertEquals(exchange.getMessage().getContent(), outMsgContent); } }; ServiceReference service = _domain.createInOutService(serviceName, provider); Exchange exchange = service.createExchange(consumer); Message inMsg = exchange.createMessage(); inMsg.setContent(inMsgContent); exchange.send(inMsg); }
/** * Create a Message from the given SOAP message. * * @param soapMessage the SOAP message to be converted * @param exchange The message Exchange. * @return a Message * @throws SOAPException If the SOAP message is not correct. */ public Message compose(final SOAPMessage soapMessage, final Exchange exchange) throws SOAPException { Message message = exchange.createMessage(); final SOAPBody soapBody = soapMessage.getSOAPBody(); if (soapBody == null) { throw new SOAPException("Missing SOAP body from request"); } final Iterator children = soapBody.getChildElements(); boolean found = false; try { while (children.hasNext()) { final Node node = (Node) children.next(); if (node instanceof SOAPElement) { if (found) { throw new SOAPException("Found multiple SOAPElements in SOAPBody"); } node.detachNode(); message.setContent(node); found = true; } } } catch (Exception ex) { if (ex instanceof SOAPException) { throw (SOAPException) ex; } throw new SOAPException(ex); } if (!found) { throw new SOAPException("Could not find SOAPElement in SOAPBody"); } return message; }
/** {@inheritDoc} */ @Override public void handleMessage(Exchange exchange) throws HandlerException { if (!ExchangePhase.IN.equals(exchange.getPhase())) { return; } Context context = exchange.getContext(); ServiceOperation serviceOperation = exchange.getContract().getServiceOperation(); ProcessActionModel processActionModel = _actionModels.get(serviceOperation.getName()); ProcessActionType processActionType = getProcessActionType(context, processActionModel); Message messageIn = exchange.getMessage(); Long processInstanceId = null; ProcessInstance processInstance = null; switch (processActionType) { case START_PROCESS: if (_processId != null) { Object messageContentIn = messageIn.getContent(); if (messageContentIn != null) { Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(_messageContentInName, messageContentIn); processInstance = _ksession.startProcess(_processId, parameters); } else { processInstance = _ksession.startProcess(_processId); } processInstanceId = Long.valueOf(processInstance.getId()); } else { throwNullParameterException(processActionType, PROCESS_ID_VAR); } break; case SIGNAL_EVENT: String processEventType = getProcessEventType(context, processActionModel); Object processEvent = getProcessEvent(context, messageIn); processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.signalEvent(processEventType, processEvent, processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; case ABORT_PROCESS_INSTANCE: processInstanceId = getProcessInstanceId(context); if (processInstanceId != null) { _ksession.abortProcessInstance(processInstanceId.longValue()); } else { throwNullParameterException(processActionType, PROCESS_INSTANCE_ID_VAR); } break; } if (processInstanceId != null) { context.setProperty(PROCESS_INSTANCE_ID_VAR, processInstanceId, Scope.EXCHANGE); ExchangePattern exchangePattern = serviceOperation.getExchangePattern(); if (ExchangePattern.IN_OUT.equals(exchangePattern)) { if (processInstance == null) { processInstance = _ksession.getProcessInstance(processInstanceId.longValue()); } Message messageOut = exchange.createMessage(); Object messageContentOut = null; if (processInstance != null) { messageContentOut = ((WorkflowProcessInstance) processInstance).getVariable(_messageContentOutName); } if (messageContentOut != null) { messageOut.setContent(messageContentOut); } exchange.send(messageOut); } } }