Search in sources :

Example 1 with XmlInspectionResponse

use of io.atlasmap.xml.v2.XmlInspectionResponse in project atlasmap by atlasmap.

the class XmlService method inspectClass.

public Response inspectClass(XmlInspectionRequest request) {
    long startTime = System.currentTimeMillis();
    XmlInspectionResponse response = new XmlInspectionResponse();
    XmlDocument d = null;
    try {
        if (request.getType() == null) {
            response.setErrorMessage("Instance or Schema type must be specified in request");
        } else {
            XmlInspectionService s = new XmlInspectionService();
            switch(request.getType()) {
                case INSTANCE:
                    d = s.inspectXmlDocument(request.getXmlData());
                    break;
                case SCHEMA:
                    d = s.inspectSchema(request.getXmlData());
                    break;
                default:
                    response.setErrorMessage("Unsupported inspection type: " + request.getType());
                    break;
            }
        }
    } catch (Exception e) {
        LOG.error("Error inspecting xml: " + e.getMessage(), e);
        response.setErrorMessage(e.getMessage());
    } finally {
        response.setExecutionTime(System.currentTimeMillis() - startTime);
    }
    response.setXmlDocument(d);
    return Response.ok().entity(toJson(response)).build();
}
Also used : XmlInspectionService(io.atlasmap.xml.inspect.XmlInspectionService) XmlDocument(io.atlasmap.xml.v2.XmlDocument) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) WebApplicationException(javax.ws.rs.WebApplicationException) XmlInspectionResponse(io.atlasmap.xml.v2.XmlInspectionResponse)

Example 2 with XmlInspectionResponse

use of io.atlasmap.xml.v2.XmlInspectionResponse in project atlasmap by atlasmap.

the class XmlServiceTest method testValidJsonData.

@Test
public void testValidJsonData() throws Exception {
    final String source = "<xs:schema attributeFormDefault=\"unqualified\" elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n" + "  <xs:element name=\"data\">\n" + "    <xs:complexType>\n" + "      <xs:sequence>\n" + "        <xs:element type=\"xs:int\" name=\"intField\"/>\n" + "        <xs:element type=\"xs:long\" name=\"longField\"/>\n" + "        <xs:element type=\"xs:string\" name=\"stringField\"/>\n" + "        <xs:element type=\"xs:boolean\" name=\"booleanField\"/>\n" + "        <xs:element type=\"xs:double\" name=\"doubleField\"/>\n" + "        <xs:element type=\"xs:short\" name=\"shortField\"/>\n" + "        <xs:element type=\"xs:float\" name=\"floatField\"/>\n" + "        <xs:element type=\"xs:string\" name=\"charField\"/>\n" + "      </xs:sequence>\n" + "    </xs:complexType>\n" + "  </xs:element>\n" + "</xs:schema>";
    XmlInspectionRequest request = new XmlInspectionRequest();
    request.setType(InspectionType.SCHEMA);
    request.setXmlData(source);
    Response res = xmlService.inspectClass(request);
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    XmlInspectionResponse inspectionResponse = Json.mapper().readValue((byte[]) entity, XmlInspectionResponse.class);
    XmlDocument xmlDoc = inspectionResponse.getXmlDocument();
    assertEquals(1, xmlDoc.getFields().getField().size());
    XmlComplexType root = (XmlComplexType) xmlDoc.getFields().getField().get(0);
    assertNotNull(root);
    assertEquals(8, root.getXmlFields().getXmlField().size());
}
Also used : Response(javax.ws.rs.core.Response) XmlInspectionResponse(io.atlasmap.xml.v2.XmlInspectionResponse) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) XmlDocument(io.atlasmap.xml.v2.XmlDocument) XmlInspectionRequest(io.atlasmap.xml.v2.XmlInspectionRequest) XmlInspectionResponse(io.atlasmap.xml.v2.XmlInspectionResponse) Test(org.junit.Test)

Aggregations

XmlDocument (io.atlasmap.xml.v2.XmlDocument)2 XmlInspectionResponse (io.atlasmap.xml.v2.XmlInspectionResponse)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 XmlInspectionService (io.atlasmap.xml.inspect.XmlInspectionService)1 XmlComplexType (io.atlasmap.xml.v2.XmlComplexType)1 XmlInspectionRequest (io.atlasmap.xml.v2.XmlInspectionRequest)1 IOException (java.io.IOException)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 Response (javax.ws.rs.core.Response)1 Test (org.junit.Test)1