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;
020:
021: import org.apache.axiom.om.OMElement;
022: import org.apache.axiom.om.OMOutputFormat;
023: import org.apache.axis2.AxisFault;
024: import org.apache.axis2.Constants;
025: import org.apache.axis2.util.JavaUtils;
026: import org.apache.axis2.description.WSDL2Constants;
027: import org.apache.axis2.context.MessageContext;
028: import org.apache.axis2.transport.MessageFormatter;
029: import org.apache.axis2.transport.http.util.URLTemplatingUtil;
030:
031: import java.io.IOException;
032: import java.io.OutputStream;
033: import java.net.URL;
034: import java.util.Iterator;
035:
036: /**
037: * Formates the request message as application/x-www-form-urlencoded
038: */
039: public class XFormURLEncodedFormatter implements MessageFormatter {
040:
041: public byte[] getBytes(MessageContext messageContext,
042: OMOutputFormat format) throws AxisFault {
043:
044: OMElement omElement = messageContext.getEnvelope().getBody()
045: .getFirstElement();
046:
047: if (omElement != null) {
048: Iterator it = omElement.getChildElements();
049: String paraString = "";
050:
051: while (it.hasNext()) {
052: OMElement ele1 = (OMElement) it.next();
053: String parameter;
054:
055: parameter = ele1.getLocalName() + "=" + ele1.getText();
056: paraString = "".equals(paraString) ? parameter
057: : (paraString + "&" + parameter);
058: }
059:
060: return paraString.getBytes();
061: }
062:
063: return new byte[0];
064: }
065:
066: public void writeTo(MessageContext messageContext,
067: OMOutputFormat format, OutputStream outputStream,
068: boolean preserve) throws AxisFault {
069:
070: try {
071: outputStream.write(getBytes(messageContext, format));
072: } catch (IOException e) {
073: throw new AxisFault(
074: "An error occured while writing the request");
075: }
076: }
077:
078: public String getContentType(MessageContext messageContext,
079: OMOutputFormat format, String soapAction) {
080:
081: String encoding = format.getCharSetEncoding();
082: String contentType = HTTPConstants.MEDIA_TYPE_X_WWW_FORM;
083:
084: if (encoding != null) {
085: contentType += "; charset=" + encoding;
086: }
087:
088: // if soap action is there (can be there is soap response MEP is used) add it.
089: if ((soapAction != null) && !"".equals(soapAction.trim())
090: && !"\"\"".equals(soapAction.trim())) {
091: contentType = contentType + ";action=\"" + soapAction
092: + "\";";
093: }
094:
095: return contentType;
096: }
097:
098: public URL getTargetAddress(MessageContext messageContext,
099: OMOutputFormat format, URL targetURL) throws AxisFault {
100:
101: // Check whether there is a template in the URL, if so we have to replace then with data
102: // values and create a new target URL.
103: targetURL = URLTemplatingUtil.getTemplatedURL(targetURL,
104: messageContext, true);
105: String ignoreUncited = (String) messageContext
106: .getProperty(WSDL2Constants.ATTR_WHTTP_IGNORE_UNCITED);
107:
108: // Need to have this check here cause
109: if (ignoreUncited == null
110: || !JavaUtils.isTrueExplicitly(ignoreUncited)) {
111: String httpMethod = (String) messageContext
112: .getProperty(Constants.Configuration.HTTP_METHOD);
113: if (Constants.Configuration.HTTP_METHOD_GET
114: .equals(httpMethod)
115: || Constants.Configuration.HTTP_METHOD_DELETE
116: .equals(httpMethod)) {
117: targetURL = URLTemplatingUtil.appendQueryParameters(
118: messageContext, targetURL);
119: }
120: } else {
121: messageContext.getEnvelope().getBody().getFirstElement()
122: .detach();
123: }
124:
125: return targetURL;
126: }
127:
128: public String formatSOAPAction(MessageContext messageContext,
129: OMOutputFormat format, String soapAction) {
130: return soapAction;
131: }
132: }
|