Author: antelder
Date: Sat Dec  2 10:49:15 2006
New Revision: 481601

URL: http://svn.apache.org/viewvc?view=rev&rev=481601
Log:
SYNAPSE-45, apply patch from Rajith for a new attachments mediator

Added:
    
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/
    
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
   (with props)
    
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
   (with props)
    
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
   (with props)
Modified:
    
incubator/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory

Added: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java?view=auto&rev=481601
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
 (added)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
 Sat Dec  2 10:49:15 2006
@@ -0,0 +1,88 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.synapse.mediators.attachment;
+
+import java.util.List;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMText;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.axiom.soap.SOAPBody;
+import org.apache.axiom.soap.SOAPFactory;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.jaxen.JaxenException;
+
+public class AttachmentConverter {
+
+       private static final Log log = 
LogFactory.getLog(AttachmentConverter.class);
+       private static SOAPFactory factory = 
OMAbstractFactory.getSOAP11Factory();;
+       
+       /**
+        * If MTOM is set to false in axis2.xml this method is really not need
+        * However if MTOM is ebabled then this method should help 
+        */
+       public static void convertMTOM_TO_Base64(AXIOMXPath xpath, String 
mimeType, MessageContext synCtx) throws SynapseException{
+               
+               log.debug("Converting MTOM to Base64");
+               
+               // This should do the trick, however currently it has no effect
+               // we need to manually turn off MTOM in axis2.xml
+               synCtx.setDoingMTOM(false);                             
+       }
+
+       /**
+        * Even if MTOM is enabled we need to specify that the content needs to 
be optimized 
+        */
+       public static void convertBase64_TO_MTOM(AXIOMXPath xpath, String 
mimeType, MessageContext synCtx) throws SynapseException{
+               log.debug("Converting Base64 to MTOM");
+               
+               SOAPBody soapBody = synCtx.getEnvelope().getBody();
+               //OMElementUtils.addNameSpaces(xpath, 
soapBody.getFirstElement(), log);         
+               OMElement attachmentNode = getMatchingElement(xpath, soapBody);
+               
+               OMText binaryNode = 
factory.createOMText(attachmentNode.getText(),mimeType,true);
+               attachmentNode.addChild(binaryNode);            
+       }
+       
+       
+       private static OMElement getMatchingElement(AXIOMXPath xpath, SOAPBody 
soapBody){
+               try {                   
+            Object result = xpath.evaluate(soapBody);
+            
+            if(result instanceof OMElement){
+                 return (OMElement)result;
+            } else if (result instanceof List && !((List) result).isEmpty()) {
+               return (OMElement) ((List) result).get(0);  // Always fetches 
*only* the first
+            } else{
+                throw new SynapseException("Error in XPath expression, no 
matching element found");
+            }
+
+        } catch (JaxenException je) {
+               String error = "Evaluation of the XPath expression " + 
xpath.toString() +
+                " resulted in an error";
+            log.error(error,je);
+            
+            throw new SynapseException(error,je);              
+        }
+       }
+}

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentConverter.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java?view=auto&rev=481601
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
 (added)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
 Sat Dec  2 10:49:15 2006
@@ -0,0 +1,86 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.synapse.mediators.attachment;
+
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.mediators.AbstractMediator;
+
+public class AttachmentMediator extends AbstractMediator {
+
+       private static final Log log = 
LogFactory.getLog(AttachmentMediator.class);
+       
+       public static final int BASE64_TO_MTOM = 0;
+       public static final int MTOM_TO_BASE64 = 1;
+       public static final int SwA_TO_BASE64 = 2;
+       public static final int BASE64_TO_SwA = 3;
+       public static final int SwA_TO_MTOM = 4;
+       public static final int MTOM_TO_SwA = 5;
+               
+       private AXIOMXPath attachmentPath;
+       private int mode = 0;
+       private String mimeType;
+
+       public boolean mediate(MessageContext synCtx) {                 
+               
+               log.debug("Attachment Mediator, ready to mediate");
+               
+               switch(mode) {
+               
+                       case BASE64_TO_MTOM :
+                               
AttachmentConverter.convertBase64_TO_MTOM(attachmentPath, mimeType, synCtx);
+                               break;
+                       case MTOM_TO_BASE64 :
+                               
AttachmentConverter.convertMTOM_TO_Base64(attachmentPath, mimeType, synCtx);
+                               break;  
+                       default :
+                               throw new SynapseException("Invalid Attachment 
Type Convertion");
+               }       
+               return true;
+       }
+       
+       
+       public AXIOMXPath getAttachmentPath() {
+               return attachmentPath;
+       }
+
+       public void setAttachmentPath(AXIOMXPath attachmentPath) {
+               this.attachmentPath = attachmentPath;
+       }
+
+       public String getMimeType() {
+               return mimeType;
+       }
+
+       public void setMimeType(String mimeType) {
+               this.mimeType = mimeType;
+       }
+
+       public int getMode() {
+               return mode;
+       }
+
+       public void setMode(int mode) {
+               this.mode = mode;
+       }
+
+}

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediator.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java?view=auto&rev=481601
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
 (added)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
 Sat Dec  2 10:49:15 2006
@@ -0,0 +1,84 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.synapse.mediators.attachment;
+
+import javax.xml.namespace.QName;
+
+import org.apache.axiom.om.OMAttribute;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.xpath.AXIOMXPath;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.config.xml.Constants;
+import org.apache.synapse.config.xml.MediatorFactory;
+import org.apache.synapse.config.xml.OMElementUtils;
+import org.jaxen.JaxenException;
+
+public class AttachmentMediatorFactory implements MediatorFactory {
+
+       private static final Log log = 
LogFactory.getLog(AttachmentMediatorFactory.class);
+    private static final QName TAG_NAME    = new 
QName(Constants.SYNAPSE_NAMESPACE, "attachments");
+       
+       public Mediator createMediator(OMElement elem) {        
+               AttachmentMediator mediator = new AttachmentMediator();
+               
+               OMAttribute attMode   = elem.getAttribute(new 
QName(Constants.NULL_NAMESPACE, "mode"));        
+        OMAttribute attMimeType = elem.getAttribute(new 
QName(Constants.NULL_NAMESPACE, "mimeType"));
+        OMElement omPath = elem.getFirstChildWithName(new 
QName(Constants.NULL_NAMESPACE, "attachmentPath"));
+        OMElement omNS = elem.getFirstChildWithName(new 
QName(Constants.NULL_NAMESPACE, "attachmentNS"));       
+        
+        if(attMode != null && 
attMode.getAttributeValue().equals("MTOM_TO_BASE64")){
+               mediator.setMode(AttachmentMediator.MTOM_TO_BASE64);
+        }
+        
+        if (attMimeType != null){
+               mediator.setMimeType(attMimeType.getAttributeValue());          
+        }
+        
+        if (omPath == null){
+               throw new SynapseException("Please specify the XPath to the 
base64 element");
+        }else{
+               AXIOMXPath xp;
+                       try {
+                               xp = new AXIOMXPath(omPath.getText());
+                               OMElementUtils.addNameSpaces(xp, elem, log);
+                               
+                               if(omNS != null){
+                                       String prefix = 
omNS.getAttributeValue(new QName(Constants.NULL_NAMESPACE, "prefix"));
+                                       String name = 
omNS.getAttributeValue(new QName(Constants.NULL_NAMESPACE, "name"));
+                                       xp.addNamespace(prefix,name);           
+                               }
+                               
+                               mediator.setAttachmentPath(xp);
+                       } catch (JaxenException e) {
+                                log.error("Error creating XPath for Base64 
element", e);
+                            throw new SynapseException("Error creating XPath 
for Base64 element", e);
+                       }               
+        }
+        
+               return mediator;
+       }
+
+       public QName getTagQName() {
+               return TAG_NAME;
+       }
+
+}

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
incubator/synapse/trunk/java/modules/extensions/src/main/java/org/apache/synapse/mediators/attachment/AttachmentMediatorFactory.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: 
incubator/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
URL: 
http://svn.apache.org/viewvc/incubator/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory?view=diff&rev=481601&r1=481600&r2=481601
==============================================================================
--- 
incubator/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
 (original)
+++ 
incubator/synapse/trunk/java/modules/extensions/src/main/resources/META-INF/services/org.apache.synapse.config.xml.MediatorFactory
 Sat Dec  2 10:49:15 2006
@@ -3,3 +3,4 @@
 org.apache.synapse.mediators.spring.SpringMediatorFactory
 org.apache.synapse.mediators.json.JsonMediatorFactory
 org.apache.synapse.mediators.bsf.ScriptMediatorFactory
+org.apache.synapse.mediators.attachment.AttachmentMediatorFactory



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to