/** * Closes the transport level conduit in the client. Reopening a new connection, requires creating * a new client object using the build() method in this builder. * * @param root The resource returned by the build() method of this builder class */ public static void closeClient(ApiRootResource root) { ClientConfiguration config = WebClient.getConfig(root); HTTPConduit conduit = config.getHttpConduit(); if (conduit == null) { throw new IllegalArgumentException("Client is not using the HTTP transport"); } conduit.close(); }
protected WebClient createWebClient(final String url, final String mediaType) { final List<?> providers = Arrays.asList(new JacksonJsonProvider()); final WebClient wc = WebClient.create("http://localhost:" + getPort() + url, providers).accept(mediaType); WebClient.getConfig(wc).getHttpConduit().getClient().setReceiveTimeout(10000000L); return wc; }
public static <S> S createServiceClient(URL url, Class<S> serviceClass) { String baseAddress = url.toString().concat("ldp"); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Create service client '" + serviceClass.getCanonicalName() + "'"); LOGGER.debug(String.format("\t- Using base address '%s'...", baseAddress)); } S proxy = JAXRSClientFactory.create(baseAddress, serviceClass); WebClient.getConfig(proxy).getBus().setProperty("org.apache.cxf.http.header.split", true); return proxy; }
@Override public Builder request() { checkClosed(); initTargetClientIfNeeded(); ClientProviderFactory pf = ClientProviderFactory.getInstance(WebClient.getConfig(targetClient).getEndpoint()); List<Object> providers = new LinkedList<Object>(); Configuration cfg = configImpl.getConfiguration(); for (Object p : cfg.getInstances()) { if (!(p instanceof Feature)) { Map<Class<?>, Integer> contracts = cfg.getContracts(p.getClass()); if (contracts == null || contracts.isEmpty()) { providers.add(p); } else { providers.add(new FilterProviderInfo<Object>(p, pf.getBus(), contracts)); } } } pf.setUserProviders(providers); WebClient.getConfig(targetClient) .getRequestContext() .putAll(getConfiguration().getProperties()); WebClient.getConfig(targetClient) .getRequestContext() .put(Client.class.getName(), ClientImpl.this); WebClient.getConfig(targetClient) .getRequestContext() .put(Configuration.class.getName(), getConfiguration()); // TLS TLSClientParameters tlsParams = secConfig.getTlsClientParams(); if (tlsParams.getSSLSocketFactory() != null || tlsParams.getTrustManagers() != null) { WebClient.getConfig(targetClient).getHttpConduit().setTlsClientParameters(tlsParams); } // start building the invocation return new InvocationBuilderImpl(WebClient.fromClient(targetClient)); }
@org.junit.Test public void testKerberos() throws Exception { URL busFile = JAXRSAuthenticationTest.class.getResource("cxf-client.xml"); String address = "https://*****:*****@service.ws.apache.org"); authSupplier.setServiceNameType(GSSName.NT_HOSTBASED_SERVICE); WebClient.getConfig(client).getHttpConduit().setAuthSupplier(authSupplier); Number numberToDouble = new Number(); numberToDouble.setDescription("This is the number to double"); numberToDouble.setNumber(25); Response response = client.post(numberToDouble); Assert.assertEquals(response.getStatus(), 200); Assert.assertEquals(response.readEntity(Number.class).getNumber(), 50); }
@Override public Builder property(String name, Object value) { Map<String, Object> contextProps = WebClient.getConfig(webClient).getRequestContext(); Map<String, Object> filterProps = CastUtils.cast((Map<?, ?>) contextProps.get(PROPERTY_KEY)); if (filterProps == null) { filterProps = new HashMap<String, Object>(); contextProps.put(PROPERTY_KEY, filterProps); } if (value == null) { filterProps.remove(name); } else { filterProps.put(name, value); } return this; }
/** * Build a client proxy, for a specific proxy type. * * @param proxyType proxy type class * @return client proxy stub */ protected <T> T build(Class<T> proxyType) { String address = generateAddress(); T rootResource; // Synchronized on the class to correlate with the scope of clientStaticResources // We want to ensure that the shared bean isn't set concurrently in multiple callers synchronized (ClouderaManagerClientBuilder.class) { JAXRSClientFactoryBean bean = cleanFactory(clientStaticResources.getUnchecked(proxyType)); bean.setAddress(address); if (username != null) { bean.setUsername(username); bean.setPassword(password); } if (enableLogging) { bean.setFeatures(Arrays.<AbstractFeature>asList(new LoggingFeature())); } rootResource = bean.create(proxyType); } boolean isTlsEnabled = address.startsWith("https://"); ClientConfiguration config = WebClient.getConfig(rootResource); HTTPConduit conduit = (HTTPConduit) config.getConduit(); if (isTlsEnabled) { TLSClientParameters tlsParams = new TLSClientParameters(); if (!validateCerts) { tlsParams.setTrustManagers(new TrustManager[] {new AcceptAllTrustManager()}); } else if (trustManagers != null) { tlsParams.setTrustManagers(trustManagers); } tlsParams.setDisableCNCheck(!validateCn); conduit.setTlsClientParameters(tlsParams); } HTTPClientPolicy policy = conduit.getClient(); policy.setConnectionTimeout(connectionTimeoutUnits.toMillis(connectionTimeout)); policy.setReceiveTimeout(receiveTimeoutUnits.toMillis(receiveTimeout)); return rootResource; }
@Override public DataSource invoke(DataSource msg) { try { final URL url = new URL(this.endpoint); final String httpMethod = (String) this.requestContext.get(MessageContext.HTTP_REQUEST_METHOD); Map<String, List<String>> header = (Map<String, List<String>>) this.requestContext.get(MessageContext.HTTP_REQUEST_HEADERS); for (Map.Entry<String, List<String>> entry : header.entrySet()) { this.client.header(entry.getKey(), entry.getValue().toArray()); } String username = (String) this.requestContext.get(Dispatch.USERNAME_PROPERTY); String password = (String) this.requestContext.get(Dispatch.PASSWORD_PROPERTY); if (username != null) { this.client.header( AUTHORIZATION, "Basic " + Base64.encodeBytes((username + ':' + password).getBytes())); // $NON-NLS-1$ } else if (this.requestContext.get(GSSCredential.class.getName()) != null) { WebClient.getConfig(this.client) .getRequestContext() .put( GSSCredential.class.getName(), this.requestContext.get(GSSCredential.class.getName())); WebClient.getConfig(this.client) .getRequestContext() .put("auth.spnego.requireCredDelegation", true); // $NON-NLS-1$ } else if (this.requestContext.get(OAuthCredential.class.getName()) != null) { OAuthCredential credential = (OAuthCredential) this.requestContext.get(OAuthCredential.class.getName()); this.client.header( AUTHORIZATION, credential.getAuthorizationHeader(this.endpoint, httpMethod)); } InputStream payload = null; if (msg != null) { payload = msg.getInputStream(); } HTTPClientPolicy clientPolicy = WebClient.getConfig(this.client).getHttpConduit().getClient(); Long timeout = (Long) this.requestContext.get(RECEIVE_TIMEOUT); if (timeout != null) { clientPolicy.setReceiveTimeout(timeout); } timeout = (Long) this.requestContext.get(CONNECTION_TIMEOUT); if (timeout != null) { clientPolicy.setConnectionTimeout(timeout); } javax.ws.rs.core.Response response = this.client.invoke(httpMethod, payload); this.responseContext.put(WSConnection.STATUS_CODE, response.getStatus()); this.responseContext.putAll(response.getMetadata()); ArrayList contentTypes = (ArrayList) this.responseContext.get("content-type"); // $NON-NLS-1$ String contentType = contentTypes != null ? (String) contentTypes.get(0) : "application/octet-stream"; //$NON-NLS-1$ return new HttpDataSource(url, (InputStream) response.getEntity(), contentType); } catch (IOException e) { throw new WebServiceException(e); } }