Save This Page
Home » synapse-1.2-src » org.apache.synapse.format » [javadoc | source]
    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.format;
   21   
   22   import org.apache.axis2.transport.http.util.URLTemplatingUtil;
   23   import org.apache.axis2.context.MessageContext;
   24   import org.apache.axis2.AxisFault;
   25   import org.apache.axiom.om.OMOutputFormat;
   26   import org.apache.axiom.om.OMElement;
   27   import org.apache.synapse.transport.base.BaseConstants;
   28   
   29   import java.io.ByteArrayOutputStream;
   30   import java.io.OutputStream;
   31   import java.io.IOException;
   32   import java.io.OutputStreamWriter;
   33   import java.io.Writer;
   34   import java.net.URL;
   35   
   36   import javax.activation.DataSource;
   37   import javax.xml.stream.XMLStreamException;
   38   import javax.xml.stream.XMLStreamReader;
   39   
   40   public class PlainTextFormatter implements MessageFormatterEx {
   41   
   42       public byte[] getBytes(MessageContext messageContext, OMOutputFormat format) throws AxisFault {
   43       	ByteArrayOutputStream baos = new ByteArrayOutputStream();
   44       	writeTo(messageContext, format, baos, true);
   45       	return baos.toByteArray();
   46       }
   47   
   48       public void writeTo(MessageContext messageContext, OMOutputFormat format, OutputStream outputStream, boolean preserve) throws AxisFault {
   49           OMElement textElt = messageContext.getEnvelope().getBody().getFirstElement();
   50           if (BaseConstants.DEFAULT_TEXT_WRAPPER.equals(textElt.getQName())) {
   51   	        try {
   52   		        Writer out = new OutputStreamWriter(outputStream, format.getCharSetEncoding());
   53                   XMLStreamReader reader = preserve ? textElt.getXMLStreamReader() : textElt.getXMLStreamReaderWithoutCaching();
   54                   while (reader.hasNext()) {
   55                   	int eventType = reader.next();
   56                       if (eventType == XMLStreamReader.CHARACTERS || eventType == XMLStreamReader.CDATA) {
   57                           out.write(reader.getText());
   58                       }
   59                   }
   60                   out.flush();
   61   	        } catch (IOException e) {
   62   	            throw new AxisFault("Error writing text message to stream", e);
   63   	        } catch (XMLStreamException e) {
   64   				throw new AxisFault("Error extracting the text payload from the message", e);
   65   			}
   66           }
   67       }
   68   
   69       public String getContentType(MessageContext messageContext, OMOutputFormat format, String soapAction) {
   70           String encoding = format.getCharSetEncoding();
   71           String contentType = "text/plain";
   72   
   73           if (encoding != null) {
   74               contentType += "; charset=" + encoding;
   75           }
   76   
   77           // if soap action is there (can be there is soap response MEP is used) add it.
   78           if ((soapAction != null)
   79                   && !"".equals(soapAction.trim())
   80                   && !"\"\"".equals(soapAction.trim())) {
   81               contentType = contentType + ";action=\"" + soapAction + "\";";
   82           }
   83   
   84           return contentType;
   85       }
   86   
   87       public URL getTargetAddress(MessageContext msgCtxt, OMOutputFormat format, URL targetURL) throws AxisFault {
   88           // Check whether there is a template in the URL, if so we have to replace then with data
   89           // values and create a new target URL.
   90           targetURL = URLTemplatingUtil.getTemplatedURL(targetURL, msgCtxt, false);
   91           return targetURL;
   92       }
   93   
   94       public String formatSOAPAction(MessageContext messageContext, OMOutputFormat format, String soapAction) {
   95           return null;
   96       }
   97   
   98       public DataSource getDataSource(MessageContext messageContext,
   99               OMOutputFormat format, String soapAction) throws AxisFault {
  100           return new TextFromElementDataSource(
  101                   messageContext.getEnvelope().getBody().getFirstElement(),
  102                   format.getCharSetEncoding(),
  103                   getContentType(messageContext, format, soapAction));
  104       }
  105   }

Save This Page
Home » synapse-1.2-src » org.apache.synapse.format » [javadoc | source]