Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit a19243a

Browse files
committed
Working towards better support for Resource links and query paramters. Exmaple updated to show
both working.
1 parent a5eb074 commit a19243a

File tree

4 files changed

+585
-0
lines changed

4 files changed

+585
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.glassfish.jersey.examples.linking.representation;
41+
42+
import org.glassfish.jersey.examples.linking.resources.ItemResource;
43+
import org.glassfish.jersey.linking.Binding;
44+
import org.glassfish.jersey.linking.InjectLinks;
45+
import org.glassfish.jersey.linking.InjectLink;
46+
import org.glassfish.jersey.linking.InjectLink.Style;
47+
48+
import java.util.List;
49+
import javax.ws.rs.core.Link;
50+
import javax.xml.bind.annotation.XmlAccessType;
51+
import javax.xml.bind.annotation.XmlAccessorType;
52+
import javax.xml.bind.annotation.XmlElement;
53+
import javax.xml.bind.annotation.XmlElementWrapper;
54+
import javax.xml.bind.annotation.XmlRootElement;
55+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
56+
57+
/**
58+
* JAXB representation of an item
59+
*
60+
*
61+
* @author Mark Hadley
62+
* @author Gerard Davison (gerard.davison at oracle.com)
63+
*/
64+
@XmlAccessorType(XmlAccessType.NONE)
65+
@XmlRootElement(name = "item")
66+
@InjectLinks({
67+
@InjectLink(
68+
resource = ItemResource.class,
69+
style = Style.ABSOLUTE,
70+
condition = "${resource.next}",
71+
bindings = @Binding(name = "id", value = "${resource.nextId}"),
72+
rel = "next"
73+
),
74+
@InjectLink(
75+
resource = ItemResource.class,
76+
style = Style.ABSOLUTE,
77+
condition = "${resource.prev}",
78+
bindings = @Binding(name = "id", value = "${resource.prevId}"),
79+
rel = "prev"
80+
)
81+
})
82+
public class ItemRepresentation {
83+
84+
@XmlElement
85+
private String name;
86+
87+
@InjectLink(
88+
resource = ItemResource.class,
89+
style = Style.ABSOLUTE,
90+
bindings = @Binding(name = "id", value = "${resource.id}"),
91+
rel = "self"
92+
)
93+
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
94+
@XmlElement(name="link")
95+
Link self;
96+
97+
@InjectLinks({
98+
@InjectLink(
99+
resource = ItemResource.class,
100+
style = Style.ABSOLUTE,
101+
condition = "${resource.next}",
102+
bindings = @Binding(name = "id", value = "${resource.nextId}"),
103+
rel = "next"
104+
),
105+
@InjectLink(
106+
resource = ItemResource.class,
107+
style = Style.ABSOLUTE,
108+
condition = "${resource.prev}",
109+
bindings = @Binding(name = "id", value = "${resource.prevId}"),
110+
rel = "prev"
111+
)})
112+
@XmlElement(name="link")
113+
@XmlElementWrapper(name = "links")
114+
@XmlJavaTypeAdapter(Link.JaxbAdapter.class)
115+
List<Link> links;
116+
117+
public ItemRepresentation() {
118+
this.name = "";
119+
}
120+
121+
public ItemRepresentation(String name) {
122+
this.name = name;
123+
}
124+
125+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2010-2014 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.examples.linking.resources;
42+
43+
import org.glassfish.jersey.examples.linking.model.ItemModel;
44+
import org.glassfish.jersey.examples.linking.model.ItemsModel;
45+
import org.glassfish.jersey.examples.linking.representation.ItemRepresentation;
46+
import javax.ws.rs.GET;
47+
import javax.ws.rs.NotFoundException;
48+
import javax.ws.rs.Path;
49+
import javax.ws.rs.PathParam;
50+
import javax.ws.rs.Produces;
51+
import javax.ws.rs.core.MediaType;
52+
53+
/**
54+
* Resource that provides access to one item from a set of items managed
55+
* by ItemsModel
56+
*
57+
* @author Mark Hadley
58+
* @author Gerard Davison (gerard.davison at oracle.com)
59+
*/
60+
@Path("items/{id}")
61+
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
62+
public class ItemResource {
63+
64+
private ItemsModel itemsModel;
65+
private ItemModel itemModel;
66+
private String id;
67+
68+
public ItemResource(@PathParam("id") String id) {
69+
this.id = id;
70+
itemsModel = ItemsModel.getInstance();
71+
try {
72+
itemModel = itemsModel.getItem(id);
73+
} catch (IndexOutOfBoundsException ex) {
74+
throw new NotFoundException();
75+
}
76+
}
77+
78+
@GET
79+
public ItemRepresentation get() {
80+
return new ItemRepresentation(itemModel.getName());
81+
}
82+
83+
/**
84+
* Determines whether there is a next item.
85+
* @return
86+
*/
87+
public boolean isNext() {
88+
return itemsModel.hasNext(id);
89+
}
90+
91+
/**
92+
* Determines whether there is a previous item
93+
* @return
94+
*/
95+
public boolean isPrev() {
96+
return itemsModel.hasPrev(id);
97+
}
98+
99+
public String getNextId() {
100+
return itemsModel.getNextId(id);
101+
}
102+
103+
public String getPrevId() {
104+
return itemsModel.getPrevId(id);
105+
}
106+
107+
public String getId() {
108+
return id;
109+
}
110+
}

0 commit comments

Comments
 (0)