/
BookmarksResource.java
109 lines (91 loc) · 3.54 KB
/
BookmarksResource.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2010, 2019 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.jersey.examples.bookmark.resource;
import java.net.URI;
import java.util.Collection;
import java.util.Date;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.persistence.EntityManager;
import org.glassfish.jersey.examples.bookmark.entity.BookmarkEntity;
import org.glassfish.jersey.examples.bookmark.util.tx.TransactionManager;
import org.glassfish.jersey.examples.bookmark.util.tx.Transactional;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
* @author Jakub Podlesak
* @author Paul Sandoz
* @author Michal Gajdos
*/
public class BookmarksResource {
UriInfo uriInfo; // actual uri info
EntityManager em; // entity manager provided by parent resource
UserResource userResource; // parent user resource
/**
* Creates a new instance of BookmarksResource
*/
public BookmarksResource(UriInfo uriInfo, EntityManager em, UserResource userResource) {
this.uriInfo = uriInfo;
this.em = em;
this.userResource = userResource;
}
public Collection<BookmarkEntity> getBookmarks() {
return userResource.getUserEntity().getBookmarkEntityCollection();
}
@Path("{bmid: .+}")
public BookmarkResource getBookmark(@PathParam("bmid") String bmid) {
return new BookmarkResource(uriInfo, em, userResource.getUserEntity(), bmid);
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public JSONArray getBookmarksAsJsonArray() {
JSONArray uriArray = new JSONArray();
for (BookmarkEntity bookmarkEntity : getBookmarks()) {
UriBuilder ub = uriInfo.getAbsolutePathBuilder();
URI bookmarkUri = ub
.path(bookmarkEntity.getBookmarkEntityPK().getBmid())
.build();
uriArray.put(bookmarkUri.toASCIIString());
}
return uriArray;
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response postForm(JSONObject bookmark) throws JSONException {
final BookmarkEntity bookmarkEntity = new BookmarkEntity(getBookmarkId(bookmark.getString("uri")),
userResource.getUserEntity().getUserid());
bookmarkEntity.setUri(bookmark.getString("uri"));
bookmarkEntity.setUpdated(new Date());
bookmarkEntity.setSdesc(bookmark.getString("sdesc"));
bookmarkEntity.setLdesc(bookmark.getString("ldesc"));
userResource.getUserEntity().getBookmarkEntityCollection().add(bookmarkEntity);
TransactionManager.manage(new Transactional(em) {
public void transact() {
em.merge(userResource.getUserEntity());
}
});
URI bookmarkUri = uriInfo.getAbsolutePathBuilder()
.path(bookmarkEntity.getBookmarkEntityPK().getBmid())
.build();
return Response.created(bookmarkUri).build();
}
private String getBookmarkId(String uri) {
return uri;
}
}