|
| 1 | +/* |
| 2 | + * Copyright (c) 2019 Oracle and/or its affiliates. All rights reserved. |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the |
| 5 | + * terms of the Eclipse Public License v. 2.0, which is available at |
| 6 | + * http://www.eclipse.org/legal/epl-2.0. |
| 7 | + * |
| 8 | + * This Source Code may also be made available under the following Secondary |
| 9 | + * Licenses when the conditions for such availability set forth in the |
| 10 | + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, |
| 11 | + * version 2 with the GNU Classpath Exception, which is available at |
| 12 | + * https://www.gnu.org/software/classpath/license.html. |
| 13 | + * |
| 14 | + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 |
| 15 | + */ |
| 16 | + |
| 17 | +package org.glassfish.jersey.client; |
| 18 | + |
| 19 | +import org.glassfish.jersey.client.spi.InvocationBuilderListener; |
| 20 | +import org.glassfish.jersey.internal.inject.InjectionManager; |
| 21 | +import org.glassfish.jersey.internal.inject.Providers; |
| 22 | +import org.glassfish.jersey.model.internal.RankedComparator; |
| 23 | + |
| 24 | +import javax.ws.rs.client.Invocation; |
| 25 | +import javax.ws.rs.core.CacheControl; |
| 26 | +import javax.ws.rs.core.Configuration; |
| 27 | +import javax.ws.rs.core.Cookie; |
| 28 | +import javax.ws.rs.core.HttpHeaders; |
| 29 | +import javax.ws.rs.core.MediaType; |
| 30 | +import javax.ws.rs.core.MultivaluedMap; |
| 31 | +import java.net.URI; |
| 32 | +import java.util.Collection; |
| 33 | +import java.util.Iterator; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Locale; |
| 36 | +import java.util.Map; |
| 37 | + |
| 38 | +/** |
| 39 | + * Client request processing stage. During a request creation, when the {@link Invocation.Builder} |
| 40 | + * would be created, this class is utilized. |
| 41 | + */ |
| 42 | +/* package */ class InvocationBuilderListenerStage { |
| 43 | + final Iterator<InvocationBuilderListener> invocationBuilderListenerIterator; |
| 44 | + |
| 45 | + /* package */ InvocationBuilderListenerStage(InjectionManager injectionManager) { |
| 46 | + final RankedComparator<InvocationBuilderListener> comparator = |
| 47 | + new RankedComparator<>(RankedComparator.Order.ASCENDING); |
| 48 | + invocationBuilderListenerIterator = Providers |
| 49 | + .getAllProviders(injectionManager, InvocationBuilderListener.class, comparator).iterator(); |
| 50 | + } |
| 51 | + |
| 52 | + /* package */ void invokeListener(JerseyInvocation.Builder builder) { |
| 53 | + while (invocationBuilderListenerIterator.hasNext()) { |
| 54 | + invocationBuilderListenerIterator.next().onNewBuilder(new InvocationBuilderContextImpl(builder)); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static class InvocationBuilderContextImpl implements InvocationBuilderListener.InvocationBuilderContext { |
| 59 | + private final JerseyInvocation.Builder builder; |
| 60 | + |
| 61 | + private InvocationBuilderContextImpl(JerseyInvocation.Builder builder) { |
| 62 | + this.builder = builder; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public InvocationBuilderListener.InvocationBuilderContext accept(String... mediaTypes) { |
| 67 | + builder.accept(mediaTypes); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public InvocationBuilderListener.InvocationBuilderContext accept(MediaType... mediaTypes) { |
| 73 | + builder.accept(mediaTypes); |
| 74 | + return this; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public InvocationBuilderListener.InvocationBuilderContext acceptLanguage(Locale... locales) { |
| 79 | + builder.acceptLanguage(locales); |
| 80 | + return this; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public InvocationBuilderListener.InvocationBuilderContext acceptLanguage(String... locales) { |
| 85 | + builder.acceptLanguage(locales); |
| 86 | + return this; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public InvocationBuilderListener.InvocationBuilderContext acceptEncoding(String... encodings) { |
| 91 | + builder.acceptEncoding(encodings); |
| 92 | + return this; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public InvocationBuilderListener.InvocationBuilderContext cookie(Cookie cookie) { |
| 97 | + builder.cookie(cookie); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public InvocationBuilderListener.InvocationBuilderContext cookie(String name, String value) { |
| 103 | + builder.cookie(name, value); |
| 104 | + return this; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public InvocationBuilderListener.InvocationBuilderContext cacheControl(CacheControl cacheControl) { |
| 109 | + builder.cacheControl(cacheControl); |
| 110 | + return this; |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public List<String> getAccepted() { |
| 115 | + return getHeader(HttpHeaders.ACCEPT); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public List<String> getAcceptedLanguages() { |
| 120 | + return getHeader(HttpHeaders.ACCEPT_LANGUAGE); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public List<CacheControl> getCacheControls() { |
| 125 | + return (List<CacheControl>) (List<?>) builder.request().getHeaders().get(HttpHeaders.CACHE_CONTROL); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Configuration getConfiguration() { |
| 130 | + return builder.request().getConfiguration(); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public Map<String, Cookie> getCookies() { |
| 135 | + return builder.request().getCookies(); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public List<String> getEncodings() { |
| 140 | + return getHeader(HttpHeaders.ACCEPT_ENCODING); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public List<String> getHeader(String name) { |
| 145 | + return builder.request().getRequestHeader(name); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public MultivaluedMap<String, Object> getHeaders() { |
| 150 | + return builder.request().getHeaders(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public Object getProperty(String name) { |
| 155 | + return builder.request().getProperty(name); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public Collection<String> getPropertyNames() { |
| 160 | + return builder.request().getPropertyNames(); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public URI getUri() { |
| 165 | + return builder.request().getUri(); |
| 166 | + } |
| 167 | + |
| 168 | + |
| 169 | + @Override |
| 170 | + public InvocationBuilderListener.InvocationBuilderContext header(String name, Object value) { |
| 171 | + builder.header(name, value); |
| 172 | + return this; |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public InvocationBuilderListener.InvocationBuilderContext headers(MultivaluedMap<String, Object> headers) { |
| 177 | + builder.headers(headers); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public InvocationBuilderListener.InvocationBuilderContext property(String name, Object value) { |
| 183 | + builder.property(name, value); |
| 184 | + return this; |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void removeProperty(String name) { |
| 189 | + builder.request().removeProperty(name); |
| 190 | + } |
| 191 | + } |
| 192 | +} |
| 193 | + |
0 commit comments