protected boolean injectMessage(InputStream in, String contentType) { try { org.apache.synapse.MessageContext msgCtx = createMessageContext(); if (log.isDebugEnabled()) { log.debug("Processed Custom inbound EP Message of Content-type : " + contentType); } MessageContext axis2MsgCtx = ((org.apache.synapse.core.axis2.Axis2MessageContext) msgCtx).getAxis2MessageContext(); // Determine the message builder to use Builder builder; if (contentType == null) { log.debug("No content type specified. Using SOAP builder."); builder = new SOAPBuilder(); } else { int index = contentType.indexOf(';'); String type = index > 0 ? contentType.substring(0, index) : contentType; builder = BuilderUtil.getBuilderFromSelector(type, axis2MsgCtx); if (builder == null) { if (log.isDebugEnabled()) { log.debug("No message builder found for type '" + type + "'. Falling back to SOAP."); } builder = new SOAPBuilder(); } } OMElement documentElement = builder.processDocument(in, contentType, axis2MsgCtx); // Inject the message to the sequence. msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); if (injectingSeq == null || injectingSeq.equals("")) { log.error("Sequence name not specified. Sequence : " + injectingSeq); return false; } SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(injectingSeq); if (seq != null) { if (log.isDebugEnabled()) { log.debug("injecting message to sequence : " + injectingSeq); } seq.setErrorHandler(onErrorSeq); if (!seq.isInitialized()) { seq.init(synapseEnvironment); } if (!synapseEnvironment.injectInbound(msgCtx, seq, sequential)) { return false; } } else { log.error("Sequence: " + injectingSeq + " not found"); } } catch (Exception e) { log.error("Error while processing the Custom Inbound EP Message."); } return true; }
/** * Process a single file through Axis2 * * @param entry the PollTableEntry for the file (or its parent directory or archive) * @param file the file that contains the actual message pumped into Axis2 * @throws AxisFault on error */ private void processFile(PollTableEntry entry, FileObject file) throws AxisFault { try { FileContent content = file.getContent(); String fileName = file.getName().getBaseName(); String filePath = file.getName().getPath(); metrics.incrementBytesReceived(content.getSize()); Map<String, Object> transportHeaders = new HashMap<String, Object>(); transportHeaders.put(VFSConstants.FILE_PATH, filePath); transportHeaders.put(VFSConstants.FILE_NAME, fileName); try { transportHeaders.put(VFSConstants.FILE_LENGTH, content.getSize()); transportHeaders.put(VFSConstants.LAST_MODIFIED, content.getLastModifiedTime()); } catch (FileSystemException ignore) { } MessageContext msgContext = entry.createMessageContext(); String contentType = entry.getContentType(); if (BaseUtils.isBlank(contentType)) { if (file.getName().getExtension().toLowerCase().endsWith(".xml")) { contentType = "text/xml"; } else if (file.getName().getExtension().toLowerCase().endsWith(".txt")) { contentType = "text/plain"; } } else { // Extract the charset encoding from the configured content type and // set the CHARACTER_SET_ENCODING property as e.g. SOAPBuilder relies on this. String charSetEnc = null; try { if (contentType != null) { charSetEnc = new ContentType(contentType).getParameter("charset"); } } catch (ParseException ex) { // ignore } msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc); } // if the content type was not found, but the service defined it.. use it if (contentType == null) { if (entry.getContentType() != null) { contentType = entry.getContentType(); } else if (VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE) != null) { contentType = VFSUtils.getProperty(content, BaseConstants.CONTENT_TYPE); } } // does the service specify a default reply file URI ? String replyFileURI = entry.getReplyFileURI(); if (replyFileURI != null) { msgContext.setProperty( Constants.OUT_TRANSPORT_INFO, new VFSOutTransportInfo(replyFileURI, entry.isFileLockingEnabled())); } // Determine the message builder to use Builder builder; if (contentType == null) { log.debug("No content type specified. Using SOAP builder."); builder = new SOAPBuilder(); } else { int index = contentType.indexOf(';'); String type = index > 0 ? contentType.substring(0, index) : contentType; builder = BuilderUtil.getBuilderFromSelector(type, msgContext); if (builder == null) { if (log.isDebugEnabled()) { log.debug("No message builder found for type '" + type + "'. Falling back to SOAP."); } builder = new SOAPBuilder(); } } // set the message payload to the message context InputStream in; ManagedDataSource dataSource; if (builder instanceof DataSourceMessageBuilder && entry.isStreaming()) { in = null; dataSource = ManagedDataSourceFactory.create(new FileObjectDataSource(file, contentType)); } else { in = new AutoCloseInputStream(content.getInputStream()); dataSource = null; } try { OMElement documentElement; if (in != null) { documentElement = builder.processDocument(in, contentType, msgContext); } else { documentElement = ((DataSourceMessageBuilder) builder) .processDocument(dataSource, contentType, msgContext); } msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); handleIncomingMessage( msgContext, transportHeaders, null, // * SOAP Action - not applicable *// contentType); } finally { if (dataSource != null) { dataSource.destroy(); } } if (log.isDebugEnabled()) { log.debug("Processed file : " + file + " of Content-type : " + contentType); } } catch (FileSystemException e) { handleException("Error reading file content or attributes : " + file, e); } finally { try { file.close(); } catch (FileSystemException warn) { // log.warn("Cannot close file after processing : " + file.getName().getPath(), warn); // ignore the warning, since we handed over the stream close job to AutocloseInputstream.. } } }
/** Invoke the mediation logic for the passed message */ public boolean invoke(Object object, String name) throws SynapseException { Message msg = (Message) object; try { org.apache.synapse.MessageContext msgCtx = createMessageContext(); msgCtx.setProperty("inbound.endpoint.name", name); InboundEndpoint inboundEndpoint = msgCtx.getConfiguration().getInboundEndpoint(name); CustomLogSetter.getInstance().setLogAppender(inboundEndpoint.getArtifactContainerName()); String contentType = msg.getJMSType(); if (contentType == null || contentType.trim().equals("")) { String contentTypeProperty = jmsProperties.getProperty(JMSConstants.CONTENT_TYPE_PROPERTY); if (contentTypeProperty != null) { contentType = msg.getStringProperty(contentTypeProperty); } } else { msgCtx.setProperty(JMSConstants.JMS_MESSAGE_TYPE, contentType); } if (contentType == null || contentType.trim().equals("")) { contentType = jmsProperties.getProperty(JMSConstants.CONTENT_TYPE); } if (log.isDebugEnabled()) { log.debug("Processed JMS Message of Content-type : " + contentType); } MessageContext axis2MsgCtx = ((org.apache.synapse.core.axis2.Axis2MessageContext) msgCtx).getAxis2MessageContext(); // set the JMS Message ID as the Message ID of the MessageContext try { msgCtx.setMessageID(msg.getJMSMessageID()); String jmsCorrelationID = msg.getJMSCorrelationID(); if (jmsCorrelationID != null && !jmsCorrelationID.isEmpty()) { msgCtx.setProperty(JMSConstants.JMS_COORELATION_ID, jmsCorrelationID); } else { msgCtx.setProperty(JMSConstants.JMS_COORELATION_ID, msg.getJMSMessageID()); } } catch (JMSException ignore) { log.warn("Error getting the COORELATION ID from the message."); } // Handle dual channel Destination replyTo = msg.getJMSReplyTo(); if (replyTo != null) { msgCtx.setProperty(SynapseConstants.IS_INBOUND, true); // Create the cachedJMSConnectionFactory with the existing // connection CachedJMSConnectionFactory cachedJMSConnectionFactory = new CachedJMSConnectionFactory(jmsProperties, connection); String strUserName = jmsProperties.getProperty(JMSConstants.PARAM_JMS_USERNAME); String strPassword = jmsProperties.getProperty(JMSConstants.PARAM_JMS_PASSWORD); JMSReplySender jmsReplySender = new JMSReplySender(replyTo, cachedJMSConnectionFactory, strUserName, strPassword); msgCtx.setProperty( InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER, jmsReplySender); } else if (replyDestination != null) { msgCtx.setProperty(SynapseConstants.IS_INBOUND, true); // Create the cachedJMSConnectionFactory with the existing // connection CachedJMSConnectionFactory cachedJMSConnectionFactory = new CachedJMSConnectionFactory(jmsProperties, connection); String strUserName = jmsProperties.getProperty(JMSConstants.PARAM_JMS_USERNAME); String strPassword = jmsProperties.getProperty(JMSConstants.PARAM_JMS_PASSWORD); JMSReplySender jmsReplySender = new JMSReplySender( replyDestination, cachedJMSConnectionFactory, strUserName, strPassword); msgCtx.setProperty( InboundEndpointConstants.INBOUND_ENDPOINT_RESPONSE_WORKER, jmsReplySender); } // Determine the message builder to use Builder builder; if (contentType == null) { log.debug("No content type specified. Using SOAP builder."); builder = new SOAPBuilder(); } else { int index = contentType.indexOf(';'); String type = index > 0 ? contentType.substring(0, index) : contentType; builder = BuilderUtil.getBuilderFromSelector(type, axis2MsgCtx); if (builder == null) { if (log.isDebugEnabled()) { log.debug("No message builder found for type '" + type + "'. Falling back to SOAP."); } builder = new SOAPBuilder(); } } OMElement documentElement = null; // set the message payload to the message context if (msg instanceof TextMessage) { String message = ((TextMessage) msg).getText(); InputStream in = new AutoCloseInputStream(new ByteArrayInputStream(message.getBytes())); documentElement = builder.processDocument(in, contentType, axis2MsgCtx); } else if (msg instanceof BytesMessage) { if (builder instanceof DataSourceMessageBuilder) { documentElement = ((DataSourceMessageBuilder) builder) .processDocument( new BytesMessageDataSource((BytesMessage) msg), contentType, axis2MsgCtx); } else { documentElement = builder.processDocument( new BytesMessageInputStream((BytesMessage) msg), contentType, axis2MsgCtx); } } else if (msg instanceof MapMessage) { documentElement = convertJMSMapToXML((MapMessage) msg); } // Inject the message to the sequence. msgCtx.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement)); if (injectingSeq == null || injectingSeq.equals("")) { log.error("Sequence name not specified. Sequence : " + injectingSeq); return false; } SequenceMediator seq = (SequenceMediator) synapseEnvironment.getSynapseConfiguration().getSequence(injectingSeq); if (seq != null) { if (log.isDebugEnabled()) { log.debug("injecting message to sequence : " + injectingSeq); } seq.setErrorHandler(onErrorSeq); if (!synapseEnvironment.injectInbound(msgCtx, seq, sequential)) { return false; } } else { log.error("Sequence: " + injectingSeq + " not found"); } Object o = msgCtx.getProperty(JMSConstants.SET_ROLLBACK_ONLY); if (o != null) { if ((o instanceof Boolean && ((Boolean) o)) || (o instanceof String && Boolean.valueOf((String) o))) { return false; } } } catch (SynapseException se) { throw se; } catch (Exception e) { log.error("Error while processing the JMS Message", e); throw new SynapseException("Error while processing the JMS Message", e); } return true; }
private void send(MessageContext msgctx) throws AxisFault { // create the responseMessageContext and set that its related to the current outgoing // message, so that it could be tied back to the original request even if the response // envelope does not contain addressing headers MessageContext responseMessageContext = new MessageContext(); responseMessageContext.setMessageID(msgctx.getMessageID()); responseMessageContext.setProperty( SynapseConstants.RELATES_TO_FOR_POX, msgctx.getMessageID()); responseMessageContext.setOptions(options); responseMessageContext.setServerSide(true); addMessageContext(responseMessageContext); responseMessageContext.setProperty("synapse.send", "true"); AxisEngine.send(msgctx); // did the engine receive a immediate synchronous response? // e.g. sometimes the transport sender may listen for a syncronous reply if (msgctx.getProperty(MessageContext.TRANSPORT_IN) != null) { responseMessageContext.setOperationContext(msgctx.getOperationContext()); responseMessageContext.setAxisMessage( msgctx .getOperationContext() .getAxisOperation() .getMessage(WSDLConstants.MESSAGE_LABEL_IN_VALUE)); responseMessageContext.setAxisService(msgctx.getAxisService()); responseMessageContext.setProperty( MessageContext.TRANSPORT_OUT, msgctx.getProperty(MessageContext.TRANSPORT_OUT)); responseMessageContext.setProperty( org.apache.axis2.Constants.OUT_TRANSPORT_INFO, msgctx.getProperty(org.apache.axis2.Constants.OUT_TRANSPORT_INFO)); responseMessageContext.setProperty( org.apache.synapse.SynapseConstants.ISRESPONSE_PROPERTY, Boolean.TRUE); responseMessageContext.setTransportIn(msgctx.getTransportIn()); responseMessageContext.setTransportOut(msgctx.getTransportOut()); // If request is REST assume that the responseMessageContext is REST too responseMessageContext.setDoingREST(msgctx.isDoingREST()); responseMessageContext.setProperty( MessageContext.TRANSPORT_IN, msgctx.getProperty(MessageContext.TRANSPORT_IN)); responseMessageContext.setTransportIn(msgctx.getTransportIn()); responseMessageContext.setTransportOut(msgctx.getTransportOut()); // Options object reused above so soapAction needs to be removed so // that soapAction+wsa:Action on response don't conflict responseMessageContext.setSoapAction(""); if (responseMessageContext.getEnvelope() == null) { // If request is REST we assume the responseMessageContext is // REST, so set the variable Options options = responseMessageContext.getOptions(); if (options != null) { RelatesTo relatesTo = options.getRelatesTo(); if (relatesTo != null) { relatesTo.setValue(msgctx.getMessageID()); } else { options.addRelatesTo(new RelatesTo(msgctx.getMessageID())); } } SOAPEnvelope resenvelope = TransportUtils.createSOAPMessage(responseMessageContext); if (resenvelope != null) { responseMessageContext.setEnvelope(resenvelope); AxisEngine.receive(responseMessageContext); if (responseMessageContext.getReplyTo() != null) { sc.setTargetEPR(responseMessageContext.getReplyTo()); } complete(msgctx); } else { throw new AxisFault(Messages.getMessage("blockingInvocationExpectsResponse")); } } } }