use of javax.ws.rs.client.WebTarget in project jersey by jersey.
the class TaskResource method getTasksResponse.
/**
* Queries task data from google.
*
* @param client Client configured for authentication with access token.
* @return Google task data response or redirect to google authorize page response.
*/
private Response getTasksResponse(final Client client) {
client.register(JacksonFeature.class);
client.register(new LoggingFeature(Logger.getLogger("example.client.tasks"), LoggingFeature.Verbosity.PAYLOAD_ANY));
final WebTarget baseTarget = client.target(GOOGLE_TASKS_BASE_URI);
final Response response = baseTarget.path("users/@me/lists").request().get();
final List<TaskListModel> listOfTaskLists;
switch(response.getStatus()) {
case //Response.Status.UNAUTHORIZED
401:
SimpleOAuthService.setAccessToken(null);
return googleAuthRedirect();
case //Response.Status.OK
200:
listOfTaskLists = processTaskLists(baseTarget, response.readEntity(TaskRootBean.class));
break;
default:
listOfTaskLists = null;
}
final AllTaskListsModel tasks = new AllTaskListsModel(listOfTaskLists);
return Response.ok(tasks).build();
}
use of javax.ws.rs.client.WebTarget in project jersey by jersey.
the class WebAppFelixTest method testNonRecursiveWebResources.
@Test
public void testNonRecursiveWebResources() throws Exception {
final WebTarget target = webAppTestTarget("/n-webresources");
// send request and check response - helloworld resource
final String helloResult = target.path("/helloworld").request().build("GET").invoke().readEntity(String.class);
LOGGER.info("HELLO RESULT = " + helloResult);
assertEquals("Hello World", helloResult);
// send request and check response - another resource
final String anotherResult = target.path("/another").request().build("GET").invoke().readEntity(String.class);
LOGGER.info("ANOTHER RESULT = " + anotherResult);
assertEquals("Another", anotherResult);
// send request and check response for the additional bundle - should fail now
final String additionalResult = target.path("/additional").request().build("GET").invoke().readEntity(String.class);
LOGGER.info("ADDITIONAL RESULT = " + additionalResult);
assertEquals("Additional Bundle!", additionalResult);
// send request and check response for the sub-packaged additional bundle
final Response subAdditionalResponse = target.path("/subadditional").request().build("GET").invoke();
LOGGER.info("SUB-PACKAGED ADDITIONAL http status = " + subAdditionalResponse.getStatus());
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), subAdditionalResponse.getStatus());
// send request and check response for the WEB-INF classes located resource
final String webInfClassesResourceResult = target.path("/webinf").request().build("GET").invoke().readEntity(String.class);
LOGGER.info("WEB-INF CLASSES RESOURCE RESULT = " + webInfClassesResourceResult);
assertEquals("WebInfClassesResource", webInfClassesResourceResult);
// send request and check response for the WEB-INF classes located resource
final Response webInfClassesSubPackagedResourceResponse = target.path("/subwebinf").request().build("GET").invoke();
LOGGER.info("WEB-INF CLASSES SUB-PACKAGED http status = " + webInfClassesSubPackagedResourceResponse.getStatus());
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), webInfClassesSubPackagedResourceResponse.getStatus());
}
use of javax.ws.rs.client.WebTarget in project jersey by jersey.
the class TraceSupportTest method prepareTarget.
private WebTarget prepareTarget(String path) {
final WebTarget target = target();
target.register(LoggingFeature.class);
return target.path(path);
}
use of javax.ws.rs.client.WebTarget 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.WebTarget in project jersey by jersey.
the class MultiPartWebAppTest method testXmlJAXBPart.
@Test
public void testXmlJAXBPart() {
final WebTarget target = target().path("form/xml-jaxb-part");
final FormDataMultiPart mp = new FormDataMultiPart();
mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("bean").fileName("bean").build(), new Bean("BEAN"), MediaType.APPLICATION_XML_TYPE));
mp.bodyPart(new FormDataBodyPart(FormDataContentDisposition.name("string").fileName("string").build(), "STRING"));
final String s = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE), String.class);
assertEquals("STRING:string,BEAN:bean", s);
}
Aggregations