org.apache.cxf.jaxrs.client.spec.ClientImpl Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.cxf.jaxrs.client.spec;
import java.net.URI;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.Invocation.Builder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Configurable;
import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.apache.cxf.configuration.jsse.TLSClientParameters;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.ClientProviderFactory;
import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.model.FilterProviderInfo;
import org.apache.cxf.transport.https.SSLUtils;
public class ClientImpl implements Client {
private static final String HTTP_CONNECTION_TIMEOUT_PROP = "http.connection.timeout";
private static final String HTTP_RECEIVE_TIMEOUT_PROP = "http.receive.timeout";
private static final String HTTP_PROXY_SERVER_PROP = "http.proxy.server.uri";
private static final String HTTP_PROXY_SERVER_PORT_PROP = "http.proxy.server.port";
private Configurable configImpl;
private TLSConfiguration secConfig;
private boolean closed;
private Set baseClients =
Collections.newSetFromMap(new WeakHashMap());
public ClientImpl(Configuration config,
TLSConfiguration secConfig) {
configImpl = new ClientConfigurableImpl(this, config);
this.secConfig = secConfig;
}
@Override
public void close() {
if (!closed) {
for (WebClient wc : baseClients) {
wc.close();
}
baseClients = null;
closed = true;
}
}
@Override
public Builder invocation(Link link) {
checkNull(link);
checkClosed();
Builder builder = target(link.getUriBuilder()).request();
String type = link.getType();
if (type != null) {
builder.accept(type);
}
return builder;
}
@Override
public WebTarget target(UriBuilder builder) {
checkNull(builder);
checkClosed();
return new WebTargetImpl(builder, getConfiguration());
}
@Override
public WebTarget target(String address) {
checkNull(address);
if (address.isEmpty()) {
address = "/";
}
return target(UriBuilder.fromUri(address));
}
@Override
public WebTarget target(Link link) {
checkNull(link);
return target(link.getUriBuilder());
}
@Override
public WebTarget target(URI uri) {
checkNull(uri);
return target(UriBuilder.fromUri(uri));
}
private void checkNull(Object... target) {
for (Object o : target) {
if (o == null) {
throw new NullPointerException("Value is null");
}
}
}
@Override
public HostnameVerifier getHostnameVerifier() {
checkClosed();
return secConfig.getTlsClientParams().getHostnameVerifier();
}
@Override
public SSLContext getSslContext() {
checkClosed();
if (secConfig.getSslContext() != null) {
return secConfig.getSslContext();
} else if (secConfig.getTlsClientParams().getTrustManagers() != null) {
try {
return SSLUtils.getSSLContext(secConfig.getTlsClientParams());
} catch (Exception ex) {
throw new ProcessingException(ex);
}
} else {
return null;
}
}
private void checkClosed() {
if (closed) {
throw new IllegalStateException();
}
}
@Override
public Configuration getConfiguration() {
checkClosed();
return configImpl.getConfiguration();
}
@Override
public Client property(String name, Object value) {
checkClosed();
return configImpl.property(name, value);
}
@Override
public Client register(Class> cls) {
checkClosed();
return configImpl.register(cls);
}
@Override
public Client register(Object object) {
checkClosed();
return configImpl.register(object);
}
@Override
public Client register(Class> cls, int index) {
checkClosed();
return configImpl.register(cls, index);
}
@Override
public Client register(Class> cls, Class>... contracts) {
checkClosed();
return configImpl.register(cls, contracts);
}
@Override
public Client register(Class> cls, Map, Integer> map) {
checkClosed();
return configImpl.register(cls, map);
}
@Override
public Client register(Object object, int index) {
checkClosed();
return configImpl.register(object, index);
}
@Override
public Client register(Object object, Class>... contracts) {
checkClosed();
return configImpl.register(object, contracts);
}
@Override
public Client register(Object object, Map, Integer> map) {
checkClosed();
return configImpl.register(object, map);
}
public class WebTargetImpl implements WebTarget {
private Configurable configImpl;
private UriBuilder uriBuilder;
private WebClient targetClient;
public WebTargetImpl(UriBuilder uriBuilder,
Configuration config) {
this(uriBuilder, config, null);
}
public WebTargetImpl(UriBuilder uriBuilder,
Configuration config,
WebClient targetClient) {
this.configImpl = new ClientConfigurableImpl(this, config);
this.uriBuilder = uriBuilder.clone();
this.targetClient = targetClient;
}
public WebClient getWebClient() {
return this.targetClient;
}
@Override
public Builder request() {
checkClosed();
initTargetClientIfNeeded();
ClientProviderFactory pf =
ClientProviderFactory.getInstance(WebClient.getConfig(targetClient).getEndpoint());
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy