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.transport.http;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.om.OMOutputFormat;
024: import org.apache.axis2.AxisFault;
025: import org.apache.axis2.context.MessageContext;
026: import org.apache.commons.httpclient.methods.RequestEntity;
027:
028: import javax.xml.stream.FactoryConfigurationError;
029: import javax.xml.stream.XMLStreamException;
030: import java.io.ByteArrayOutputStream;
031: import java.io.IOException;
032: import java.io.OutputStream;
033:
034: public class RESTRequestEntity implements RequestEntity {
035: private byte[] bytes;
036: private String charSetEnc;
037: private boolean chunked;
038: private OMElement element;
039: private MessageContext msgCtxt;
040: private String soapActionString;
041: private OMOutputFormat format;
042:
043: public RESTRequestEntity(OMElement element, boolean chunked,
044: MessageContext msgCtxt, String charSetEncoding,
045: String soapActionString, OMOutputFormat format) {
046: this .element = element;
047: this .chunked = chunked;
048: this .msgCtxt = msgCtxt;
049: this .charSetEnc = charSetEncoding;
050: this .soapActionString = soapActionString;
051: this .format = format;
052: }
053:
054: private void handleOMOutput(OutputStream out, boolean doingMTOM)
055: throws XMLStreamException {
056: format.setDoOptimize(doingMTOM);
057: element.serializeAndConsume(out, format);
058: }
059:
060: public byte[] writeBytes() throws AxisFault {
061: try {
062: ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
063: if (!format.isOptimized()) {
064: OMOutputFormat format2 = new OMOutputFormat();
065: format2.setCharSetEncoding(charSetEnc);
066: element.serializeAndConsume(bytesOut, format2);
067: return bytesOut.toByteArray();
068: } else {
069: format.setCharSetEncoding(charSetEnc);
070: format.setDoOptimize(true);
071: element.serializeAndConsume(bytesOut, format);
072: return bytesOut.toByteArray();
073: }
074: } catch (XMLStreamException e) {
075: throw AxisFault.makeFault(e);
076: } catch (FactoryConfigurationError e) {
077: throw AxisFault.makeFault(e);
078: }
079: }
080:
081: public void writeRequest(OutputStream out) throws IOException {
082: try {
083: if (chunked) {
084: this .handleOMOutput(out, format.isDoingSWA());
085: } else {
086: if (bytes == null) {
087: bytes = writeBytes();
088: }
089: out.write(bytes);
090: }
091: out.flush();
092: } catch (XMLStreamException e) {
093: throw AxisFault.makeFault(e);
094: } catch (FactoryConfigurationError e) {
095: throw AxisFault.makeFault(e);
096: } catch (IOException e) {
097: throw AxisFault.makeFault(e);
098: }
099: }
100:
101: public long getContentLength() {
102: try {
103: if (chunked) {
104: return -1;
105: } else {
106: if (bytes == null) {
107: bytes = writeBytes();
108: }
109: return bytes.length;
110: }
111: } catch (AxisFault e) {
112: return -1;
113: }
114: }
115:
116: public String getContentType() {
117: String encoding = format.getCharSetEncoding();
118: String contentType = format.getContentType();
119: if (encoding != null) {
120: contentType += "; charset=" + encoding;
121: }
122:
123: // action header is not mandated in SOAP 1.2. So putting it, if available
124: if (!msgCtxt.isSOAP11() && (soapActionString != null)
125: && !"".equals(soapActionString.trim())
126: && !"\"\"".equals(soapActionString.trim())) {
127: contentType = contentType + ";action=\"" + soapActionString
128: + "\";";
129: }
130: return contentType;
131: }
132:
133: public boolean isRepeatable() {
134: return true;
135: }
136: }
|