use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.
the class JMSUtils method setSOAPEnvelope.
/**
* Set the SOAPEnvelope to the Axis2 MessageContext, from the JMS Message passed in
* @param message the JMS message read
* @param msgContext the Axis2 MessageContext to be populated
* @param contentType content type for the message
* @throws AxisFault
* @throws JMSException
*/
public static void setSOAPEnvelope(Message message, MessageContext msgContext, String contentType) throws AxisFault, JMSException {
if (contentType == null) {
if (message instanceof TextMessage) {
contentType = "text/plain";
} else {
contentType = "application/octet-stream";
}
if (log.isDebugEnabled()) {
log.debug("No content type specified; assuming " + contentType);
}
}
int index = contentType.indexOf(';');
String type = index > 0 ? contentType.substring(0, index) : contentType;
Builder 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();
}
OMElement documentElement;
if (message instanceof BytesMessage) {
// Extract the charset encoding from the 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 (builder instanceof DataSourceMessageBuilder) {
documentElement = ((DataSourceMessageBuilder) builder).processDocument(new BytesMessageDataSource((BytesMessage) message), contentType, msgContext);
} else {
documentElement = builder.processDocument(new BytesMessageInputStream((BytesMessage) message), contentType, msgContext);
}
} else if (message instanceof TextMessage) {
TextMessageBuilder textMessageBuilder;
if (builder instanceof TextMessageBuilder) {
textMessageBuilder = (TextMessageBuilder) builder;
} else {
textMessageBuilder = new TextMessageBuilderAdapter(builder);
}
String content = ((TextMessage) message).getText();
documentElement = textMessageBuilder.processDocument(content, contentType, msgContext);
} else if (message instanceof MapMessage) {
documentElement = convertJMSMapToXML((MapMessage) message);
} else {
handleException("Unsupported JMS message type " + message.getClass().getName());
// Make compiler happy
return;
}
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
use of org.apache.axis2.builder.SOAPBuilder in project wso2-synapse by wso2.
the class VFSTransportListener method processFile.
/**
* 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();
String fileURI = file.getName().getURI();
metrics.incrementBytesReceived(content.getSize());
Map<String, Object> transportHeaders = new HashMap<String, Object>();
transportHeaders.put(VFSConstants.FILE_PATH, filePath);
transportHeaders.put(VFSConstants.FILE_NAME, fileName);
transportHeaders.put(VFSConstants.FILE_URI, fileURI);
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) {
if (log.isDebugEnabled()) {
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, // * SOAP Action - not applicable *//
null, contentType);
} finally {
if (dataSource != null) {
dataSource.destroy();
}
}
if (log.isDebugEnabled()) {
log.debug("Processed file : " + VFSUtils.maskURLPassword(file.toString()) + " of Content-type : " + contentType);
}
} catch (FileSystemException e) {
handleException("Error reading file content or attributes : " + VFSUtils.maskURLPassword(file.toString()), e);
} finally {
try {
if (file != null) {
if (fsManager != null && file.getName() != null && file.getName().getScheme() != null && file.getName().getScheme().startsWith("file")) {
fsManager.closeFileSystem(file.getParent().getFileSystem());
}
file.close();
}
} catch (FileSystemException warn) {
// ignore the warning, since we handed over the stream close job to
// AutocloseInputstream..
}
}
}
use of org.apache.axis2.builder.SOAPBuilder in project wso2-synapse by wso2.
the class DeferredMessageBuilder method getDocument.
public OMElement getDocument(MessageContext msgCtx, InputStream in) throws XMLStreamException, IOException {
/**
* HTTP Delete requests may contain entity body or not. Hence if the request is a HTTP DELETE, we have to verify
* that the payload stream is empty or not.
*/
if (HTTPConstants.HEADER_DELETE.equals(msgCtx.getProperty(Constants.Configuration.HTTP_METHOD)) && RelayUtils.isEmptyPayloadStream(in)) {
msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, Boolean.TRUE);
return TransportUtils.createSOAPEnvelope(null);
}
String contentType = (String) msgCtx.getProperty(Constants.Configuration.CONTENT_TYPE);
String _contentType = getContentType(contentType, msgCtx);
in = HTTPTransportUtils.handleGZip(msgCtx, in);
AxisConfiguration configuration = msgCtx.getConfigurationContext().getAxisConfiguration();
Parameter useFallbackParameter = configuration.getParameter(Constants.Configuration.USE_DEFAULT_FALLBACK_BUILDER);
boolean useFallbackBuilder = false;
if (useFallbackParameter != null) {
useFallbackBuilder = JavaUtils.isTrueExplicitly(useFallbackParameter.getValue(), useFallbackBuilder);
}
Map transportHeaders = (Map) msgCtx.getProperty(MessageContext.TRANSPORT_HEADERS);
String contentLength = null;
String trasferEncoded = null;
if (transportHeaders != null) {
contentLength = (String) transportHeaders.get(HTTP.CONTENT_LEN);
trasferEncoded = (String) transportHeaders.get(HTTP.TRANSFER_ENCODING);
if (contentType.equals(PassThroughConstants.DEFAULT_CONTENT_TYPE) && (contentLength == null || Integer.valueOf(contentLength) == 0) && trasferEncoded == null) {
msgCtx.setProperty(PassThroughConstants.NO_ENTITY_BODY, true);
msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, "");
msgCtx.setProperty(PassThroughConstants.RELAY_EARLY_BUILD, true);
return new SOAP11Factory().getDefaultEnvelope();
}
}
OMElement element = null;
Builder builder;
if (contentType != null) {
// loading builder from externally..
// builder = configuration.getMessageBuilder(_contentType,useFallbackBuilder);
builder = MessageProcessorSelector.getMessageBuilder(_contentType, msgCtx);
if (builder != null) {
try {
if (contentLength != null && "0".equals(contentLength)) {
element = new org.apache.axiom.soap.impl.llom.soap11.SOAP11Factory().getDefaultEnvelope();
// since we are setting an empty envelop to achieve the empty body, we have to set a different
// content-type other than text/xml, application/soap+xml or any other content-type which will
// invoke the soap builder, otherwise soap builder will get hit and an empty envelope
// will be send out
msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
} else {
element = builder.processDocument(in, contentType, msgCtx);
}
} catch (AxisFault axisFault) {
log.error("Error building message", axisFault);
throw axisFault;
}
}
}
if (element == null) {
if (msgCtx.isDoingREST()) {
try {
element = BuilderUtil.getPOXBuilder(in, null).getDocumentElement();
} catch (XMLStreamException e) {
log.error("Error building message using POX Builder", e);
throw e;
}
} else {
// switch to default
builder = new SOAPBuilder();
try {
if (contentLength != null && "0".equals(contentLength)) {
element = new SOAP11Factory().getDefaultEnvelope();
// since we are setting an empty envelop to achieve the empty body, we have to set a different
// content-type other than text/xml, application/soap+xml or any other content-type which will
// invoke the soap builder, otherwise soap builder will get hit and an empty envelope
// will be send out
msgCtx.setProperty(Constants.Configuration.MESSAGE_TYPE, "application/xml");
} else {
element = builder.processDocument(in, contentType, msgCtx);
}
} catch (AxisFault axisFault) {
log.error("Error building message using SOAP builder");
throw axisFault;
}
}
}
// build the soap headers and body
if (element instanceof SOAPEnvelope) {
SOAPEnvelope env = (SOAPEnvelope) element;
env.hasFault();
}
// setting up original contentType (resetting the content type)
if (contentType != null && !contentType.isEmpty()) {
msgCtx.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
}
return element;
}
use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.
the class MqttUtils method invoke.
public static void invoke(MqttMessage mqttMessage, MessageContext msgContext, String contentType) throws AxisFault, AxisMqttException {
if (contentType == null) {
contentType = "text/plain";
}
Builder builder = BuilderUtil.getBuilderFromSelector(contentType, msgContext);
if (builder == null) {
if (log.isDebugEnabled()) {
log.debug("No message builder found for type '" + contentType + "'. Falling back to SOAP.");
}
builder = new SOAPBuilder();
}
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, "UTF-8");
OMElement documentElement = null;
byte[] bytes = mqttMessage.getPayload();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
documentElement = builder.processDocument(byteArrayInputStream, contentType, msgContext);
msgContext.setEnvelope(TransportUtils.createSOAPEnvelope(documentElement));
}
use of org.apache.axis2.builder.SOAPBuilder in project wso2-axis2-transports by wso2.
the class MailTransportListener method processMail.
/**
* Process a mail message through Axis2
*
* @param message the email message
* @param entry the poll table entry
* @throws MessagingException on error
* @throws IOException on error
*/
private void processMail(Message message, PollTableEntry entry) throws MessagingException, IOException {
updateMetrics(message);
// populate transport headers using the mail headers
Map trpHeaders = getTransportHeaders(message, entry);
// Allow the content type to be overridden by configuration.
String contentType = entry.getContentType();
Part messagePart;
if (contentType != null) {
messagePart = message;
} else {
messagePart = getMessagePart(message, cfgCtx.getAxisConfiguration());
contentType = messagePart.getContentType();
}
// FIXME: remove this ugly hack when Axis2 has learned that content types are case insensitive...
int idx = contentType.indexOf(';');
if (idx == -1) {
contentType = contentType.toLowerCase();
} else {
contentType = contentType.substring(0, idx).toLowerCase() + contentType.substring(idx);
}
// if the content type was not found, we have an error
if (contentType == null) {
processFailure("Unable to determine Content-type for message : " + message.getMessageNumber() + " :: " + message.getSubject(), null, entry);
return;
} else if (log.isDebugEnabled()) {
log.debug("Processing message as Content-Type : " + contentType);
}
MessageContext msgContext = entry.createMessageContext();
// 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;
try {
charSetEnc = new ContentType(contentType).getParameter("charset");
} catch (ParseException ex) {
// ignore
charSetEnc = null;
}
msgContext.setProperty(Constants.Configuration.CHARACTER_SET_ENCODING, charSetEnc);
MailOutTransportInfo outInfo = buildOutTransportInfo(message, entry);
// save out transport information
msgContext.setProperty(Constants.OUT_TRANSPORT_INFO, outInfo);
// this property only useful for supporting smtp with Sandesha2.
msgContext.setProperty(RequestResponseTransport.TRANSPORT_CONTROL, new MailRequestResponseTransport());
// set message context From
if (outInfo.getFromAddress() != null) {
msgContext.setFrom(new EndpointReference(MailConstants.TRANSPORT_PREFIX + outInfo.getFromAddress().getAddress()));
}
// save original mail message id message context MessageID
msgContext.setMessageID(outInfo.getRequestMessageID());
// set the message payload to the message context
InputStream in = messagePart.getInputStream();
try {
try {
msgContext.setEnvelope(TransportUtils.createSOAPMessage(msgContext, in, contentType));
} catch (XMLStreamException ex) {
handleException("Error parsing message", ex);
}
String soapAction = (String) trpHeaders.get(BaseConstants.SOAPACTION);
if (soapAction == null && message.getSubject() != null && message.getSubject().startsWith(BaseConstants.SOAPACTION)) {
soapAction = message.getSubject().substring(BaseConstants.SOAPACTION.length());
if (soapAction.startsWith(":")) {
soapAction = soapAction.substring(1).trim();
}
}
handleIncomingMessage(msgContext, trpHeaders, soapAction, contentType);
} finally {
in.close();
}
if (log.isDebugEnabled()) {
log.debug("Processed message : " + message.getMessageNumber() + " :: " + message.getSubject());
}
}
Aggregations