001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.util;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axis2.description.AxisDescription;
024: import org.apache.axis2.description.AxisMessage;
025: import org.apache.axis2.description.AxisOperation;
026: import org.apache.axis2.description.AxisService;
027: import org.apache.axis2.description.PolicyInclude;
028: import org.apache.neethi.Constants;
029: import org.apache.neethi.Policy;
030: import org.apache.neethi.PolicyComponent;
031: import org.apache.neethi.PolicyEngine;
032: import org.apache.neethi.PolicyReference;
033:
034: import javax.xml.stream.FactoryConfigurationError;
035: import javax.xml.stream.XMLOutputFactory;
036: import javax.xml.stream.XMLStreamException;
037: import javax.xml.stream.XMLStreamWriter;
038: import javax.xml.transform.Transformer;
039: import javax.xml.transform.TransformerFactory;
040: import javax.xml.transform.dom.DOMSource;
041: import javax.xml.transform.stream.StreamResult;
042: import java.io.ByteArrayInputStream;
043: import java.io.ByteArrayOutputStream;
044: import java.io.InputStream;
045: import java.util.Iterator;
046: import java.util.List;
047:
048: import com.ibm.wsdl.util.xml.DOM2Writer;
049:
050: public class PolicyUtil {
051:
052: public static String getSafeString(String unsafeString) {
053: StringBuffer sbuf = new StringBuffer();
054:
055: char[] chars = unsafeString.toCharArray();
056:
057: for (int i = 0; i < chars.length; i++) {
058: char c = chars[i];
059:
060: switch (c) {
061: case '\\':
062: sbuf.append('\\');
063: sbuf.append('\\');
064: break;
065: case '"':
066: sbuf.append('\\');
067: sbuf.append('"');
068: break;
069: case '\n':
070: sbuf.append('\\');
071: sbuf.append('n');
072: break;
073: case '\r':
074: sbuf.append('\\');
075: sbuf.append('r');
076: break;
077: default:
078: sbuf.append(c);
079: }
080: }
081:
082: return sbuf.toString();
083: }
084:
085: public static OMElement getPolicyComponentAsOMElement(
086: PolicyComponent policyComponent,
087: ExternalPolicySerializer externalPolicySerializer)
088: throws XMLStreamException, FactoryConfigurationError {
089:
090: ByteArrayOutputStream baos = new ByteArrayOutputStream();
091:
092: if (policyComponent instanceof Policy) {
093: externalPolicySerializer.serialize(
094: (Policy) policyComponent, baos);
095:
096: } else {
097: XMLStreamWriter writer = XMLOutputFactory.newInstance()
098: .createXMLStreamWriter(baos);
099: policyComponent.serialize(writer);
100: writer.flush();
101: }
102:
103: ByteArrayInputStream bais = new ByteArrayInputStream(baos
104: .toByteArray());
105: return (OMElement) XMLUtils.toOM(bais);
106:
107: }
108:
109: public static OMElement getPolicyComponentAsOMElement(
110: PolicyComponent component) throws XMLStreamException,
111: FactoryConfigurationError {
112:
113: ByteArrayOutputStream baos = new ByteArrayOutputStream();
114: XMLStreamWriter writer = XMLOutputFactory.newInstance()
115: .createXMLStreamWriter(baos);
116:
117: component.serialize(writer);
118: writer.flush();
119:
120: ByteArrayInputStream bais = new ByteArrayInputStream(baos
121: .toByteArray());
122: return (OMElement) XMLUtils.toOM(bais);
123: }
124:
125: public static PolicyComponent getPolicyComponentFromOMElement(
126: OMElement policyComponent) throws IllegalArgumentException {
127:
128: if (policyComponent instanceof Policy) {
129: return PolicyEngine.getPolicy(policyComponent);
130:
131: } else if (policyComponent instanceof PolicyReference) {
132: return PolicyEngine.getPolicyReference(policyComponent);
133:
134: } else {
135: throw new IllegalArgumentException(
136: "Agrument is neither a <wsp:Policy> nor a <wsp:PolicyReference> element");
137: }
138: }
139:
140: public static PolicyComponent getPolicyComponent(
141: org.w3c.dom.Element element) {
142: if (Constants.URI_POLICY_NS.equals(element.getNamespaceURI())) {
143:
144: if (Constants.ELEM_POLICY.equals(element.getLocalName())) {
145: return PolicyEngine.getPolicy(nodeToStream(element));
146:
147: } else if (Constants.ELEM_POLICY_REF.equals(element
148: .getLocalName())) {
149: return PolicyEngine
150: .getPolicyReferene(nodeToStream(element));
151: }
152: }
153:
154: throw new IllegalArgumentException(
155: "Agrument is neither a <wsp:Policy> nor a <wsp:PolicyReference> element");
156: }
157:
158: private static InputStream nodeToStream(org.w3c.dom.Element element) {
159: ByteArrayOutputStream baos = new ByteArrayOutputStream();
160: Transformer tf;
161: try {
162: // tf = TransformerFactory.newInstance().newTransformer();
163: // tf.transform(new DOMSource(element), new StreamResult(baos));
164: String nodeString = DOM2Writer.nodeToString(element);
165: return new ByteArrayInputStream(nodeString.getBytes());
166: } catch (Exception e) {
167: throw new RuntimeException("Unable to process policy");
168: }
169: }
170:
171: public static String policyComponentToString(
172: PolicyComponent policyComponent) throws XMLStreamException,
173: FactoryConfigurationError {
174:
175: ByteArrayOutputStream baos = new ByteArrayOutputStream();
176: XMLStreamWriter writer = XMLOutputFactory.newInstance()
177: .createXMLStreamWriter(baos);
178:
179: policyComponent.serialize(writer);
180: writer.flush();
181:
182: return baos.toString();
183: }
184:
185: public static String generateId(AxisDescription description) {
186: PolicyInclude policyInclude = description.getPolicyInclude();
187: String identifier = "-policy-1";
188:
189: if (description instanceof AxisMessage) {
190: identifier = "msg-" + ((AxisMessage) description).getName()
191: + identifier;
192: description = description.getParent();
193: }
194:
195: if (description instanceof AxisOperation) {
196: identifier = "op-"
197: + ((AxisOperation) description).getName()
198: + identifier;
199: description = description.getParent();
200: }
201:
202: if (description instanceof AxisService) {
203: identifier = "service-"
204: + ((AxisService) description).getName()
205: + identifier;
206: }
207:
208: /*
209: * Int 49 is the value of the Character '1'. Here we want to change '1' to '2' or
210: * '2' to '3' .. etc. to construct a unique identifier.
211: */
212: for (int index = 49; policyInclude.getPolicy(identifier) != null; index++) {
213: identifier = identifier.replace((char) index,
214: (char) (index + 1));
215: }
216:
217: return identifier;
218: }
219:
220: public static Policy getMergedPolicy(List policies,
221: AxisDescription description) {
222:
223: Policy policy = null;
224:
225: for (Iterator iterator = policies.iterator(); iterator
226: .hasNext();) {
227: Object policyElement = iterator.next();
228: if (policyElement instanceof Policy) {
229: policy = (policy == null) ? (Policy) policyElement
230: : (Policy) policy.merge((Policy) policyElement);
231:
232: } else {
233: PolicyReference policyReference = (PolicyReference) policyElement;
234: Policy policy2 = (Policy) policyReference.normalize(
235: new AxisPolicyLocator(description), false);
236: policy = (policy == null) ? policy2 : (Policy) policy
237: .merge(policy2);
238: }
239: }
240:
241: if (policy != null) {
242: policy = (Policy) policy.normalize(new AxisPolicyLocator(
243: description), false);
244: }
245:
246: return policy;
247: }
248: }
|