Search in sources :

Example 6 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class AtlasService method updateMappingRequest.

/**
 * Updates existing mapping file on the server.
 * @param mapping mapping
 * @param mappingDefinitionId mapping definition ID
 * @param uriInfo URI info
 * @return empty response
 */
@POST
@Path("/mapping/{mappingDefinitionId}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Update Mapping", description = "Update existing mapping file on the server")
@RequestBody(description = "Mapping file content", content = @Content(schema = @Schema(implementation = AtlasMapping.class)))
@ApiResponses(@ApiResponse(responseCode = "200", description = "Succeeded"))
public Response updateMappingRequest(InputStream mapping, @Parameter(description = "Mapping Definition ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId, @Context UriInfo uriInfo) {
    ADMArchiveHandler handler = loadExplodedMappingDirectory(mappingDefinitionId);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    try {
        handler.setMappingDefinitionBytes(mapping);
        handler.persist();
        builder.path(handler.getMappingDefinition().getName());
    } catch (AtlasException e) {
        LOG.error("Error saving Mapping Definition file.\n" + e.getMessage(), e);
        throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
    }
    return Response.ok().location(builder.build()).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) UriBuilder(javax.ws.rs.core.UriBuilder) AtlasException(io.atlasmap.api.AtlasException) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 7 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class AtlasService method createMappingRequest.

/**
 * Saves a file on the server.
 * @param mapping request payload
 * @param mappingFormat file type
 * @param mappingDefinitionId mapping definition ID
 * @param uriInfo URI info
 * @return empty response
 */
@PUT
@Path("/mapping/{mappingFormat}/{mappingDefinitionId}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM })
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "Create Mapping", description = "Save a mapping file on the server")
@RequestBody(description = "Mapping file content", content = @Content(schema = @Schema(implementation = AtlasMapping.class)))
@ApiResponses({ @ApiResponse(responseCode = "200", description = "Succeeded"), @ApiResponse(responseCode = "500", description = "Mapping file save error") })
public Response createMappingRequest(InputStream mapping, @Parameter(description = "Mapping Format") @PathParam("mappingFormat") MappingFileType mappingFormat, @Parameter(description = "Mapping ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId, @Context UriInfo uriInfo) {
    LOG.debug("createMappingRequest (save) with format '{}'", mappingFormat);
    UriBuilder builder = uriInfo.getAbsolutePathBuilder();
    ADMArchiveHandler admHandler = loadExplodedMappingDirectory(mappingDefinitionId);
    switch(mappingFormat) {
        case JSON:
            try {
                admHandler.setMappingDefinitionBytes(mapping);
                admHandler.persist();
                if (admHandler.getMappingDefinition() != null) {
                    builder.path(admHandler.getMappingDefinition().getName());
                }
            } catch (AtlasException e) {
                LOG.error("Error saving Mapping Definition file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
            return Response.ok().location(builder.build()).build();
        case GZ:
            LOG.debug("  saveGzippedADMDigestRequest '{}' - ID: {}", admHandler.getGzippedADMDigestFileName(), mappingDefinitionId);
            try {
                admHandler.setGzippedADMDigest(mapping);
                admHandler.persist();
            } catch (AtlasException e) {
                LOG.error("Error saving gzipped ADM digest file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
            builder.path(admHandler.getGzippedADMDigestFileName());
            return Response.ok().location(builder.build()).build();
        case ZIP:
            LOG.debug("  importADMArchiveRequest - ID:'{}'", mappingDefinitionId);
            try {
                admHandler.setIgnoreLibrary(false);
                admHandler.setLibraryDirectory(Paths.get(libFolder));
                admHandler.load(mapping);
                this.libraryLoader.reload();
                admHandler.persist();
                LOG.debug("  importADMArchiveRequest complete - ID:'{}'", mappingDefinitionId);
            } catch (Exception e) {
                LOG.error("Error importing ADM archive.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
            builder.path("atlasmap-" + mappingDefinitionId + ".adm");
            return Response.ok().location(builder.build()).build();
        case XML:
            throw new WebApplicationException("XML mapping format is no longer supported. Please use JSON format instead.");
        default:
            throw new WebApplicationException("Unrecognized mapping format: " + mappingFormat, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) UriBuilder(javax.ws.rs.core.UriBuilder) AtlasException(io.atlasmap.api.AtlasException) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasException(io.atlasmap.api.AtlasException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Operation(io.swagger.v3.oas.annotations.Operation) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses) RequestBody(io.swagger.v3.oas.annotations.parameters.RequestBody)

Example 8 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class AtlasService method listMappings.

/**
 * Retrieves a list of mapping file name saved with specified mapping definition ID.
 * @param uriInfo URI info
 * @param filter filter
 * @param mappingDefinitionId mapping definition ID
 * @return A list of mapping file name in {@link StringMap}
 */
@GET
@Path("/mappings/{mappingDefinitionId}")
@Produces(MediaType.APPLICATION_JSON)
@Operation(summary = "List Mappings", description = "Retrieves a list of mapping file name saved with specified mappingDefinitionId")
@ApiResponses(@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = StringMap.class)), description = "Return a list of a pair of mapping file name and content"))
public Response listMappings(@Context UriInfo uriInfo, @QueryParam("filter") final String filter, @Parameter(description = "Mapping Definition ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId) {
    StringMap sMap = new StringMap();
    LOG.debug("listMappings with filter '{}'", filter);
    ADMArchiveHandler handler = loadExplodedMappingDirectory(mappingDefinitionId);
    AtlasMapping map = handler.getMappingDefinition();
    if (map == null) {
        return Response.ok().entity(toJson(sMap)).build();
    }
    StringMapEntry mapEntry = new StringMapEntry();
    mapEntry.setName(map.getName());
    UriBuilder builder = uriInfo.getBaseUriBuilder().path("v2").path("atlas").path("mapping").path(map.getName());
    mapEntry.setValue(builder.build().toString());
    sMap.getStringMapEntry().add(mapEntry);
    byte[] serialized = toJson(sMap);
    if (LOG.isDebugEnabled()) {
        LOG.debug(new String(serialized));
    }
    return Response.ok().entity(serialized).build();
}
Also used : StringMap(io.atlasmap.v2.StringMap) AtlasMapping(io.atlasmap.v2.AtlasMapping) StringMapEntry(io.atlasmap.v2.StringMapEntry) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) UriBuilder(javax.ws.rs.core.UriBuilder) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Example 9 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class NestedCollectionJsonTest method testAsymmetricSingleTarget.

@Test
public void testAsymmetricSingleTarget() throws Exception {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-asymmetric.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !"3-1".equals(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.json").toURI())));
    session.setSourceDocument("JSONInstanceNestedCollection", source);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    assertTrue(session.hasWarns(), TestHelper.printAudit(session));
    Object output = session.getTargetDocument("JSONInstanceNestedCollection");
    assertEquals(String.class, output.getClass());
    JsonNode outputJson = mapper.readTree((String) output);
    String prettyPrinted = mapper.writeValueAsString(outputJson);
    ArrayNode firstArray = (ArrayNode) outputJson.get("firstArray");
    assertEquals(10, firstArray.size(), prettyPrinted);
    assertEquals("thirdArrayValue0-0-0", firstArray.get(0).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(0).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-0-1", firstArray.get(1).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(1).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-0", firstArray.get(2).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(2).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-1", firstArray.get(3).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(3).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue0-1-2", firstArray.get(4).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(4).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-0", firstArray.get(5).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(5).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-1", firstArray.get(6).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(6).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-0-2", firstArray.get(7).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(7).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-1-0", firstArray.get(8).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(8).get("secondArray"), prettyPrinted);
    assertEquals("thirdArrayValue1-1-1", firstArray.get(9).get("value").asText(), prettyPrinted);
    assertNull(firstArray.get(9).get("secondArray"), prettyPrinted);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) JsonNode(com.fasterxml.jackson.databind.JsonNode) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasSession(io.atlasmap.api.AtlasSession) Test(org.junit.jupiter.api.Test)

Example 10 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class NestedCollectionXmlTest method processXmlNestedCollection.

private String processXmlNestedCollection(List<String> mappingsToProcess, boolean assertNoWarnings) throws AtlasException, IOException, URISyntaxException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-xml.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !mappingsToProcess.contains(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.xml").toURI())));
    session.setSourceDocument("XMLInstanceNestedCollection", source);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    if (assertNoWarnings) {
        assertFalse(session.hasWarns(), TestHelper.printAudit(session));
    }
    Object output = session.getTargetDocument("XMLInstanceNestedCollection");
    return (String) output;
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) AtlasSession(io.atlasmap.api.AtlasSession)

Aggregations

ADMArchiveHandler (io.atlasmap.core.ADMArchiveHandler)11 AtlasMapping (io.atlasmap.v2.AtlasMapping)6 AtlasContext (io.atlasmap.api.AtlasContext)5 AtlasSession (io.atlasmap.api.AtlasSession)5 InputStream (java.io.InputStream)5 AtlasException (io.atlasmap.api.AtlasException)4 Operation (io.swagger.v3.oas.annotations.Operation)4 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 UriBuilder (javax.ws.rs.core.UriBuilder)3 Test (org.junit.jupiter.api.Test)3 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1