001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.soap.interceptors.wsdl;
018    
019    import java.util.ArrayList;
020    import java.util.List;
021    
022    import javax.xml.namespace.QName;
023    import javax.xml.stream.XMLStreamConstants;
024    import javax.xml.stream.XMLStreamReader;
025    
026    import org.apache.servicemix.soap.api.Message;
027    import org.apache.servicemix.soap.api.model.Binding;
028    import org.apache.servicemix.soap.api.model.Operation;
029    import org.apache.servicemix.soap.bindings.soap.SoapFault;
030    import org.apache.servicemix.soap.core.AbstractInterceptor;
031    
032    /**
033     * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
034     */
035    public class WsdlOperationInInterceptor extends AbstractInterceptor {
036    
037        public void handleMessage(Message message) {
038            if (message.get(Operation.class) != null) {
039                return;
040            }
041            XMLStreamReader reader = message.getContent(XMLStreamReader.class);
042            if (reader == null || reader.getEventType() != XMLStreamConstants.START_ELEMENT) {
043                return;
044            }
045            Binding<?> binding = message.get(Binding.class);
046            List<Operation> matching = new ArrayList<Operation>();
047            QName name = reader.getName();
048            for (Operation<?> oper : binding.getOperations()) {
049                if (name.equals(oper.getInput().getElementName())) {
050                    matching.add(oper);
051                }
052            }
053            if (matching.size() == 1) {
054                Operation op = matching.get(0);
055                message.put(Operation.class, op);
056            } else {
057                StringBuilder sb = new StringBuilder();
058                sb.append("No matching operation found for element: ");
059                sb.append(name);
060                sb.append("\n");
061                sb.append("Expected one of: \n");
062                for (Operation<?> oper : binding.getOperations()) {
063                    sb.append("  - ");
064                    sb.append(oper.getInput().getElementName());
065                    sb.append("\n");
066                }
067                throw new SoapFault(SoapFault.SENDER, sb.toString());
068            }
069        }
070    
071    }