Save This Page
Home » axis2-1.5-src » org.apache » axis2 » util » [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.axis2.util;
   21   
   22   import java.io.ByteArrayInputStream;
   23   import java.io.ByteArrayOutputStream;
   24   import java.io.InputStream;
   25   import java.util.Iterator;
   26   import java.util.List;
   27   
   28   import javax.xml.namespace.QName;
   29   import javax.xml.stream.FactoryConfigurationError;
   30   import javax.xml.stream.XMLOutputFactory;
   31   import javax.xml.stream.XMLStreamException;
   32   import javax.xml.stream.XMLStreamWriter;
   33   import javax.xml.transform.Transformer;
   34   
   35   import org.apache.axiom.om.OMAbstractFactory;
   36   import org.apache.axiom.om.OMElement;
   37   import org.apache.axiom.om.OMFactory;
   38   import org.apache.axiom.om.util.UUIDGenerator;
   39   import org.apache.axis2.description.AxisDescription;
   40   import org.apache.axis2.description.AxisMessage;
   41   import org.apache.axis2.description.AxisOperation;
   42   import org.apache.axis2.description.AxisService;
   43   import org.apache.axis2.description.PolicyInclude;
   44   import org.apache.neethi.Constants;
   45   import org.apache.neethi.Policy;
   46   import org.apache.neethi.PolicyComponent;
   47   import org.apache.neethi.PolicyEngine;
   48   import org.apache.neethi.PolicyReference;
   49   
   50   import com.ibm.wsdl.util.xml.DOM2Writer;
   51   
   52   public class PolicyUtil {
   53   
   54   	public static String getSafeString(String unsafeString) {
   55   		StringBuffer sbuf = new StringBuffer();
   56   
   57   		char[] chars = unsafeString.toCharArray();
   58   
   59   		for (int i = 0; i < chars.length; i++) {
   60   			char c = chars[i];
   61   
   62   			switch (c) {
   63   			case '\\':
   64   				sbuf.append('\\');
   65   				sbuf.append('\\');
   66   				break;
   67   			case '"':
   68   				sbuf.append('\\');
   69   				sbuf.append('"');
   70   				break;
   71   			case '\n':
   72   				sbuf.append('\\');
   73   				sbuf.append('n');
   74   				break;
   75   			case '\r':
   76   				sbuf.append('\\');
   77   				sbuf.append('r');
   78   				break;
   79   			default:
   80   				sbuf.append(c);
   81   			}
   82   		}
   83   
   84   		return sbuf.toString();
   85   	}
   86   
   87   	public static PolicyReference createPolicyReference(Policy policy) {
   88   		PolicyReference policyReference = new PolicyReference();
   89   		String key = policy.getName();
   90   		if (key == null) {
   91   			key = policy.getId();
   92   			if (key == null) {
   93   				key = UUIDGenerator.getUUID();
   94   				policy.setId(key);
   95   			}
   96   			policyReference.setURI("#" + key);
   97   		} else {
   98   			policyReference.setURI(key);
   99   		}
  100   		return policyReference;
  101   	}
  102   
  103   	public static OMElement getPolicyComponentAsOMElement(
  104   			PolicyComponent policyComponent,
  105   			ExternalPolicySerializer externalPolicySerializer)
  106   			throws XMLStreamException, FactoryConfigurationError {
  107   
  108   		if (policyComponent instanceof Policy) {
  109   			ByteArrayOutputStream baos = new ByteArrayOutputStream();
  110   			externalPolicySerializer.serialize((Policy) policyComponent, baos);
  111   			ByteArrayInputStream bais = new ByteArrayInputStream(baos
  112   					.toByteArray());
  113   			return (OMElement) XMLUtils.toOM(bais);
  114   
  115   		} else {
  116   			OMFactory fac = OMAbstractFactory.getOMFactory();
  117   			OMElement elem = fac.createOMElement(Constants.ELEM_POLICY_REF,
  118   					Constants.URI_POLICY_NS, Constants.ATTR_WSP);
  119   			elem.addAttribute(Constants.ATTR_URI,
  120   					((PolicyReference) policyComponent).getURI(), null);
  121   			return elem;
  122   		}
  123   	}
  124   
  125   	public static OMElement getPolicyComponentAsOMElement(
  126   			PolicyComponent component) throws XMLStreamException,
  127   			FactoryConfigurationError {
  128   
  129   		ByteArrayOutputStream baos = new ByteArrayOutputStream();
  130   		XMLStreamWriter writer = XMLOutputFactory.newInstance()
  131   				.createXMLStreamWriter(baos);
  132   
  133   		component.serialize(writer);
  134   		writer.flush();
  135   
  136   		ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
  137   		return (OMElement) XMLUtils.toOM(bais);
  138   	}
  139   
  140   	public static PolicyComponent getPolicyComponentFromOMElement(
  141   			OMElement policyComponent) throws IllegalArgumentException {
  142   
  143   		if (Constants.Q_ELEM_POLICY.equals(policyComponent.getQName())) {
  144   			return PolicyEngine.getPolicy(policyComponent);
  145   
  146   		} else if (policyComponent.getQName().equals(
  147   				new QName(Constants.URI_POLICY_NS, Constants.ELEM_POLICY_REF))) {
  148   			return PolicyEngine.getPolicyReference(policyComponent);
  149   
  150   		} else {
  151   			throw new IllegalArgumentException(
  152   					"Agrument is neither a <wsp:Policy> nor a <wsp:PolicyReference> element");
  153   		}
  154   	}
  155   
  156   	public static Policy getPolicyFromOMElement(OMElement policyElement) {
  157   		if (Constants.Q_ELEM_POLICY.equals(policyElement.getQName())) {
  158   			return PolicyEngine.getPolicy(policyElement);
  159   		} else {
  160   			throw new IllegalArgumentException(
  161   					"argument is not a <wsp:Policy ..> element");
  162   		}
  163   	}
  164   
  165   	public static PolicyReference getPolicyReferenceFromOMElement(
  166   			OMElement policyRefElement) {
  167   		if (Constants.URI_POLICY_NS.equals(policyRefElement.getNamespace()
  168   				.getNamespaceURI())
  169   				&& Constants.ELEM_POLICY_REF.equals(policyRefElement
  170   						.getLocalName())) {
  171   			return PolicyEngine.getPolicyReference(policyRefElement);
  172   		} else {
  173   			throw new IllegalArgumentException(
  174   					"argument is not a <wsp:PolicyReference> element");
  175   		}
  176   	}
  177   
  178   	public static PolicyComponent getPolicyComponent(org.w3c.dom.Element element) {
  179   		if (Constants.URI_POLICY_NS.equals(element.getNamespaceURI())) {
  180   
  181   			if (Constants.ELEM_POLICY.equals(element.getLocalName())) {
  182   				return PolicyEngine.getPolicy(nodeToStream(element));
  183   
  184   			} else if (Constants.ELEM_POLICY_REF.equals(element.getLocalName())) {
  185   				return PolicyEngine.getPolicyReferene(nodeToStream(element));
  186   			}
  187   		}
  188   		throw new IllegalArgumentException(
  189   				"Agrument is neither a <wsp:Policy> nor a <wsp:PolicyReference> element");
  190   	}
  191   
  192   	private static InputStream nodeToStream(org.w3c.dom.Element element) {
  193   		ByteArrayOutputStream baos = new ByteArrayOutputStream();
  194   		Transformer tf;
  195   		try {
  196   			// tf = TransformerFactory.newInstance().newTransformer();
  197   			// tf.transform(new DOMSource(element), new StreamResult(baos));
  198   			String nodeString = DOM2Writer.nodeToString(element);
  199   			return new ByteArrayInputStream(nodeString.getBytes());
  200   		} catch (Exception e) {
  201   			throw new RuntimeException("Unable to process policy");
  202   		}
  203   	}
  204   
  205   	public static String policyComponentToString(PolicyComponent policyComponent)
  206   			throws XMLStreamException, FactoryConfigurationError {
  207   
  208   		ByteArrayOutputStream baos = new ByteArrayOutputStream();
  209   		XMLStreamWriter writer = XMLOutputFactory.newInstance()
  210   				.createXMLStreamWriter(baos);
  211   
  212   		policyComponent.serialize(writer);
  213   		writer.flush();
  214   
  215   		return baos.toString();
  216   	}
  217   
  218   	public static String generateId(AxisDescription description) {
  219   		PolicyInclude policyInclude = description.getPolicyInclude();
  220   		String identifier = "-policy-1";
  221   
  222   		if (description instanceof AxisMessage) {
  223   			identifier = "msg-" + ((AxisMessage) description).getName()
  224   					+ identifier;
  225   			description = description.getParent();
  226   		}
  227   
  228   		if (description instanceof AxisOperation) {
  229   			identifier = "op-" + ((AxisOperation) description).getName()
  230   					+ identifier;
  231   			description = description.getParent();
  232   		}
  233   
  234   		if (description instanceof AxisService) {
  235   			identifier = "service-" + ((AxisService) description).getName()
  236   					+ identifier;
  237   		}
  238   
  239   		/*
  240   		 * Int 49 is the value of the Character '1'. Here we want to change '1'
  241   		 * to '2' or '2' to '3' .. etc. to construct a unique identifier.
  242   		 */
  243   		for (int index = 49; policyInclude.getPolicy(identifier) != null; index++) {
  244   			identifier = identifier.replace((char) index, (char) (index + 1));
  245   		}
  246   
  247   		return identifier;
  248   	}
  249   
  250   	public static Policy getMergedPolicy(List policies,
  251   			AxisDescription description) {
  252   
  253   		Policy policy = null;
  254   
  255   		for (Iterator iterator = policies.iterator(); iterator.hasNext();) {
  256   			Object policyElement = iterator.next();
  257   			if (policyElement instanceof Policy) {
  258   				policy = (policy == null) ? (Policy) policyElement
  259   						: (Policy) policy.merge((Policy) policyElement);
  260   
  261   			} else {
  262   				PolicyReference policyReference = (PolicyReference) policyElement;
  263   				Policy policy2 = (Policy) policyReference.normalize(
  264   						new AxisPolicyLocator(description), false);
  265   				policy = (policy == null) ? policy2 : (Policy) policy
  266   						.merge(policy2);
  267   			}
  268   		}
  269   
  270   		if (policy != null) {
  271   			policy = (Policy) policy.normalize(new AxisPolicyLocator(
  272   					description), false);
  273   		}
  274   
  275   		return policy;
  276   	}
  277   
  278   	public static Policy getMergedPolicy(List policies, AxisService service) {
  279   
  280   		Policy policy = null;
  281   
  282   		for (Iterator iterator = policies.iterator(); iterator.hasNext();) {
  283   			Object policyElement = iterator.next();
  284   			if (policyElement instanceof Policy) {
  285   				policy = (policy == null) ? (Policy) policyElement
  286   						: (Policy) policy.merge((Policy) policyElement);
  287   
  288   			} else {
  289   				PolicyReference policyReference = (PolicyReference) policyElement;
  290   				Policy policy2 = (Policy) policyReference.normalize(
  291   						new PolicyLocator(service), false);
  292   				policy = (policy == null) ? policy2 : (Policy) policy
  293   						.merge(policy2);
  294   			}
  295   		}
  296   
  297   		if (policy != null) {
  298   			policy = (Policy) policy.normalize(new PolicyLocator(service),
  299   					false);
  300   		}
  301   
  302   		return policy;
  303   	}
  304   }

Save This Page
Home » axis2-1.5-src » org.apache » axis2 » util » [javadoc | source]