Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions docs/src/main/docbook/declarative-linking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,58 @@ final Application application = new ResourceConfig()
.packages("org.glassfish.jersey.examples.linking")
.register(DeclarativeLinkingFeature.class);</programlisting>
</example>

You can also change the default linking style for the whole application. Either by using the constructor of
<literal>DeclarativeLinkingFeature</literal>, or by setting the config value for
<literal>DeclarativeLinkingFeature.DEFAULT_LINK_STYLE</literal>.

<example>
<title>Creating JAX-RS application with Declarative Linking and default link style set to ABSOLUTE.</title>

<programlisting language="java">// Create JAX-RS application.
final Application application = new ResourceConfig()
.packages("org.glassfish.jersey.examples.linking")
.register(new DeclarativeLinkingFeature(InjectLink.Style.ABSOLUTE));</programlisting>
</example>

The default value will be overridden if it is defined on the annotation
<literal>@InjectLink</literal>/<literal>@ProvideLink</literal>.

</para>

<para>
Possible values are:
<variablelist>
<varlistentry>
<term>
<literal>ABSOLUTE</literal>
</term>
<listitem>
<para>
An absolute URI. The URI template will be prefixed with the absolute base URI of the application.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>ABSOLUTE_PATH</literal>
</term>
<listitem>
<para>
An absolute path. The URI template will be prefixed with the absolute base path of the application.
This is also the <literal>DEFAULT</literal> behavior if nothing else is set.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<literal>RELATIVE_PATH</literal>
</term>
<listitem>
<para>A relative path. The URI template will be converted to a relative path with no prefix.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</section>
</chapter>
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import org.glassfish.jersey.examples.linking.resources.ItemsResource;
import org.glassfish.jersey.linking.Binding;
import org.glassfish.jersey.linking.InjectLink;
import org.glassfish.jersey.linking.InjectLink.Style;
import org.glassfish.jersey.linking.InjectLinks;

/**
Expand All @@ -70,7 +69,6 @@
@InjectLinks({
@InjectLink(
resource = ItemsResource.class,
style = Style.ABSOLUTE,
method = "query",
condition = "${instance.offset + instance.limit < instance.modelLimit}",
bindings = {
Expand All @@ -81,7 +79,6 @@
),
@InjectLink(
resource = ItemsResource.class,
style = Style.ABSOLUTE,
method = "query",
condition = "${instance.offset - instance.limit >= 0}",
bindings = {
Expand All @@ -105,7 +102,6 @@ public class ItemsRepresentation {
@InjectLink(
resource = ItemsResource.class,
method = "query",
style = Style.ABSOLUTE,
bindings = {@Binding(name = "offset", value = "${instance.offset}"),
@Binding(name = "limit", value = "${instance.limit}")
},
Expand All @@ -118,7 +114,6 @@ public class ItemsRepresentation {
@InjectLinks({
@InjectLink(
resource = ItemsResource.class,
style = Style.ABSOLUTE,
method = "query",
condition = "${instance.offset + instance.limit < instance.modelLimit}",
bindings = {
Expand All @@ -129,7 +124,6 @@ public class ItemsRepresentation {
),
@InjectLink(
resource = ItemsResource.class,
style = Style.ABSOLUTE,
method = "query",
condition = "${instance.offset - instance.limit >= 0}",
bindings = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

import org.glassfish.jersey.examples.linking.resources.ItemsResource;
import org.glassfish.jersey.linking.DeclarativeLinkingFeature;
import org.glassfish.jersey.linking.InjectLink;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.glassfish.jersey.test.TestProperties;
Expand All @@ -66,7 +67,7 @@ public class LinkWebAppTest extends JerseyTest {
protected ResourceConfig configure() {
enable(TestProperties.LOG_TRAFFIC);
final ResourceConfig rc = new ResourceConfig(ItemsResource.class);
rc.register(DeclarativeLinkingFeature.class);
rc.register(new DeclarativeLinkingFeature(InjectLink.Style.ABSOLUTE));
return rc;
}

Expand Down
5 changes: 5 additions & 0 deletions incubator/declarative-linking/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,36 @@
@Beta
public class DeclarativeLinkingFeature implements Feature {

/**
* Sets the default link Style for this feature, see {@link InjectLink.Style} for the possible values.
*/
public static final String DEFAULT_LINK_STYLE = "org.glassfish.jersey.linking.default.link.style";

private final InjectLink.Style defaultLinkStyle;

public DeclarativeLinkingFeature(InjectLink.Style defaultLinkStyle) {

this.defaultLinkStyle = defaultLinkStyle;
}

public DeclarativeLinkingFeature(){
this(null);
}

@Override
public boolean configure(FeatureContext context) {

Configuration config = context.getConfiguration();

Object linkStyle = (defaultLinkStyle == null) ? config.getProperty(DEFAULT_LINK_STYLE) : defaultLinkStyle;
if (linkStyle instanceof String) {
context.property(DEFAULT_LINK_STYLE, InjectLink.Style.valueOf(((String) linkStyle).toUpperCase()));
} else if (linkStyle instanceof InjectLink.Style){
context.property(DEFAULT_LINK_STYLE, linkStyle);
} else if (linkStyle == null) {
context.property(DEFAULT_LINK_STYLE, InjectLink.Style.DEFAULT);
}

if (!config.isRegistered(ResponseLinkFilter.class)) {
context.register(new AbstractBinder() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,24 @@ static URI buildURI(InjectLinkDescriptor link,
template = expr.getValue(context).toString();

// now process any embedded URI template parameters
UriBuilder ub = applyLinkStyle(template, link.getLinkStyle(), uriInfo);
UriBuilder ub = applyLinkStyle(template, link.getLinkStyle(), rmc.getLinkStyle(), uriInfo);
UriTemplateParser parser = new UriTemplateParser(template);
List<String> parameterNames = parser.getNames();
Map<String, Object> valueMap = getParameterValues(parameterNames, link, context, uriInfo);
return ub.buildFromMap(valueMap);
}

private static UriBuilder applyLinkStyle(String template, InjectLink.Style style, UriInfo uriInfo) {
private static UriBuilder applyLinkStyle(String template,
InjectLink.Style linkStyle,
InjectLink.Style contextStyle,
UriInfo uriInfo) {
UriBuilder ub = null;
InjectLink.Style style = (linkStyle == InjectLink.Style.DEFAULT) ? contextStyle : linkStyle;
switch (style) {
case ABSOLUTE:
ub = uriInfo.getBaseUriBuilder().path(template);
break;
case DEFAULT:
case ABSOLUTE_PATH:
String basePath = uriInfo.getBaseUri().getPath();
ub = UriBuilder.fromPath(basePath).path(template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,20 @@ enum Style {
* A relative path. The URI template will be converted to a relative
* path with no prefix.
*/
RELATIVE_PATH
RELATIVE_PATH,

/**
* Default style which can be configured for all links by {@link DeclarativeLinkingFeature#DEFAULT_LINK_STYLE}.
* If both are {@code DEFAULT} then it will use the {@link #ABSOLUTE_PATH} style.
*/
DEFAULT

}

/**
* The style of URI to inject
*/
Style style() default Style.ABSOLUTE_PATH;
Style style() default Style.DEFAULT;

/**
* Specifies a URI template that will be used to build the injected URI. The
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
/**
* The style of URI to inject
*/
InjectLink.Style style() default InjectLink.Style.ABSOLUTE_PATH;
InjectLink.Style style() default InjectLink.Style.DEFAULT;

/**
* Provide links for representation classes listed here.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Configuration;
import javax.ws.rs.core.Context;

import org.glassfish.jersey.linking.DeclarativeLinkingFeature;
import org.glassfish.jersey.linking.InjectLink;
import org.glassfish.jersey.process.Inflector;
import org.glassfish.jersey.server.ExtendedResourceContext;
import org.glassfish.jersey.server.model.HandlerConstructor;
Expand Down Expand Up @@ -76,8 +78,11 @@ public class NaiveResourceMappingContext implements ResourceMappingContext {

private Map<Class<?>, ResourceMappingContext.Mapping> mappings;

public NaiveResourceMappingContext(@Context final ExtendedResourceContext erc) {
private final InjectLink.Style linkStyle;

public NaiveResourceMappingContext(@Context final ExtendedResourceContext erc, @Context Configuration configuration) {
this.erc = erc;
this.linkStyle = (InjectLink.Style) configuration.getProperty(DeclarativeLinkingFeature.DEFAULT_LINK_STYLE);
}

@Override
Expand All @@ -86,6 +91,11 @@ public Mapping getMapping(final Class<?> resource) {
return mappings.get(resource);
}

@Override
public InjectLink.Style getLinkStyle() {
return linkStyle;
}

private void buildMappings() {
if (mappings != null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

package org.glassfish.jersey.linking.mapping;

import org.glassfish.jersey.linking.InjectLink;
import org.glassfish.jersey.uri.UriTemplate;

/**
Expand All @@ -51,10 +52,12 @@

public interface ResourceMappingContext {

public interface Mapping {
interface Mapping {

public UriTemplate getTemplate();
UriTemplate getTemplate();
}

public Mapping getMapping(Class<?> resource);
Mapping getMapping(Class<?> resource);

InjectLink.Style getLinkStyle();
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public static class TestClassA {
public ResourceMappingContext.Mapping getMapping(Class<?> resource) {
return null;
}

@Override
public InjectLink.Style getLinkStyle() {
return InjectLink.Style.DEFAULT;
}
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
import org.glassfish.jersey.uri.UriTemplate;

import org.junit.Test;
import org.mockito.Mockito;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -245,6 +246,11 @@ public URI relativize(URI uri) {
public ResourceMappingContext.Mapping getMapping(Class<?> resource) {
return null;
}

@Override
public InjectLink.Style getLinkStyle() {
return InjectLink.Style.DEFAULT;
}
};

protected final ResourceLinkContributionContext mockRlcc = new ResourceLinkContributionContext() {
Expand Down Expand Up @@ -408,6 +414,34 @@ public void testLinkStyles() {
assertEquals("http://example.com/application/resources/widgets/10", testClass.absolute);
}

@Test
public void testDefaultLinkStylesCanBeOverridden() {
LOG.info("Link styles default");
FieldProcessor<TestClassG> instance = new FieldProcessor(TestClassG.class);
ResourceMappingContext resourceMappingContext = Mockito.mock(ResourceMappingContext.class);
for (InjectLink.Style style : Arrays.asList(InjectLink.Style.values())) {
Mockito.when(resourceMappingContext.getLinkStyle()).thenReturn(style);
TestClassG testClass = new TestClassG("10");
instance.processLinks(testClass, mockUriInfo, resourceMappingContext, mockRlcc);
assertEquals("widgets/10", testClass.relativePath);
assertEquals("/application/resources/widgets/10", testClass.absolutePath);
assertEquals("http://example.com/application/resources/widgets/10", testClass.absolute);
switch (style) {
case ABSOLUTE:
assertEquals("http://example.com/application/resources/widgets/10", testClass.defaultStyle);
break;
case DEFAULT:
case ABSOLUTE_PATH:
assertEquals("/application/resources/widgets/10", testClass.defaultStyle);
break;
case RELATIVE_PATH:
assertEquals("widgets/10", testClass.defaultStyle);
break;
}

}
}

public static class TestClassH {

@InjectLink(TEMPLATE_B)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ public List<Resource> getLocatorSubResources() {
public ResourceMappingContext.Mapping getMapping(Class<?> resource) {
return null;
}

@Override
public InjectLink.Style getLinkStyle() {
return InjectLink.Style.DEFAULT;
}
};

@InjectLink(value = "A")
Expand Down