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.ws.rm.soap;
019:
020: import java.util.logging.Level;
021: import java.util.logging.Logger;
022:
023: import javax.xml.bind.JAXBContext;
024: import javax.xml.bind.Marshaller;
025: import javax.xml.parsers.DocumentBuilderFactory;
026:
027: import org.w3c.dom.Document;
028: import org.w3c.dom.Element;
029:
030: import org.apache.cxf.binding.Binding;
031: import org.apache.cxf.binding.soap.Soap11;
032: import org.apache.cxf.binding.soap.SoapBinding;
033: import org.apache.cxf.binding.soap.SoapFault;
034: import org.apache.cxf.binding.soap.SoapVersion;
035: import org.apache.cxf.common.i18n.Message;
036: import org.apache.cxf.common.logging.LogUtils;
037: import org.apache.cxf.common.util.PackageUtils;
038: import org.apache.cxf.interceptor.Fault;
039: import org.apache.cxf.ws.rm.BindingFaultFactory;
040: import org.apache.cxf.ws.rm.Identifier;
041: import org.apache.cxf.ws.rm.RMConstants;
042: import org.apache.cxf.ws.rm.SequenceAcknowledgement;
043: import org.apache.cxf.ws.rm.SequenceFault;
044: import org.apache.cxf.ws.rm.SequenceType;
045:
046: /**
047: *
048: */
049: public class SoapFaultFactory implements BindingFaultFactory {
050:
051: private static final Logger LOG = LogUtils
052: .getL7dLogger(SoapFaultFactory.class);
053: private static final String WS_RM_PACKAGE = PackageUtils
054: .getPackageName(SequenceType.class);
055:
056: private SoapVersion version;
057:
058: public SoapFaultFactory(Binding binding) {
059: version = ((SoapBinding) binding).getSoapVersion();
060: }
061:
062: public Fault createFault(SequenceFault sf) {
063: Fault f = null;
064: if (version == Soap11.getInstance()) {
065: f = createSoap11Fault(sf);
066: // so we can encode the SequenceFault as header
067: f.initCause(sf);
068: } else {
069: f = createSoap12Fault(sf);
070: }
071: return f;
072: }
073:
074: Fault createSoap11Fault(SequenceFault sf) {
075: SoapFault fault = new SoapFault(sf.getReason(),
076: sf.isSender() ? version.getSender() : version
077: .getReceiver());
078: fault.setSubCode(sf.getSubCode());
079: return fault;
080: }
081:
082: Fault createSoap12Fault(SequenceFault sf) {
083: SoapFault fault = (SoapFault) createSoap11Fault(sf);
084: Object detail = sf.getDetail();
085: if (null == detail) {
086: return fault;
087: }
088:
089: try {
090: setDetail(fault, detail);
091: } catch (Exception ex) {
092: LogUtils.log(LOG, Level.SEVERE, "MARSHAL_FAULT_DETAIL_EXC",
093: ex);
094: ex.printStackTrace();
095: }
096: return fault;
097: }
098:
099: void setDetail(SoapFault fault, Object detail) throws Exception {
100: DocumentBuilderFactory factory = DocumentBuilderFactory
101: .newInstance();
102: factory.setNamespaceAware(true);
103: Document doc = factory.newDocumentBuilder().newDocument();
104: Element elem = null;
105:
106: JAXBContext ctx = JAXBContext.newInstance(WS_RM_PACKAGE);
107: Marshaller m = ctx.createMarshaller();
108: if (RMConstants.getInvalidAcknowledgmentFaultCode().equals(
109: fault.getSubCode())) {
110: SequenceAcknowledgement ack = (SequenceAcknowledgement) detail;
111: m.marshal(ack, doc);
112: } else if (!RMConstants.getCreateSequenceRefusedFaultCode()
113: .equals(fault.getSubCode())) {
114: Identifier id = (Identifier) detail;
115: m.marshal(id, doc);
116: }
117: elem = (Element) doc.getChildNodes().item(0);
118: fault.setDetail(elem);
119: }
120:
121: public String toString(Fault f) {
122: SoapFault sf = (SoapFault) f;
123: Message msg = new Message("SEQ_FAULT_MSG", LOG, new Object[] {
124: sf.getReason(), sf.getFaultCode(), sf.getSubCode() });
125: return msg.toString();
126: }
127:
128: }
|