Java Code Examples for org.bson.types.ObjectId#isValid()
The following examples show how to use
org.bson.types.ObjectId#isValid() .
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: OccasionResource.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
@GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Response retrieveOccasion(@PathParam("id") String id) { String method = "retrieveOccasion"; logger.entering(clazz, method, id); // Validate the JWT. At this point, anyone can get an occasion if they // have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { logger.exiting(clazz, method, Status.UNAUTHORIZED); return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } Response response; // Ensure we recieved a valid ID if (!ObjectId.isValid(id)) { response = Response.status(400).entity("Invalid occasion id").build(); } else { // perform the query and return the occasion, if we find one DBObject occasion = getCollection().findOne(new BasicDBObject(Occasion.OCCASION_ID_KEY, new ObjectId(id))); logger.fine("In " + method + " with Occasion: \n\n" + occasion); String occasionJson = new Occasion(occasion).toString(); if (null == occasionJson || occasionJson.isEmpty()) { response = Response.status(400).entity("no occasion found for given id").build(); } else { response = Response.ok(occasionJson, MediaType.APPLICATION_JSON).build(); } } logger.exiting(clazz, method, response); return response; }
Example 2
Source File: OccasionResource.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
@DELETE @Path("{id}") @Consumes(MediaType.APPLICATION_JSON) public Response deleteOccasion(@PathParam("id") String id) { String method = "deleteOccasion"; logger.entering(clazz, method, id); // Validate the JWT. At this point, anyone can delete an occasion // if they have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { logger.exiting(clazz, method, Status.UNAUTHORIZED); return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } Response response; // Ensure we recieved a valid ID; empty query here deletes everything if (null == id || id.isEmpty() || !ObjectId.isValid(id)) { response = Response.status(400).entity("invalid occasion id").build(); } else { // perform the deletion if (deleteOccasion(new ObjectId(id)) != true) { response = Response.status(400).entity("occasion deletion failed").build(); } else { // Cancel occasion with the orchestrator orchestrator.cancelOccasion(id); response = Response.ok().build(); } } logger.exiting(clazz, method, response); return response; }
Example 3
Source File: GroupResource.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
@GET @Path("{id}") @Produces(MediaType.APPLICATION_JSON) public Response getGroupInfo(@PathParam("id") String id) { // Validate the JWT. At this point, anyone can get a group info if they // have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } // Check if the id is a valid mongo object id. If not, the server will // throw a 500 if (!ObjectId.isValid(id)) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group was not found") .build(); } // Query Mongo for group with specified id BasicDBObject group = (BasicDBObject) getGroupCollection().findOne(new ObjectId(id)); if (group == null) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group was not found") .build(); } // Create a JSON payload with the group content String responsePayload = (new Group(group)).getJson(); return Response.ok().entity(responsePayload).build(); }
Example 4
Source File: GroupResource.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
@GET @Path("/") @Produces(MediaType.APPLICATION_JSON) public Response getGroups(@QueryParam("userId") String userId) { // Validate the JWT. At this point, anyone can get a group list if they // have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } DBCursor groupCursor = null; BasicDBList groupList = new BasicDBList(); if (userId != null) { if (!ObjectId.isValid(userId)) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The user id provided is not valid.") .build(); } BasicDBObject queryObj = new BasicDBObject(Group.JSON_KEY_MEMBERS_LIST, userId); groupCursor = getGroupCollection().find(queryObj); } else { groupCursor = getGroupCollection().find(); } while (groupCursor.hasNext()) { groupList.add((new Group(groupCursor.next()).getJson())); } String responsePayload = (new BasicDBObject(Group.JSON_KEY_GROUPS, groupList)).toString(); return Response.ok(responsePayload).build(); }
Example 5
Source File: GroupResource.java From sample-acmegifts with Eclipse Public License 1.0 | 5 votes |
@DELETE @Path("{id}") public Response deleteGroup(@PathParam("id") String id) { // Validate the JWT. At this point, anyone can delete a group if they // have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } // Check if the id is a valid mongo object id. If not, the server will // throw a 500 if (!ObjectId.isValid(id)) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group ID is not valid.") .build(); } // Query Mongo for group with specified id BasicDBObject group = (BasicDBObject) getGroupCollection().findOne(new ObjectId(id)); if (group == null) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group was not found") .build(); } // Delete group getGroupCollection().remove(group); return Response.ok().build(); }
Example 6
Source File: MongoClientImpl.java From vertx-mongo-client with Apache License 2.0 | 5 votes |
JsonObject encodeKeyWhenUseObjectId(JsonObject json) { if (!useObjectId) return json; Object idString = json.getValue(ID_FIELD, null); if (idString instanceof String && ObjectId.isValid((String) idString)) { json.put(ID_FIELD, new JsonObject().put(JsonObjectCodec.OID_FIELD, idString)); } return json; }
Example 7
Source File: OccasionResource.java From sample-acmegifts with Eclipse Public License 1.0 | 4 votes |
@PUT @Path("{id}") @Consumes(MediaType.APPLICATION_JSON) public Response updateOccasion(@PathParam("id") String id, JsonObject json) { String method = "updateOccasion"; logger.entering(clazz, method, new Object[] {id, json}); // Validate the JWT. At this point, anyone can update an occasion // if they have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { logger.exiting(clazz, method, Status.UNAUTHORIZED); return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } Response response = Response.ok().build(); // Ensure we recieved a valid ID if (!ObjectId.isValid(id) || null == json || json.isEmpty()) { response = Response.status(400).entity("invalid occasion id").build(); } else { // perform the update using $set to prevent overwriting data in the event of an incomplete // payload Occasion updatedOccasion = new Occasion(json); BasicDBObject updateObject = new BasicDBObject("$set", updatedOccasion.toDbo()); BasicDBObject query = new BasicDBObject(Occasion.OCCASION_ID_KEY, new ObjectId(id)); // Update and return the new document. DBObject updatedObject = getCollection().findAndModify(query, null, null, false, updateObject, true, false); // Reschedule occasion with the orchestrator. Use the updated object from // mongo because it will contain all fields that the orchestator may need. try { orchestrator.cancelOccasion(id); orchestrator.scheduleOccasion(new Occasion(updatedObject)); } catch (ParseException e) { e.printStackTrace(); response = Response.status(400).entity("Invalid date given. Format must be YYYY-MM-DD").build(); } } logger.exiting(clazz, method, response); return response; }
Example 8
Source File: Occasion.java From sample-acmegifts with Eclipse Public License 1.0 | 4 votes |
public void setId(String id) { if (null != id && !id.isEmpty() && ObjectId.isValid(id)) { this.id = new ObjectId(id); } }
Example 9
Source File: Occasion.java From sample-acmegifts with Eclipse Public License 1.0 | 4 votes |
public BasicDBObject toDbo() { String method = "toDbo"; logger.entering(clazz, method); logger.fine("id: " + id); logger.fine("date: " + date); logger.fine("groupId: " + groupId); logger.fine("interval: " + interval); logger.fine("name: " + name); logger.fine("organizerId: " + organizerId); logger.fine("recipientId: " + recipientId); logger.fine("contributions: " + Contribution.listToString(contributions)); // build the db object BasicDBObject dbo = new BasicDBObject(); if (null != id && ObjectId.isValid(id.toString())) { dbo.append(OCCASION_ID_KEY, id); } if (null != date && !date.isEmpty()) { dbo.append(OCCASION_DATE_KEY, date); } if (null != groupId && !groupId.isEmpty()) { dbo.append(OCCASION_GROUP_ID_KEY, groupId); } if (null != interval && !interval.isEmpty()) { dbo.append(OCCASION_INTERVAL_KEY, interval); } if (null != name && !name.isEmpty()) { dbo.append(OCCASION_NAME_KEY, name); } if (null != organizerId && !organizerId.isEmpty()) { dbo.append(OCCASION_ORGANIZER_ID_KEY, organizerId); } if (null != recipientId && !recipientId.isEmpty()) { dbo.append(OCCASION_RECIPIENT_ID_KEY, recipientId); } BasicDBList contributionDbList = Contribution.listToDBList(contributions); if (null != contributionDbList && !contributionDbList.isEmpty()) { dbo.append(OCCASION_CONTRIBUTIONS_KEY, contributionDbList); } logger.exiting(clazz, method, dbo); return dbo; }
Example 10
Source File: GroupResource.java From sample-acmegifts with Eclipse Public License 1.0 | 4 votes |
@PUT @Path("{id}") @Consumes(MediaType.APPLICATION_JSON) public Response updateGroup(@PathParam("id") String id, JsonObject payload) { // Validate the JWT. At this point, anyone can update a group if they // have a valid JWT. try { validateJWT(); } catch (JWTException jwte) { return Response.status(Status.UNAUTHORIZED) .type(MediaType.TEXT_PLAIN) .entity(jwte.getMessage()) .build(); } // Check if the id is a valid mongo object id. If not, the server will // throw a 500 if (!ObjectId.isValid(id)) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group was not found") .build(); } // Query Mongo for group with specified id BasicDBObject oldGroup = (BasicDBObject) getGroupCollection().findOne(new ObjectId(id)); if (oldGroup == null) { return Response.status(Status.BAD_REQUEST) .type(MediaType.TEXT_PLAIN) .entity("The group was not found") .build(); } // Create new group based on payload content Group updatedGroup = new Group(payload); // Update database with new group getGroupCollection().findAndModify(oldGroup, updatedGroup.getDBObject(true)); return Response.ok().build(); }
Example 11
Source File: WrapObjectId.java From jphp with Apache License 2.0 | 4 votes |
@Signature public static boolean isValid(String hexString) { return ObjectId.isValid(hexString); }