Package org.switchyard

Examples of org.switchyard.Message


     * {@inheritDoc}
     */
    @Override
    public void handleOperation(Exchange exchange, KnowledgeOperation operation) throws HandlerException {
        Integer sessionId = null;
        Message inputMessage = exchange.getMessage();
        ExchangePattern exchangePattern = exchange.getContract().getProviderOperation().getExchangePattern();
        Map<String, Object> expressionVariables = new HashMap<String, Object>();
        RulesOperationType operationType = (RulesOperationType)operation.getType();
        switch (operationType) {
            case EXECUTE: {
                KnowledgeSession session = newStatelessSession();
                sessionId = session.getId();
                setGlobals(inputMessage, operation, session);
                if (ExchangePattern.IN_ONLY.equals(exchangePattern)) {
                    List<Object> facts = getInputList(inputMessage, operation, session);
                    session.getStateless().execute(facts);
                } else if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
                    KieCommands cmds = KieServices.Factory.get().getCommands();
                    List<Command<?>> batch = new ArrayList<Command<?>>();
                    Map<String, Object> inouts = getInputOutputMap(inputMessage, operation, session);
                    for (Entry<String, Object> inout : inouts.entrySet()) {
                        batch.add(cmds.newInsert(inout.getValue(), inout.getKey()));
                    }
                    List<Object> facts = getInputOnlyList(inputMessage, operation, session);
                    batch.add(cmds.newInsertElements(facts));
                    batch.add(cmds.newFireAllRules());
                    BatchExecutionCommand exec = cmds.newBatchExecution(batch, KnowledgeConstants.RESULT);
                    ExecutionResults results = session.getStateless().execute(exec);
                    for (String id : inouts.keySet()) {
                        expressionVariables.put(id, results.getValue(id));
                    }
                    expressionVariables.putAll(getGlobalVariables(session));
                }
                break;
            }
            case INSERT:
            case FIRE_ALL_RULES: {
                /*
                if (!isContinue(exchange)) {
                    disposeStatefulSession();
                }
                */
                KnowledgeSession session = getStatefulSession();
                sessionId = session.getId();
                setGlobals(inputMessage, operation, session);
                List<Object> facts = getInputList(inputMessage, operation, session);
                for (Object fact : facts) {
                    session.getStateful().insert(fact);
                }
                if (RulesOperationType.FIRE_ALL_RULES.equals(operationType)) {
                    session.getStateful().fireAllRules();
                }
                if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
                    expressionVariables.putAll(getGlobalVariables(session));
                }
                if (isDispose(exchange, inputMessage)) {
                    disposeStatefulSession();
                }
                break;
            }
            case FIRE_UNTIL_HALT: {
                /*
                if (!isContinue(exchange, inputMessage)) {
                    disposeStatefulSession();
                }
                */
                KnowledgeSession session = getStatefulSession();
                sessionId = session.getId();
                setGlobals(inputMessage, operation, session);
                if (_fireUntilHaltThread == null) {
                    ClassLoader fireUntilHaltLoader = Classes.getTCCL();
                    if (fireUntilHaltLoader == null) {
                        fireUntilHaltLoader = getLoader();
                    }
                    FireUntilHalt fireUntilHalt = new FireUntilHalt(this, session, fireUntilHaltLoader);
                    session.addDisposals(fireUntilHalt);
                    _fireUntilHaltThread = fireUntilHalt.startThread();
                }
                final String undefinedVariable = toVariable(exchange);
                Map<String, List<Object>> inputMap = getListMap(inputMessage, operation.getInputExpressionMappings(), true, undefinedVariable);
                if (inputMap.size() > 0) {
                    for (Entry<String, List<Object>> inputEntry : inputMap.entrySet()) {
                        String key = inputEntry.getKey();
                        if (undefinedVariable.equals(key)) {
                            String eventId = Strings.trimToNull(operation.getEventId());
                            if (eventId != null) {
                                key = eventId;
                            }
                        }
                        List<Object> facts = inputEntry.getValue();
                        if (undefinedVariable.equals(key)) {
                            for (Object fact : facts) {
                                session.getStateful().insert(fact);
                            }
                        } else {
                            EntryPoint entryPoint = session.getStateful().getEntryPoint(key);
                            if (entryPoint != null) {
                                for (Object fact : facts) {
                                    entryPoint.insert(fact);
                                }
                            } else {
                                throw RulesMessages.MESSAGES.unknownEntryPoint(key);
                            }
                        }
                    }
                } else {
                    List<Object> facts = getInputList(inputMessage, operation, session);
                    for (Object fact : facts) {
                        session.getStateful().insert(fact);
                    }
                }
                if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
                    expressionVariables.putAll(getGlobalVariables(session));
                }
                if (isDispose(exchange, inputMessage)) {
                    disposeStatefulSession();
                }
                break;
            }
            default: {
                throw RulesMessages.MESSAGES.unsupportedOperationType(operationType.toString());
            }
        }
        if (ExchangePattern.IN_OUT.equals(exchangePattern)) {
            Message outputMessage = exchange.createMessage();
            Context outputContext = exchange.getContext(outputMessage);
            if (sessionId != null && sessionId.intValue() > 0) {
                outputContext.setProperty(RulesConstants.SESSION_ID_PROPERTY, sessionId);
            }
            setFaults(outputMessage, operation, expressionVariables);
            if (outputMessage.getContent() != null) {
                exchange.sendFault(outputMessage);
            } else {
                setOutputs(outputMessage, operation, expressionVariables);
                exchange.send(outputMessage);
            }
View Full Code Here


    public static final String COMPOSE_PREFIX = "Composer compose ";
    public static final String DECOMPOSE_PREFIX = "Composer decompose ";

    @Override
    public Message compose(CamelBindingData source, Exchange exchange) throws Exception {
        Message message = super.compose(source, exchange);
        message.setContent(COMPOSE_PREFIX + message.getContent(String.class));
        return message;
    }
View Full Code Here

    private Invoker domOperation;

    @Test
    public void test_invokeWithWrongInputParameterType() {
        // A basic type conversion should happen automatically...
        Message response = domOperation.sendInOut("<a><b/></a>");

        Assert.assertEquals("<c/>", response.getContent());
    }
View Full Code Here

        return "";
    }

    @Override
    public String sendReply(String msg) {
        Message message = exchange.createMessage();
        message.setContent(ExchangeInjectionTest.TEST_OUT_CONTENT);
        message.getContext().setProperty(ExchangeInjectionTest.TEST_OUT_PROPERTY,
                ExchangeInjectionTest.TEST_OUT_PROPERTY);
        exchange.send(message);
        return "THIS SHOULD NOT BE RETURNED AS MESSAGE CONTENT";
    }
View Full Code Here

    public String attachments(String msg) {
        DataSource attachIn = exchange.getMessage().getAttachment(
                ExchangeInjectionTest.TEST_IN_ATTACHMENT);
        Assert.assertNotNull(attachIn);
        Assert.assertEquals(ExchangeInjectionTest.TEST_IN_ATTACHMENT, attachIn.getName());
        Message message = exchange.createMessage();
        message.setContent(ExchangeInjectionTest.TEST_OUT_CONTENT);
        message.addAttachment(ExchangeInjectionTest.TEST_OUT_ATTACHMENT,
                new DummyDataSource(ExchangeInjectionTest.TEST_OUT_ATTACHMENT));
        exchange.send(message);
        return "";
    }
View Full Code Here

     * {@inheritDoc}
     */
    @Override
    public Message compose(SOAPBindingData source, Exchange exchange) throws Exception {
        final SOAPMessage soapMessage = source.getSOAPMessage();
        final Message message = exchange.createMessage();
        getContextMapper().mapFrom(source, exchange.getContext(message));

        final SOAPEnvelope envelope = soapMessage.getSOAPPart().getEnvelope();
        if (envelope == null) {
            return message;
        }

        final SOAPBody soapBody = envelope.getBody();
        if (soapBody == null) {
            throw SOAPMessages.MESSAGES.missingSOAPBodyFromRequest();
        }

        try {
            if (soapBody.hasFault()) {
                // peel off the Fault element
                SOAPFault fault = soapBody.getFault();
                Node faultNode = fault.getParentNode().removeChild(fault);
                message.setContent(faultNode);
                return message;
            }

            List<Element> bodyChildren = getChildElements(soapBody);
            if (bodyChildren.size() > 1) {
                throw SOAPMessages.MESSAGES.foundMultipleSOAPElementsInSOAPBody();
            } else if (bodyChildren.size() == 0 || bodyChildren.get(0) == null) {
                throw SOAPMessages.MESSAGES.couldNotFindSOAPElementInSOAPBody();
            }

            Node bodyNode = bodyChildren.get(0);
            if (_documentStyle) {
                if (_unwrapped) {
                    String opName = exchange.getContract().getConsumerOperation().getName();
                    // peel off the operation wrapper, if present
                    if (opName != null && opName.equals(bodyNode.getLocalName())) {
                        List<Element> subChildren = getChildElements(bodyNode);
                        if (subChildren.size() == 0 || subChildren.size() > 1) {
                            _log.debug("Unable to unwrap element: " + bodyNode.getLocalName()
                                   + ". A single child element is required.");
                        } else {
                            bodyNode = subChildren.get(0);
                        }
                    }
                }
            }
            bodyNode = bodyNode.getParentNode().removeChild(bodyNode);

            // SOAP Attachments
            Map<String, DataSource> attachments = new HashMap<String, DataSource>();
            Iterator<AttachmentPart> aparts = (Iterator<AttachmentPart>) soapMessage.getAttachments();
            while (aparts.hasNext()) {
                AttachmentPart apRequest = aparts.next();
                String[] contentId = apRequest.getMimeHeader(CONTENT_ID);
                String name = null;
                if (_mtomEnabled) {
                    if (contentId == null) {
                        throw SOAPMessages.MESSAGES.contentIDHeaderMissingForAttachmentPart();
                    }
                    name = contentId[0];
                } else {
                    name = apRequest.getDataHandler().getDataSource().getName();
                    if ((name == null) || (name.length() == 0)) {
                        String[] disposition = apRequest.getMimeHeader(CONTENT_DISPOSITION);
                        name = (contentId != null) ? contentId[0] : null;
                        if ((name == null) && (disposition != null)) {
                            int start = disposition[0].indexOf(CONTENT_DISPOSITION_NAME);
                            String namePart = disposition[0].substring(start + CONTENT_DISPOSITION_NAME.length() + 1);
                            int end = namePart.indexOf(CONTENT_DISPOSITION_QUOTE);
                            name = namePart.substring(0, end);
                        } else if (name == null) {
                            // TODO: Identify the extension using content-type
                            name = UUID.randomUUID() + TEMP_FILE_EXTENSION;
                        }
                    }
                }
                if (name.startsWith(CONTENT_ID_START)) {
                    name = name.substring(1, name.length() - 1);
                }
                if (_mtomEnabled && _xopExpand) {
                    // Using a different map because Camel throws java.lang.StackOverflowError
                    // when we do message.removeAttachment(cid);
                    attachments.put(name, apRequest.getDataHandler().getDataSource());
                } else {
                    message.addAttachment(name, apRequest.getDataHandler().getDataSource());
                }
            }
            if (_mtomEnabled && _xopExpand) {
                // Expand xop message by inlining Base64 content
                bodyNode = SOAPUtil.expandXop((Element)bodyNode, attachments);
            }
            message.setContent(bodyNode);
        } catch (Exception ex) {
            if (ex instanceof SOAPException) {
                throw (SOAPException) ex;
            }
            throw new SOAPException(ex);
View Full Code Here

     * {@inheritDoc}
     */
    @Override
    public SOAPBindingData decompose(Exchange exchange, SOAPBindingData target) throws Exception {
        final SOAPMessage soapMessage = target.getSOAPMessage();
        final Message message = exchange.getMessage();
        final Boolean input = exchange.getPhase() == null;

        if (message != null) {
            // check to see if the payload is null or it's a full SOAP Message
            if (message.getContent() == null) {
                throw SOAPMessages.MESSAGES.unableToCreateSOAPBodyDueToNullMessageContent();
            }
            if (message.getContent() instanceof SOAPMessage) {
                return new SOAPBindingData((SOAPMessage)message.getContent());
            }
           
            try {
                // convert the message content to a form we can work with
                Node messageNode = message.getContent(Node.class);
                if (messageNode != null) {
                    Node messageNodeImport = soapMessage.getSOAPBody().getOwnerDocument().importNode(messageNode, true);
                    if (exchange.getState() != ExchangeState.FAULT || isSOAPFaultPayload(messageNode)) {
                        if (_documentStyle) {
                            String opName = exchange.getContract().getProviderOperation().getName();
                            if (_unwrapped) {
                                String ns = getWrapperNamespace(opName, input);
                                // Don't wrap if it's already wrapped
                                if (!messageNodeImport.getLocalName().equals(opName + DOC_LIT_WRAPPED_REPLY_SUFFIX)) {
                                    Element wrapper = messageNodeImport.getOwnerDocument().createElementNS(
                                            ns, opName + DOC_LIT_WRAPPED_REPLY_SUFFIX);
                                    wrapper.appendChild(messageNodeImport);
                                    messageNodeImport = wrapper;
                                }
                            }
                        }
                        soapMessage.getSOAPBody().appendChild(messageNodeImport);
                        // SOAP Attachments
                        for (String name : message.getAttachmentMap().keySet()) {
                            AttachmentPart apResponse = soapMessage.createAttachmentPart();
                            apResponse.setDataHandler(new DataHandler(message.getAttachment(name)));
                            apResponse.setContentId("<" + name + ">");
                            soapMessage.addAttachmentPart(apResponse);
                        }
                    } else {
                        // convert to SOAP Fault since ExchangeState is FAULT but the message is not SOAP Fault
View Full Code Here

        Exchange ex = attachments.createExchange(handler);
        ex.send(ex.createMessage()
                .setContent(TEST_IN_CONTENT)
                .addAttachment(TEST_IN_ATTACHMENT, new DummyDataSource(TEST_IN_ATTACHMENT)));
        Assert.assertEquals(1, handler.waitForOKMessage().getMessages().size());
        Message reply = handler.getMessages().poll().getMessage();
        DataSource attachOut = reply.getAttachment(TEST_OUT_ATTACHMENT);
        Assert.assertNotNull(attachOut);
        Assert.assertEquals(TEST_OUT_ATTACHMENT, attachOut.getName());
    }
View Full Code Here

*/
public class SOAPProvider extends BaseHandler {

    @Override
    public void handleMessage(Exchange exchange) throws HandlerException {
        Message message = exchange.createMessage();
        if (exchange.getContract().getProviderOperation().getExchangePattern().equals(ExchangePattern.IN_OUT)) {
            Message receivedMessage = exchange.getMessage();
            Node request = receivedMessage.getContent(Node.class);
            Element name = XMLHelper.getFirstChildElementByName(request, "arg0");
            Element fault = XMLHelper.getFirstChildElementByName(request, "CustomFaultMessage");
           
            String toWhom = "";
            if (name != null) {
View Full Code Here

            }
            if (response != null) {
                // This property vanishes once message composer processes this message
                // so caching it here
                Boolean hasFault = response.getSOAPBody().hasFault();
                Message message;
                try {
                    SOAPBindingData bindingData = new SOAPBindingData(response);
                    if (hasFault) {
                        SOAPFaultInfo faultInfo = new SOAPFaultInfo();
                        faultInfo.copyFaultInfo(response);
View Full Code Here

TOP

Related Classes of org.switchyard.Message

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.