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.InputStream;
021: import java.util.HashMap;
022: import java.util.Map;
023: import java.util.ResourceBundle;
024: import java.util.logging.Logger;
025:
026: import javax.xml.stream.XMLInputFactory;
027: import javax.xml.stream.XMLStreamException;
028: import javax.xml.stream.XMLStreamReader;
029:
030: import org.apache.cxf.common.classloader.ClassLoaderUtils;
031: import org.apache.cxf.common.i18n.BundleUtils;
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 StaxInInterceptor extends
041: AbstractPhaseInterceptor<Message> {
042: private static final ResourceBundle BUNDLE = BundleUtils
043: .getBundle(StaxInInterceptor.class);
044: private static final Logger LOG = Logger
045: .getLogger(StaxInInterceptor.class.getName());
046:
047: private static Map<Object, XMLInputFactory> factories = new HashMap<Object, XMLInputFactory>();
048:
049: public StaxInInterceptor() {
050: super (Phase.POST_STREAM);
051: }
052:
053: public StaxInInterceptor(String phase) {
054: super (phase);
055: }
056:
057: public void handleMessage(Message message) {
058: if (isGET(message)
059: || message.getContentFormats().contains(
060: XMLStreamReader.class)) {
061: LOG.info("StaxInInterceptor skipped.");
062: return;
063: }
064: InputStream is = message.getContent(InputStream.class);
065: assert is != null;
066:
067: String encoding = (String) message.get(Message.ENCODING);
068:
069: XMLStreamReader reader;
070: try {
071: reader = getXMLInputFactory(message).createXMLStreamReader(
072: is, encoding);
073: } catch (XMLStreamException e) {
074: throw new Fault(new org.apache.cxf.common.i18n.Message(
075: "STREAM_CREATE_EXC", BUNDLE), e);
076: }
077:
078: message.setContent(XMLStreamReader.class, reader);
079: }
080:
081: public static XMLInputFactory getXMLInputFactory(Message m)
082: throws Fault {
083: Object o = m.getContextualProperty(XMLInputFactory.class
084: .getName());
085: if (o instanceof XMLInputFactory) {
086: return (XMLInputFactory) o;
087: } else if (o != null) {
088: XMLInputFactory xif = (XMLInputFactory) factories.get(o);
089: if (xif == null) {
090: Class cls;
091: if (o instanceof Class) {
092: cls = (Class) o;
093: } else if (o instanceof String) {
094: try {
095: cls = ClassLoaderUtils.loadClass((String) o,
096: StaxInInterceptor.class);
097: } catch (ClassNotFoundException e) {
098: throw new Fault(e);
099: }
100: } else {
101: throw new Fault(
102: new org.apache.cxf.common.i18n.Message(
103: "INVALID_INPUT_FACTORY", BUNDLE, o));
104: }
105:
106: try {
107: xif = (XMLInputFactory) (cls.newInstance());
108: factories.put(o, xif);
109: } catch (InstantiationException e) {
110: throw new Fault(e);
111: } catch (IllegalAccessException e) {
112: throw new Fault(e);
113: }
114: }
115: return xif;
116: } else {
117: return StaxUtils.getXMLInputFactory();
118: }
119: }
120: }
|