Search in sources :

Example 1 with UserEntity

use of org.glassfish.jersey.examples.bookmark_em.entity.UserEntity in project jersey by jersey.

the class UsersResource method getUsersAsJsonArray.

@GET
@Produces("application/json")
public JSONArray getUsersAsJsonArray() {
    JSONArray uriArray = new JSONArray();
    for (UserEntity userEntity : getUsers()) {
        UriBuilder ub = uriInfo.getAbsolutePathBuilder();
        URI userUri = ub.path(userEntity.getUserid()).build();
        uriArray.put(userUri.toASCIIString());
    }
    return uriArray;
}
Also used : JSONArray(org.codehaus.jettison.json.JSONArray) UriBuilder(javax.ws.rs.core.UriBuilder) URI(java.net.URI) UserEntity(org.glassfish.jersey.examples.bookmark_em.entity.UserEntity) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with UserEntity

use of org.glassfish.jersey.examples.bookmark_em.entity.UserEntity in project jersey by jersey.

the class UserResource method putUser.

@PUT
@Consumes("application/json")
public Response putUser(JSONObject jsonEntity) throws JSONException {
    String jsonUserid = jsonEntity.getString("userid");
    if ((null != jsonUserid) && !jsonUserid.equals(userid)) {
        return Response.status(409).entity("userids differ!\n").build();
    }
    // insert or update ?
    final boolean newRecord = (null == userEntity);
    if (newRecord) {
        // new user record to be inserted
        userEntity = new UserEntity();
        userEntity.setUserid(userid);
    }
    userEntity.setUsername(jsonEntity.getString("username"));
    userEntity.setEmail(jsonEntity.getString("email"));
    userEntity.setPassword(jsonEntity.getString("password"));
    if (newRecord) {
        TransactionManager.manage(utx, new Transactional(em) {

            public void transact() {
                em.joinTransaction();
                em.persist(userEntity);
            }
        });
        return Response.created(uriInfo.getAbsolutePath()).build();
    } else {
        TransactionManager.manage(utx, new Transactional(em) {

            public void transact() {
                em.merge(userEntity);
            }
        });
        return Response.noContent().build();
    }
}
Also used : UserEntity(org.glassfish.jersey.examples.bookmark_em.entity.UserEntity) Transactional(org.glassfish.jersey.examples.bookmark_em.util.tx.Transactional) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

UserEntity (org.glassfish.jersey.examples.bookmark_em.entity.UserEntity)2 URI (java.net.URI)1 Consumes (javax.ws.rs.Consumes)1 GET (javax.ws.rs.GET)1 PUT (javax.ws.rs.PUT)1 Produces (javax.ws.rs.Produces)1 UriBuilder (javax.ws.rs.core.UriBuilder)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 Transactional (org.glassfish.jersey.examples.bookmark_em.util.tx.Transactional)1