Search in sources :

Example 31 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class DefaultBulkApiClient method unmarshalResponse.

private <T> T unmarshalResponse(InputStream response, Request request, Class<T> resultClass) throws SalesforceException {
    try {
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<T> result = unmarshaller.unmarshal(new StreamSource(response), resultClass);
        return result.getValue();
    } catch (JAXBException e) {
        throw new SalesforceException(String.format("Error unmarshaling response {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    } catch (IllegalArgumentException e) {
        throw new SalesforceException(String.format("Error unmarshaling response for {%s:%s} : %s", request.getMethod(), request.getURI(), e.getMessage()), e);
    }
}
Also used : SalesforceException(org.apache.camel.component.salesforce.api.SalesforceException) StreamSource(javax.xml.transform.stream.StreamSource) JAXBException(javax.xml.bind.JAXBException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 32 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project camel by apache.

the class RouteInfoCommand method executeOnRoute.

@Override
public void executeOnRoute(CamelController camelController, String contextName, String routeId, PrintStream out, PrintStream err) throws Exception {
    out.println(stringEscape.unescapeJava("Camel Route " + routeId + ""));
    out.println(stringEscape.unescapeJava("\tCamel Context: " + contextName));
    String xml = camelController.getRouteStatsAsXml(routeId, contextName, true, false);
    if (xml != null) {
        JAXBContext context = JAXBContext.newInstance(RouteStatDump.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        RouteStatDump route = (RouteStatDump) unmarshaller.unmarshal(new StringReader(xml));
        out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
        out.println(stringEscape.unescapeJava("\tState: " + route.getState()));
        out.println("");
        out.println("");
        out.println(stringEscape.unescapeJava("Statistics"));
        long total = route.getExchangesCompleted() + route.getExchangesFailed();
        out.println(stringEscape.unescapeJava("\tExchanges Total: " + total));
        out.println(stringEscape.unescapeJava("\tExchanges Completed: " + route.getExchangesCompleted()));
        out.println(stringEscape.unescapeJava("\tExchanges Failed: " + route.getExchangesFailed()));
        out.println(stringEscape.unescapeJava("\tExchanges Inflight: " + route.getExchangesInflight()));
        out.println(stringEscape.unescapeJava("\tMin Processing Time: " + route.getMinProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tMax Processing Time: " + route.getMaxProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tMean Processing Time: " + route.getMeanProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tTotal Processing Time: " + route.getTotalProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tLast Processing Time: " + route.getLastProcessingTime() + " ms"));
        out.println(stringEscape.unescapeJava("\tDelta Processing Time: " + route.getDeltaProcessingTime() + " ms"));
        if (isEmpty(route.getStartTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tStart Statistics Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getStartTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tStart Statistics Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getResetTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tReset Statistics Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getResetTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tReset Statistics Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getFirstExchangeCompletedTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tFirst Exchange Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getFirstExchangeCompletedTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tFirst Exchange Date: " + text));
        }
        // Test for null to see if a any exchanges have been processed first to avoid NPE
        if (isEmpty(route.getLastExchangeCompletedTimestamp())) {
            // Print an empty value for scripting
            out.println(stringEscape.unescapeJava("\tLast Exchange Date:"));
        } else {
            Date date = new SimpleDateFormat(XML_TIMESTAMP_FORMAT).parse(route.getLastExchangeCompletedTimestamp());
            String text = new SimpleDateFormat(OUTPUT_TIMESTAMP_FORMAT).format(date);
            out.println(stringEscape.unescapeJava("\tLast Exchange Date: " + text));
        }
    }
}
Also used : RouteStatDump(org.apache.camel.util.RouteStatDump) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 33 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project midpoint by Evolveum.

the class ModelClientUtil method unmarshallResource.

public static <O> O unmarshallResource(String path) throws JAXBException, FileNotFoundException {
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    InputStream is = null;
    JAXBElement<O> element = null;
    try {
        is = ModelClientUtil.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<O>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 34 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project midpoint by Evolveum.

the class DescriptorLoader method loadData.

public void loadData(MidPointApplication application) {
    LOGGER.debug("Loading data from descriptor files.");
    try (InputStream baseInput = application.getServletContext().getResourceAsStream(baseFileName);
        InputStream customInput = application.getServletContext().getResourceAsStream(customFileName)) {
        if (baseInput == null) {
            LOGGER.error("Couldn't find " + baseFileName + " file, can't load application menu and other stuff.");
        }
        JAXBContext context = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        JAXBElement<DescriptorType> element = (JAXBElement) unmarshaller.unmarshal(baseInput);
        DescriptorType descriptor = element.getValue();
        LOGGER.debug("Loading menu bar from " + baseFileName + " .");
        DescriptorType customDescriptor = null;
        if (customInput != null) {
            element = (JAXBElement) unmarshaller.unmarshal(customInput);
            customDescriptor = element.getValue();
        }
        scanPackagesForPages(descriptor.getPackagesToScan(), application);
        if (customDescriptor != null) {
            scanPackagesForPages(customDescriptor.getPackagesToScan(), application);
        }
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("loaded:\n{}", debugDump(1));
        }
    } catch (Exception ex) {
        LoggingUtils.logUnexpectedException(LOGGER, "Couldn't process application descriptor", ex);
    }
}
Also used : InputStream(java.io.InputStream) DescriptorType(com.evolveum.midpoint.xml.ns._public.gui.admin_1.DescriptorType) JAXBContext(javax.xml.bind.JAXBContext) JAXBElement(javax.xml.bind.JAXBElement) Unmarshaller(javax.xml.bind.Unmarshaller)

Example 35 with Unmarshaller

use of javax.xml.bind.Unmarshaller in project jdk8u_jdk by JetBrains.

the class UnmarshalTest method unmarshalUnexpectedNsTest.

@Test
public void unmarshalUnexpectedNsTest() throws Exception {
    JAXBContext context;
    Unmarshaller unm;
    // Create JAXB context from testTypes package
    context = JAXBContext.newInstance("testTypes");
    // Create unmarshaller from JAXB context
    unm = context.createUnmarshaller();
    // Unmarshall xml document with unqualified dtime element
    Root r = (Root) unm.unmarshal(new InputSource(new StringReader(DOC)));
    // Print dtime value and check if it is null
    System.out.println("dtime is:" + r.getWhen().getDtime());
    assertNull(r.getWhen().getDtime());
}
Also used : InputSource(org.xml.sax.InputSource) Root(testTypes.Root) StringReader(java.io.StringReader) JAXBContext(javax.xml.bind.JAXBContext) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.testng.annotations.Test)

Aggregations

Unmarshaller (javax.xml.bind.Unmarshaller)292 JAXBContext (javax.xml.bind.JAXBContext)240 JAXBException (javax.xml.bind.JAXBException)97 InputStream (java.io.InputStream)91 Test (org.junit.Test)79 StringReader (java.io.StringReader)40 BaseTest (org.orcid.core.BaseTest)39 V2Convertible (org.orcid.core.version.V2Convertible)39 File (java.io.File)33 InputSource (org.xml.sax.InputSource)22 IOException (java.io.IOException)21 JAXBElement (javax.xml.bind.JAXBElement)18 Marshaller (javax.xml.bind.Marshaller)18 ByteArrayInputStream (java.io.ByteArrayInputStream)17 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParserFactory (javax.xml.parsers.SAXParserFactory)13 XMLInputFactory (javax.xml.stream.XMLInputFactory)13 XMLStreamException (javax.xml.stream.XMLStreamException)13 XMLStreamReader (javax.xml.stream.XMLStreamReader)13 Schema (javax.xml.validation.Schema)13