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: package org.apache.axis2.util;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.OMFactory;
023: import org.apache.axiom.om.OMNamespace;
024: import org.apache.axis2.AxisFault;
025: import org.apache.axis2.context.MessageContext;
026: import org.apache.axis2.description.WSDL20DefaultValueHolder;
027: import org.apache.axis2.description.WSDL2Constants;
028: import org.apache.axis2.transport.http.util.URIEncoderDecoder;
029: import org.apache.woden.wsdl20.extensions.http.HTTPLocation;
030: import org.apache.woden.wsdl20.extensions.http.HTTPLocationTemplate;
031: import org.apache.woden.wsdl20.extensions.soap.SOAPFaultCode;
032: import org.apache.woden.wsdl20.extensions.soap.SOAPFaultSubcodes;
033:
034: import javax.xml.namespace.QName;
035: import java.io.UnsupportedEncodingException;
036: import java.util.Iterator;
037: import java.util.Map;
038:
039: public class WSDL20Util {
040: public static void extractWSDL20SoapFaultInfo(Map options,
041: OMElement bindingMessageElement, OMFactory omFactory,
042: OMNamespace wsoap) {
043: // Fault specific properties
044: SOAPFaultCode faultCode = (SOAPFaultCode) options
045: .get(WSDL2Constants.ATTR_WSOAP_CODE);
046: if (faultCode != null && faultCode.getQName() != null) {
047: bindingMessageElement
048: .addAttribute(omFactory.createOMAttribute(
049: WSDL2Constants.ATTRIBUTE_CODE, wsoap,
050: faultCode.getQName().getLocalPart()));
051: }
052: SOAPFaultSubcodes soapFaultSubcodes = (SOAPFaultSubcodes) options
053: .get(WSDL2Constants.ATTR_WSOAP_SUBCODES);
054: QName faultCodes[];
055: if (soapFaultSubcodes != null
056: && (faultCodes = soapFaultSubcodes.getQNames()) != null) {
057: for (int i = 0; i < faultCodes.length; i++) {
058: bindingMessageElement.addAttribute(omFactory
059: .createOMAttribute(
060: WSDL2Constants.ATTRIBUTE_SUBCODES,
061: wsoap, faultCodes[0].getLocalPart()));
062: }
063: }
064: }
065:
066: /**
067: * This method is used to resolve httplocation property. It changes the URL as stipulated by
068: * the httplocation property.
069: *
070: * @param messageContext - The MessageContext of the request
071: * @param rawURLString - The raw URL containing httplocation templates
072: * @param detach - Boolean value specifying whether the element should be detached from the
073: * envelop. When serializing data as application/x-form-urlencoded what goes in the body is the
074: * remainder and therefore we should detach the element from the envelop.
075: * @return - String with templated values replaced
076: * @throws org.apache.axis2.AxisFault - Thrown in case an exception occurs
077: */
078: public static String applyURITemplating(
079: MessageContext messageContext, String rawURLString,
080: boolean detach) throws AxisFault {
081:
082: OMElement firstElement;
083: if (detach) {
084: firstElement = messageContext.getEnvelope().getBody()
085: .getFirstElement();
086: } else {
087: firstElement = messageContext.getEnvelope().getBody()
088: .getFirstElement().cloneOMElement();
089: }
090: String queryParameterSeparator = (String) messageContext
091: .getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
092: if (queryParameterSeparator == null) {
093: queryParameterSeparator = WSDL20DefaultValueHolder.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR_DEFAULT;
094: }
095: HTTPLocation httpLocation = new HTTPLocation(rawURLString);
096: HTTPLocationTemplate[] templates = httpLocation.getTemplates();
097:
098: for (int i = 0; i < templates.length; i++) {
099: HTTPLocationTemplate template = templates[i];
100: String localName = template.getName();
101: String elementValue = getOMElementValue(localName,
102: firstElement);
103: if (template.isEncoded()) {
104: try {
105:
106: if (template.isQuery()) {
107: template
108: .setValue(URIEncoderDecoder
109: .quoteIllegal(
110: elementValue,
111: WSDL2Constants.LEGAL_CHARACTERS_IN_QUERY
112: .replaceAll(
113: queryParameterSeparator,
114: "")));
115: } else {
116: template
117: .setValue(URIEncoderDecoder
118: .quoteIllegal(
119: elementValue,
120: WSDL2Constants.LEGAL_CHARACTERS_IN_PATH));
121: }
122: } catch (UnsupportedEncodingException e) {
123: throw new AxisFault("Unable to encode Query String");
124: }
125:
126: } else {
127: template.setValue(elementValue);
128: }
129: }
130:
131: return httpLocation.getFormattedLocation();
132: }
133:
134: /**
135: * This method is used to retrive elements from the soap envelop
136: *
137: * @param elementName - The name of the required element
138: * @param parentElement - The parent element that the required element should be retrived from
139: * @return - The value of the element as a string
140: */
141: private static String getOMElementValue(String elementName,
142: OMElement parentElement) {
143:
144: OMElement httpURLParam = null;
145: Iterator children = parentElement.getChildElements();
146:
147: while (children.hasNext()) {
148: OMElement child = (OMElement) children.next();
149: QName qName = child.getQName();
150: if (elementName.equals(qName.getLocalPart())) {
151: httpURLParam = child;
152: break;
153: }
154: }
155:
156: if (httpURLParam != null) {
157: httpURLParam.detach();
158:
159: if (parentElement.getFirstOMChild() == null) {
160: parentElement.detach();
161: }
162: return httpURLParam.getText();
163: }
164: return "";
165:
166: }
167:
168: }
|