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);
}
}
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());
}
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"));
}
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));
}
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));
}
Aggregations