Search in sources :

Example 1 with PureJavaReflectionProvider

use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project camel by apache.

the class DefaultCompositeApiClient method configureXStream.

static XStream configureXStream() {
    final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
    final XppDriver hierarchicalStreamDriver = new XppDriver(new NoNameCoder()) {

        @Override
        public HierarchicalStreamWriter createWriter(final Writer out) {
            return new CompactWriter(out, getNameCoder());
        }
    };
    final XStream xStream = new XStream(reflectionProvider, hierarchicalStreamDriver);
    xStream.aliasSystemAttribute(null, "class");
    xStream.ignoreUnknownElements();
    XStreamUtils.addDefaultPermissions(xStream);
    xStream.registerConverter(new DateTimeConverter());
    xStream.setMarshallingStrategy(new TreeMarshallingStrategy());
    xStream.processAnnotations(ADDITIONAL_TYPES);
    return xStream;
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) TreeMarshallingStrategy(com.thoughtworks.xstream.core.TreeMarshallingStrategy) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with PureJavaReflectionProvider

use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project camel by apache.

the class SObjectBatchTest method shouldSerializeToXml.

@Test
public void shouldSerializeToXml() {
    final String xml = //
    "<batch>\n" + //
    "  <batchRequests>\n" + //
    "    <batchRequest>\n" + //
    "      <method>POST</method>\n" + //
    "      <url>v37.0/sobjects/Account/</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewAccountName</Name>\n" + //
    "          <Industry>Environmental</Industry>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>DELETE</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ?fields=Name,BillingPostalCode</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ/CreatedBy?fields=Name</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/limits/</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/001D000000K0fXOIAZ</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "          <AccountNumber>AC12345</AccountNumber>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/sobjects/Account/EPK/12345</url>\n" + //
    "      <richInput>\n" + //
    "        <Account>\n" + //
    "          <Name>NewName</Name>\n" + //
    "        </Account>\n" + //
    "      </richInput>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>PATCH</method>\n" + //
    "      <url>v37.0/some/url</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/query/?q=SELECT Name FROM Account</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/queryAll/?q=SELECT Name FROM Account</url>\n" + //
    "    </batchRequest>\n" + //
    "    <batchRequest>\n" + //
    "      <method>GET</method>\n" + //
    "      <url>v37.0/search/?q=FIND {joe}</url>\n" + //
    "    </batchRequest>\n" + //
    "  </batchRequests>\n" + "</batch>";
    final PureJavaReflectionProvider reflectionProvider = new PureJavaReflectionProvider(new FieldDictionary(new AnnotationFieldKeySorter()));
    final XStream xStream = new XStream(reflectionProvider);
    xStream.aliasSystemAttribute(null, "class");
    xStream.processAnnotations(SObjectBatch.class);
    xStream.processAnnotations(batch.objectTypes());
    final String serialized = xStream.toXML(batch);
    assertEquals("Should serialize as expected by Salesforce", xml, serialized);
}
Also used : AnnotationFieldKeySorter(org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter) XStream(com.thoughtworks.xstream.XStream) FieldDictionary(com.thoughtworks.xstream.converters.reflection.FieldDictionary) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider) Test(org.junit.Test)

Example 3 with PureJavaReflectionProvider

use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project jgnash by ccavanaugh.

the class BinaryContainer method writeBinary.

/**
     * Writes an XML file given a collection of StoredObjects. TrashObjects and
     * objects marked for removal are not written. If the file already exists,
     * it will be overwritten.
     *
     * @param objects Collection of StoredObjects to write
     * @param path    file to write
     */
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
    final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
    if (!Files.exists(path.getParent())) {
        try {
            Files.createDirectories(path.getParent());
            logger.info("Created missing directories");
        } catch (final IOException e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }
    createBackup(path);
    List<StoredObject> list = new ArrayList<>();
    list.addAll(query(objects, Budget.class));
    list.addAll(query(objects, Config.class));
    list.addAll(query(objects, CommodityNode.class));
    list.addAll(query(objects, ExchangeRate.class));
    list.addAll(query(objects, RootAccount.class));
    list.addAll(query(objects, Reminder.class));
    // remove any objects marked for removal
    list.removeIf(StoredObject::isMarkedForRemoval);
    // sort the list
    list.sort(new StoredObjectComparator());
    logger.info("Writing Binary file");
    try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
        final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
        try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
            out.writeObject(list);
            out.flush();
        }
        // forcibly flush before letting go of the resources to help older windows systems write correctly
        os.flush();
    } catch (IOException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    logger.info("Writing Binary file complete");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) Budget(jgnash.engine.budget.Budget) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) BufferedOutputStream(java.io.BufferedOutputStream) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 4 with PureJavaReflectionProvider

use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project jgnash by ccavanaugh.

the class XMLContainer method writeXML.

/**
     * Writes an XML file given a collection of StoredObjects. TrashObjects and
     * objects marked for removal are not written. If the file already exists,
     * it will be overwritten.
     *
     * @param objects Collection of StoredObjects to write
     * @param path    file to write
     */
static synchronized void writeXML(final Collection<StoredObject> objects, final Path path) {
    Logger logger = Logger.getLogger(XMLContainer.class.getName());
    if (!Files.exists(path.getParent())) {
        try {
            Files.createDirectories(path.getParent());
            logger.info("Created missing directories");
        } catch (final IOException e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }
    createBackup(path);
    List<StoredObject> list = new ArrayList<>();
    list.addAll(query(objects, Budget.class));
    list.addAll(query(objects, Config.class));
    list.addAll(query(objects, CommodityNode.class));
    list.addAll(query(objects, ExchangeRate.class));
    list.addAll(query(objects, RootAccount.class));
    list.addAll(query(objects, Reminder.class));
    // remove any objects marked for removal
    list.removeIf(StoredObject::isMarkedForRemoval);
    // sort the list
    list.sort(new StoredObjectComparator());
    logger.info("Writing XML file");
    try (final Writer writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8)) {
        writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
        writer.write("<?fileFormat " + Engine.CURRENT_MAJOR_VERSION + "." + Engine.CURRENT_MINOR_VERSION + "?>\n");
        final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new KXml2Driver()));
        try (final ObjectOutputStream out = xstream.createObjectOutputStream(new PrettyPrintWriter(writer))) {
            out.writeObject(list);
            // forcibly flush before letting go of the resources to help older windows systems write correctly
            out.flush();
        } catch (final Exception e) {
            logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    } catch (final IOException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    logger.info("Writing XML file complete");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) IOException(java.io.IOException) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) KXml2Driver(com.thoughtworks.xstream.io.xml.KXml2Driver) Budget(jgnash.engine.budget.Budget) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) Writer(java.io.Writer) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 5 with PureJavaReflectionProvider

use of com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider in project hudson-2.x by hudson.

the class RobustReflectionConverter method writeValueToImplicitCollection.

private Map writeValueToImplicitCollection(UnmarshallingContext context, Object value, Map implicitCollections, Object result, String itemFieldName) {
    String fieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), value.getClass(), itemFieldName);
    if (fieldName != null) {
        if (implicitCollections == null) {
            // lazy instantiation
            implicitCollections = new HashMap();
        }
        Collection collection = (Collection) implicitCollections.get(fieldName);
        if (collection == null) {
            Class fieldType = mapper.defaultImplementationOf(reflectionProvider.getFieldType(result, fieldName, null));
            if (!Collection.class.isAssignableFrom(fieldType)) {
                throw new ObjectAccessException("Field " + fieldName + " of " + result.getClass().getName() + " is configured for an implicit Collection, but field is of type " + fieldType.getName());
            }
            if (pureJavaReflectionProvider == null) {
                pureJavaReflectionProvider = new PureJavaReflectionProvider();
            }
            collection = (Collection) pureJavaReflectionProvider.newInstance(fieldType);
            reflectionProvider.writeField(result, fieldName, collection, null);
            implicitCollections.put(fieldName, collection);
        }
        collection.add(value);
    }
    return implicitCollections;
}
Also used : HashMap(java.util.HashMap) ObjectAccessException(com.thoughtworks.xstream.converters.reflection.ObjectAccessException) Collection(java.util.Collection) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Aggregations

PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)6 XStream (com.thoughtworks.xstream.XStream)5 FieldDictionary (com.thoughtworks.xstream.converters.reflection.FieldDictionary)2 KXml2Driver (com.thoughtworks.xstream.io.xml.KXml2Driver)2 IOException (java.io.IOException)2 ObjectOutputStream (java.io.ObjectOutputStream)2 Writer (java.io.Writer)2 ArrayList (java.util.ArrayList)2 Logger (java.util.logging.Logger)2 CommodityNode (jgnash.engine.CommodityNode)2 Config (jgnash.engine.Config)2 ExchangeRate (jgnash.engine.ExchangeRate)2 RootAccount (jgnash.engine.RootAccount)2 StoredObject (jgnash.engine.StoredObject)2 StoredObjectComparator (jgnash.engine.StoredObjectComparator)2 Budget (jgnash.engine.budget.Budget)2 Reminder (jgnash.engine.recurring.Reminder)2 AnnotationFieldKeySorter (org.apache.camel.component.salesforce.api.dto.AnnotationFieldKeySorter)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 ObjectAccessException (com.thoughtworks.xstream.converters.reflection.ObjectAccessException)1