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.bindings.http.interceptors;
018    
019    import org.apache.servicemix.soap.api.Fault;
020    import org.apache.servicemix.soap.api.Message;
021    import org.apache.servicemix.soap.api.model.Binding;
022    import org.apache.servicemix.soap.api.model.Operation;
023    import org.apache.servicemix.soap.bindings.http.HttpConstants;
024    import org.apache.servicemix.soap.bindings.http.model.Wsdl2HttpBinding;
025    import org.apache.servicemix.soap.bindings.http.model.Wsdl2HttpOperation;
026    import org.apache.servicemix.soap.core.AbstractInterceptor;
027    
028    /**
029     * @author <a href="mailto:gnodet [at] gmail.com">Guillaume Nodet</a>
030     */
031    public class HttpInOperationInterceptor extends AbstractInterceptor {
032    
033        public void handleMessage(Message message) {
034            Binding<?> binding = message.get(Binding.class);
035            if (binding instanceof Wsdl2HttpBinding == false) {
036                return;
037            }
038            Wsdl2HttpBinding httpBinding = (Wsdl2HttpBinding) binding;
039            String uri = message.getTransportHeaders().get(HttpConstants.REQUEST_URI);
040            if (uri == null) {
041                throw new Fault("Transport header not set: " + HttpConstants.REQUEST_URI);
042            }
043            String mth = message.getTransportHeaders().get(HttpConstants.REQUEST_METHOD);
044            if (mth == null) {
045                throw new Fault("Transport header not set: " + HttpConstants.REQUEST_METHOD);
046            }
047            for (Wsdl2HttpOperation operation : httpBinding.getOperations()) {
048                if (mth.equalsIgnoreCase(operation.getHttpMethod())) {
049                    String loc = IriDecoderHelper.combine(binding.getLocation(), operation.getHttpLocation());
050                    String path1 = getUriPath(uri);
051                    String path2 = getUriPath(loc);
052                    if (matchPath(path1, path2)) {
053                        message.put(Operation.class, operation);
054                        return;
055                    }
056                }
057            }
058        }
059    
060        private boolean matchPath(String path, String template) {
061            int idx = template.indexOf('{');
062            while (idx >= 0 && template.charAt(idx + 1) == '{') {
063                idx = template.indexOf('{', idx + 2);
064            }
065            if (idx > 0) {
066                return path.regionMatches(0, template, 0, idx - 1);
067            } else {
068                return path.equals(template);
069            }
070        }
071    
072        private String getUriPath(String uri) {
073            int idx = uri.indexOf("://");
074            int idx2 = uri.indexOf('/', idx + 3);
075            return uri.substring(idx2 + 1);
076        }
077    
078    }