Search in sources :

Example 11 with AxisOperation

use of org.apache.axis2.description.AxisOperation in project wso2-axis2-transports by wso2.

the class XMPPSender method getParameterListForOperation.

/**
 * Retrieves list of parameter names & their type for a given operation
 * @param operation
 */
private static String getParameterListForOperation(AxisOperation operation) {
    // Logic copied from BuilderUtil.buildsoapMessage(...)
    StringBuffer paramList = new StringBuffer();
    AxisMessage axisMessage = operation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE);
    XmlSchemaElement xmlSchemaElement = axisMessage.getSchemaElement();
    if (xmlSchemaElement != null) {
        XmlSchemaType schemaType = xmlSchemaElement.getSchemaType();
        if (schemaType instanceof XmlSchemaComplexType) {
            XmlSchemaComplexType complexType = ((XmlSchemaComplexType) schemaType);
            XmlSchemaParticle particle = complexType.getParticle();
            if (particle instanceof XmlSchemaSequence || particle instanceof XmlSchemaAll) {
                XmlSchemaGroupBase xmlSchemaGroupBase = (XmlSchemaGroupBase) particle;
                Iterator iterator = xmlSchemaGroupBase.getItems().getIterator();
                while (iterator.hasNext()) {
                    XmlSchemaElement innerElement = (XmlSchemaElement) iterator.next();
                    QName qName = innerElement.getQName();
                    if (qName == null && innerElement.getSchemaTypeName().equals(org.apache.ws.commons.schema.constants.Constants.XSD_ANYTYPE)) {
                        break;
                    }
                    long minOccurs = innerElement.getMinOccurs();
                    boolean nillable = innerElement.isNillable();
                    String name = qName != null ? qName.getLocalPart() : innerElement.getName();
                    String type = innerElement.getSchemaTypeName().toString();
                    paramList.append("," + type + " " + name);
                }
            }
        }
    }
    // remove first ","
    String list = paramList.toString();
    return list.replaceFirst(",", "");
}
Also used : XmlSchemaElement(org.apache.ws.commons.schema.XmlSchemaElement) QName(javax.xml.namespace.QName) XmlSchemaParticle(org.apache.ws.commons.schema.XmlSchemaParticle) XmlSchemaType(org.apache.ws.commons.schema.XmlSchemaType) XmlSchemaGroupBase(org.apache.ws.commons.schema.XmlSchemaGroupBase) XmlSchemaSequence(org.apache.ws.commons.schema.XmlSchemaSequence) XmlSchemaAll(org.apache.ws.commons.schema.XmlSchemaAll) Iterator(java.util.Iterator) XmlSchemaComplexType(org.apache.ws.commons.schema.XmlSchemaComplexType) AxisMessage(org.apache.axis2.description.AxisMessage)

Example 12 with AxisOperation

use of org.apache.axis2.description.AxisOperation in project wso2-axis2-transports by wso2.

the class XMPPSender method sendMessage.

/**
 * Send the given message over XMPP transport
 *
 * @param msgCtx the axis2 message context
 * @throws AxisFault on error
 */
public void sendMessage(MessageContext msgCtx, String targetAddress, OutTransportInfo outTransportInfo) throws AxisFault {
    XMPPConnection xmppConnection = null;
    XMPPOutTransportInfo xmppOutTransportInfo = null;
    XMPPConnectionFactory connectionFactory;
    // if on client side,create connection to xmpp server
    if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO);
        connectionFactory = xmppOutTransportInfo.getConnectionFactory();
    } else {
        getConnectionDetailsFromClientOptions(msgCtx);
        connectionFactory = defaultConnectionFactory;
    }
    synchronized (this) {
        xmppConnection = connectionFactory.getXmppConnection();
        if (xmppConnection == null) {
            connectionFactory.connect(serverCredentials);
            xmppConnection = connectionFactory.getXmppConnection();
        }
    }
    Message message = new Message();
    Options options = msgCtx.getOptions();
    String serviceName = XMPPUtils.getServiceName(targetAddress);
    SOAPVersion version = msgCtx.getEnvelope().getVersion();
    if (version instanceof SOAP12Version) {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_SOAP_XML + "; action=" + msgCtx.getSoapAction());
    } else {
        message.setProperty(XMPPConstants.CONTENT_TYPE, HTTPConstants.MEDIA_TYPE_TEXT_XML);
    }
    if (targetAddress != null) {
        xmppOutTransportInfo = new XMPPOutTransportInfo(targetAddress);
        xmppOutTransportInfo.setConnectionFactory(defaultConnectionFactory);
    } else if (msgCtx.getTo() != null && !msgCtx.getTo().hasAnonymousAddress()) {
    // TODO
    } else if (msgCtx.isServerSide()) {
        xmppOutTransportInfo = (XMPPOutTransportInfo) msgCtx.getProperty(Constants.OUT_TRANSPORT_INFO);
    }
    try {
        if (msgCtx.isServerSide()) {
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(false));
            message.setProperty(XMPPConstants.IN_REPLY_TO, xmppOutTransportInfo.getInReplyTo());
            message.setProperty(XMPPConstants.SEQUENCE_ID, xmppOutTransportInfo.getSequenceID());
        } else {
            // message is going to be processed on server side
            message.setProperty(XMPPConstants.IS_SERVER_SIDE, new Boolean(true));
            // we are sending a soap envelope as a message
            message.setProperty(XMPPConstants.CONTAINS_SOAP_ENVELOPE, new Boolean(true));
            message.setProperty(XMPPConstants.SERVICE_NAME, serviceName);
            String action = options.getAction();
            if (action == null) {
                AxisOperation axisOperation = msgCtx.getAxisOperation();
                if (axisOperation != null) {
                    action = axisOperation.getSoapAction();
                }
            }
            if (action != null) {
                message.setProperty(XMPPConstants.ACTION, action);
            }
        }
        if (xmppConnection == null) {
            handleException("Connection to XMPP Server is not established.");
        }
        // initialize the chat manager using connection
        ChatManager chatManager = xmppConnection.getChatManager();
        Chat chat = chatManager.createChat(xmppOutTransportInfo.getDestinationAccount(), null);
        boolean waitForResponse = msgCtx.getOperationContext() != null && WSDL2Constants.MEP_URI_OUT_IN.equals(msgCtx.getOperationContext().getAxisOperation().getMessageExchangePattern());
        OMElement msgElement;
        String messageToBeSent = "";
        if (XMPPConstants.XMPP_CONTENT_TYPE_STRING.equals(xmppOutTransportInfo.getContentType())) {
            // if request is received from a chat client, whole soap envelope
            // should not be sent.
            OMElement soapBodyEle = msgCtx.getEnvelope().getBody();
            OMElement responseEle = soapBodyEle.getFirstElement();
            if (responseEle != null) {
                msgElement = responseEle.getFirstElement();
            } else {
                msgElement = responseEle;
            }
        } else {
            // if request received from a ws client whole soap envelope
            // must be sent.
            msgElement = msgCtx.getEnvelope();
        }
        messageToBeSent = msgElement.toString();
        message.setBody(messageToBeSent);
        String key = null;
        if (waitForResponse && !msgCtx.isServerSide()) {
            PacketFilter filter = new PacketTypeFilter(message.getClass());
            xmppConnection.addPacketListener(xmppClientSidePacketListener, filter);
            key = UUID.randomUUID().toString();
            xmppClientSidePacketListener.listenForResponse(key, msgCtx);
            message.setProperty(XMPPConstants.SEQUENCE_ID, key);
        }
        chat.sendMessage(message);
        log.debug("Sent message :" + message.toXML());
        // Is this the best way to do this?
        if (waitForResponse && !msgCtx.isServerSide()) {
            xmppClientSidePacketListener.waitFor(key);
            // xmppConnection.disconnect();
            log.debug("Received response sucessfully");
        }
    } catch (XMPPException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } catch (InterruptedException e) {
        log.error("Error occurred while sending the message : " + message.toXML(), e);
        handleException("Error occurred while sending the message : " + message.toXML(), e);
    } finally {
    // if(xmppConnection != null && !msgCtx.isServerSide()){
    // xmppConnection.disconnect();
    // }
    }
}
Also used : Options(org.apache.axis2.client.Options) XMPPConnectionFactory(org.apache.axis2.transport.xmpp.util.XMPPConnectionFactory) PacketFilter(org.jivesoftware.smack.filter.PacketFilter) AxisMessage(org.apache.axis2.description.AxisMessage) Message(org.jivesoftware.smack.packet.Message) AxisOperation(org.apache.axis2.description.AxisOperation) OMElement(org.apache.axiom.om.OMElement) PacketTypeFilter(org.jivesoftware.smack.filter.PacketTypeFilter) XMPPConnection(org.jivesoftware.smack.XMPPConnection) XMPPOutTransportInfo(org.apache.axis2.transport.xmpp.util.XMPPOutTransportInfo) SOAPVersion(org.apache.axiom.soap.SOAPVersion) Chat(org.jivesoftware.smack.Chat) XMPPException(org.jivesoftware.smack.XMPPException) ChatManager(org.jivesoftware.smack.ChatManager) SOAP12Version(org.apache.axiom.soap.SOAP12Version)

Example 13 with AxisOperation

use of org.apache.axis2.description.AxisOperation in project wso2-axis2-transports by wso2.

the class DefaultSMSMessageBuilderImpl method buildMessaage.

public MessageContext buildMessaage(SMSMessage msg, ConfigurationContext configurationContext) throws InvalidMessageFormatException {
    String message = msg.getContent();
    String sender = msg.getSender();
    String receiver = msg.getReceiver();
    String[] parts = message.split(":");
    // may be can add feature to send message format for a request like ????
    if (parts.length < 2) {
        throw new InvalidMessageFormatException("format must be  \"service_name \" : \"opration_name\" : " + "\"parm_1=val_1\" :..:\"param_n = val_n\"");
    } else {
        AxisConfiguration repo = configurationContext.getAxisConfiguration();
        MessageContext messageContext = configurationContext.createMessageContext();
        parts = trimSplited(parts);
        try {
            AxisService axisService = repo.getService(parts[0]);
            if (axisService == null) {
                throw new InvalidMessageFormatException("Service : " + parts[0] + "does not exsist");
            } else {
                messageContext.setAxisService(axisService);
                AxisOperation axisOperation = axisService.getOperation(new QName(parts[1]));
                if (axisOperation == null) {
                    throw new InvalidMessageFormatException("Operation: " + parts[1] + " does not exsist");
                }
                messageContext.setAxisOperation(axisOperation);
                messageContext.setAxisMessage(axisOperation.getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE));
                Map params = getParams(parts, 2);
                SOAPEnvelope soapEnvelope = createSoapEnvelope(messageContext, params);
                messageContext.setServerSide(true);
                messageContext.setEnvelope(soapEnvelope);
                TransportInDescription in = configurationContext.getAxisConfiguration().getTransportIn("sms");
                TransportOutDescription out = configurationContext.getAxisConfiguration().getTransportOut("sms");
                messageContext.setIncomingTransportName("sms");
                messageContext.setProperty(SMSTransportConstents.SEND_TO, sender);
                messageContext.setProperty(SMSTransportConstents.DESTINATION, receiver);
                messageContext.setTransportIn(in);
                messageContext.setTransportOut(out);
                handleSMSProperties(msg, messageContext);
                return messageContext;
            }
        } catch (AxisFault axisFault) {
            log.debug("[DefaultSMSMessageBuilderImpl] Error while extracting the axis2Service \n" + axisFault);
        }
    }
    return null;
}
Also used : AxisFault(org.apache.axis2.AxisFault) AxisConfiguration(org.apache.axis2.engine.AxisConfiguration) QName(javax.xml.namespace.QName) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) MessageContext(org.apache.axis2.context.MessageContext) HashMap(java.util.HashMap) Map(java.util.Map)

Example 14 with AxisOperation

use of org.apache.axis2.description.AxisOperation in project wso2-axis2-transports by wso2.

the class UtilsTransportServer method deployEchoService.

/**
 * Deploy the standard Echo service with the custom parameters passed in
 * @param name the service name to assign
 * @param parameters the parameters for the service
 * @throws Exception
 */
public void deployEchoService(String name, List<Parameter> parameters) throws Exception {
    AxisService service = new AxisService(name);
    service.setClassLoader(Thread.currentThread().getContextClassLoader());
    service.addParameter(new Parameter(Constants.SERVICE_CLASS, Echo.class.getName()));
    // add operation echoOMElement
    AxisOperation axisOp = new InOutAxisOperation(new QName("echoOMElement"));
    axisOp.setMessageReceiver(new RawXMLINOutMessageReceiver());
    axisOp.setStyle(WSDLConstants.STYLE_RPC);
    service.addOperation(axisOp);
    service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI + "/echoOMElement", axisOp);
    // add operation echoOMElementNoResponse
    axisOp = new InOutAxisOperation(new QName("echoOMElementNoResponse"));
    axisOp.setMessageReceiver(new RawXMLINOnlyMessageReceiver());
    axisOp.setStyle(WSDLConstants.STYLE_RPC);
    service.addOperation(axisOp);
    service.mapActionToOperation(Constants.AXIS2_NAMESPACE_URI + "/echoOMElementNoResponse", axisOp);
    for (Parameter parameter : parameters) {
        service.addParameter(parameter);
    }
    cfgCtx.getAxisConfiguration().addService(service);
}
Also used : InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation) AxisOperation(org.apache.axis2.description.AxisOperation) QName(javax.xml.namespace.QName) AxisService(org.apache.axis2.description.AxisService) RawXMLINOutMessageReceiver(org.apache.axis2.receivers.RawXMLINOutMessageReceiver) Parameter(org.apache.axis2.description.Parameter) RawXMLINOnlyMessageReceiver(org.apache.axis2.receivers.RawXMLINOnlyMessageReceiver) InOutAxisOperation(org.apache.axis2.description.InOutAxisOperation)

Aggregations

AxisOperation (org.apache.axis2.description.AxisOperation)12 QName (javax.xml.namespace.QName)8 AxisService (org.apache.axis2.description.AxisService)6 MessageContext (org.apache.axis2.context.MessageContext)5 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)4 AxisFault (org.apache.axis2.AxisFault)4 SOAPFactory (org.apache.axiom.soap.SOAPFactory)3 AxisMessage (org.apache.axis2.description.AxisMessage)3 Parameter (org.apache.axis2.description.Parameter)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2 OMElement (org.apache.axiom.om.OMElement)2 Options (org.apache.axis2.client.Options)2 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)2 StringTokenizer (java.util.StringTokenizer)1 TreeMap (java.util.TreeMap)1 ContentType (javax.mail.internet.ContentType)1 FactoryConfigurationError (javax.xml.parsers.FactoryConfigurationError)1 OMException (org.apache.axiom.om.OMException)1