agave-metadata/metadata-api/src/main/java/org/iplantc/service/metadata/resources/MetadataSchemaCollection.java
/**
	 * This method represents the HTTP GET action. The schema is
	 * retrieved from the database and sent to the user as a {@link org.json.JSONArray JSONArray} of {@link org.json.JSONObject JSONObject}.
	 *
	 */
This method represents the HTTP GET action. The schema is
retrieved from the database and sent to the user as a  org.json.JSONArray JSONArray of  org.json.JSONObject JSONObject.
This method represents the HTTP GET action . The schema is retrieved from the database and sent to the user as a org . json . JSONArray JSONArray of org . json . JSONObject JSONObject .
@Override
	public Representation represent(Variant variant) throws ResourceException
	{
		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.");
			}
			BasicDBObject query = null;
			if (StringUtils.isEmpty(userQuery)) {
				AgaveLogServiceClient.log(METADATA02.name(), SchemaList.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
				query = new BasicDBObject("tenantId", TenancyHelper.getCurrentTenantId());
				if (!includeRecordsWithImplicitPermissions) {
					List<String> accessibleUuids = MetadataSchemaPermissionDao.getUuidOfAllSharedMetataSchemaItemReadableByUser(this.username);
					BasicDBList or = new BasicDBList();
					or.add(new BasicDBObject("uuid", new BasicDBObject("$in", accessibleUuids)));
					or.add(new BasicDBObject("owner", this.username));
					query.append("$or", or);
				}
			}
			else {
				AgaveLogServiceClient.log(METADATA02.name(), SchemaSearch.name(), username, "", getRequest().getClientInfo().getUpstreamAddress());
				query = BasicDBObject.parse(userQuery);
				for (String key: query.keySet()) {
					if (query.get(key) instanceof String) {
						if (((String) query.get(key)).contains("*")) {
							try {
								Pattern regexPattern = Pattern.compile(query.getString(key), Pattern.LITERAL | Pattern.CASE_INSENSITIVE);
								query.put(key, regexPattern);
							} catch (Exception e) {
								throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Invalid regular expression for " + key + " query");
							}
						}
					}
				}
				query.append("tenantId", TenancyHelper.getCurrentTenantId());
				if (!includeRecordsWithImplicitPermissions) {
					List<String> accessibleUuids = MetadataSchemaPermissionDao.getUuidOfAllSharedMetataSchemaItemReadableByUser(this.username, getOffset(), getLimit());
					List<String> accessibleOwners = Arrays.asList(this.username, Settings.PUBLIC_USER_USERNAME, Settings.WORLD_USER_USERNAME);
					BasicDBList or = new BasicDBList();
					or.add(new BasicDBObject("uuid", new BasicDBObject("$in", accessibleUuids)));
					or.add(new BasicDBObject("owner", new BasicDBObject("$in", accessibleOwners)));
					query.append("$or", or);
				}
			}
			List<String> sortableFields = Arrays.asList("uuid","tenantId", "schema","internalUsername","lastUpdated", "created", "owner");
			String orderField = getOrderBy("lastUpdated");
			if (StringUtils.isBlank(orderField) || !sortableFields.contains(orderField)) {
				throw new SortSyntaxException("Invalid order field. Please specify one of " +
						StringUtils.join(sortableFields, ","), new MetadataException("Invalid sort field"));
			}
			int orderDirection = getOrder(AgaveResourceResultOrdering.DESC).isAscending() ? 1 : -1;
			cursor = collection.find(query, new BasicDBObject("_id", false))
					.sort(new BasicDBObject(orderField, orderDirection))
					.skip(offset)
					.limit(limit);
			List<DBObject> permittedResults = new ArrayList<DBObject>();
			for(DBObject result: cursor.toArray()) {
				result = formatMetadataSchemaObject(result);
				permittedResults.add(result);
			}
			return new IplantSuccessRepresentation(ServiceUtils.unescapeSchemaRefFieldNames(permittedResults.toString()));
		}
		catch(JSONParseException e) {
			throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "Malformed JSON Query", e);
		}
		catch (ResourceException e) {
			log.error("Failed to list metadata schema", e);
			throw e;
		}
		catch (Throwable e)
		{
			log.error("Failed to list metadata schema", e);
			throw new ResourceException(org.restlet.data.Status.SERVER_ERROR_INTERNAL,
					"An error occurred while fetching the metadata schema. " +
							"If this problem persists, " +
							"please contact the system administrators.", e);
		}
		finally {
			try {
				if (cursor != null) {
					cursor.close();
				}
			} catch (Exception ignored) {}
		}
	}
@ Override public Representation represent ( Variant variant ) throws ResourceException { 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." ) ; } BasicDBObject query = null ; if ( StringUtils . isEmpty ( userQuery ) ) { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaList . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; query = new BasicDBObject ( "tenantId" , TenancyHelper . getCurrentTenantId ( ) ) ; if ( ! includeRecordsWithImplicitPermissions ) { List < String > accessibleUuids = MetadataSchemaPermissionDao . getUuidOfAllSharedMetataSchemaItemReadableByUser ( this . username ) ; BasicDBList or = new BasicDBList ( ) ; or . add ( new BasicDBObject ( "uuid" , new BasicDBObject ( "$in" , accessibleUuids ) ) ) ; or . add ( new BasicDBObject ( "owner" , this . username ) ) ; query . append ( "$or" , or ) ; } } else { AgaveLogServiceClient . log ( METADATA02 . name ( ) , SchemaSearch . name ( ) , username , "" , getRequest ( ) . getClientInfo ( ) . getUpstreamAddress ( ) ) ; query = BasicDBObject . parse ( userQuery ) ; for ( String key : query . keySet ( ) ) { if ( query . get ( key ) instanceof String ) { if ( ( ( String ) query . get ( key ) ) . contains ( "*" ) ) { try { Pattern regexPattern = Pattern . compile ( query . getString ( key ) , Pattern . LITERAL | Pattern . CASE_INSENSITIVE ) ; query . put ( key , regexPattern ) ; } catch ( Exception e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Invalid regular expression for " + key + " query" ) ; } } } } query . append ( "tenantId" , TenancyHelper . getCurrentTenantId ( ) ) ; if ( ! includeRecordsWithImplicitPermissions ) { List < String > accessibleUuids = MetadataSchemaPermissionDao . getUuidOfAllSharedMetataSchemaItemReadableByUser ( this . username , getOffset ( ) , getLimit ( ) ) ; List < String > accessibleOwners = Arrays . asList ( this . username , Settings . PUBLIC_USER_USERNAME , Settings . WORLD_USER_USERNAME ) ; BasicDBList or = new BasicDBList ( ) ; or . add ( new BasicDBObject ( "uuid" , new BasicDBObject ( "$in" , accessibleUuids ) ) ) ; or . add ( new BasicDBObject ( "owner" , new BasicDBObject ( "$in" , accessibleOwners ) ) ) ; query . append ( "$or" , or ) ; } } List < String > sortableFields = Arrays . asList ( "uuid" , "tenantId" , "schema" , "internalUsername" , "lastUpdated" , "created" , "owner" ) ; String orderField = getOrderBy ( "lastUpdated" ) ; if ( StringUtils . isBlank ( orderField ) || ! sortableFields . contains ( orderField ) ) { throw new SortSyntaxException ( "Invalid order field. Please specify one of " + StringUtils . join ( sortableFields , "," ) , new MetadataException ( "Invalid sort field" ) ) ; } int orderDirection = getOrder ( AgaveResourceResultOrdering . DESC ) . isAscending ( ) ? 1 : - 1 ; cursor = collection . find ( query , new BasicDBObject ( "_id" , false ) ) . sort ( new BasicDBObject ( orderField , orderDirection ) ) . skip ( offset ) . limit ( limit ) ; List < DBObject > permittedResults = new ArrayList < DBObject > ( ) ; for ( DBObject result : cursor . toArray ( ) ) { result = formatMetadataSchemaObject ( result ) ; permittedResults . add ( result ) ; } return new IplantSuccessRepresentation ( ServiceUtils . unescapeSchemaRefFieldNames ( permittedResults . toString ( ) ) ) ; } catch ( JSONParseException e ) { throw new ResourceException ( Status . CLIENT_ERROR_BAD_REQUEST , "Malformed JSON Query" , e ) ; } catch ( ResourceException e ) { log . error ( "Failed to list metadata schema" , e ) ; throw e ; } catch ( Throwable e ) { log . error ( "Failed to list metadata schema" , e ) ; throw new ResourceException ( org . restlet . data . Status . SERVER_ERROR_INTERNAL , "An error occurred while fetching the metadata schema. " + "If this problem persists, " + "please contact the system administrators." , e ) ; } finally { try { if ( cursor != null ) { cursor . close ( ) ; } } catch ( Exception ignored ) { } } }
This method represents the HTTP GET action.
This method represents the HTTP GET action .
// Include user defined query clauses given within the URL as q=<clauses> // filter results if querying without implicity permissions // append tenancy info // filter results if querying without implicity permissions // permissions are separated from the metadata, so we need to look up available uuid for user. // permission check is not needed since the list came from // a white list of allowsed uuids