@Test public void testCreateMapping() throws Exception{ WebTarget target = target("/" + MappingOptionListContainer.MAPPINGS); String id = "SampleId2"; String description = "NONE"; String srcSchemaId = "edm"; String srcSchemaVersion = "1.0"; String destSchemaId = "ese"; String destSchemaVersion = "3.3"; String xslFilename = "myXSLT2"; boolean isXslVersion2 = true; MetadataTransformation mtdTransformation = new MetadataTransformation(); mtdTransformation.setId(id); mtdTransformation.setDescription(description); mtdTransformation.setSourceSchemaId(srcSchemaId); mtdTransformation.setDestinationSchemaId(destSchemaId); mtdTransformation.setStylesheet(xslFilename); mtdTransformation.setSourceSchemaVersion(srcSchemaVersion); mtdTransformation.setDestSchemaVersion(destSchemaVersion); mtdTransformation.setVersionTwo(isXslVersion2); File tempFile = File.createTempFile("example", "xsl"); tempFile.deleteOnExit(); final FileDataBodyPart filePart = new FileDataBodyPart("myFile", tempFile, MediaType.APPLICATION_OCTET_STREAM_TYPE); MetadataTransformation mtdTransformationFake = new MetadataTransformation(); MultiPart multiPartEntity = new MultiPart(); multiPartEntity.bodyPart(new BodyPart(mtdTransformationFake, MediaType.APPLICATION_XML_TYPE)); multiPartEntity.bodyPart(filePart); Response response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setId(id); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setSourceSchemaId(srcSchemaId); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setDestinationSchemaId(destSchemaId); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setStylesheet(xslFilename); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setSourceSchemaVersion(srcSchemaVersion); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(406, response.getStatus()); mtdTransformationFake.setDestSchemaVersion("3.0"); mtdTransformationFake.setStylesheet("example.txt"); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(400, response.getStatus()); multiPartEntity.close(); multiPartEntity = new MultiPart(); multiPartEntity.bodyPart(new BodyPart(mtdTransformation, MediaType.APPLICATION_XML_TYPE)); multiPartEntity.bodyPart(filePart); when(metadataTransformationManager.getXsltDir()).thenReturn(null); when(TransformationsFileManager.writeXslFile(anyString(), any(File.class), any(InputStream.class))).thenReturn(TransformationsFileManager.Response.ERROR) .thenReturn(TransformationsFileManager.Response.FILE_TOO_BIG).thenReturn(TransformationsFileManager.Response.XSL_ALREADY_EXISTS) .thenReturn(TransformationsFileManager.Response.SUCCESS); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(500, response.getStatus()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(500, response.getStatus()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(409, response.getStatus()); when(metadataSchemaManager.getSchemaXSD(anyString(), anyDouble())).thenReturn(null).thenReturn("SomeSourceSchema").thenReturn(null).thenReturn("SomeDestinationSchema"); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(404, response.getStatus()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(404, response.getStatus()); MetadataSchema metadataSchema = mock(MetadataSchema.class); when(metadataSchemaManager.getMetadataSchema(anyString())).thenReturn(metadataSchema); when(metadataSchema.getNamespace()).thenReturn("SomeNamespace"); doThrow(new IOException()).doThrow(new SameStylesheetTransformationException("Another mapping has the same xslt file!")).doNothing().when(metadataTransformationManager) .saveMetadataTransformation(any(MetadataTransformation.class), anyString()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(500, response.getStatus()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(409, response.getStatus()); response = target.request().post(Entity.entity(multiPartEntity, new MediaType("multipart", "mixed")), Response.class); assertEquals(201, response.getStatus()); multiPartEntity.close(); }
@POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response createMapping(MultiPart multiPart) throws AlreadyExistsException, InternalServerErrorException, MissingArgumentsException, DoesNotExistException, InvalidArgumentsException { MetadataTransformation metadataTransformation = multiPart.getBodyParts().get(0).getEntityAs(MetadataTransformation.class); BodyPartEntity fileBodyPartEntity = (BodyPartEntity)multiPart.getBodyParts().get(1).getEntity(); InputStream xsltInputStream = fileBodyPartEntity.getInputStream(); if (metadataTransformation == null) throw new MissingArgumentsException("Missing value: " + "MetadataTransformation was null"); else if (xsltInputStream == null) throw new MissingArgumentsException("Missing value: " + "xsltInputStream was null"); if (metadataTransformation.getId() == null || metadataTransformation.getId().equals("")) throw new MissingArgumentsException("Missing value: " + "Id must not be empty"); else if (metadataTransformation.getSourceSchemaId() == null || metadataTransformation.getSourceSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaId must not be empty"); else if (metadataTransformation.getDestinationSchemaId() == null || metadataTransformation.getDestinationSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "DestinationSchemaId must not be empty"); else if (metadataTransformation.getStylesheet() == null || metadataTransformation.getStylesheet().equals("")) throw new MissingArgumentsException("Missing value: " + "Stylesheet must not be empty"); else if (metadataTransformation.getSourceSchemaVersion() == null || metadataTransformation.getSourceSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaVersion must not be empty"); else if (metadataTransformation.getDestSchemaVersion() == null || metadataTransformation.getDestSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "DestSchemaVersion must not be empty"); File xsltDir = metadataTransformationManager.getXsltDir(); String extension = FilenameUtils.getExtension(metadataTransformation.getStylesheet()); if (!extension.equals("xsl") && !extension.equals("")) throw new InvalidArgumentsException("Stylesheet name must be without extension or with .xsl"); else if (extension.equals("xsl")) { metadataTransformation.setStylesheet(FilenameUtils.removeExtension(metadataTransformation.getStylesheet())); } if (oldMappingId != null && !oldMappingId.isEmpty()) { MetadataTransformation mapping = getMapping(oldMappingId); TransformationsFileManager.deleteTransformationFiles(FilenameUtils.removeExtension(mapping.getStylesheet()), xsltDir, metadataTransformationManager.getXmapDir()); } String srcXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getSourceSchemaId(), Double.valueOf(metadataTransformation.getSourceSchemaVersion())); if (srcXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getSourceSchemaId() + " and version " + metadataTransformation.getSourceSchemaVersion() + " does NOT exist!"); String destXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getDestinationSchemaId(), Double.valueOf(metadataTransformation.getDestSchemaVersion())); if (destXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getDestinationSchemaId() + " and version " + metadataTransformation.getDestSchemaVersion() + " does NOT exist!"); String destNamespace = metadataSchemaManager.getMetadataSchema(metadataTransformation.getDestinationSchemaId()).getNamespace(); TransformationsFileManager.Response result = TransformationsFileManager.writeXslFile(metadataTransformation.getStylesheet() + ".xsl", xsltDir, xsltInputStream); if (result == TransformationsFileManager.Response.ERROR) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" error during file saving."); } else if (result == TransformationsFileManager.Response.FILE_TOO_BIG) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xsd file is too big."); } else if (result == TransformationsFileManager.Response.XSL_ALREADY_EXISTS) { throw new AlreadyExistsException("Already exists: " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xslt filename already exists."); } metadataTransformation.setEditable(false); metadataTransformation.setSourceSchema(srcXsdLink); metadataTransformation.setDestSchema(destXsdLink); metadataTransformation.setDestNamespace(destNamespace); metadataTransformation.setMDRCompliant(true); metadataTransformation.setDeleteOldFiles(true); try { metadataTransformationManager.saveMetadataTransformation(metadataTransformation, oldMappingId); } catch (IOException | DocumentException e) { throw new InternalServerErrorException("Error in server : " + e.getMessage()); } catch (SameStylesheetTransformationException e) { throw new AlreadyExistsException("Already exists: " + "Same Stylesheet"); } return Response.status(201).entity(new Result("Mapping with id " + metadataTransformation.getId() + " created!")).build(); }
MappingResource { @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response createMapping(MultiPart multiPart) throws AlreadyExistsException, InternalServerErrorException, MissingArgumentsException, DoesNotExistException, InvalidArgumentsException { MetadataTransformation metadataTransformation = multiPart.getBodyParts().get(0).getEntityAs(MetadataTransformation.class); BodyPartEntity fileBodyPartEntity = (BodyPartEntity)multiPart.getBodyParts().get(1).getEntity(); InputStream xsltInputStream = fileBodyPartEntity.getInputStream(); if (metadataTransformation == null) throw new MissingArgumentsException("Missing value: " + "MetadataTransformation was null"); else if (xsltInputStream == null) throw new MissingArgumentsException("Missing value: " + "xsltInputStream was null"); if (metadataTransformation.getId() == null || metadataTransformation.getId().equals("")) throw new MissingArgumentsException("Missing value: " + "Id must not be empty"); else if (metadataTransformation.getSourceSchemaId() == null || metadataTransformation.getSourceSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaId must not be empty"); else if (metadataTransformation.getDestinationSchemaId() == null || metadataTransformation.getDestinationSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "DestinationSchemaId must not be empty"); else if (metadataTransformation.getStylesheet() == null || metadataTransformation.getStylesheet().equals("")) throw new MissingArgumentsException("Missing value: " + "Stylesheet must not be empty"); else if (metadataTransformation.getSourceSchemaVersion() == null || metadataTransformation.getSourceSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaVersion must not be empty"); else if (metadataTransformation.getDestSchemaVersion() == null || metadataTransformation.getDestSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "DestSchemaVersion must not be empty"); File xsltDir = metadataTransformationManager.getXsltDir(); String extension = FilenameUtils.getExtension(metadataTransformation.getStylesheet()); if (!extension.equals("xsl") && !extension.equals("")) throw new InvalidArgumentsException("Stylesheet name must be without extension or with .xsl"); else if (extension.equals("xsl")) { metadataTransformation.setStylesheet(FilenameUtils.removeExtension(metadataTransformation.getStylesheet())); } if (oldMappingId != null && !oldMappingId.isEmpty()) { MetadataTransformation mapping = getMapping(oldMappingId); TransformationsFileManager.deleteTransformationFiles(FilenameUtils.removeExtension(mapping.getStylesheet()), xsltDir, metadataTransformationManager.getXmapDir()); } String srcXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getSourceSchemaId(), Double.valueOf(metadataTransformation.getSourceSchemaVersion())); if (srcXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getSourceSchemaId() + " and version " + metadataTransformation.getSourceSchemaVersion() + " does NOT exist!"); String destXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getDestinationSchemaId(), Double.valueOf(metadataTransformation.getDestSchemaVersion())); if (destXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getDestinationSchemaId() + " and version " + metadataTransformation.getDestSchemaVersion() + " does NOT exist!"); String destNamespace = metadataSchemaManager.getMetadataSchema(metadataTransformation.getDestinationSchemaId()).getNamespace(); TransformationsFileManager.Response result = TransformationsFileManager.writeXslFile(metadataTransformation.getStylesheet() + ".xsl", xsltDir, xsltInputStream); if (result == TransformationsFileManager.Response.ERROR) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" error during file saving."); } else if (result == TransformationsFileManager.Response.FILE_TOO_BIG) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xsd file is too big."); } else if (result == TransformationsFileManager.Response.XSL_ALREADY_EXISTS) { throw new AlreadyExistsException("Already exists: " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xslt filename already exists."); } metadataTransformation.setEditable(false); metadataTransformation.setSourceSchema(srcXsdLink); metadataTransformation.setDestSchema(destXsdLink); metadataTransformation.setDestNamespace(destNamespace); metadataTransformation.setMDRCompliant(true); metadataTransformation.setDeleteOldFiles(true); try { metadataTransformationManager.saveMetadataTransformation(metadataTransformation, oldMappingId); } catch (IOException | DocumentException e) { throw new InternalServerErrorException("Error in server : " + e.getMessage()); } catch (SameStylesheetTransformationException e) { throw new AlreadyExistsException("Already exists: " + "Same Stylesheet"); } return Response.status(201).entity(new Result("Mapping with id " + metadataTransformation.getId() + " created!")).build(); } }
MappingResource { @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response createMapping(MultiPart multiPart) throws AlreadyExistsException, InternalServerErrorException, MissingArgumentsException, DoesNotExistException, InvalidArgumentsException { MetadataTransformation metadataTransformation = multiPart.getBodyParts().get(0).getEntityAs(MetadataTransformation.class); BodyPartEntity fileBodyPartEntity = (BodyPartEntity)multiPart.getBodyParts().get(1).getEntity(); InputStream xsltInputStream = fileBodyPartEntity.getInputStream(); if (metadataTransformation == null) throw new MissingArgumentsException("Missing value: " + "MetadataTransformation was null"); else if (xsltInputStream == null) throw new MissingArgumentsException("Missing value: " + "xsltInputStream was null"); if (metadataTransformation.getId() == null || metadataTransformation.getId().equals("")) throw new MissingArgumentsException("Missing value: " + "Id must not be empty"); else if (metadataTransformation.getSourceSchemaId() == null || metadataTransformation.getSourceSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaId must not be empty"); else if (metadataTransformation.getDestinationSchemaId() == null || metadataTransformation.getDestinationSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "DestinationSchemaId must not be empty"); else if (metadataTransformation.getStylesheet() == null || metadataTransformation.getStylesheet().equals("")) throw new MissingArgumentsException("Missing value: " + "Stylesheet must not be empty"); else if (metadataTransformation.getSourceSchemaVersion() == null || metadataTransformation.getSourceSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaVersion must not be empty"); else if (metadataTransformation.getDestSchemaVersion() == null || metadataTransformation.getDestSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "DestSchemaVersion must not be empty"); File xsltDir = metadataTransformationManager.getXsltDir(); String extension = FilenameUtils.getExtension(metadataTransformation.getStylesheet()); if (!extension.equals("xsl") && !extension.equals("")) throw new InvalidArgumentsException("Stylesheet name must be without extension or with .xsl"); else if (extension.equals("xsl")) { metadataTransformation.setStylesheet(FilenameUtils.removeExtension(metadataTransformation.getStylesheet())); } if (oldMappingId != null && !oldMappingId.isEmpty()) { MetadataTransformation mapping = getMapping(oldMappingId); TransformationsFileManager.deleteTransformationFiles(FilenameUtils.removeExtension(mapping.getStylesheet()), xsltDir, metadataTransformationManager.getXmapDir()); } String srcXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getSourceSchemaId(), Double.valueOf(metadataTransformation.getSourceSchemaVersion())); if (srcXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getSourceSchemaId() + " and version " + metadataTransformation.getSourceSchemaVersion() + " does NOT exist!"); String destXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getDestinationSchemaId(), Double.valueOf(metadataTransformation.getDestSchemaVersion())); if (destXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getDestinationSchemaId() + " and version " + metadataTransformation.getDestSchemaVersion() + " does NOT exist!"); String destNamespace = metadataSchemaManager.getMetadataSchema(metadataTransformation.getDestinationSchemaId()).getNamespace(); TransformationsFileManager.Response result = TransformationsFileManager.writeXslFile(metadataTransformation.getStylesheet() + ".xsl", xsltDir, xsltInputStream); if (result == TransformationsFileManager.Response.ERROR) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" error during file saving."); } else if (result == TransformationsFileManager.Response.FILE_TOO_BIG) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xsd file is too big."); } else if (result == TransformationsFileManager.Response.XSL_ALREADY_EXISTS) { throw new AlreadyExistsException("Already exists: " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xslt filename already exists."); } metadataTransformation.setEditable(false); metadataTransformation.setSourceSchema(srcXsdLink); metadataTransformation.setDestSchema(destXsdLink); metadataTransformation.setDestNamespace(destNamespace); metadataTransformation.setMDRCompliant(true); metadataTransformation.setDeleteOldFiles(true); try { metadataTransformationManager.saveMetadataTransformation(metadataTransformation, oldMappingId); } catch (IOException | DocumentException e) { throw new InternalServerErrorException("Error in server : " + e.getMessage()); } catch (SameStylesheetTransformationException e) { throw new AlreadyExistsException("Already exists: " + "Same Stylesheet"); } return Response.status(201).entity(new Result("Mapping with id " + metadataTransformation.getId() + " created!")).build(); } MappingResource();  MappingResource(DefaultDataManager dataManager, MetadataTransformationManager metadataTransformationManager, MetadataSchemaManager metadataSchemaManager); }
MappingResource { @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response createMapping(MultiPart multiPart) throws AlreadyExistsException, InternalServerErrorException, MissingArgumentsException, DoesNotExistException, InvalidArgumentsException { MetadataTransformation metadataTransformation = multiPart.getBodyParts().get(0).getEntityAs(MetadataTransformation.class); BodyPartEntity fileBodyPartEntity = (BodyPartEntity)multiPart.getBodyParts().get(1).getEntity(); InputStream xsltInputStream = fileBodyPartEntity.getInputStream(); if (metadataTransformation == null) throw new MissingArgumentsException("Missing value: " + "MetadataTransformation was null"); else if (xsltInputStream == null) throw new MissingArgumentsException("Missing value: " + "xsltInputStream was null"); if (metadataTransformation.getId() == null || metadataTransformation.getId().equals("")) throw new MissingArgumentsException("Missing value: " + "Id must not be empty"); else if (metadataTransformation.getSourceSchemaId() == null || metadataTransformation.getSourceSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaId must not be empty"); else if (metadataTransformation.getDestinationSchemaId() == null || metadataTransformation.getDestinationSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "DestinationSchemaId must not be empty"); else if (metadataTransformation.getStylesheet() == null || metadataTransformation.getStylesheet().equals("")) throw new MissingArgumentsException("Missing value: " + "Stylesheet must not be empty"); else if (metadataTransformation.getSourceSchemaVersion() == null || metadataTransformation.getSourceSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaVersion must not be empty"); else if (metadataTransformation.getDestSchemaVersion() == null || metadataTransformation.getDestSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "DestSchemaVersion must not be empty"); File xsltDir = metadataTransformationManager.getXsltDir(); String extension = FilenameUtils.getExtension(metadataTransformation.getStylesheet()); if (!extension.equals("xsl") && !extension.equals("")) throw new InvalidArgumentsException("Stylesheet name must be without extension or with .xsl"); else if (extension.equals("xsl")) { metadataTransformation.setStylesheet(FilenameUtils.removeExtension(metadataTransformation.getStylesheet())); } if (oldMappingId != null && !oldMappingId.isEmpty()) { MetadataTransformation mapping = getMapping(oldMappingId); TransformationsFileManager.deleteTransformationFiles(FilenameUtils.removeExtension(mapping.getStylesheet()), xsltDir, metadataTransformationManager.getXmapDir()); } String srcXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getSourceSchemaId(), Double.valueOf(metadataTransformation.getSourceSchemaVersion())); if (srcXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getSourceSchemaId() + " and version " + metadataTransformation.getSourceSchemaVersion() + " does NOT exist!"); String destXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getDestinationSchemaId(), Double.valueOf(metadataTransformation.getDestSchemaVersion())); if (destXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getDestinationSchemaId() + " and version " + metadataTransformation.getDestSchemaVersion() + " does NOT exist!"); String destNamespace = metadataSchemaManager.getMetadataSchema(metadataTransformation.getDestinationSchemaId()).getNamespace(); TransformationsFileManager.Response result = TransformationsFileManager.writeXslFile(metadataTransformation.getStylesheet() + ".xsl", xsltDir, xsltInputStream); if (result == TransformationsFileManager.Response.ERROR) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" error during file saving."); } else if (result == TransformationsFileManager.Response.FILE_TOO_BIG) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xsd file is too big."); } else if (result == TransformationsFileManager.Response.XSL_ALREADY_EXISTS) { throw new AlreadyExistsException("Already exists: " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xslt filename already exists."); } metadataTransformation.setEditable(false); metadataTransformation.setSourceSchema(srcXsdLink); metadataTransformation.setDestSchema(destXsdLink); metadataTransformation.setDestNamespace(destNamespace); metadataTransformation.setMDRCompliant(true); metadataTransformation.setDeleteOldFiles(true); try { metadataTransformationManager.saveMetadataTransformation(metadataTransformation, oldMappingId); } catch (IOException | DocumentException e) { throw new InternalServerErrorException("Error in server : " + e.getMessage()); } catch (SameStylesheetTransformationException e) { throw new AlreadyExistsException("Already exists: " + "Same Stylesheet"); } return Response.status(201).entity(new Result("Mapping with id " + metadataTransformation.getId() + " created!")).build(); } MappingResource();  MappingResource(DefaultDataManager dataManager, MetadataTransformationManager metadataTransformationManager, MetadataSchemaManager metadataSchemaManager); @OPTIONS @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over mappings conext.", httpMethod = "OPTIONS", response = MappingOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) MappingOptionListContainer getOptions(); @GET @Path("/" + MappingOptionListContainer.OPTIONS) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over mappings conext.", httpMethod = "GET", response = MappingOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) MappingOptionListContainer getGETOptions(); @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response createMapping(MultiPart multiPart); @GET @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Retrieves the specified mapping.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing an Dataset)"), @ApiResponse(code = 404, message = "DoesNotExistException") }) MetadataTransformation getMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId); @DELETE @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Delete a mapping.", httpMethod = "DELETE", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response deleteMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId); @PUT @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Update a mapping.", httpMethod = "PUT", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response updateMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId,
            MultiPart multiPart); }
MappingResource { @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) public Response createMapping(MultiPart multiPart) throws AlreadyExistsException, InternalServerErrorException, MissingArgumentsException, DoesNotExistException, InvalidArgumentsException { MetadataTransformation metadataTransformation = multiPart.getBodyParts().get(0).getEntityAs(MetadataTransformation.class); BodyPartEntity fileBodyPartEntity = (BodyPartEntity)multiPart.getBodyParts().get(1).getEntity(); InputStream xsltInputStream = fileBodyPartEntity.getInputStream(); if (metadataTransformation == null) throw new MissingArgumentsException("Missing value: " + "MetadataTransformation was null"); else if (xsltInputStream == null) throw new MissingArgumentsException("Missing value: " + "xsltInputStream was null"); if (metadataTransformation.getId() == null || metadataTransformation.getId().equals("")) throw new MissingArgumentsException("Missing value: " + "Id must not be empty"); else if (metadataTransformation.getSourceSchemaId() == null || metadataTransformation.getSourceSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaId must not be empty"); else if (metadataTransformation.getDestinationSchemaId() == null || metadataTransformation.getDestinationSchemaId().equals("")) throw new MissingArgumentsException("Missing value: " + "DestinationSchemaId must not be empty"); else if (metadataTransformation.getStylesheet() == null || metadataTransformation.getStylesheet().equals("")) throw new MissingArgumentsException("Missing value: " + "Stylesheet must not be empty"); else if (metadataTransformation.getSourceSchemaVersion() == null || metadataTransformation.getSourceSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "SourceSchemaVersion must not be empty"); else if (metadataTransformation.getDestSchemaVersion() == null || metadataTransformation.getDestSchemaVersion().equals("")) throw new MissingArgumentsException("Missing value: " + "DestSchemaVersion must not be empty"); File xsltDir = metadataTransformationManager.getXsltDir(); String extension = FilenameUtils.getExtension(metadataTransformation.getStylesheet()); if (!extension.equals("xsl") && !extension.equals("")) throw new InvalidArgumentsException("Stylesheet name must be without extension or with .xsl"); else if (extension.equals("xsl")) { metadataTransformation.setStylesheet(FilenameUtils.removeExtension(metadataTransformation.getStylesheet())); } if (oldMappingId != null && !oldMappingId.isEmpty()) { MetadataTransformation mapping = getMapping(oldMappingId); TransformationsFileManager.deleteTransformationFiles(FilenameUtils.removeExtension(mapping.getStylesheet()), xsltDir, metadataTransformationManager.getXmapDir()); } String srcXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getSourceSchemaId(), Double.valueOf(metadataTransformation.getSourceSchemaVersion())); if (srcXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getSourceSchemaId() + " and version " + metadataTransformation.getSourceSchemaVersion() + " does NOT exist!"); String destXsdLink = metadataSchemaManager.getSchemaXSD(metadataTransformation.getDestinationSchemaId(), Double.valueOf(metadataTransformation.getDestSchemaVersion())); if (destXsdLink == null) throw new DoesNotExistException("Schema with id " + metadataTransformation.getDestinationSchemaId() + " and version " + metadataTransformation.getDestSchemaVersion() + " does NOT exist!"); String destNamespace = metadataSchemaManager.getMetadataSchema(metadataTransformation.getDestinationSchemaId()).getNamespace(); TransformationsFileManager.Response result = TransformationsFileManager.writeXslFile(metadataTransformation.getStylesheet() + ".xsl", xsltDir, xsltInputStream); if (result == TransformationsFileManager.Response.ERROR) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" error during file saving."); } else if (result == TransformationsFileManager.Response.FILE_TOO_BIG) { throw new InternalServerErrorException("Error in server : " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xsd file is too big."); } else if (result == TransformationsFileManager.Response.XSL_ALREADY_EXISTS) { throw new AlreadyExistsException("Already exists: " + "Error creating Mapping: id \"" + metadataTransformation.getId() + "\" xslt filename already exists."); } metadataTransformation.setEditable(false); metadataTransformation.setSourceSchema(srcXsdLink); metadataTransformation.setDestSchema(destXsdLink); metadataTransformation.setDestNamespace(destNamespace); metadataTransformation.setMDRCompliant(true); metadataTransformation.setDeleteOldFiles(true); try { metadataTransformationManager.saveMetadataTransformation(metadataTransformation, oldMappingId); } catch (IOException | DocumentException e) { throw new InternalServerErrorException("Error in server : " + e.getMessage()); } catch (SameStylesheetTransformationException e) { throw new AlreadyExistsException("Already exists: " + "Same Stylesheet"); } return Response.status(201).entity(new Result("Mapping with id " + metadataTransformation.getId() + " created!")).build(); } MappingResource();  MappingResource(DefaultDataManager dataManager, MetadataTransformationManager metadataTransformationManager, MetadataSchemaManager metadataSchemaManager); @OPTIONS @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over mappings conext.", httpMethod = "OPTIONS", response = MappingOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) MappingOptionListContainer getOptions(); @GET @Path("/" + MappingOptionListContainer.OPTIONS) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Get options over mappings conext.", httpMethod = "GET", response = MappingOptionListContainer.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a list of all available options)") }) MappingOptionListContainer getGETOptions(); @POST @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Create a new mapping - XSL file through HTTP POST.", httpMethod = "POST", response = String.class) @ApiResponses(value = { @ApiResponse(code = 201, message = "Created (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response createMapping(MultiPart multiPart); @GET @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Retrieves the specified mapping.", httpMethod = "GET", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing an Dataset)"), @ApiResponse(code = 404, message = "DoesNotExistException") }) MetadataTransformation getMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId); @DELETE @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @ApiOperation(value = "Delete a mapping.", httpMethod = "DELETE", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response deleteMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId); @PUT @Path("/" + MappingOptionListContainer.MAPPINGID) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Consumes("multipart/mixed") @ApiOperation(value = "Update a mapping.", httpMethod = "PUT", response = String.class) @ApiResponses(value = { @ApiResponse(code = 200, message = "OK (Response containing a String message)"), @ApiResponse(code = 400, message = "InvalidArgumentsException"), @ApiResponse(code = 404, message = "DoesNotExistException"), @ApiResponse(code = 406, message = "MissingArgumentsException"), @ApiResponse(code = 409, message = "AlreadyExistsException"), @ApiResponse(code = 500, message = "InternalServerErrorException") }) Response updateMapping(@ApiParam(value = "Id of mapping", required = true) @PathParam("mappingId") String mappingId,
            MultiPart multiPart); public DefaultDataManager dataManager; public MetadataTransformationManager metadataTransformationManager; public MetadataSchemaManager metadataSchemaManager; }