01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.axis2.builder;
21:
22: import org.apache.axiom.attachments.Attachments;
23: import org.apache.axiom.om.OMElement;
24: import org.apache.axiom.om.impl.builder.StAXBuilder;
25: import org.apache.axiom.om.util.StAXUtils;
26: import org.apache.axiom.soap.SOAPEnvelope;
27: import org.apache.axiom.soap.impl.builder.MTOMStAXSOAPModelBuilder;
28: import org.apache.axis2.AxisFault;
29: import org.apache.axis2.Constants;
30: import org.apache.axis2.context.MessageContext;
31:
32: import javax.xml.stream.XMLStreamException;
33: import javax.xml.stream.XMLStreamReader;
34: import java.io.IOException;
35: import java.io.InputStream;
36: import java.io.PushbackInputStream;
37:
38: public class MTOMBuilder implements Builder {
39:
40: public OMElement processDocument(InputStream inputStream,
41: String contentType, MessageContext messageContext)
42: throws AxisFault {
43: XMLStreamReader streamReader;
44: try {
45: Attachments attachments = messageContext.getAttachmentMap();
46: String charSetEncoding = (String) messageContext
47: .getProperty(Constants.Configuration.CHARACTER_SET_ENCODING);
48:
49: // Get the actual encoding by looking at the BOM of the InputStream
50: PushbackInputStream pis = BuilderUtil
51: .getPushbackInputStream(inputStream);
52: String actualCharSetEncoding = BuilderUtil
53: .getCharSetEncoding(pis, charSetEncoding);
54:
55: // Get the XMLStreamReader for this input stream
56: streamReader = StAXUtils.createXMLStreamReader(pis,
57: actualCharSetEncoding);
58: StAXBuilder builder = new MTOMStAXSOAPModelBuilder(
59: streamReader, attachments);
60: SOAPEnvelope envelope = (SOAPEnvelope) builder
61: .getDocumentElement();
62: BuilderUtil.validateSOAPVersion(BuilderUtil
63: .getEnvelopeNamespace(contentType), envelope);
64: BuilderUtil.validateCharSetEncoding(charSetEncoding,
65: builder.getDocument().getCharsetEncoding(),
66: envelope.getNamespace().getNamespaceURI());
67: //Overriding the earlier setting by MIMEBuilder
68: messageContext.setDoingSwA(false);
69: messageContext.setDoingMTOM(true);
70: return envelope;
71: } catch (IOException e) {
72: throw AxisFault.makeFault(e);
73: } catch (XMLStreamException e) {
74: throw AxisFault.makeFault(e);
75: }
76: }
77: }
|