本文整理匯總了Java中javax.ws.rs.client.ClientBuilder.build方法的典型用法代碼示例。如果您正苦於以下問題:Java ClientBuilder.build方法的具體用法?Java ClientBuilder.build怎麽用?Java ClientBuilder.build使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.ws.rs.client.ClientBuilder
的用法示例。
在下文中一共展示了ClientBuilder.build方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createClientFromConfig
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
private static Client createClientFromConfig(NiFiRegistryClientConfig registryClientConfig) {
final ClientConfig clientConfig = new ClientConfig();
clientConfig.register(jacksonJaxbJsonProvider());
final ClientBuilder clientBuilder = ClientBuilder.newBuilder().withConfig(clientConfig);
final SSLContext sslContext = registryClientConfig.getSslContext();
if (sslContext != null) {
clientBuilder.sslContext(sslContext);
}
final HostnameVerifier hostnameVerifier = registryClientConfig.getHostnameVerifier();
if (hostnameVerifier != null) {
clientBuilder.hostnameVerifier(hostnameVerifier);
}
return clientBuilder.build();
}
示例2: callTokenService
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
/**
* Invoke the token web service with specified parameters.
*/
@Nullable
private AccessTokenResponse callTokenService( @Nonnull final MultivaluedMap<String, String> parameters )
{
final ClientBuilder builder =
ClientBuilder.newBuilder().register( JacksonFeature.class );
final String clientSecret = _config.getClientSecret();
if ( null != clientSecret )
{
builder.register( new BasicAuthFilter( _config.getClientID(), clientSecret ) );
}
final Client client = builder.build();
try
{
final WebTarget target = client.
target( _config.getServerUrl() ).
path( "/realms/" ).path( _config.getRealm() ).path( "/protocol/openid-connect/token" );
final Response response = target.
request( MediaType.APPLICATION_FORM_URLENCODED ).
accept( MediaType.APPLICATION_JSON ).
post( Entity.form( parameters ) );
if ( Response.Status.Family.SUCCESSFUL == response.getStatusInfo().getFamily() )
{
return response.readEntity( AccessTokenResponse.class );
}
else
{
return null;
}
}
finally
{
client.close();
}
}
示例3: obtainAccessToken
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
public String obtainAccessToken(String username, String password) {
Form form = new Form();
form.param("grant_type", "password");
form.param("username", username);
form.param("password", password);
form.param("client_id", deployment.getClientId());
String secret = deployment.getClientCredentials().get("secret").toString();
form.param("client_secret", secret);
Client client = null;
try {
ClientBuilder clientBuilder = ClientBuilder.newBuilder();
SSLContext sslcontext = deployment.getSSLContext();
if(sslcontext != null) {
client = clientBuilder.sslContext(sslcontext).hostnameVerifier(new AnyHostnameVerifier()).build();
} else {
client = clientBuilder.build();
}
String tokenURL = String.format("%s/auth/realms/%s/protocol/openid-connect/token",
deployment.getAuthServerUrl(), deployment.getRealm());
WebTarget target = client.target(tokenURL);
if(deployment.getDebug() > 0)
target.register(new LoggingFilter());
String json = target.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE), String.class);
AccessToken accessToken = JsonSerialization.readValue(json, AccessToken.class);
return accessToken.getToken();
} catch (Exception e) {
throw new RuntimeException("Failed to request token", e);
} finally {
if (client != null) {
client.close();
}
}
}
示例4: getClient
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
private Client getClient( List<String> clientIdKeys, ClientBuilder newClientBuilder ) {
// sort so can get same key for same inputs
Collections.sort(clientIdKeys);
finalClientIdKey = clientIdKeys.toString();
Client client = clients.get(finalClientIdKey);
if (client == null) {
// no appropriate client, create one
client = newClientBuilder.build();
clients.put(finalClientIdKey, client);
}
return client;
}
示例5: configure
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
@Override
public void configure(HttpConfig config, ScriptContext context) {
ClientConfig cc = new ClientConfig();
// support request body for DELETE (non-standard)
cc.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
if (!config.isFollowRedirects()) {
cc.property(ClientProperties.FOLLOW_REDIRECTS, false);
}
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
.withConfig(cc)
.register(new LoggingInterceptor(context)) // must be first
.register(MultiPartFeature.class);
if (config.isSslEnabled()) {
SSLContext sslContext;
if (config.getSslTrustStore() != null) {
String trustStoreFile = config.getSslTrustStore();
String password = config.getSslTrustStorePassword();
char[] passwordChars = password == null ? null : password.toCharArray();
String algorithm = config.getSslAlgorithm();
String type = config.getSslTrustStoreType();
if (type == null) {
type = KeyStore.getDefaultType();
}
try {
KeyStore trustStore = KeyStore.getInstance(type);
InputStream is = FileUtils.getFileStream(trustStoreFile, context);
trustStore.load(is, passwordChars);
context.logger.debug("trust store key count: {}", trustStore.size());
sslContext = SslConfigurator.newInstance()
.securityProtocol(algorithm) // will default to TLS if null
.trustStore(trustStore)
// .keyStore(trustStore)
.createSSLContext();
} catch (Exception e) {
context.logger.error("ssl config failed: {}", e.getMessage());
throw new RuntimeException(e);
}
} else {
sslContext = HttpUtils.getSslContext(config.getSslAlgorithm());
}
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
clientBuilder.sslContext(sslContext);
clientBuilder.hostnameVerifier((host, session) -> true);
}
client = clientBuilder.build();
client.property(ClientProperties.CONNECT_TIMEOUT, config.getConnectTimeout());
client.property(ClientProperties.READ_TIMEOUT, config.getReadTimeout());
if (config.getProxyUri() != null) {
client.property(ClientProperties.PROXY_URI, config.getProxyUri());
if (config.getProxyUsername() != null && config.getProxyPassword() != null) {
client.property(ClientProperties.PROXY_USERNAME, config.getProxyUsername());
client.property(ClientProperties.PROXY_PASSWORD, config.getProxyPassword());
}
}
}
示例6: doGet
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
PrintWriter out = response.getWriter();
out.println("<html><body><h2>RP Signed JWT App</h2><br>");
out.println("<HR>RS Invocation<p>");
// call the RS with the jwt we just built.
out.println("Using RS JEE Endpoint of : " + RS_JEE_ENDPOINT + "<br>");
out.println("Using RS SPRING Endpoint of : " + RS_SPRING_ENDPOINT + "<br>");
ClientBuilder cb = ClientBuilder.newBuilder();
cb.property("com.ibm.ws.jaxrs.client.disableCNCheck", true);
cb.property("com.ibm.ws.jaxrs.client.ssl.config", "defaultSSLConfig");
cb.register(JwtJaxRSClientFilter.class);
Client c = cb.build();
String result = "";
try {
result = c.target(RS_JEE_ENDPOINT).request().get(String.class);
result = "<hr><br>[START_RESPONSE_FROM_JEE_RS]<br>" + result + "<br>[END_RESPONSE_FROM_JEE_RS]";
} finally {
c.close();
}
c = cb.build();
try {
String springresult = c.target(RS_SPRING_ENDPOINT).request().get(String.class);
result = result + "<hr><br>[START_RESPONSE_FROM_SPRING_RS]<br>" + springresult + "<br>[END_RESPONSE_FROM_SPRING_RS]";
} finally {
c.close();
}
// in a real app, the response would likely be some json, or
// info to use within the app processing. But in this example,
// the response is just some text for us to display back to the
// user to show the processing performed by the RS.
out.println(result);
}
示例7: createBackup
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
public static BackupStats createBackup(DACConfig dacConfig, String userName, String password, KeyStore trustStore, URI uri) throws IOException {
final JacksonJaxbJsonProvider provider = new JacksonJaxbJsonProvider();
provider.setMapper(JSONUtil.prettyMapper());
ClientBuilder clientBuilder = ClientBuilder.newBuilder()
.register(provider)
.register(MultiPartFeature.class);
if (trustStore != null) {
clientBuilder.trustStore(trustStore);
} else {
SSLContext sslContext = SSLHelper.newAllTrustingSSLContext("SSL");
HostnameVerifier verifier = SSLHelper.newAllValidHostnameVerifier();
clientBuilder.hostnameVerifier(verifier);
clientBuilder.sslContext(sslContext);
}
final Client client = clientBuilder.build();
WebTarget target = client.target(format("%s://%s:%d",
dacConfig.webSSLEnabled ? "https" : "http", dacConfig.masterNode, dacConfig.getHttpPort())).path("apiv2");
final UserLogin userLogin = new UserLogin(userName, password);
final UserLoginSession userLoginSession = readEntity(UserLoginSession.class, target.path("/login").request(JSON).buildPost(Entity.json(userLogin)));
return readEntity(BackupStats.class, target.path("/backup").request(JSON).header(HttpHeader.AUTHORIZATION.toString(),
TokenUtils.AUTH_HEADER_PREFIX + userLoginSession.getToken()).buildPost(Entity.json(uri.toString())));
}
示例8: getRatings
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
private JsonObject getRatings(Cookie user, String xreq, String xtraceid, String xspanid,
String xparentspanid, String xsampled, String xflags, String xotspan){
ClientBuilder cb = ClientBuilder.newBuilder();
String timeout = star_color.equals("black") ? "10000" : "2500";
cb.property("com.ibm.ws.jaxrs.client.connection.timeout", timeout);
cb.property("com.ibm.ws.jaxrs.client.receive.timeout", timeout);
Client client = cb.build();
WebTarget ratingsTarget = client.target(ratings_service);
Invocation.Builder builder = ratingsTarget.request(MediaType.APPLICATION_JSON);
if(xreq!=null) {
builder.header("x-request-id",xreq);
}
if(xtraceid!=null) {
builder.header("x-b3-traceid",xtraceid);
}
if(xspanid!=null) {
builder.header("x-b3-spanid",xspanid);
}
if(xparentspanid!=null) {
builder.header("x-b3-parentspanid",xparentspanid);
}
if(xsampled!=null) {
builder.header("x-b3-sampled",xsampled);
}
if(xflags!=null) {
builder.header("x-b3-flags",xflags);
}
if(xotspan!=null) {
builder.header("x-ot-span-context",xotspan);
}
if(user!=null) {
builder.cookie(user);
}
Response r = builder.get();
int statusCode = r.getStatusInfo().getStatusCode();
if (statusCode == Response.Status.OK.getStatusCode() ) {
StringReader stringReader = new StringReader(r.readEntity(String.class));
try (JsonReader jsonReader = Json.createReader(stringReader)) {
JsonObject j = jsonReader.readObject();
JsonObjectBuilder jb = Json.createObjectBuilder();
for(String key : j.keySet()){
int count = j.getInt(key);
String stars = "<font color=\""+ star_color +"\">";
for(int i=0; i<count; i++){
stars += "<span class=\"glyphicon glyphicon-star\"></span>";
}
stars += "</font>";
if(count<5){
for(int i=0; i<(5-count); i++){
stars += "<span class=\"glyphicon glyphicon-star-empty\"></span>";
}
}
jb.add(key,stars);
}
JsonObject result = jb.build();
return result;
}
}else{
System.out.println("Error: unable to contact "+ratings_service+" got status of "+statusCode);
return null;
}
}
示例9: JerseyNiFiRegistryClient
import javax.ws.rs.client.ClientBuilder; //導入方法依賴的package包/類
private JerseyNiFiRegistryClient(final NiFiRegistryClient.Builder builder) {
final NiFiRegistryClientConfig registryClientConfig = builder.getConfig();
if (registryClientConfig == null) {
throw new IllegalArgumentException("NiFiRegistryClientConfig cannot be null");
}
String baseUrl = registryClientConfig.getBaseUrl();
if (StringUtils.isBlank(baseUrl)) {
throw new IllegalArgumentException("Base URL cannot be blank");
}
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}
if (!baseUrl.endsWith(NIFI_REGISTRY_CONTEXT)) {
baseUrl = baseUrl + "/" + NIFI_REGISTRY_CONTEXT;
}
try {
new URI(baseUrl);
} catch (final Exception e) {
throw new IllegalArgumentException("Invalid base URL: " + e.getMessage(), e);
}
final SSLContext sslContext = registryClientConfig.getSslContext();
final HostnameVerifier hostnameVerifier = registryClientConfig.getHostnameVerifier();
final ClientBuilder clientBuilder = ClientBuilder.newBuilder();
if (sslContext != null) {
clientBuilder.sslContext(sslContext);
}
if (hostnameVerifier != null) {
clientBuilder.hostnameVerifier(hostnameVerifier);
}
final int connectTimeout = registryClientConfig.getConnectTimeout() == null ? DEFAULT_CONNECT_TIMEOUT : registryClientConfig.getConnectTimeout();
final int readTimeout = registryClientConfig.getReadTimeout() == null ? DEFAULT_READ_TIMEOUT : registryClientConfig.getReadTimeout();
final ClientConfig clientConfig = new ClientConfig();
clientConfig.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout);
clientConfig.property(ClientProperties.READ_TIMEOUT, readTimeout);
clientConfig.register(jacksonJaxbJsonProvider());
clientBuilder.withConfig(clientConfig);
this.client = clientBuilder.build();
this.baseTarget = client.target(baseUrl);
this.bucketClient = new JerseyBucketClient(baseTarget);
this.flowClient = new JerseyFlowClient(baseTarget);
this.flowSnapshotClient = new JerseyFlowSnapshotClient(baseTarget);
this.itemsClient = new JerseyItemsClient(baseTarget);
}