agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataSchemaResource.java
/**
     * HTTP POST for Updating {@link MetadataSchemaItem}
     * @param entity the json metadata schema object to save
     */
HTTP POST for Updating  MetadataSchemaItem
@param entity the json metadata schema object to save
HTTP POST for Updating MetadataSchemaItem @param entity the json metadata schema object to save
@Override
    public void acceptRepresentation(Representation entity)
    {
    	AgaveLogServiceClient.log(METADATA02.name(), SchemaEdit.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
        DBCursor cursor = null;
        try 
        {
        	if (collection == null) {
        		throw new ResourceException(Status.SERVER_ERROR_INTERNAL,
        				"Unable to connect to metadata store. " +
                        "If this problem persists, please contact the system administrators.");
            }
        	if (StringUtils.isEmpty(uuid)) {
            	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
            			"No metadata schema identifier provided.",
            			new NullPointerException("Metadata Schemata UUID cannot be null"));
            } 
	        ObjectMapper mapper = new ObjectMapper();
	        JsonNode jsonSchema;
        	try {
        		jsonSchema = super.getPostedEntityAsObjectNode(false);
	        } 
        	catch (ResourceException e) {
	        	 throw e;
	        } 
        	catch(Exception e) {
	            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
	            		"Unable to parse form. " + e.getMessage());
	        }
	        try 
	        {
	            SyntaxValidator validator = JsonSchemaFactory.byDefault().getSyntaxValidator();
	            if (!validator.schemaIsValid(jsonSchema)) {
	            	ProcessingReport report = validator.validateSchema(jsonSchema);
	            	StringBuilder errorMessages = new StringBuilder();
					for (ProcessingMessage message : report) {
						errorMessages.append(message.getMessage()).append("\n");
					}
	                throw new MetadataSchemaValidationException("The supplied JSON Schema definition is invalid. " +
	                		"For more information on JSON Schema, please visit http://json-schema.org/.\n" +
							errorMessages);
	            }
	        } 
	        catch (MetadataSchemaValidationException e) {
	        	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, 
	        			e.getMessage(), e);
	        }
	        catch(Throwable e) {
	        	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
	        			"The supplied JSON Schema definition is invalid. " +
	            		"For more information on JSON Schema, please visit  http://json-schema.org/", e);
	        }
	        BasicDBObject doc;
	        String timestamp = new DateTime().toString();
	        try 
	        {
	            doc = new BasicDBObject("uuid", uuid)
	                    .append("internalUsername", internalUsername)
	                    .append("lastUpdated", timestamp)
	                    .append("schema", BasicDBObject.parse(ServiceUtils.escapeSchemaRefFieldNames(mapper.writeValueAsString(jsonSchema))));
	        } 
	        catch (Exception e) {
	        	throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
	        			e.getMessage(), e);
	        }
	        MetadataSchemaPermissionManager pm;
        	BasicDBObject query = new BasicDBObject("uuid", uuid);
    		query.append("tenantId", TenancyHelper.getCurrentTenantId());
    		cursor = collection.find(query);
    		String sdoc;
    		if (cursor.hasNext()) {
    			BasicDBObject currentMetadata = (BasicDBObject)cursor.next();
	            pm = new MetadataSchemaPermissionManager(uuid, (String)currentMetadata.get("owner"));
                if (pm.canWrite(username)) {
                    doc.append("created", currentMetadata.get("created"));
                    doc.append("owner", currentMetadata.get("owner"));
                    doc.append("tenantId", currentMetadata.get("tenantId"));
                    collection.update(query, doc);
                    sdoc = ServiceUtils.unescapeSchemaRefFieldNames(formatMetadataSchemaObject(doc).toString());
                    NotificationManager.process(uuid, "UPDATED", username, sdoc);
                } 
                else {
                	throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
                    	"User does not have permission to update metadata schema");
                }
            } 
            else {
            	throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
                        "No metadata schema found for user with id " + uuid);
            }
            getResponse().setEntity(new IplantSuccessRepresentation(sdoc));
        } 
        catch (ResourceException e) {
        	log.error("Failed to update metadata schema " + uuid + ". " + e.getMessage());
        	getResponse().setStatus(e.getStatus());
            getResponse().setEntity(new IplantErrorRepresentation(e.getMessage()));
        }
        catch (Throwable e) {
        	log.error("Failed to update metadata schema " + uuid, e);
        	getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
        	getResponse().setEntity(new IplantErrorRepresentation("Unable to update the metadata schema object. " +
                    "If this problem persists, please contact the system administrators."));
        } 
        finally {
            try {
				assert cursor != null;
				cursor.close();
            } catch (Exception ignore) {}
        }
    }
@ Override public void acceptRepresentation ( Representation entity ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaEdit . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; DBCursor cursor = null ; try { if ( collection == null ) { throw new ResourceException ( Status . SERVER_ERROR_INTERNAL , "Unable to connect to metadata store. " + "If this problem persists, please contact the system administrators." ) ; } if ( StringUtils . isEmpty ( uuid ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No metadata schema identifier provided." , new NullPointerException ( "Metadata Schemata UUID cannot be null" ) ) ; } ObjectMapper mapper = new ObjectMapper ( ) ; JsonNode jsonSchema ; try { jsonSchema = super . getPostedEntityAsObjectNode ( false ) ; } catch ( ResourceException e ) { throw e ; } catch ( Exception e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Unable to parse form. " + e . getMessage ( ) ) ; } try { SyntaxValidator validator = JsonSchemaFactory . byDefault ( ) . getSyntaxValidator ( ) ; if ( ! validator . schemaIsValid ( jsonSchema ) ) { ProcessingReport report = validator . validateSchema ( jsonSchema ) ; StringBuilder errorMessages = new StringBuilder ( ) ; for ( ProcessingMessage message : report ) { errorMessages . append ( message . getMessage ( ) ) . append ( "\n" ) ; } throw new MetadataSchemaValidationException ( "The supplied JSON Schema definition is invalid. " + "For more information on JSON Schema, please visit http://json-schema.org/.\n" + errorMessages ) ; } } catch ( MetadataSchemaValidationException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) , e ) ; } catch ( Throwable e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The supplied JSON Schema definition is invalid. " + "For more information on JSON Schema, please visit  http://json-schema.org/" , e ) ; } BasicDBObject doc ; String timestamp = new DateTime ( ) . toString ( ) ; try { doc = new BasicDBObject ( "uuid" , uuid ) . append ( "internalUsername" , internalUsername ) . append ( "lastUpdated" , timestamp ) . append ( "schema" , BasicDBObject . parse ( ServiceUtils . escapeSchemaRefFieldNames ( mapper . writeValueAsString ( jsonSchema ) ) ) ) ; } catch ( Exception e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) , e ) ; } MetadataSchemaPermissionManager pm ; BasicDBObject query = new BasicDBObject ( "uuid" , uuid ) ; query . append ( "tenantId" , TenancyHelper . getCurrentTenantId ( ) ) ; cursor = collection . find ( query ) ; String sdoc ; if ( cursor . hasNext ( ) ) { BasicDBObject currentMetadata = ( BasicDBObject ) cursor . next ( ) ; pm = new MetadataSchemaPermissionManager ( uuid , ( String ) currentMetadata . get ( "owner" ) ) ; if ( pm . canWrite ( username ) ) { doc . append ( "created" , currentMetadata . get ( "created" ) ) ; doc . append ( "owner" , currentMetadata . get ( "owner" ) ) ; doc . append ( "tenantId" , currentMetadata . get ( "tenantId" ) ) ; collection . update ( query , doc ) ; sdoc = ServiceUtils . unescapeSchemaRefFieldNames ( formatMetadataSchemaObject ( doc ) . toString ( ) ) ; NotificationManager . process ( uuid , "UPDATED" , username , sdoc ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have permission to update metadata schema" ) ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No metadata schema found for user with id " + uuid ) ; } getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( sdoc ) ) ; } catch ( ResourceException e ) { log . error ( "Failed to update metadata schema " + uuid + ". " + e . getMessage ( ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; } catch ( Throwable e ) { log . error ( "Failed to update metadata schema " + uuid , e ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Unable to update the metadata schema object. " + "If this problem persists, please contact the system administrators." ) ) ; } finally { try { assert cursor != null ; cursor . close ( ) ; } catch ( Exception ignore ) { } } }
HTTP POST for Updating {@link MetadataSchemaItem}
 @param entity the json metadata schema object to save
HTTP POST for Updating { @link MetadataSchemaItem } @param entity the json metadata schema object to save
// validate that the schema is valid json // persist the MetadataSchema object // If schema is not valid JSON, throw exception // Insert or Update //doc.remove("created");