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.binding.coloc;
019:
020: import javax.xml.namespace.QName;
021: import javax.xml.ws.WebFault;
022:
023: import org.apache.cxf.interceptor.Fault;
024: import org.apache.cxf.message.Message;
025: import org.apache.cxf.phase.AbstractPhaseInterceptor;
026: import org.apache.cxf.phase.Phase;
027: import org.apache.cxf.service.model.BindingOperationInfo;
028: import org.apache.cxf.service.model.FaultInfo;
029: import org.apache.cxf.service.model.MessagePartInfo;
030: import org.apache.cxf.service.model.OperationInfo;
031: import org.apache.cxf.service.model.UnwrappedOperationInfo;
032:
033: public class WebFaultInInterceptor extends
034: AbstractPhaseInterceptor<Message> {
035:
036: public WebFaultInInterceptor() {
037: super (Phase.PRE_LOGICAL);
038: }
039:
040: public void handleMessage(Message message) throws Fault {
041: Exception ex = message.getContent(Exception.class);
042: if (ex != null) {
043: message.put(Message.RESPONSE_CODE, Integer.valueOf(500));
044: }
045:
046: if (ex instanceof Fault) {
047: Fault f = (Fault) ex;
048: ex = (Exception) f.getCause();
049: }
050:
051: QName faultName = this .getFaultName(ex);
052: if (faultName == null) {
053: return;
054: }
055:
056: BindingOperationInfo boi = message.getExchange().get(
057: BindingOperationInfo.class);
058: MessagePartInfo part = getFaultMessagePart(faultName, boi
059: .getOperationInfo());
060:
061: if (part != null) {
062: message.setContent(Exception.class, ex);
063: }
064: }
065:
066: private QName getFaultName(Exception webFault) {
067: QName faultName = null;
068: WebFault wf = webFault.getClass().getAnnotation(WebFault.class);
069: if (wf != null) {
070: faultName = new QName(wf.targetNamespace(), wf.name());
071: }
072:
073: return faultName;
074: }
075:
076: private MessagePartInfo getFaultMessagePart(QName qname,
077: OperationInfo op) {
078: if (op.isUnwrapped()) {
079: op = ((UnwrappedOperationInfo) op).getWrappedOperation();
080: }
081:
082: for (FaultInfo faultInfo : op.getFaults()) {
083: for (MessagePartInfo mpi : faultInfo.getMessageParts()) {
084: String ns = null;
085: if (mpi.isElement()) {
086: ns = mpi.getElementQName().getNamespaceURI();
087: } else {
088: ns = mpi.getTypeQName().getNamespaceURI();
089: }
090: if (qname.getLocalPart().equals(
091: mpi.getConcreteName().getLocalPart())
092: && qname.getNamespaceURI().equals(ns)) {
093: return mpi;
094: }
095: }
096:
097: }
098: return null;
099: }
100: }
|