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   package org.apache.synapse.format;
   20   
   21   import java.io.IOException;
   22   import java.io.InputStream;
   23   
   24   import javax.xml.namespace.QName;
   25   
   26   import org.apache.axiom.om.OMAbstractFactory;
   27   import org.apache.axiom.om.OMElement;
   28   import org.apache.axiom.om.OMFactory;
   29   import org.apache.axis2.AxisFault;
   30   import org.apache.axis2.builder.Builder;
   31   import org.apache.axis2.builder.BuilderUtil;
   32   import org.apache.axis2.context.MessageContext;
   33   import org.apache.axis2.description.Parameter;
   34   import org.apache.commons.io.IOUtils;
   35   import org.apache.synapse.transport.base.BaseConstants;
   36   import org.apache.synapse.transport.base.BaseUtils;
   37   
   38   /**
   39    * Message builder for plain text payloads.
   40    * <p>
   41    * This builder processes the input message as plain text and wraps
   42    * the text in a wrapper element. The name of the wrapper element can
   43    * be configured as a service parameter (see {@link BaseConstants#WRAPPER_PARAM}).
   44    * It defaults to {@link BaseConstants#DEFAULT_TEXT_WRAPPER}.
   45    * If the provided content type specifies a <tt>charset</tt> parameter
   46    * (e.g. <tt>text/plain; charset=ISO-8859-15</tt>) it is used to decode the text.
   47    * Otherwise the default charset encoding specified by
   48    * {@link MessageContext#DEFAULT_CHAR_SET_ENCODING} is used.
   49    */
   50   public class PlainTextBuilder implements Builder {
   51       public OMElement processDocument(InputStream inputStream,
   52                                        String contentType,
   53                                        MessageContext msgContext) throws AxisFault {
   54   
   55           QName wrapperQName = BaseConstants.DEFAULT_TEXT_WRAPPER;
   56           if (msgContext.getAxisService() != null) {
   57               Parameter wrapperParam = msgContext.getAxisService().getParameter(BaseConstants.WRAPPER_PARAM);
   58               if (wrapperParam != null) {
   59                   wrapperQName = BaseUtils.getQNameFromString(wrapperParam.getValue());
   60               }
   61           }        
   62   
   63           OMFactory factory = OMAbstractFactory.getOMFactory();
   64           OMElement wrapper = factory.createOMElement(wrapperQName, null);
   65           String charSetEnc = BuilderUtil.getCharSetEncoding(contentType);
   66           String textPayload;
   67           try {
   68               textPayload = IOUtils.toString(inputStream, charSetEnc);
   69           } catch (IOException ex) {
   70               throw new AxisFault("Unable to read message payload", ex);
   71           }
   72           wrapper.addChild(factory.createOMText(textPayload));
   73           return wrapper;
   74       }
   75   }

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