Search in sources :

Example 1 with Link

use of javax.ws.rs.core.Link in project jersey by jersey.

the class EntityDescriptor method findFields.

/**
     * Find and cache the fields of the supplied class and its superclasses and
     * interfaces.
     * @param entityClass the class
     */
private void findFields(Class<?> entityClass) {
    for (Field f : entityClass.getDeclaredFields()) {
        InjectLink a = f.getAnnotation(InjectLink.class);
        Class<?> t = f.getType();
        if (a != null) {
            if (t.equals(String.class) || t.equals(URI.class) || Link.class.isAssignableFrom(t)) {
                if (!linkFields.containsKey(f.getName())) {
                    linkFields.put(f.getName(), new InjectLinkFieldDescriptor(f, a, t));
                }
            } else {
            // TODO unsupported type
            }
        } else if (f.isAnnotationPresent(InjectLinks.class)) {
            if (List.class.isAssignableFrom(t) || t.isArray() && Link.class.isAssignableFrom(t.getComponentType())) {
                InjectLinks a2 = f.getAnnotation(InjectLinks.class);
                linkFields.put(f.getName(), new InjectLinksFieldDescriptor(f, a2, t));
            } else {
                throw new IllegalArgumentException("Can only inject links onto a List<Link> or Link[] object");
            }
        } else {
            // see issue http://java.net/jira/browse/JERSEY-625
            if ((f.getModifiers() & Modifier.STATIC) > 0 || f.getName().startsWith("java.") || f.getName().startsWith("javax.")) {
                continue;
            }
            nonLinkFields.put(f.getName(), new FieldDescriptor(f));
        }
    }
    // look for nonLinkFields in superclasses
    Class<?> sc = entityClass.getSuperclass();
    if (sc != null && sc != Object.class) {
        findFields(sc);
    }
    // look for nonLinkFields in interfaces
    for (Class<?> ic : entityClass.getInterfaces()) {
        findFields(ic);
    }
}
Also used : Field(java.lang.reflect.Field) Link(javax.ws.rs.core.Link)

Example 2 with Link

use of javax.ws.rs.core.Link in project jersey by jersey.

the class LinkTest method testFromResourceMethod.

@Test
public void testFromResourceMethod() {
    Link link = Link.fromMethod(Resource.class, "producesXml").build();
    assertEquals("producesxml", link.getUri().toString());
}
Also used : Link(javax.ws.rs.core.Link) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 3 with Link

use of javax.ws.rs.core.Link in project jersey by jersey.

the class ResponseLinksTest method testGetLinks.

/**
     * Reproducer for JERSEY-2168
     */
@Test
public void testGetLinks() {
    WebTarget target = target("test");
    Response response = target.path("2").request(MediaType.APPLICATION_JSON).get();
    Set<Link> links = response.getLinks();
    Assert.assertEquals(2, links.size());
    Assert.assertNotNull(response.getLink("prev"));
    Assert.assertTrue(response.getLink("prev").getUri().toString().endsWith("2?limit=50"));
    Assert.assertNotNull(response.getLink("next"));
    Assert.assertEquals("next page", response.getLink("next").getTitle());
    Assert.assertTrue(response.getLink("next").getUri().toString().endsWith("2?limit=50&action=next"));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Link(javax.ws.rs.core.Link) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 4 with Link

use of javax.ws.rs.core.Link in project jersey by jersey.

the class InboundMessageContextTest method testGetLinksWithMultipleLinksInOneHeaderWithLtInValue.

@Test
public void testGetLinksWithMultipleLinksInOneHeaderWithLtInValue() {
    InboundMessageContext r = createInboundMessageContext();
    Link link1 = Link.fromUri("https://example.org/one/api/groups?page=2").rel("next").param("foo", "<bar>").build();
    Link link2 = Link.fromUri("https://example.org/one/api/groups?page=39").rel("last").param("bar", "<<foo").build();
    r.header("Link", "<https://example.org/one/api/groups?page=2>; rel=\"next\"; foo=\"<bar>\", <https://example.org/one/api/groups?page=39>; rel=\"last\"; bar=\"<<foo\"");
    assertEquals(2, r.getLinks().size());
    assertTrue(r.getLinks().contains(link1));
    assertTrue(r.getLinks().contains(link2));
}
Also used : Link(javax.ws.rs.core.Link) Test(org.junit.Test)

Example 5 with Link

use of javax.ws.rs.core.Link in project jersey by jersey.

the class InboundMessageContextTest method testGetLink.

@Test
public void testGetLink() {
    InboundMessageContext r = createInboundMessageContext();
    Link link1 = Link.fromUri("http://example.org/app/link1").param("produces", "application/json").param("method", "GET").rel("self").build();
    Link link2 = Link.fromUri("http://example.org/app/link2").param("produces", "application/xml").param("method", "PUT").rel("update").build();
    Link link3 = Link.fromUri("http://example.org/app/link2").param("produces", "application/xml").param("method", "POST").rel("update").build();
    r.header("Link", link1.toString());
    r.header("Link", link2.toString());
    r.header("Link", link3.toString());
    assertTrue(r.getLink("self").equals(link1));
    assertTrue(r.getLink("update").equals(link2) || r.getLink("update").equals(link3));
}
Also used : Link(javax.ws.rs.core.Link) Test(org.junit.Test)

Aggregations

Link (javax.ws.rs.core.Link)76 Test (org.junit.Test)45 URI (java.net.URI)11 Response (javax.ws.rs.core.Response)10 JerseyTest (org.glassfish.jersey.test.JerseyTest)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 GET (javax.ws.rs.GET)2 Client (javax.ws.rs.client.Client)2 WebTarget (javax.ws.rs.client.WebTarget)2 ObjectStoreException (com.arjuna.ats.arjuna.exceptions.ObjectStoreException)1 JoinLRAException (io.narayana.lra.client.participant.JoinLRAException)1 TerminationException (io.narayana.lra.client.participant.TerminationException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1