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: */package org.apache.cxf.interceptor;
019:
020: import java.io.OutputStream;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.ResourceBundle;
024:
025: import javax.xml.stream.XMLOutputFactory;
026: import javax.xml.stream.XMLStreamException;
027: import javax.xml.stream.XMLStreamWriter;
028:
029: import org.apache.cxf.common.classloader.ClassLoaderUtils;
030: import org.apache.cxf.common.i18n.BundleUtils;
031: import org.apache.cxf.message.Exchange;
032: import org.apache.cxf.message.Message;
033: import org.apache.cxf.phase.AbstractPhaseInterceptor;
034: import org.apache.cxf.phase.Phase;
035: import org.apache.cxf.staxutils.StaxUtils;
036:
037: /**
038: * Creates an XMLStreamReader from the InputStream on the Message.
039: */
040: public class StaxOutInterceptor extends
041: AbstractPhaseInterceptor<Message> {
042: private static final ResourceBundle BUNDLE = BundleUtils
043: .getBundle(StaxOutInterceptor.class);
044: private static Map<Object, XMLOutputFactory> factories = new HashMap<Object, XMLOutputFactory>();
045:
046: private StaxOutEndingInterceptor ending = new StaxOutEndingInterceptor();
047:
048: public StaxOutInterceptor() {
049: super (Phase.PRE_STREAM);
050: addAfter(AttachmentOutInterceptor.class.getName());
051: }
052:
053: public void handleMessage(Message message) {
054: OutputStream os = message.getContent(OutputStream.class);
055: XMLStreamWriter writer = message
056: .getContent(XMLStreamWriter.class);
057: if (os == null || writer != null) {
058: return;
059: }
060: // assert os != null;
061:
062: // TODO: where does encoding constant go?
063: String encoding = getEncoding(message);
064:
065: try {
066: writer = getXMLOutputFactory(message)
067: .createXMLStreamWriter(os, encoding);
068: } catch (XMLStreamException e) {
069: throw new Fault(new org.apache.cxf.common.i18n.Message(
070: "STREAM_CREATE_EXC", BUNDLE), e);
071: }
072: message.setContent(XMLStreamWriter.class, writer);
073:
074: // Add a final interceptor to write end elements
075: message.getInterceptorChain().add(ending);
076: }
077:
078: private String getEncoding(Message message) {
079: Exchange ex = message.getExchange();
080: String encoding = (String) message.get(Message.ENCODING);
081: if (encoding == null && ex.getInMessage() != null) {
082: encoding = (String) ex.getInMessage().get(Message.ENCODING);
083: message.put(Message.ENCODING, encoding);
084: }
085:
086: if (encoding == null) {
087: encoding = "UTF-8";
088: message.put(Message.ENCODING, encoding);
089: }
090: return encoding;
091: }
092:
093: public static XMLOutputFactory getXMLOutputFactory(Message m)
094: throws Fault {
095: Object o = m.getContextualProperty(XMLOutputFactory.class
096: .getName());
097: if (o instanceof XMLOutputFactory) {
098: return (XMLOutputFactory) o;
099: } else if (o != null) {
100: XMLOutputFactory xif = (XMLOutputFactory) factories.get(o);
101: if (xif == null) {
102: Class cls;
103: if (o instanceof Class) {
104: cls = (Class) o;
105: } else if (o instanceof String) {
106: try {
107: cls = ClassLoaderUtils.loadClass((String) o,
108: StaxInInterceptor.class);
109: } catch (ClassNotFoundException e) {
110: throw new Fault(e);
111: }
112: } else {
113: throw new Fault(
114: new org.apache.cxf.common.i18n.Message(
115: "INVALID_INPUT_FACTORY", BUNDLE, o));
116: }
117:
118: try {
119: xif = (XMLOutputFactory) (cls.newInstance());
120: factories.put(o, xif);
121: } catch (InstantiationException e) {
122: throw new Fault(e);
123: } catch (IllegalAccessException e) {
124: throw new Fault(e);
125: }
126: }
127: return xif;
128: } else {
129: return StaxUtils.getXMLOutputFactory();
130: }
131: }
132:
133: public class StaxOutEndingInterceptor extends
134: AbstractPhaseInterceptor<Message> {
135: public StaxOutEndingInterceptor() {
136: super (Phase.PRE_STREAM_ENDING);
137: getAfter()
138: .add(
139: AttachmentOutInterceptor.AttachmentOutEndingInterceptor.class
140: .getName());
141: }
142:
143: public void handleMessage(Message message) throws Fault {
144: try {
145: XMLStreamWriter xtw = message
146: .getContent(XMLStreamWriter.class);
147: if (xtw != null) {
148: xtw.writeEndDocument();
149: xtw.close();
150: }
151: } catch (XMLStreamException e) {
152: throw new Fault(new org.apache.cxf.common.i18n.Message(
153: "STAX_WRITE_EXC", BUNDLE), e);
154: }
155: }
156:
157: }
158: }
|