ohair@286: /* alanb@368: * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.xml.internal.ws.transport.http; ohair@286: ohair@286: import com.sun.xml.internal.ws.transport.http.DeploymentDescriptorParser.AdapterFactory; ohair@286: import com.sun.xml.internal.ws.api.server.WSEndpoint; ohair@286: import com.sun.xml.internal.ws.api.server.PortAddressResolver; ohair@286: import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; ohair@286: import com.sun.istack.internal.NotNull; ohair@286: ohair@286: import javax.xml.namespace.QName; ohair@286: import java.util.List; ohair@286: import java.util.ArrayList; ohair@286: import java.util.Map; ohair@286: import java.util.HashMap; ohair@286: import java.util.AbstractList; alanb@368: import java.util.Map.Entry; ohair@286: ohair@286: /** ohair@286: * List of {@link HttpAdapter}s created together. ohair@286: * ohair@286: *

ohair@286: * Some cases WAR file may contain multiple endpoints for ports in a WSDL. ohair@286: * If the runtime knows these ports, their port addresses can be patched. ohair@286: * This class keeps a list of {@link HttpAdapter}s and use that information to patch ohair@286: * multiple port addresses. ohair@286: * ohair@286: *

ohair@286: * Concrete implementations of this class need to override {@link #createHttpAdapter} ohair@286: * method to create implementations of {@link HttpAdapter}. ohair@286: * ohair@286: * @author Jitendra Kotamraju ohair@286: */ ohair@286: public abstract class HttpAdapterList extends AbstractList implements AdapterFactory { ohair@286: private final List adapters = new ArrayList(); ohair@286: private final Map addressMap = new HashMap(); ohair@286: ohair@286: // TODO: documented because it's used by AS alanb@368: @Override ohair@286: public T createAdapter(String name, String urlPattern, WSEndpoint endpoint) { ohair@286: T t = createHttpAdapter(name, urlPattern, endpoint); ohair@286: adapters.add(t); ohair@286: WSDLPort port = endpoint.getPort(); ohair@286: if (port != null) { alanb@368: PortInfo portInfo = new PortInfo(port.getOwner().getName(),port.getName().getLocalPart(), endpoint.getImplementationClass()); ohair@286: addressMap.put(portInfo, getValidPath(urlPattern)); ohair@286: } ohair@286: return t; ohair@286: } ohair@286: ohair@286: /** ohair@286: * Implementations need to override this one to create a concrete class ohair@286: * of HttpAdapter ohair@286: */ ohair@286: protected abstract T createHttpAdapter(String name, String urlPattern, WSEndpoint endpoint); ohair@286: ohair@286: /** ohair@286: * @return urlPattern without "/*" ohair@286: */ ohair@286: private String getValidPath(@NotNull String urlPattern) { ohair@286: if (urlPattern.endsWith("/*")) { ohair@286: return urlPattern.substring(0, urlPattern.length() - 2); ohair@286: } else { ohair@286: return urlPattern; ohair@286: } ohair@286: } ohair@286: ohair@286: /** ohair@286: * Creates a PortAddressResolver that maps portname to its address alanb@368: * alanb@368: * @param endpointImpl application endpoint Class that eventually serves the request. ohair@286: */ alanb@368: public PortAddressResolver createPortAddressResolver(final String baseAddress, final Class endpointImpl) { ohair@286: return new PortAddressResolver() { alanb@368: @Override ohair@286: public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) { alanb@368: String urlPattern = addressMap.get(new PortInfo(serviceName,portName, endpointImpl)); alanb@368: if (urlPattern == null) { alanb@368: //if a WSDL defines more ports, urlpattern is null (portName does not match endpointImpl) alanb@368: //so fallback to the default behaviour where only serviceName/portName is checked alanb@368: for (Entry e : addressMap.entrySet()) { alanb@368: if (serviceName.equals(e.getKey().serviceName) && portName.equals(e.getKey().portName)) { alanb@368: urlPattern = e.getValue(); alanb@368: break; alanb@368: } alanb@368: } alanb@368: } ohair@286: return (urlPattern == null) ? null : baseAddress+urlPattern; ohair@286: } ohair@286: }; ohair@286: } ohair@286: ohair@286: alanb@368: @Override ohair@286: public T get(int index) { ohair@286: return adapters.get(index); ohair@286: } ohair@286: alanb@368: @Override ohair@286: public int size() { ohair@286: return adapters.size(); ohair@286: } ohair@286: ohair@286: private static class PortInfo { ohair@286: private final QName serviceName; ohair@286: private final String portName; alanb@368: private final Class implClass; ohair@286: alanb@368: PortInfo(@NotNull QName serviceName, @NotNull String portName, Class implClass) { ohair@286: this.serviceName = serviceName; ohair@286: this.portName = portName; alanb@368: this.implClass = implClass; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public boolean equals(Object portInfo) { ohair@286: if (portInfo instanceof PortInfo) { ohair@286: PortInfo that = (PortInfo)portInfo; alanb@368: if (this.implClass == null) { alanb@368: return this.serviceName.equals(that.serviceName) && this.portName.equals(that.portName) && that.implClass == null; alanb@368: } alanb@368: return this.serviceName.equals(that.serviceName) && this.portName.equals(that.portName) && this.implClass.equals(that.implClass); ohair@286: } ohair@286: return false; ohair@286: } ohair@286: ohair@286: @Override ohair@286: public int hashCode() { alanb@368: int retVal = serviceName.hashCode()+portName.hashCode(); alanb@368: return implClass != null ? retVal + implClass.hashCode() : retVal; ohair@286: } ohair@286: } ohair@286: }