Thu, 31 Aug 2017 15:18:52 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.xml.internal.ws.transport.http; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import com.sun.xml.internal.ws.transport.http.DeploymentDescriptorParser.AdapterFactory; |
aoqi@0 | 29 | import com.sun.xml.internal.ws.api.server.WSEndpoint; |
aoqi@0 | 30 | import com.sun.xml.internal.ws.api.server.PortAddressResolver; |
aoqi@0 | 31 | import com.sun.xml.internal.ws.api.model.wsdl.WSDLPort; |
aoqi@0 | 32 | import com.sun.istack.internal.NotNull; |
aoqi@0 | 33 | |
aoqi@0 | 34 | import javax.xml.namespace.QName; |
aoqi@0 | 35 | import java.util.List; |
aoqi@0 | 36 | import java.util.ArrayList; |
aoqi@0 | 37 | import java.util.Map; |
aoqi@0 | 38 | import java.util.HashMap; |
aoqi@0 | 39 | import java.util.AbstractList; |
aoqi@0 | 40 | import java.util.Map.Entry; |
aoqi@0 | 41 | |
aoqi@0 | 42 | /** |
aoqi@0 | 43 | * List of {@link HttpAdapter}s created together. |
aoqi@0 | 44 | * |
aoqi@0 | 45 | * <p> |
aoqi@0 | 46 | * Some cases WAR file may contain multiple endpoints for ports in a WSDL. |
aoqi@0 | 47 | * If the runtime knows these ports, their port addresses can be patched. |
aoqi@0 | 48 | * This class keeps a list of {@link HttpAdapter}s and use that information to patch |
aoqi@0 | 49 | * multiple port addresses. |
aoqi@0 | 50 | * |
aoqi@0 | 51 | * <p> |
aoqi@0 | 52 | * Concrete implementations of this class need to override {@link #createHttpAdapter} |
aoqi@0 | 53 | * method to create implementations of {@link HttpAdapter}. |
aoqi@0 | 54 | * |
aoqi@0 | 55 | * @author Jitendra Kotamraju |
aoqi@0 | 56 | */ |
aoqi@0 | 57 | public abstract class HttpAdapterList<T extends HttpAdapter> extends AbstractList<T> implements AdapterFactory<T> { |
aoqi@0 | 58 | private final List<T> adapters = new ArrayList<T>(); |
aoqi@0 | 59 | private final Map<PortInfo, String> addressMap = new HashMap<PortInfo, String>(); |
aoqi@0 | 60 | |
aoqi@0 | 61 | // TODO: documented because it's used by AS |
aoqi@0 | 62 | @Override |
aoqi@0 | 63 | public T createAdapter(String name, String urlPattern, WSEndpoint<?> endpoint) { |
aoqi@0 | 64 | T t = createHttpAdapter(name, urlPattern, endpoint); |
aoqi@0 | 65 | adapters.add(t); |
aoqi@0 | 66 | WSDLPort port = endpoint.getPort(); |
aoqi@0 | 67 | if (port != null) { |
aoqi@0 | 68 | PortInfo portInfo = new PortInfo(port.getOwner().getName(),port.getName().getLocalPart(), endpoint.getImplementationClass()); |
aoqi@0 | 69 | addressMap.put(portInfo, getValidPath(urlPattern)); |
aoqi@0 | 70 | } |
aoqi@0 | 71 | return t; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * Implementations need to override this one to create a concrete class |
aoqi@0 | 76 | * of HttpAdapter |
aoqi@0 | 77 | */ |
aoqi@0 | 78 | protected abstract T createHttpAdapter(String name, String urlPattern, WSEndpoint<?> endpoint); |
aoqi@0 | 79 | |
aoqi@0 | 80 | /** |
aoqi@0 | 81 | * @return urlPattern without "/*" |
aoqi@0 | 82 | */ |
aoqi@0 | 83 | private String getValidPath(@NotNull String urlPattern) { |
aoqi@0 | 84 | if (urlPattern.endsWith("/*")) { |
aoqi@0 | 85 | return urlPattern.substring(0, urlPattern.length() - 2); |
aoqi@0 | 86 | } else { |
aoqi@0 | 87 | return urlPattern; |
aoqi@0 | 88 | } |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | /** |
aoqi@0 | 92 | * Creates a PortAddressResolver that maps portname to its address |
aoqi@0 | 93 | * |
aoqi@0 | 94 | * @param endpointImpl application endpoint Class that eventually serves the request. |
aoqi@0 | 95 | */ |
aoqi@0 | 96 | public PortAddressResolver createPortAddressResolver(final String baseAddress, final Class<?> endpointImpl) { |
aoqi@0 | 97 | return new PortAddressResolver() { |
aoqi@0 | 98 | @Override |
aoqi@0 | 99 | public String getAddressFor(@NotNull QName serviceName, @NotNull String portName) { |
aoqi@0 | 100 | String urlPattern = addressMap.get(new PortInfo(serviceName,portName, endpointImpl)); |
aoqi@0 | 101 | if (urlPattern == null) { |
aoqi@0 | 102 | //if a WSDL defines more ports, urlpattern is null (portName does not match endpointImpl) |
aoqi@0 | 103 | //so fallback to the default behaviour where only serviceName/portName is checked |
aoqi@0 | 104 | for (Entry<PortInfo, String> e : addressMap.entrySet()) { |
aoqi@0 | 105 | if (serviceName.equals(e.getKey().serviceName) && portName.equals(e.getKey().portName)) { |
aoqi@0 | 106 | urlPattern = e.getValue(); |
aoqi@0 | 107 | break; |
aoqi@0 | 108 | } |
aoqi@0 | 109 | } |
aoqi@0 | 110 | } |
aoqi@0 | 111 | return (urlPattern == null) ? null : baseAddress+urlPattern; |
aoqi@0 | 112 | } |
aoqi@0 | 113 | }; |
aoqi@0 | 114 | } |
aoqi@0 | 115 | |
aoqi@0 | 116 | |
aoqi@0 | 117 | @Override |
aoqi@0 | 118 | public T get(int index) { |
aoqi@0 | 119 | return adapters.get(index); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | @Override |
aoqi@0 | 123 | public int size() { |
aoqi@0 | 124 | return adapters.size(); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | private static class PortInfo { |
aoqi@0 | 128 | private final QName serviceName; |
aoqi@0 | 129 | private final String portName; |
aoqi@0 | 130 | private final Class<?> implClass; |
aoqi@0 | 131 | |
aoqi@0 | 132 | PortInfo(@NotNull QName serviceName, @NotNull String portName, Class<?> implClass) { |
aoqi@0 | 133 | this.serviceName = serviceName; |
aoqi@0 | 134 | this.portName = portName; |
aoqi@0 | 135 | this.implClass = implClass; |
aoqi@0 | 136 | } |
aoqi@0 | 137 | |
aoqi@0 | 138 | @Override |
aoqi@0 | 139 | public boolean equals(Object portInfo) { |
aoqi@0 | 140 | if (portInfo instanceof PortInfo) { |
aoqi@0 | 141 | PortInfo that = (PortInfo)portInfo; |
aoqi@0 | 142 | if (this.implClass == null) { |
aoqi@0 | 143 | return this.serviceName.equals(that.serviceName) && this.portName.equals(that.portName) && that.implClass == null; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | return this.serviceName.equals(that.serviceName) && this.portName.equals(that.portName) && this.implClass.equals(that.implClass); |
aoqi@0 | 146 | } |
aoqi@0 | 147 | return false; |
aoqi@0 | 148 | } |
aoqi@0 | 149 | |
aoqi@0 | 150 | @Override |
aoqi@0 | 151 | public int hashCode() { |
aoqi@0 | 152 | int retVal = serviceName.hashCode()+portName.hashCode(); |
aoqi@0 | 153 | return implClass != null ? retVal + implClass.hashCode() : retVal; |
aoqi@0 | 154 | } |
aoqi@0 | 155 | } |
aoqi@0 | 156 | } |