Search in sources :

Example 71 with WebTarget

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();
}
Also used : Response(javax.ws.rs.core.Response) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) AllTaskListsModel(org.glassfish.jersey.examples.oauth2.googleclient.model.AllTaskListsModel) WebTarget(javax.ws.rs.client.WebTarget) TaskListModel(org.glassfish.jersey.examples.oauth2.googleclient.model.TaskListModel)

Example 72 with WebTarget

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());
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 73 with WebTarget

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);
}
Also used : WebTarget(javax.ws.rs.client.WebTarget)

Example 74 with WebTarget

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"));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) SslConfigurator(org.glassfish.jersey.SslConfigurator)

Example 75 with WebTarget

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);
}
Also used : FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) FormDataMultiPart(org.glassfish.jersey.media.multipart.FormDataMultiPart) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Aggregations

WebTarget (javax.ws.rs.client.WebTarget)1393 Response (javax.ws.rs.core.Response)712 Test (org.junit.Test)702 Client (javax.ws.rs.client.Client)318 JerseyTest (org.glassfish.jersey.test.JerseyTest)297 Invocation (javax.ws.rs.client.Invocation)106 List (java.util.List)76 ProcessingException (javax.ws.rs.ProcessingException)71 Builder (javax.ws.rs.client.Invocation.Builder)71 ClientBuilder (javax.ws.rs.client.ClientBuilder)69 GenericType (javax.ws.rs.core.GenericType)64 ArrayList (java.util.ArrayList)61 IOException (java.io.IOException)59 Message (com.remswork.project.alice.model.support.Message)58 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)58 InputStream (java.io.InputStream)52 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)52 ClientConfig (org.glassfish.jersey.client.ClientConfig)52 Form (javax.ws.rs.core.Form)42 MultiPart (org.glassfish.jersey.media.multipart.MultiPart)42