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: */package org.apache.geronimo.cxf;
017:
018: import java.util.Collections;
019: import java.util.List;
020:
021: import javax.xml.namespace.QName;
022: import javax.xml.ws.handler.Handler;
023: import javax.xml.ws.handler.PortInfo;
024:
025: import org.apache.cxf.jaxws.handler.AnnotationHandlerChainBuilder;
026: import org.apache.cxf.jaxws.javaee.HandlerChainType;
027: import org.apache.geronimo.jaxws.JAXWSUtils;
028:
029: public class GeronimoHandlerChainBuilder extends
030: 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()),
061: namePattern);
062: }
063:
064: private boolean matchPortName(PortInfo info, String namePattern) {
065: return match((info == null ? null : info.getPortName()),
066: namePattern);
067: }
068:
069: private boolean matchBinding(PortInfo info, List<String> bindings) {
070: return match((info == null ? null : info.getBindingID()),
071: bindings);
072: }
073:
074: private boolean match(String binding, List<String> bindings) {
075: if (binding == null) {
076: return (bindings == null || bindings.isEmpty());
077: } else {
078: if (bindings == null || bindings.isEmpty()) {
079: return true;
080: } else {
081: String actualBindingURI = JAXWSUtils
082: .getBindingURI(binding);
083: for (String bindingToken : bindings) {
084: String bindingURI = JAXWSUtils
085: .getBindingURI(bindingToken);
086: if (actualBindingURI.equals(bindingURI)) {
087: return true;
088: }
089: }
090: return false;
091: }
092: }
093: }
094:
095: /*
096: * Performs basic localName matching, namespaces are not checked!
097: */
098: private boolean match(QName name, String namePattern) {
099: if (name == null) {
100: return (namePattern == null || namePattern.equals("*"));
101: } else {
102: if (namePattern == null) {
103: return true;
104: } else {
105: String localNamePattern;
106:
107: // get the local name from pattern
108: int pos = namePattern.indexOf(':');
109: localNamePattern = (pos == -1) ? namePattern
110: : namePattern.substring(pos + 1);
111: localNamePattern = localNamePattern.trim();
112:
113: if (localNamePattern.equals("*")) {
114: // matches anything
115: return true;
116: } else if (localNamePattern.endsWith("*")) {
117: // match start
118: localNamePattern = localNamePattern.substring(0,
119: localNamePattern.length() - 1);
120: return name.getLocalPart().startsWith(
121: localNamePattern);
122: } else {
123: // match exact
124: return name.getLocalPart().equals(localNamePattern);
125: }
126: }
127: }
128: }
129:
130: }
|