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.geronimo.cxf; 018 019 import java.util.Collections; 020 import java.util.List; 021 022 import javax.xml.namespace.QName; 023 import javax.xml.ws.handler.Handler; 024 import javax.xml.ws.handler.PortInfo; 025 026 import org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder; 027 import org.apache.cxf.jaxws.javaee.HandlerChainType; 028 import org.apache.geronimo.jaxws.JAXWSUtils; 029 030 public class GeronimoHandlerChainBuilder extends AnnotationHandlerChainBuilder { 031 032 private ClassLoader classLoader = null; 033 private PortInfo portInfo; 034 035 public GeronimoHandlerChainBuilder(ClassLoader classloader, 036 PortInfo portInfo) { 037 this.classLoader = classloader; 038 this.portInfo = portInfo; 039 040 // we'll do our own resource injection 041 setHandlerInitEnabled(false); 042 } 043 044 public ClassLoader getHandlerClassLoader() { 045 return this.classLoader; 046 } 047 048 protected List<Handler> buildHandlerChain(HandlerChainType hc, 049 ClassLoader classLoader) { 050 if (matchServiceName(portInfo, hc.getServiceNamePattern()) 051 && matchPortName(portInfo, hc.getPortNamePattern()) 052 && matchBinding(portInfo, hc.getProtocolBindings())) { 053 return super.buildHandlerChain(hc, classLoader); 054 } else { 055 return Collections.emptyList(); 056 } 057 } 058 059 private boolean matchServiceName(PortInfo info, String namePattern) { 060 return match((info == null ? null : info.getServiceName()), namePattern); 061 } 062 063 private boolean matchPortName(PortInfo info, String namePattern) { 064 return match((info == null ? null : info.getPortName()), namePattern); 065 } 066 067 private boolean matchBinding(PortInfo info, List<String> bindings) { 068 return match((info == null ? null : info.getBindingID()), bindings); 069 } 070 071 private boolean match(String binding, List<String> bindings) { 072 if (binding == null) { 073 return (bindings == null || bindings.isEmpty()); 074 } else { 075 if (bindings == null || bindings.isEmpty()) { 076 return true; 077 } else { 078 String actualBindingURI = JAXWSUtils.getBindingURI(binding); 079 for (String bindingToken : bindings) { 080 String bindingURI = JAXWSUtils.getBindingURI(bindingToken); 081 if (actualBindingURI.equals(bindingURI)) { 082 return true; 083 } 084 } 085 return false; 086 } 087 } 088 } 089 090 /* 091 * Performs basic localName matching, namespaces are not checked! 092 */ 093 private boolean match(QName name, String namePattern) { 094 if (name == null) { 095 return (namePattern == null || namePattern.equals("*")); 096 } else { 097 if (namePattern == null) { 098 return true; 099 } else { 100 String localNamePattern; 101 102 // get the local name from pattern 103 int pos = namePattern.indexOf(':'); 104 localNamePattern = (pos == -1) ? namePattern : namePattern 105 .substring(pos + 1); 106 localNamePattern = localNamePattern.trim(); 107 108 if (localNamePattern.equals("*")) { 109 // matches anything 110 return true; 111 } else if (localNamePattern.endsWith("*")) { 112 // match start 113 localNamePattern = localNamePattern.substring(0, 114 localNamePattern.length() - 1); 115 return name.getLocalPart().startsWith(localNamePattern); 116 } else { 117 // match exact 118 return name.getLocalPart().equals(localNamePattern); 119 } 120 } 121 } 122 } 123 124 }