Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix filtering for schema documents. #7

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,6 @@ public static Specification<DataResource> findByAccessRights(Specification<DataR
* @return Specification with schemaIds added.
*/
public static Specification<DataResource> findBySchemaId(Specification<DataResource> specification, List<String> schemaIds) {
// ToDo....
Specification<DataResource> specWithSchema = specification;
if (schemaIds != null) {
List<String> allSchemaIds = new ArrayList<>();
Expand All @@ -685,32 +684,37 @@ public static Specification<DataResource> findBySchemaId(Specification<DataResou
public static final Specification<DataResource> findByMimetypes(List<String> mimeTypes) {
// Search for both mimetypes (xml & json)
ResourceType resourceType = null;
final int JSON = 1; // bit 0
final int XML = 2; // bit 1
//
int searchFor = 0; // 1 - JSON, 2 - XML, 3 - both
if (mimeTypes != null) {
int searchFor = 0; // 1 - JSON, 2 - XML, 3 - both
for (String mimeType : mimeTypes) {
if (mimeType.contains("json")) {
searchFor += 1;
searchFor |= JSON;
}
if (mimeType.contains("xml")) {
searchFor += 2;
searchFor |= XML;
}
}
resourceType = switch (searchFor) {
// 1 -> search for JSON only
case 1 ->
ResourceType.createResourceType(DataResourceRecordUtil.JSON_SCHEMA_TYPE, ResourceType.TYPE_GENERAL.MODEL);
// 2 -> search for XML only
case 2 ->
ResourceType.createResourceType(DataResourceRecordUtil.XML_SCHEMA_TYPE, ResourceType.TYPE_GENERAL.MODEL);
// 3 -> Search for both mimetypes (xml & json)
case 3 ->
ResourceType.createResourceType(DataResourceRecordUtil.SCHEMA_SUFFIX, ResourceType.TYPE_GENERAL.MODEL);
// 0 -> Unknown mimetype
default ->
ResourceType.createResourceType("unknown");
};
}
} else {
searchFor = JSON | XML;
}
resourceType = switch (searchFor) {
// 1 -> search for JSON only
case JSON ->
ResourceType.createResourceType(DataResourceRecordUtil.JSON_SCHEMA_TYPE, ResourceType.TYPE_GENERAL.MODEL);
// 2 -> search for XML only
case XML ->
ResourceType.createResourceType(DataResourceRecordUtil.XML_SCHEMA_TYPE, ResourceType.TYPE_GENERAL.MODEL);
// 3 -> Search for both mimetypes (xml & json)
case JSON | XML ->
ResourceType.createResourceType(DataResourceRecordUtil.SCHEMA_SUFFIX, ResourceType.TYPE_GENERAL.MODEL);
// 0 -> Unknown mimetype
default ->
ResourceType.createResourceType("unknown");
};

return ResourceTypeSpec.toSpecification(resourceType);
}

Expand Down Expand Up @@ -1582,7 +1586,7 @@ private static DataResource fixRelatedSchemaIfNeeded(DataResource dataResource)
* given or check type.
*
* @param metastoreProperties Configuration for accessing services
* @param dataResource Data resource record of the document.
* @param dataResource Data resource record of the document.
* @param document Document of data resource.
*/
private static void validateMetadataDocument(MetastoreConfiguration metastoreProperties,
Expand Down Expand Up @@ -1809,7 +1813,7 @@ private static List<String> getAllAuthorizationIdentities() {

/**
* Set base URL for accessing instances.
*
*
* @param aBaseUrl the baseUrl to set
*/
public static void setBaseUrl(String aBaseUrl) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import edu.kit.datamanager.entities.PERMISSION;
import edu.kit.datamanager.entities.RepoUserRole;
import edu.kit.datamanager.exceptions.ResourceNotFoundException;
import edu.kit.datamanager.metastore2.configuration.ApplicationProperties;
import edu.kit.datamanager.metastore2.configuration.MetastoreConfiguration;
Expand All @@ -29,13 +27,9 @@
import edu.kit.datamanager.metastore2.web.ISchemaRegistryControllerV2;
import edu.kit.datamanager.repo.dao.IDataResourceDao;
import edu.kit.datamanager.repo.dao.spec.dataresource.LastUpdateSpecification;
import edu.kit.datamanager.repo.dao.spec.dataresource.PermissionSpecification;
import edu.kit.datamanager.repo.dao.spec.dataresource.ResourceTypeSpec;
import edu.kit.datamanager.repo.dao.spec.dataresource.StateSpecification;
import edu.kit.datamanager.repo.domain.ContentInformation;
import edu.kit.datamanager.repo.domain.DataResource;
import edu.kit.datamanager.repo.domain.ResourceType;
import edu.kit.datamanager.util.AuthenticationHelper;
import edu.kit.datamanager.util.ControllerUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -73,7 +67,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.logging.Level;

Expand All @@ -85,13 +78,13 @@
@Tag(name = "Schema Registry")
@Schema(description = "Schema Registry")
public class SchemaRegistryControllerImplV2 implements ISchemaRegistryControllerV2 {

private static final Logger LOG = LoggerFactory.getLogger(SchemaRegistryControllerImplV2.class);

private final ApplicationProperties applicationProperties;

private final MetastoreConfiguration schemaConfig;

private final IDataResourceDao dataResourceDao;

/**
Expand All @@ -112,7 +105,7 @@ public SchemaRegistryControllerImplV2(ApplicationProperties applicationPropertie
LOG.info("------{}", schemaConfig);
LOG.info("------------------------------------------------------");
}

@Override
public ResponseEntity<DataResource> createRecord(
@RequestPart(name = "record") final MultipartFile recordDocument,
Expand All @@ -134,37 +127,37 @@ public ResponseEntity<DataResource> createRecord(
java.util.logging.Logger.getLogger(SchemaRegistryControllerImplV2.class.getName()).log(Level.SEVERE, null, ex);
}
}

LOG.trace("Schema record successfully persisted.");
URI locationUri;
locationUri = SchemaRegistryControllerImplV2.getSchemaDocumentUri(dataResourceRecord);
LOG.trace("Set locationUri to '{}'", locationUri);
return ResponseEntity.created(locationUri).eTag("\"" + etag + "\"").body(dataResourceRecord);
}

@Override
public ResponseEntity<DataResource> getRecordById(
@PathVariable(value = "schemaId") String schemaId,
@RequestParam(value = "version", required = false) Long version,
WebRequest wr,
HttpServletResponse hsr) {
LOG.trace("Performing getRecordById({}, {}).", schemaId, version);

DataResource schemaRecord = DataResourceRecordUtil.getRecordByIdAndVersion(schemaConfig, schemaId, version);
String etag = schemaRecord.getEtag();

LOG.trace("Returning result.");
return ResponseEntity.ok().eTag("\"" + etag + "\"").body(schemaRecord);
}

@Override
public ResponseEntity<ContentInformation> getContentInformationById(
@PathVariable(value = "schemaId") String schemaId,
@RequestParam(value = "version", required = false) Long version,
WebRequest wr,
HttpServletResponse hsr) {
LOG.trace("Performing getContentInformationById({}, {}).", schemaId, version);

ContentInformation contentInformation = DataResourceRecordUtil.getContentInformationByIdAndVersion(schemaConfig, schemaId, version);
DataResource minimalDataResource = DataResource.factoryNewDataResource(contentInformation.getParentResource().getId());
URI locationUri;
Expand All @@ -175,7 +168,7 @@ public ResponseEntity<ContentInformation> getContentInformationById(
contentInformation.setVersioningService(null);
return ResponseEntity.ok().body(contentInformation);
}

@Override
public ModelAndView getLandingPageById(@PathVariable(value = "schemaId") String id,
@RequestParam(value = "version", required = false) Long version,
Expand All @@ -189,20 +182,20 @@ public ModelAndView getLandingPageById(@PathVariable(value = "schemaId") String
versionString = version.toString();
}
redirectUrl = "redirect:" + redirectUrl.replace(MetadataControllerImpl.PLACEHOLDER_VERSION, versionString);

LOG.trace("Redirect to '{}'", redirectUrl);

return new ModelAndView(redirectUrl);
}

@Override
public ResponseEntity getSchemaDocumentById(
@PathVariable(value = "schemaId") String schemaId,
@RequestParam(value = "version", required = false) Long version,
WebRequest wr,
HttpServletResponse hsr) {
LOG.trace("Performing getSchemaDocumentById({}, {}).", schemaId, version);

DataResource schemaRecord = DataResourceRecordUtil.getRecordByIdAndVersion(schemaConfig, schemaId, version);
ContentInformation contentInfo = DataResourceRecordUtil.getContentInformationByIdAndVersion(schemaConfig, schemaRecord.getId(), Long.valueOf(schemaRecord.getVersion()));
MediaType contentType = MediaType.valueOf(contentInfo.getMediaType());
Expand All @@ -212,14 +205,14 @@ public ResponseEntity getSchemaDocumentById(
LOG.trace("Schema document at path {} either does not exist or is no file or is not readable. Returning HTTP NOT_FOUND.", schemaDocumentPath);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Schema document on server either does not exist or is no file or is not readable.");
}

return ResponseEntity.
ok().
contentType(contentType).
header(HttpHeaders.CONTENT_LENGTH, String.valueOf(schemaDocumentPath.toFile().length())).
body(new FileSystemResource(schemaDocumentPath.toFile()));
}

public ResponseEntity<List<DataResource>> getAllVersions(
String id,
Pageable pgbl
Expand All @@ -241,12 +234,12 @@ public ResponseEntity<List<DataResource>> getAllVersions(
} catch (ResourceNotFoundException rnfe) {
LOG.info("Schema ID '{}' is unkown. Return empty list...", id);
}

String contentRange = ControllerUtils.getContentRangeHeader(pgbl.getPageNumber(), pgbl.getPageSize(), totalNoOfElements);

return ResponseEntity.status(HttpStatus.OK).header("Content-Range", contentRange).body(recordList);
}

@Override
public ResponseEntity validate(@PathVariable(value = "schemaId") String schemaId,
@RequestParam(value = "version", required = false) Long version,
Expand All @@ -258,7 +251,7 @@ public ResponseEntity validate(@PathVariable(value = "schemaId") String schemaId
LOG.trace("Metadata document validation succeeded. Returning HTTP NOT_CONTENT.");
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@Override
public ResponseEntity<List<DataResource>> getRecords(@RequestParam(value = "schemaId", required = false) String schemaId,
@RequestParam(value = "mimeType", required = false) List<String> mimeTypes,
Expand All @@ -285,7 +278,7 @@ public ResponseEntity<List<DataResource>> getRecords(@RequestParam(value = "sche
DataResource.State[] states = {DataResource.State.FIXED, DataResource.State.VOLATILE};
List<DataResource.State> stateList = Arrays.asList(states);
spec = spec.and(StateSpecification.toSpecification(stateList));

LOG.debug("Performing query for records.");
Page<DataResource> records = DataResourceRecordUtil.queryDataResources(spec, pgbl);
List<DataResource> recordList = records.getContent();
Expand All @@ -295,12 +288,12 @@ public ResponseEntity<List<DataResource>> getRecords(@RequestParam(value = "sche
LOG.trace("---> " + item.toString());
}
}

String contentRange = ControllerUtils.getContentRangeHeader(pgbl.getPageNumber(), pgbl.getPageSize(), records.getTotalElements());

return ResponseEntity.status(HttpStatus.OK).header("Content-Range", contentRange).body(recordList);
}

@Override
public ResponseEntity<DataResource> updateRecord(@PathVariable("schemaId") final String schemaId,
@RequestPart(name = "record", required = false) MultipartFile schemaRecord,
Expand All @@ -311,7 +304,7 @@ public ResponseEntity<DataResource> updateRecord(@PathVariable("schemaId") final
getById = t -> WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).getRecordById(t, null, request, response)).toString();
String eTag = ControllerUtils.getEtagFromHeader(request);
DataResource updatedSchemaRecord = DataResourceRecordUtil.updateDataResource4SchemaDocument(schemaConfig, schemaId, eTag, schemaRecord, document, getById);

LOG.trace("DataResource record successfully persisted. Updating document URI and returning result.");
String etag = updatedSchemaRecord.getEtag();

Expand All @@ -320,7 +313,7 @@ public ResponseEntity<DataResource> updateRecord(@PathVariable("schemaId") final
LOG.trace("Set locationUri to '{}'", locationUri);
return ResponseEntity.ok().location(locationUri).eTag("\"" + etag + "\"").body(updatedSchemaRecord);
}

@Override
public ResponseEntity deleteRecord(@PathVariable("schemaId") final String schemaId,
WebRequest request,
Expand All @@ -329,25 +322,25 @@ public ResponseEntity deleteRecord(@PathVariable("schemaId") final String schema
UnaryOperator<String> getById;
getById = t -> WebMvcLinkBuilder.linkTo(WebMvcLinkBuilder.methodOn(this.getClass()).getRecordById(t, null, request, hsr)).toString();
String eTag = ControllerUtils.getEtagFromHeader(request);

MetadataSchemaRecordUtil.deleteMetadataSchemaRecord(schemaConfig, schemaId, eTag, getById);

return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@Override
public void contribute(Info.Builder builder) {
LOG.trace("Check for SchemaRepo actuator information...");

URL basePath = schemaConfig.getBasepath();
Map<String, String> details = ActuatorUtil.testDirectory(basePath);

if (!details.isEmpty()) {
details.put("No of schema documents", Long.toString(DataResourceRecordUtil.getNoOfSchemaDocuments()));
builder.withDetail("schemaRepo", details);
}
}

/**
* Get URI for accessing schema document via schemaId and version.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ public void setUp() throws Exception {
prepareRepo();
}

@Test
public void testFindAllSchemaRecords() throws Exception {
ObjectMapper map = new ObjectMapper();
MvcResult res = this.mockMvc.perform(get("/api/v1/schemas/")
.header("Accept", MetadataSchemaRecord.METADATA_SCHEMA_RECORD_MEDIA_TYPE))
.andDo(print())
.andExpect(status().isOk())
.andReturn();
MetadataSchemaRecord[] result = map.readValue(res.getResponse().getContentAsString(), MetadataSchemaRecord[].class);
Assert.assertEquals("No of schema records:", MAX_NO_OF_SCHEMAS * 2, result.length);
}

@Test
public void testFindSchemaRecordsBySchemaId() throws Exception {
ObjectMapper map = new ObjectMapper();
Expand Down
Loading