Java Code Examples for javax.ws.rs.core.Link#Builder

The following examples show how to use javax.ws.rs.core.Link#Builder . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuildObjects() throws Exception {
    StringBuilder path1 = new StringBuilder().append("p1");
    ByteArrayInputStream path2 = new ByteArrayInputStream("p2".getBytes()) {
        @Override
        public String toString() {
            return "p2";
        }
    };
    URI path3 = new URI("p3");

    String expected = "<" + "http://host.com:888/" + "p1/p2/p3" + ">";
    Link.Builder builder = Link.fromUri("http://host.com:888/" + "{x1}/{x2}/{x3}");
    Link link = builder.build(path1, path2, path3);
    assertNotNull(link);
    assertEquals(link.toString(), expected);
}
 
Example 2
Source File: MementoResource.java    From trellis with Apache License 2.0 5 votes vote down vote up
/**
 * Filter link parameters from a provided Link object, if configured to do so.
 * @param link the link
 * @param filter whether to filter the memento parameters
 * @return a Link without Memento parameters, if desired; otherwise, the original link
 */
public static Link filterLinkParams(final Link link, final boolean filter) {
    // from and until parameters can cause problems with downstream applications because they contain commas. This
    // method makes it possible to filter out those params, if desired. By default, they are not filtered out.
    if (filter) {
        if (TIMEMAP.equals(link.getRel())) {
            return Link.fromUri(link.getUri()).rel(TIMEMAP).type(APPLICATION_LINK_FORMAT).build();
        } else if (link.getRels().contains(MEMENTO)) {
            final Link.Builder builder = Link.fromUri(link.getUri());
            link.getRels().forEach(builder::rel);
            return builder.build();
        }
    }
    return link;
}
 
Example 3
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildRelativized() throws Exception {

    Link.Builder linkBuilder = new LinkBuilderImpl();
    URI base = URI.create("http://example.com/page2");
    Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").buildRelativized(base);
    assertEquals("<page1>;rel=\"previous\"", prevLink.toString());
}
 
Example 4
Source File: ResponseBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLinksWithReset() {
    MetadataMap<String, Object> m = new MetadataMap<>();
    m.add("Link", Link.valueOf("<http://example.com/page3>;rel=\"next\""));
    RuntimeDelegateImpl delegate = new RuntimeDelegateImpl();
    Link.Builder linkBuilder = delegate.createLinkBuilder();
    Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").build();
    linkBuilder = delegate.createLinkBuilder();
    Link nextLink = linkBuilder.uri("http://example.com/page3").rel("next").build();
    // CHECK: Should .links() do a reset? Undocumented feature; so we'll
    // test with the awkward <code>(Link[])null</code> instead..
    // Note: .cookie() has same behavior.
    checkBuild(Response.ok().links(prevLink).links((Link[])null).links(nextLink).build(), 200, null, m);
}
 
Example 5
Source File: ResponseBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testLinksNoReset() {
    MetadataMap<String, Object> m = new MetadataMap<>();
    m.add("Link", Link.valueOf("<http://example.com/page1>;rel=\"previous\""));
    m.add("Link", Link.valueOf("<http://example.com/page3>;rel=\"next\""));
    RuntimeDelegateImpl delegate = new RuntimeDelegateImpl();
    Link.Builder linkBuilder = delegate.createLinkBuilder();
    Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").build();
    linkBuilder = delegate.createLinkBuilder();
    Link nextLink = linkBuilder.uri("http://example.com/page3").rel("next").build();
    checkBuild(Response.ok().links(prevLink).links(nextLink).build(), 200, null, m);
}
 
Example 6
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("to be fixed for TCK")
public void testNoArgsThrowsUriBuilderExceptionTest() {
    Link.Builder builder = Link.fromUri("http://:@");
    try {
        Link link = builder.build();
        fail("No exception has been thrown for link " + link);
    } catch (UriBuilderException e) {
        //expected
    }
}
 
Example 7
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateFromMethod() throws Exception {
    Link.Builder linkBuilder = Link.fromMethod(TestResource.class, "consumesAppJson");
    Link link = linkBuilder.build();
    String resource = link.toString();
    assertTrue(resource.contains("<consumesappjson>"));
}
 
Example 8
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testRelativeLink() throws Exception {
    Link.Builder linkBuilder = Link.fromUri("relative");
    linkBuilder.baseUri("http://localhost:8080/base/path");
    Link link = linkBuilder.rel("next").build();
    assertEquals("<http://localhost:8080/base/relative>;rel=\"next\"", link.toString());
}
 
Example 9
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testRelativeLink2() throws Exception {
    Link.Builder linkBuilder = Link.fromUri("/relative");
    linkBuilder.baseUri("http://localhost:8080/base/path");
    Link link = linkBuilder.rel("next").build();
    assertEquals("<http://localhost:8080/relative>;rel=\"next\"", link.toString());
}
 
Example 10
Source File: ResponseImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Link.Builder getLinkBuilder(String relation) {
    Link link = getLink(relation);
    return link == null ? null : Link.fromLink(link);
}
 
Example 11
Source File: LinkHeaderProvider.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Link fromString(String value) {

        if (value == null) {
            throw new IllegalArgumentException("Link value can not be null");
        }
        value = value.trim();
        int closeIndex = value.indexOf('>');
        if (!value.startsWith("<") || closeIndex < 2) {
            throw new IllegalArgumentException("Link URI is missing");
        }
        Link.Builder builder = new LinkBuilderImpl();
        builder.uri(value.substring(1, closeIndex).trim());
        if (closeIndex < value.length() - 1) {

            String[] tokens = value.substring(closeIndex + 1).split(";");
            for (String token : tokens) {
                String theToken = token.trim();
                if (theToken.isEmpty()) {
                    continue;
                }
                String paramName = null;
                String paramValue = null;
                int i = token.indexOf('=');
                if (i != -1) {
                    paramName = theToken.substring(0, i).trim();
                    paramValue = i == theToken.length() - 1 ? "" : theToken.substring(i + 1).trim();
                }
                if (REL.equals(paramName)) {
                    String[] rels = removeQuotesIfNeeded(paramValue).split(",");
                    for (String rel : rels) {
                        builder.rel(rel.trim());
                    }
                } else if (TYPE.equals(paramName)) {
                    builder.type(removeQuotesIfNeeded(paramValue));
                } else if (TITLE.equals(paramName)) {
                    builder.title(removeQuotesIfNeeded(paramValue));
                } else {
                    builder.param(paramName, paramValue);
                }
            }
        }
        return builder.build();

    }
 
Example 12
Source File: ResponseBuilderImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseBuilder link(URI href, String rel) {
    Link.Builder linkBuilder = new LinkBuilderImpl();
    return links(linkBuilder.uri(href).rel(rel).build());
}
 
Example 13
Source File: ResponseBuilderImpl.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Override
public ResponseBuilder link(String href, String rel) {
    Link.Builder linkBuilder = new LinkBuilderImpl();
    return links(linkBuilder.uri(href).rel(rel).build());
}
 
Example 14
Source File: MvcRuntimeDelegate.java    From portals-pluto with Apache License 2.0 4 votes vote down vote up
@Override
public Link.Builder createLinkBuilder() {
	return null;
}
 
Example 15
Source File: MSF4JRuntimeDelegate.java    From msf4j with Apache License 2.0 4 votes vote down vote up
@Override
public Link.Builder createLinkBuilder() {
    throw new UnsupportedOperationException();
}
 
Example 16
Source File: MSF4JResponse.java    From msf4j with Apache License 2.0 4 votes vote down vote up
@Override
public Link.Builder getLinkBuilder(String relation) {
    throw new UnsupportedOperationException();
}
 
Example 17
Source File: LinkBuilderImpl.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Link.Builder baseUri(URI uri) {
    this.baseUri = uri;
    return this;
}
 
Example 18
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testBuild() throws Exception {
    Link.Builder linkBuilder = new LinkBuilderImpl();
    Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").build();
    assertEquals("<http://example.com/page1>;rel=\"previous\"", prevLink.toString());
}
 
Example 19
Source File: LinkBuilderImplTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testSeveralAttributes() throws Exception {
    Link.Builder linkBuilder = new LinkBuilderImpl();
    Link prevLink = linkBuilder.uri("http://example.com/page1").rel("previous").title("A title").build();
    assertEquals("<http://example.com/page1>;rel=\"previous\";title=\"A title\"", prevLink.toString());
}
 
Example 20
Source File: RuntimeDelegateImpl.java    From everrest with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Link.Builder createLinkBuilder() {
    return new LinkBuilderImpl();
}