agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataShareResource.java
/**
	 * Post action for adding (and overwriting) permissions on a metadata iod
	 */
Post action for adding (and overwriting) permissions on a metadata iod
Post action for adding ( and overwriting ) permissions on a metadata iod
@Override
	public void acceptRepresentation(Representation entity) {
		try {
			if (StringUtils.isEmpty(uuid)) {
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
						"No metadata id provided.");
			}
			MetadataItem metadataItem = getRequestedMetadataItem();
			MetadataPermissionManager metadataItemPermissionManager = new MetadataPermissionManager(metadataItem, username);
			if (!metadataItemPermissionManager.canWrite(username)) {
				throw new ResourceException(Status.CLIENT_ERROR_FORBIDDEN,
						"User does not have permission to modify this resource.");
			}
			JSONObject postPermissionData = super.getPostedEntityAsJsonObject(true);
			String pemUsername;
			String sPermission;
			if (StringUtils.isEmpty(sharedUsername)) {
				AgaveLogServiceClient.log(METADATA02.name(), MetaPemsCreate.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
				if (postPermissionData.has("username")) {
					pemUsername = postPermissionData.getString("username");
				} else {
					throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
							"No username specified. Please specify a valid user to whom the permission will apply.");
				}
			} else {
				AgaveLogServiceClient.log(METADATA02.name(), MetaPemsUpdate.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
				if (postPermissionData.has("username") &&
						!StringUtils.equalsIgnoreCase(postPermissionData.getString("username"), sharedUsername)) {
					throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
							"The username value in the POST body, " + postPermissionData.getString("username") +
									", does not match the username in the URL, " + sharedUsername);
				} else {
					pemUsername = sharedUsername;
				}
			}
			if (postPermissionData.has("permission")) {
				sPermission = postPermissionData.getString("permission");
				if (StringUtils.equalsIgnoreCase(sPermission, "none") ||
						StringUtils.equalsIgnoreCase(sPermission, "null")) {
					sPermission = null;
				}
			} else {
				throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST,
						"Missing permission field. Please specify a valid permission of READ, WRITE, or READ_WRITE.");
			}
			try {
				if (StringUtils.isEmpty(sPermission)) {
					getResponse().setStatus(Status.SUCCESS_OK);
				} else {
					getResponse().setStatus(Status.SUCCESS_CREATED);
				}
				metadataItemPermissionManager.setPermission(pemUsername, sPermission);
				getResponse().setEntity(new IplantSuccessRepresentation(metadataItemPermissionManager.getPermission(pemUsername).toJSON(uuid)));
			} catch (IllegalArgumentException iae) {
				throw new ResourceException(
						Status.CLIENT_ERROR_BAD_REQUEST,
						"Invalid permission value. Valid values are: " + PermissionType.supportedValuesAsString());
			}
		} catch (ResourceException e) {
			getResponse().setEntity(
					new IplantErrorRepresentation(e.getMessage()));
			getResponse().setStatus(e.getStatus());
		} catch (Exception e) {
			getResponse().setEntity(
					new IplantErrorRepresentation("Failed to update metadata permissions: " + e.getMessage()));
			getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
		}
	}
@ Override public void acceptRepresentation ( Representation entity ) { try { if ( StringUtils . isEmpty ( uuid ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No metadata id provided." ) ; } MetadataItem metadataItem = getRequestedMetadataItem ( ) ; MetadataPermissionManager metadataItemPermissionManager = new MetadataPermissionManager ( metadataItem , username ) ; if ( ! metadataItemPermissionManager . canWrite ( username ) ) { throw new ResourceException ( Status . CLIENT_ERROR_FORBIDDEN , "User does not have permission to modify this resource." ) ; } JSONObject postPermissionData = super . getPostedEntityAsJsonObject ( true ) ; String pemUsername ; String sPermission ; if ( StringUtils . isEmpty ( sharedUsername ) ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , MetaPemsCreate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; if ( postPermissionData . has ( "username" ) ) { pemUsername = postPermissionData . getString ( "username" ) ; } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "No username specified. Please specify a valid user to whom the permission will apply." ) ; } } else { AgaveLogServiceClient . log ( METADATA02 . name ( ) , MetaPemsUpdate . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; if ( postPermissionData . has ( "username" ) && ! StringUtils . equalsIgnoreCase ( postPermissionData . getString ( "username" ) , sharedUsername ) ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "The username value in the POST body, " + postPermissionData . getString ( "username" ) + ", does not match the username in the URL, " + sharedUsername ) ; } else { pemUsername = sharedUsername ; } } if ( postPermissionData . has ( "permission" ) ) { sPermission = postPermissionData . getString ( "permission" ) ; if ( StringUtils . equalsIgnoreCase ( sPermission , "none" ) || StringUtils . equalsIgnoreCase ( sPermission , "null" ) ) { sPermission = null ; } } else { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Missing permission field. Please specify a valid permission of READ, WRITE, or READ_WRITE." ) ; } try { if ( StringUtils . isEmpty ( sPermission ) ) { getResponse ( ) . setStatus ( Status . SUCCESS_OK ) ; } else { getResponse ( ) . setStatus ( Status . SUCCESS_CREATED ) ; } metadataItemPermissionManager . setPermission ( pemUsername , sPermission ) ; getResponse ( ) . setEntity ( new IplantSuccessRepresentation ( metadataItemPermissionManager . getPermission ( pemUsername ) . toJSON ( uuid ) ) ) ; } catch ( IllegalArgumentException iae ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid permission value. Valid values are: " + PermissionType . supportedValuesAsString ( ) ) ; } } catch ( ResourceException e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( e . getStatus ( ) ) ; } catch ( Exception e ) { getResponse ( ) . setEntity ( new IplantErrorRepresentation ( "Failed to update metadata permissions: " + e . getMessage ( ) ) ) ; getResponse ( ) . setStatus ( Status . SERVER_ERROR_INTERNAL ) ; } }
Post action for adding (and overwriting) permissions on a metadata iod
Post action for adding ( and overwriting ) permissions on a metadata iod
// a username must be provided either in the form or the body // name in url and json, if provided, should match