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.axiom.soap.SOAP11Constants;
024: import org.apache.axiom.soap.SOAP12Constants;
025: import org.apache.axiom.soap.SOAPFault;
026: import org.apache.axiom.soap.SOAPFaultDetail;
027: import org.apache.axis2.AxisFault;
028: import org.apache.axis2.Constants;
029: import org.apache.axis2.context.MessageContext;
030: import org.apache.axis2.transport.MessageFormatter;
031: import org.apache.axis2.transport.http.util.URLTemplatingUtil;
032: import org.apache.axis2.util.JavaUtils;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: import javax.xml.stream.XMLStreamException;
037: import java.io.ByteArrayOutputStream;
038: import java.io.IOException;
039: import java.io.OutputStream;
040: import java.net.URL;
041:
042: /**
043: * Formates the request message as application/xml
044: */
045: public class ApplicationXMLFormatter implements MessageFormatter {
046:
047: private static final Log log = LogFactory
048: .getLog(ApplicationXMLFormatter.class);
049:
050: public byte[] getBytes(MessageContext messageContext,
051: OMOutputFormat format) throws AxisFault {
052:
053: if (log.isDebugEnabled()) {
054: log.debug("start getBytes()");
055: log
056: .debug(" fault flow="
057: + (messageContext.getFLOW() == MessageContext.OUT_FAULT_FLOW));
058: }
059: try {
060: OMElement omElement;
061:
062: if (messageContext.getFLOW() == MessageContext.OUT_FAULT_FLOW) {
063: SOAPFault fault = messageContext.getEnvelope()
064: .getBody().getFault();
065: SOAPFaultDetail soapFaultDetail = fault.getDetail();
066: omElement = soapFaultDetail.getFirstElement();
067:
068: if (omElement == null) {
069: omElement = fault.getReason();
070: }
071:
072: } else {
073: omElement = messageContext.getEnvelope().getBody()
074: .getFirstElement();
075: }
076: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
077:
078: if (omElement != null) {
079:
080: try {
081: omElement.serializeAndConsume(bytesOut, format);
082: } catch (XMLStreamException e) {
083: throw AxisFault.makeFault(e);
084: }
085:
086: return bytesOut.toByteArray();
087: }
088:
089: return new byte[0];
090: } finally {
091: if (log.isDebugEnabled()) {
092: log.debug("end getBytes()");
093: }
094: }
095: }
096:
097: public void writeTo(MessageContext messageContext,
098: OMOutputFormat format, OutputStream outputStream,
099: boolean preserve) throws AxisFault {
100:
101: if (log.isDebugEnabled()) {
102: log.debug("start writeTo()");
103: }
104: try {
105: OMElement omElement = null;
106:
107: if (messageContext.getFLOW() == MessageContext.OUT_FAULT_FLOW) {
108: SOAPFault fault = messageContext.getEnvelope()
109: .getBody().getFault();
110: SOAPFaultDetail soapFaultDetail = fault.getDetail();
111: if (soapFaultDetail != null) {
112: omElement = soapFaultDetail.getFirstElement();
113: }
114: if (omElement == null) {
115: omElement = fault.getReason();
116: }
117:
118: } else {
119: omElement = messageContext.getEnvelope().getBody()
120: .getFirstElement();
121: }
122: if (omElement != null) {
123: try {
124: omElement.serializeAndConsume(outputStream, format);
125: } catch (XMLStreamException e) {
126: throw AxisFault.makeFault(e);
127: }
128: }
129: try {
130: outputStream.flush();
131: } catch (IOException e) {
132: throw AxisFault.makeFault(e);
133: }
134: } finally {
135: if (log.isDebugEnabled()) {
136: log.debug("end writeTo()");
137: }
138: }
139: }
140:
141: public String getContentType(MessageContext messageContext,
142: OMOutputFormat format, String soapAction) {
143:
144: String encoding = format.getCharSetEncoding();
145: String contentType;
146: contentType = (String) messageContext
147: .getProperty(Constants.Configuration.CONTENT_TYPE);
148:
149: if (log.isDebugEnabled()) {
150: log.debug("contentType set from messageContext ="
151: + contentType);
152: log.debug("(NOTE) contentType from format is="
153: + format.getContentType());
154: }
155:
156: if (contentType == null) {
157: contentType = HTTPConstants.MEDIA_TYPE_APPLICATION_XML;
158: } else if (isSOAPContentType(contentType)) {
159: contentType = HTTPConstants.MEDIA_TYPE_APPLICATION_XML;
160: if (log.isDebugEnabled()) {
161: log
162: .debug("contentType is set incorrectly for Application XML.");
163: log.debug("It is changed to " + contentType);
164: }
165: }
166:
167: if (encoding != null) {
168: contentType += "; charset=" + encoding;
169: }
170:
171: // if soap action is there (can be there is soap response MEP is used) add it.
172: if ((soapAction != null) && !"".equals(soapAction.trim())
173: && !"\"\"".equals(soapAction.trim())) {
174: contentType = contentType;
175: }
176: if (log.isDebugEnabled()) {
177: log.debug("contentType returned =" + contentType);
178: }
179: return contentType;
180: }
181:
182: public URL getTargetAddress(MessageContext messageContext,
183: OMOutputFormat format, URL targetURL) throws AxisFault {
184:
185: // Check whether there is a template in the URL, if so we have to replace then with data
186: // values and create a new target URL.
187: targetURL = URLTemplatingUtil.getTemplatedURL(targetURL,
188: messageContext, false);
189:
190: return targetURL;
191: }
192:
193: public String formatSOAPAction(MessageContext messageContext,
194: OMOutputFormat format, String soapAction) {
195: return soapAction;
196: }
197:
198: private boolean isSOAPContentType(String contentType) {
199: if (JavaUtils.indexOfIgnoreCase(contentType,
200: SOAP12Constants.SOAP_12_CONTENT_TYPE) > -1) {
201: return true;
202: }
203: // search for "type=text/xml"
204: else if (JavaUtils.indexOfIgnoreCase(contentType,
205: SOAP11Constants.SOAP_11_CONTENT_TYPE) > -1) {
206: return true;
207: }
208: return false;
209: }
210: }
|