Home » synapse-1.2-src » org.apache.synapse.mediators.throttle » [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.mediators.throttle;
   20   
   21   import org.apache.axiom.om.OMAttribute;
   22   import org.apache.axiom.om.OMElement;
   23   import org.apache.commons.logging.Log;
   24   import org.apache.commons.logging.LogFactory;
   25   import org.apache.synapse.Mediator;
   26   import org.apache.synapse.config.xml.AbstractMediatorFactory;
   27   import org.apache.synapse.config.xml.SequenceMediatorFactory;
   28   import org.apache.synapse.config.xml.XMLConfigConstants;
   29   
   30   import javax.xml.namespace.QName;
   31   
   32   
   33   /**
   34    * The Factory for create throttle mediator- key or InLine XMl need to provide
   35    *
   36    * <pre>
   37    * &lt;throttle [onReject="string"] [onAccept="string"] id="string"&gt;
   38    *   (&lt;policy key="string"/&gt; | &lt;policy&gt;..&lt;/policy&gt;)
   39    *    &lt;onReject&gt;..&lt;/onReject&gt;?
   40    *    &lt;onAccept&gt;..&lt;/onAccept&gt;?
   41    * &lt;/throttle&gt;
   42    * </pre>
   43    */
   44   
   45   public class ThrottleMediatorFactory extends AbstractMediatorFactory {
   46   
   47       private static final Log log = LogFactory.getLog(ThrottleMediatorFactory.class);
   48   
   49       /**
   50        * The Tag Name for throttle
   51        */
   52       private static final QName TAG_NAME
   53               = new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "throttle");
   54   
   55       public Mediator createMediator(OMElement elem) {
   56   
   57           ThrottleMediator throttleMediator = new ThrottleMediator();
   58           OMElement policy = elem.getFirstChildWithName(
   59                   new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, "policy"));
   60           if (policy != null) {
   61               OMAttribute key = policy.getAttribute(new QName(XMLConfigConstants.NULL_NAMESPACE, "key"));
   62               if (key != null) {
   63                   String keyValue = key.getAttributeValue();
   64                   if (keyValue != null && !"".equals(keyValue)) {
   65                       throttleMediator.setPolicyKey(keyValue);
   66                   } else {
   67                       handleException("key attribute should have a value ");
   68                   }
   69               } else {
   70                   OMElement inLine = policy.getFirstElement();
   71                   if (inLine != null) {
   72                       throttleMediator.setInLinePolicy(inLine);
   73                   }
   74               }
   75           }
   76           // after successfully creating the mediator
   77           // set its common attributes such as tracing etc
   78           processTraceState(throttleMediator,elem);
   79   
   80           String id = elem.getAttributeValue(new QName(XMLConfigConstants.NULL_NAMESPACE, "id"));
   81           if (id != null && !"".equals(id)) {
   82               throttleMediator.setId(id.trim());
   83           } else {
   84              handleException("Idy attribute must have defined ");
   85           }
   86   
   87           SequenceMediatorFactory mediatorFactory = new SequenceMediatorFactory();
   88           OMAttribute onReject = elem.getAttribute(
   89                   new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.ONREJECT));
   90           if (onReject != null) {
   91               String onRejectValue = onReject.getAttributeValue();
   92               if (onRejectValue != null) {
   93                   throttleMediator.setOnRejectSeqKey(onRejectValue.trim());
   94               }
   95           } else {
   96               OMElement onRejectMediatorElement = elem.getFirstChildWithName(
   97                       new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ONREJECT));
   98               if (onRejectMediatorElement != null) {
   99                   throttleMediator.setOnRejectMediator(mediatorFactory.createAnonymousSequence(
  100                           onRejectMediatorElement));
  101               }
  102           }
  103           OMAttribute onAccept = elem.getAttribute(
  104                   new QName(XMLConfigConstants.NULL_NAMESPACE, XMLConfigConstants.ONACCEPT));
  105           if (onAccept != null) {
  106               String onAcceptValue = onAccept.getAttributeValue();
  107               if (onAcceptValue != null) {
  108                   throttleMediator.setOnAcceptSeqKey(onAcceptValue);
  109               }
  110           } else {
  111               OMElement onAcceptMediatorElement = elem.getFirstChildWithName(
  112                       new QName(XMLConfigConstants.SYNAPSE_NAMESPACE, XMLConfigConstants.ONACCEPT));
  113               if (onAcceptMediatorElement != null) {
  114                   throttleMediator.setOnAcceptMediator(mediatorFactory.createAnonymousSequence(
  115                           onAcceptMediatorElement));
  116               }
  117           }
  118           return throttleMediator;
  119       }
  120   
  121       public QName getTagQName() {
  122           return TAG_NAME;
  123       }
  124   }

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