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.transport.http.util;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axis2.AxisFault;
023: import org.apache.axis2.context.MessageContext;
024: import org.apache.axis2.description.WSDL20DefaultValueHolder;
025: import org.apache.axis2.description.WSDL2Constants;
026: import org.apache.axis2.util.WSDL20Util;
027:
028: import java.io.UnsupportedEncodingException;
029: import java.net.MalformedURLException;
030: import java.net.URI;
031: import java.net.URISyntaxException;
032: import java.net.URL;
033: import java.util.Iterator;
034:
035: /**
036: * This util is used on the client side for creating the URL's for all request (WSDL 2.0 allws to
037: * change the URL's of SOAP messages too). It resolves WSDL 2.0 httplocation property and also
038: * append parameters to URL's when needed.
039: */
040: public class URLTemplatingUtil {
041:
042: /**
043: * Appends Query parameters to the URL
044: *
045: * @param messageContext - The MessageContext of the request
046: * @param url - Original url string
047: * @return String containing the appended query parameters
048: */
049: public static URL appendQueryParameters(
050: MessageContext messageContext, URL url) throws AxisFault {
051:
052: String urlString = url.toString();
053: OMElement firstElement;
054: String queryParameterSeparator = (String) messageContext
055: .getProperty(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
056: // In case queryParameterSeparator is null we better use the default value
057:
058: if (queryParameterSeparator == null) {
059: queryParameterSeparator = WSDL20DefaultValueHolder
060: .getDefaultValue(WSDL2Constants.ATTR_WHTTP_QUERY_PARAMETER_SEPARATOR);
061: }
062:
063: firstElement = messageContext.getEnvelope().getBody()
064: .getFirstElement();
065: String params = "";
066:
067: if (firstElement != null) {
068: Iterator iter = firstElement.getChildElements();
069:
070: String legalCharacters = WSDL2Constants.LEGAL_CHARACTERS_IN_QUERY
071: .replaceAll(queryParameterSeparator, "");
072:
073: while (iter.hasNext()) {
074: OMElement element = (OMElement) iter.next();
075: try {
076: params = params
077: + URIEncoderDecoder.quoteIllegal(element
078: .getLocalName(), legalCharacters)
079: + "="
080: + URIEncoderDecoder.quoteIllegal(element
081: .getText(), legalCharacters)
082: + queryParameterSeparator;
083: } catch (UnsupportedEncodingException e) {
084: throw AxisFault.makeFault(e);
085: }
086: }
087: }
088:
089: if (!"".equals(params)) {
090: int index = urlString.indexOf("?");
091: if (index == -1) {
092: urlString = urlString + "?"
093: + params.substring(0, params.length() - 1);
094: } else if (index == urlString.length() - 1) {
095: urlString = urlString
096: + params.substring(0, params.length() - 1);
097:
098: } else {
099: urlString = urlString + queryParameterSeparator
100: + params.substring(0, params.length() - 1);
101: }
102:
103: try {
104: return new URL(urlString);
105: } catch (MalformedURLException e) {
106: throw AxisFault.makeFault(e);
107: }
108: }
109: return url;
110: }
111:
112: /**
113: * Returns the templated URL given the original URL
114: *
115: * @param targetURL - The original URL
116: * @param messageContext - The MessageContext of the request
117: * @param detach - Boolean value specifying whether the element should be detached from the
118: * envelop. When serializing data as application/x-form-urlencoded what goes in the body is the
119: * remainder and therefore we should detach the element from the envelop.
120: * @return The templated URL
121: * @throws AxisFault - Thrown in case an exception occurs
122: */
123: public static URL getTemplatedURL(URL targetURL,
124: MessageContext messageContext, boolean detach)
125: throws AxisFault {
126:
127: String httpLocation = (String) messageContext
128: .getProperty(WSDL2Constants.ATTR_WHTTP_LOCATION);
129:
130: // String urlString = targetURL.toString();
131: if (httpLocation != null) {
132: String replacedQuery = httpLocation;
133: int separator = httpLocation.indexOf('{');
134: try {
135:
136: if (separator > -1) {
137: replacedQuery = URIEncoderDecoder.quoteIllegal(
138: WSDL20Util.applyURITemplating(
139: messageContext, httpLocation,
140: detach),
141: WSDL2Constants.LEGAL_CHARACTERS_IN_URL);
142:
143: }
144: URI targetURI;
145: URI appendedURI;
146: if (replacedQuery.charAt(0) == '?') {
147: appendedURI = new URI(targetURL.toString()
148: + replacedQuery);
149: } else {
150: String uriString = targetURL.toString();
151: if (!uriString.endsWith("/")) {
152: targetURI = new URI(uriString + "/");
153: } else {
154: targetURI = new URI(uriString);
155: }
156: appendedURI = targetURI.resolve(replacedQuery);
157: }
158:
159: targetURL = appendedURI.toURL();
160:
161: } catch (MalformedURLException e) {
162: throw new AxisFault(
163: "An error occured while trying to create request URL");
164: } catch (URISyntaxException e) {
165: throw new AxisFault(
166: "An error occured while trying to create request URL");
167: } catch (UnsupportedEncodingException e) {
168: throw new AxisFault(
169: "An error occured while trying to create request URL");
170: }
171: }
172:
173: return targetURL;
174: }
175:
176: }
|