agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataSchemaCollection.java
/**
	 * HTTP POST for Creating and Updating Metadata JSON schema
	 * @param entity the {@link MetadataSchemaItem} POST body.
	 */
HTTP POST for Creating and Updating Metadata JSON schema
@param entity the  MetadataSchemaItem POST body.
HTTP POST for Creating and Updating Metadata JSON schema @param entity the MetadataSchemaItem POST body .
@Override
	public void acceptRepresentation(Representation entity)
	{
		AgaveLogServiceClient.log(METADATA02.name(), SchemaCreate.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
		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.");
			}
			JsonNode jsonMetadataSchema = null;
			try
			{
				jsonMetadataSchema = 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(jsonMetadataSchema)) {
					ProcessingReport report = validator.validateSchema(jsonMetadataSchema);
					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);
			}
			JsonHandler jsonHandler = new JsonHandler();
			ArrayNode notificationArrayNode = jsonHandler.parseNotificationToArrayNode(jsonMetadataSchema);
			BasicDBObject doc;
			String timestamp = new DateTime().toString();
			try
			{
				doc = new BasicDBObject("internalUsername", internalUsername)
						.append("lastUpdated", timestamp)
						.append("schema", JSON.parse(ServiceUtils.escapeSchemaRefFieldNames(objectMapper.writeValueAsString(jsonMetadataSchema))));
			}
			catch (JsonProcessingException e) {
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
						"Unable to parse jsonSchema object.", e);
			}
			MetadataSchemaPermissionManager pm = null;
			String uuid = new AgaveUUID(UUIDType.SCHEMA).toString();
			doc.put("uuid", uuid);
			doc.append("created", timestamp);
			doc.append("owner", username);
			doc.append("tenantId", TenancyHelper.getCurrentTenantId());
			collection.insert(doc);
			MetadataRequestNotificationProcessor notificationProcessor =
					new MetadataRequestNotificationProcessor(username, uuid);
			notificationProcessor.process(notificationArrayNode);
			pm = new MetadataSchemaPermissionManager(uuid, username);
			pm.setPermission(username, "ALL");
			String sdoc = ServiceUtils.unescapeSchemaRefFieldNames(formatMetadataSchemaObject(doc).toString());
			NotificationManager.process(uuid, "CREATED", username, sdoc);
			getResponse().setStatus(Status.SUCCESS_CREATED);
			getResponse().setEntity(new IplantSuccessRepresentation(sdoc));
		}
		catch (MetadataQueryException e) {
			getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
			getResponse().setEntity(new IplantErrorRepresentation(e.getMessage()));
		}
		catch (ResourceException e) {
			log.error("Failed to add metadata schema", e);
			getResponse().setStatus(e.getStatus());
			getResponse().setEntity(new IplantErrorRepresentation(e.getMessage()));
		}
		catch (Throwable e) {
			log.error("Internal error attempting to add metadata schema", e);
			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
			getResponse().setEntity(new IplantErrorRepresentation("Unable to add the metadata schema object. " +
					"If this problem persists, please contact the system administrators."));
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaCreate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; 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." ) ; } JsonNode jsonMetadataSchema = null ; try { jsonMetadataSchema = 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 ( jsonMetadataSchema ) ) { ProcessingReport report = validator . validateSchema ( jsonMetadataSchema ) ; 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 ) ; } JsonHandler jsonHandler = new JsonHandler ( ) ; ArrayNode notificationArrayNode = jsonHandler . parseNotificationToArrayNode ( jsonMetadataSchema ) ; BasicDBObject doc ; String timestamp = new DateTime ( ) . toString ( ) ; try { doc = new BasicDBObject ( "internalUsername" , internalUsername ) . append ( "lastUpdated" , timestamp ) . append ( "schema" , JSON . parse ( ServiceUtils . escapeSchemaRefFieldNames ( objectMapper . writeValueAsString ( jsonMetadataSchema ) ) ) ) ; } catch ( JsonProcessingException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Unable to parse jsonSchema object." , e ) ; } MetadataSchemaPermissionManager pm = null ; String uuid = new AgaveUUID ( UUIDType . SCHEMA ) . toString ( ) ; doc . put ( "uuid" , uuid ) ; doc . append ( "created" , timestamp ) ; doc . append ( "owner" , username ) ; doc . append ( "tenantId" , TenancyHelper . getCurrentTenantId ( ) ) ; collection . insert ( doc ) ; MetadataRequestNotificationProcessor notificationProcessor = new MetadataRequestNotificationProcessor ( username , uuid ) ; notificationProcessor . process ( notificationArrayNode ) ; pm = new MetadataSchemaPermissionManager ( uuid , username ) ; pm . setPermission ( username , "ALL" ) ; String sdoc = ServiceUtils . unescapeSchemaRefFieldNames ( formatMetadataSchemaObject ( doc ) . toString ( ) ) ; NotificationManager . process ( uuid , "CREATED" , username , sdoc ) ; getResponse ( ) . setStatus ( Status . SUCCESS_CREATED ) ; getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( sdoc ) ) ; } catch ( MetadataQueryException e ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_BAD_REQUEST ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; } catch ( ResourceException e ) { log . error ( "Failed to add metadata schema" , e ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; } catch ( Throwable e ) { log . error ( "Internal error attempting to add metadata schema" , e ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Unable to add the metadata schema object. " + "If this problem persists, please contact the system administrators." ) ) ; } }
HTTP POST for Creating and Updating Metadata JSON schema
 @param entity the {@link MetadataSchemaItem} POST body.
HTTP POST for Creating and Updating Metadata JSON schema @param entity the { @link MetadataSchemaItem } POST body .
// validate that the schema is valid json // persist the MetadataSchema object // If schema is not valid JSON, throw exception // Insert or Update // process any embedded notifications