agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataResource.java
/**
	 * HTTP POST for Creating and Updating Metadata
	 * @param entity the entity to process and update the existing metadata item with
	 */
HTTP POST for Creating and Updating Metadata
@param entity the entity to process and update the existing metadata item with
HTTP POST for Creating and Updating Metadata @param entity the entity to process and update the existing metadata item with
@Override
	public void acceptRepresentation(Representation entity) {
		AgaveLogServiceClient.log(METADATA02.name(), MetaEdit.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
		try {
			Document metadataDocument;
			try {
				JsonNode jsonMetadata = super.getPostedEntityAsObjectNode(false);
				MetadataValidation docValidation = new MetadataValidation();
				JsonHandler jsonHandler = new JsonHandler();
				if (docValidation.validateMetadataNodeFields(jsonMetadata, username) == null)
					throw new MetadataQueryException("Invalid field in request.");
				metadataDocument = jsonHandler.parseJsonMetadataToDocument(jsonMetadata);
				String formUuid = metadataDocument.getString("uuid");
				if (StringUtils.isNotBlank(formUuid) && !uuid.equals(formUuid)) {
					throw new MetadataValidationException("Metadata uuid in the request body does not match the url");
				}
			} catch (ResourceException e) {
				log.error("Failed to parse request body. " + e.getMessage());
				throw e;
			} catch (Exception e) {
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
						e.getMessage());
			}
			try {
				MetadataPermissionManager permissionManager = new MetadataPermissionManager(uuid, username);
				if (!permissionManager.canWrite(username)){
					getResponse().setStatus(Status.CLIENT_ERROR_FORBIDDEN);
					throw new PermissionException("Cannot validate - User does not have permission to update metadata");
				}
				MetadataSearch search = new MetadataSearch(username);
				search.setAccessibleOwnersImplicit();
				Document updatedMetadataDoc = search.updateMetadataItem(metadataDocument, uuid);
				if (updatedMetadataDoc == null) {
					throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,
							"No metadata item found for user with id " + uuid);
				} else {
					NotificationManager.process(uuid, "UPDATED", username);
					getResponse().setStatus(Status.SUCCESS_OK);
				}
				MetadataItemSerializer serializer = new MetadataItemSerializer( );
				Document formattedDocument = serializer.formatDocumentResult(updatedMetadataDoc);
				getResponse().setEntity(new IplantSuccessRepresentation(serializer.formatMetadataItemDocumentResult(formattedDocument).toJson()));
			} catch (PermissionException e) {
				throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
						"User does not have permission to update metadata");
			}
		}
		catch(ResourceException e) {
			log.error("Failed to update metadata item " + uuid + ". " + e.getMessage());
			getResponse().setStatus(e.getStatus());
			getResponse().setEntity(new IplantErrorRepresentation(e.getMessage()));
		} catch (Exception e) {
			log.error("Failed to update metadata item " + uuid + ". " + e.getMessage());
			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
			getResponse().setEntity(new IplantErrorRepresentation("Unable to store the metadata object. " +
					"If this problem persists, please contact the system administrators."));
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , MetaEdit . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; try { Document metadataDocument ; try { JsonNode jsonMetadata = super . getPostedEntityAsObjectNode ( false ) ; MetadataValidation docValidation = new MetadataValidation ( ) ; JsonHandler jsonHandler = new JsonHandler ( ) ; if ( docValidation . validateMetadataNodeFields ( jsonMetadata , username ) == null ) throw new MetadataQueryException ( "Invalid field in request." ) ; metadataDocument = jsonHandler . parseJsonMetadataToDocument ( jsonMetadata ) ; String formUuid = metadataDocument . getString ( "uuid" ) ; if ( StringUtils . isNotBlank ( formUuid ) && ! uuid . equals ( formUuid ) ) { throw new MetadataValidationException ( "Metadata uuid in the request body does not match the url" ) ; } } catch ( ResourceException e ) { log . error ( "Failed to parse request body. " + e . getMessage ( ) ) ; throw e ; } catch ( Exception e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , e . getMessage ( ) ) ; } try { MetadataPermissionManager permissionManager = new MetadataPermissionManager ( uuid , username ) ; if ( ! permissionManager . canWrite ( username ) ) { getResponse ( ) . setStatus ( Status . CLIENT_ERROR_FORBIDDEN ) ; throw new PermissionException ( "Cannot validate - User does not have permission to update metadata" ) ; } MetadataSearch search = new MetadataSearch ( username ) ; search . setAccessibleOwnersImplicit ( ) ; Document updatedMetadataDoc = search . updateMetadataItem ( metadataDocument , uuid ) ; if ( updatedMetadataDoc == null ) { throw new ResourceException ( Status . CLIENT_ERROR_NOT_FOUND , "No metadata item found for user with id " + uuid ) ; } else { NotificationManager . process ( uuid , "UPDATED" , username ) ; getResponse ( ) . setStatus ( Status . SUCCESS_OK ) ; } MetadataItemSerializer serializer = new MetadataItemSerializer ( ) ; Document formattedDocument = serializer . formatDocumentResult ( updatedMetadataDoc ) ; getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( serializer . formatMetadataItemDocumentResult ( formattedDocument ) . toJson ( ) ) ) ; } catch ( PermissionException e ) { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have permission to update metadata" ) ; } } catch ( ResourceException e ) { log . error ( "Failed to update metadata item " + uuid + ". " + e . getMessage ( ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; } catch ( Exception e ) { log . error ( "Failed to update metadata item " + uuid + ". " + e . getMessage ( ) ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Unable to store the metadata object. " + "If this problem persists, please contact the system administrators." ) ) ; } }
HTTP POST for Creating and Updating Metadata
 @param entity the entity to process and update the existing metadata item with
HTTP POST for Creating and Updating Metadata @param entity the entity to process and update the existing metadata item with
// TODO: Should be folded into the fetch call? Much faster, but also removes ability to determine pems //   vs a standard not found.