use of javax.ws.rs.client.Client in project jersey by jersey.
the class MainTest method _testWithoutSSLAuthentication.
/**
* Test to see that SSLHandshakeException is thrown when client don't have
* trusted key.
*/
private void _testWithoutSSLAuthentication(ClientConfig clientConfig) {
SslConfigurator sslConfig = SslConfigurator.newInstance().trustStoreFile(TRUSTORE_CLIENT_FILE).trustStorePassword(TRUSTSTORE_CLIENT_PWD);
Client client = ClientBuilder.newBuilder().withConfig(clientConfig).sslContext(sslConfig.createSSLContext()).build();
System.out.println("Client: GET " + Server.BASE_URI);
WebTarget target = client.target(Server.BASE_URI);
target.register(LoggingFeature.class);
boolean caught = false;
try {
target.path("/").request().get(String.class);
} catch (Exception e) {
caught = true;
}
assertTrue(caught);
// solaris throws java.net.SocketException instead of SSLHandshakeException
// assertTrue(msg.contains("SSLHandshakeException"));
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class App method main.
/**
* Main method that creates a {@link Client client} and initializes the OAuth support with
* configuration needed to connect to the Twitter and retrieve statuses.
* <p/>
* Execute this method to demo
*
* @param args Command line arguments.
* @throws Exception Thrown when error occurs.
*/
public static void main(final String[] args) throws Exception {
// retrieve consumer key/secret and token/secret from the property file
// or system properties
loadSettings();
final ConsumerCredentials consumerCredentials = new ConsumerCredentials(PROPERTIES.getProperty(PROPERTY_CONSUMER_KEY), PROPERTIES.getProperty(PROPERTY_CONSUMER_SECRET));
final Feature filterFeature;
if (PROPERTIES.getProperty(PROPERTY_TOKEN) == null) {
// we do not have Access Token yet. Let's perfom the Authorization Flow first,
// let the user approve our app and get Access Token.
final OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(consumerCredentials).authorizationFlow("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize").build();
final String authorizationUri = authFlow.start();
System.out.println("Enter the following URI into a web browser and authorize me:");
System.out.println(authorizationUri);
System.out.print("Enter the authorization code: ");
final String verifier;
try {
verifier = IN.readLine();
} catch (final IOException ex) {
throw new RuntimeException(ex);
}
final AccessToken accessToken = authFlow.finish(verifier);
// store access token for next application execution
PROPERTIES.setProperty(PROPERTY_TOKEN, accessToken.getToken());
PROPERTIES.setProperty(PROPERTY_TOKEN_SECRET, accessToken.getAccessTokenSecret());
// get the feature that will configure the client with consumer credentials and
// received access token
filterFeature = authFlow.getOAuth1Feature();
} else {
final AccessToken storedToken = new AccessToken(PROPERTIES.getProperty(PROPERTY_TOKEN), PROPERTIES.getProperty(PROPERTY_TOKEN_SECRET));
// build a new feature from the stored consumer credentials and access token
filterFeature = OAuth1ClientSupport.builder(consumerCredentials).feature().accessToken(storedToken).build();
}
// create a new Jersey client and register filter feature that will add OAuth signatures and
// JacksonFeature that will process returned JSON data.
final Client client = ClientBuilder.newBuilder().register(filterFeature).register(JacksonFeature.class).build();
// make requests to protected resources
// (no need to care about the OAuth signatures)
final Response response = client.target(FRIENDS_TIMELINE_URI).request().get();
if (response.getStatus() != 200) {
String errorEntity = null;
if (response.hasEntity()) {
errorEntity = response.readEntity(String.class);
}
throw new RuntimeException("Request to Twitter was not successful. Response code: " + response.getStatus() + ", reason: " + response.getStatusInfo().getReasonPhrase() + ", entity: " + errorEntity);
}
// persist the current consumer key/secret and token/secret for future use
storeSettings();
final List<Status> statuses = response.readEntity(new GenericType<List<Status>>() {
});
System.out.println("Tweets:\n");
for (final Status s : statuses) {
System.out.println(s.getText());
System.out.println("[posted by " + s.getUser().getName() + " at " + s.getCreatedAt() + "]");
}
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class HelloWorldTest method testLoggingFilterClientInstance.
@Test
@RunSeparately
public void testLoggingFilterClientInstance() {
Client client = client();
client.register(new CustomLoggingFilter()).property("foo", "bar");
CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
String s = target().path(App.ROOT_PATH).request().get(String.class);
assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
assertEquals(1, CustomLoggingFilter.preFilterCalled);
assertEquals(1, CustomLoggingFilter.postFilterCalled);
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class HelloWorldTest method testConfigurationUpdate.
@Test
@RunSeparately
public void testConfigurationUpdate() {
Client client1 = client();
client1.register(CustomLoggingFilter.class).property("foo", "bar");
Client client = ClientBuilder.newClient(client1.getConfiguration());
CustomLoggingFilter.preFilterCalled = CustomLoggingFilter.postFilterCalled = 0;
String s = target().path(App.ROOT_PATH).request().get(String.class);
assertEquals(HelloWorldResource.CLICHED_MESSAGE, s);
assertEquals(1, CustomLoggingFilter.preFilterCalled);
assertEquals(1, CustomLoggingFilter.postFilterCalled);
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class BasicClientTest method testCustomExecutorsSync.
@Test
public void testCustomExecutorsSync() throws ExecutionException, InterruptedException {
ClientConfig jerseyConfig = new ClientConfig();
jerseyConfig.register(CustomExecutorProvider.class).register(ThreadInterceptor.class);
Client client = ClientBuilder.newClient(jerseyConfig);
runCustomExecutorTestSync(client);
}
Aggregations