Search in sources :

Example 1 with AtlasMappingFormat

use of io.atlasmap.core.AtlasMappingService.AtlasMappingFormat in project atlasmap by atlasmap.

the class AtlasEndpoint method getOrCreateAtlasContext.

private AtlasContext getOrCreateAtlasContext(Message incomingMessage) throws Exception {
    String path = getResourceUri();
    ObjectHelper.notNull(path, "mappingUri");
    AtlasMappingFormat mappingFormat = path.toLowerCase().endsWith("json") ? AtlasMappingFormat.JSON : AtlasMappingFormat.XML;
    Reader reader;
    String content = incomingMessage.getHeader(AtlasConstants.ATLAS_MAPPING, String.class);
    if (content != null) {
        // use content from header
        reader = new StringReader(content);
        if (log.isDebugEnabled()) {
            log.debug("Atlas mapping content read from header {} for endpoint {}", AtlasConstants.ATLAS_MAPPING, getEndpointUri());
        }
        // remove the header to avoid it being propagated in the routing
        incomingMessage.removeHeader(AtlasConstants.ATLAS_MAPPING);
        AtlasMapping mapping = ((DefaultAtlasContextFactory) getOrCreateAtlasContextFactory()).getMappingService().loadMapping(reader, mappingFormat);
        return ((DefaultAtlasContextFactory) getOrCreateAtlasContextFactory()).createContext(mapping);
    } else if (getAtlasContext() != null) {
        // no mapping specified in header, and found an existing context
        return getAtlasContext();
    }
    // No mapping in header, and no existing context. Create new one from resourceUri
    if (log.isDebugEnabled()) {
        log.debug("Atlas mapping content read from resourceUri: {} for endpoint {}", new Object[] { path, getEndpointUri() });
    }
    reader = getEncoding() != null ? new InputStreamReader(getResourceAsInputStream(), getEncoding()) : new InputStreamReader(getResourceAsInputStream());
    AtlasMapping mapping = ((DefaultAtlasContextFactory) getOrCreateAtlasContextFactory()).getMappingService().loadMapping(reader, mappingFormat);
    atlasContext = ((DefaultAtlasContextFactory) getOrCreateAtlasContextFactory()).createContext(mapping);
    return atlasContext;
}
Also used : DefaultAtlasContextFactory(io.atlasmap.core.DefaultAtlasContextFactory) AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader) AtlasMappingFormat(io.atlasmap.core.AtlasMappingService.AtlasMappingFormat) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) StringReader(java.io.StringReader)

Example 2 with AtlasMappingFormat

use of io.atlasmap.core.AtlasMappingService.AtlasMappingFormat in project atlasmap by atlasmap.

the class AtlasMappingServiceTest method testLoadMappingStringAtlasMappingFormat.

@Test
public void testLoadMappingStringAtlasMappingFormat() throws AtlasValidationException {
    String filename = "src" + File.separator + "test" + File.separator + "resources" + File.separator + "atlasmapping.xml";
    AtlasMappingFormat format = AtlasMappingFormat.XML;
    assertNotNull(format.value());
    assertNotNull(AtlasMappingFormat.valueOf("XML"));
    AtlasMapping atlasMapping = atlasMappingService.loadMapping(filename, format);
    assertNotNull(atlasMapping);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasMappingFormat(io.atlasmap.core.AtlasMappingService.AtlasMappingFormat) Test(org.junit.Test)

Aggregations

AtlasMappingFormat (io.atlasmap.core.AtlasMappingService.AtlasMappingFormat)2 AtlasMapping (io.atlasmap.v2.AtlasMapping)2 DefaultAtlasContextFactory (io.atlasmap.core.DefaultAtlasContextFactory)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 Test (org.junit.Test)1