/** {@inheritDoc} */
  @Override
  public StreamableRecordBindingData decompose(
      Exchange exchange, StreamableRecordBindingData target) throws Exception {
    Message sourceMessage = exchange.getMessage();

    getContextMapper().mapTo(exchange.getContext(), target);
    final InputStream content = sourceMessage.getContent(InputStream.class);
    target.getRecord().read(content);
    return target;
  }
  /** {@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;
  }
Ejemplo n.º 3
0
  private org.switchyard.Exchange createMockExchangeWithBody(final Integer payload) {

    final org.switchyard.Exchange switchYardExchange = mock(org.switchyard.Exchange.class);
    final org.switchyard.Context switchYardContext = mock(org.switchyard.Context.class);
    final ExchangeContract exchangeContract = mock(ExchangeContract.class);
    final InvocationContract invocationContract = mock(InvocationContract.class);
    final Message message = mock(Message.class);
    when(message.getContent(Integer.class)).thenReturn(payload);
    when(switchYardExchange.getContext()).thenReturn(switchYardContext);
    when(switchYardExchange.getMessage()).thenReturn(message);
    when(invocationContract.getOutputType()).thenReturn(new QName("java:java.lang.String"));
    when(exchangeContract.getInvokerInvocationMetaData()).thenReturn(invocationContract);
    when(switchYardExchange.getContract()).thenReturn(exchangeContract);

    return switchYardExchange;
  }
 /** {@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);
     }
   }
 }