Search in sources :

Example 56 with XStream

use of com.thoughtworks.xstream.XStream in project cloudstack by apache.

the class RegionsApiUtil method makeAccountAPICall.

/**
     * Makes an api call using region service end_point, api command and params
     * Returns Account object on success
     * @param region
     * @param command
     * @param params
     * @return
     */
protected static RegionAccount makeAccountAPICall(Region region, String command, List<NameValuePair> params) {
    try {
        String url = buildUrl(buildParams(command, params), region);
        HttpClient client = new HttpClient();
        HttpMethod method = new GetMethod(url);
        if (client.executeMethod(method) == 200) {
            InputStream is = method.getResponseBodyAsStream();
            //Translate response to Account object
            XStream xstream = new XStream(new DomDriver());
            xstream.alias("account", RegionAccount.class);
            xstream.alias("user", RegionUser.class);
            xstream.aliasField("id", RegionAccount.class, "uuid");
            xstream.aliasField("name", RegionAccount.class, "accountName");
            xstream.aliasField("accounttype", RegionAccount.class, "type");
            xstream.aliasField("domainid", RegionAccount.class, "domainUuid");
            xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain");
            xstream.aliasField("id", RegionUser.class, "uuid");
            xstream.aliasField("accountId", RegionUser.class, "accountUuid");
            try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
                return (RegionAccount) in.readObject();
            } catch (IOException e) {
                s_logger.error(e.getMessage());
                return null;
            }
        } else {
            return null;
        }
    } catch (HttpException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (IOException e) {
        s_logger.error(e.getMessage());
        return null;
    } catch (ClassNotFoundException e) {
        s_logger.error(e.getMessage());
        return null;
    }
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) ObjectInputStream(java.io.ObjectInputStream)

Example 57 with XStream

use of com.thoughtworks.xstream.XStream in project ignite by apache.

the class IgniteNodeRunner method readCfgFromFileAndDeleteFile.

/**
     * Reads configuration from given file and delete the file after.
     *
     * @param fileName File name.
     * @return Readed configuration.
     * @throws IOException If failed.
     * @see #storeToFile(IgniteConfiguration, boolean)
     * @throws IgniteCheckedException On error.
     */
private static IgniteConfiguration readCfgFromFileAndDeleteFile(String fileName) throws IOException, IgniteCheckedException {
    try (BufferedReader cfgReader = new BufferedReader(new FileReader(fileName))) {
        IgniteConfiguration cfg = (IgniteConfiguration) new XStream().fromXML(cfgReader);
        if (cfg.getMarshaller() == null) {
            Marshaller marsh = IgniteTestResources.getMarshaller();
            cfg.setMarshaller(marsh);
        }
        X.println("Configured marshaller class: " + cfg.getMarshaller().getClass().getName());
        if (cfg.getDiscoverySpi() == null) {
            TcpDiscoverySpi disco = new TcpDiscoverySpi();
            disco.setIpFinder(GridCacheAbstractFullApiSelfTest.LOCAL_IP_FINDER);
            cfg.setDiscoverySpi(disco);
        }
        return cfg;
    } finally {
        new File(fileName).delete();
    }
}
Also used : Marshaller(org.apache.ignite.marshaller.Marshaller) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) XStream(com.thoughtworks.xstream.XStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) TcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi)

Example 58 with XStream

use of com.thoughtworks.xstream.XStream in project jmeter by apache.

the class TemplateManager method initXStream.

private XStream initXStream() {
    XStream xstream = new XStream(new DomDriver() {

        /**
             * Create the DocumentBuilderFactory instance.
             * See https://blog.compass-security.com/2012/08/secure-xml-parser-configuration/
             * See https://github.com/x-stream/xstream/issues/25
             * @return the new instance
             */
        @Override
        protected DocumentBuilderFactory createDocumentBuilderFactory() {
            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            try {
                factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
                factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
            } catch (ParserConfigurationException e) {
                throw new StreamException(e);
            }
            factory.setExpandEntityReferences(false);
            return factory;
        }
    });
    xstream.alias("template", Template.class);
    xstream.alias("templates", Templates.class);
    xstream.useAttributeFor(Template.class, "isTestPlan");
    // templates i
    xstream.addImplicitMap(Templates.class, // $NON-NLS-1$
    "templates", Template.class, // $NON-NLS-1$
    "name");
    return xstream;
}
Also used : DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) XStream(com.thoughtworks.xstream.XStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) StreamException(com.thoughtworks.xstream.io.StreamException)

Example 59 with XStream

use of com.thoughtworks.xstream.XStream in project jmeter by apache.

the class ObjectMessageRenderer method getValueFromText.

/**
     * Try to load an object via XStream from XML text, so that it can be used as body
     * for a JMS message.
     * An {@link IllegalStateException} will be thrown if transforming the XML to an object fails.
     *
     * @param xmlMessage String containing XML text as input for the transformation
     * @return Serialized object instance
     */
@Override
public Serializable getValueFromText(final String xmlMessage) {
    Serializable readObject = null;
    try {
        XStream xstream = new XStream();
        readObject = (Serializable) xstream.fromXML(xmlMessage, readObject);
    } catch (Exception e) {
        throw new IllegalStateException("Unable to load object instance from text", e);
    }
    return readObject;
}
Also used : Serializable(java.io.Serializable) XStream(com.thoughtworks.xstream.XStream) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException)

Example 60 with XStream

use of com.thoughtworks.xstream.XStream in project maven-plugins by apache.

the class EvaluateMojo method getXStream.

/**
     * @return lazy loading xstream object.
     */
private XStream getXStream() {
    if (xstream == null) {
        xstream = new XStream();
        addAlias(xstream);
        // handle Properties a la Maven
        xstream.registerConverter(new PropertiesConverter() {

            /** {@inheritDoc} */
            public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
                return Properties.class == type;
            }

            /** {@inheritDoc} */
            public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
                Properties properties = (Properties) source;
                // sort
                Map<?, ?> map = new TreeMap<Object, Object>(properties);
                for (Map.Entry<?, ?> entry : map.entrySet()) {
                    writer.startNode(entry.getKey().toString());
                    writer.setValue(entry.getValue().toString());
                    writer.endNode();
                }
            }
        });
    }
    return xstream;
}
Also used : JarEntry(java.util.jar.JarEntry) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) MarshallingContext(com.thoughtworks.xstream.converters.MarshallingContext) Properties(java.util.Properties) PropertiesConverter(com.thoughtworks.xstream.converters.collections.PropertiesConverter) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

XStream (com.thoughtworks.xstream.XStream)183 Test (org.junit.Test)54 IOException (java.io.IOException)31 InputStream (java.io.InputStream)28 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)22 Metacard (ddf.catalog.data.Metacard)21 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)16 HashMap (java.util.HashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)12 Matchers.anyString (org.mockito.Matchers.anyString)11 HierarchicalStreamWriter (com.thoughtworks.xstream.io.HierarchicalStreamWriter)10 CswRecordCollection (org.codice.ddf.spatial.ogc.csw.catalog.common.CswRecordCollection)10 GmlGeometryConverter (org.codice.ddf.spatial.ogc.wfs.catalog.converter.impl.GmlGeometryConverter)8 XStreamException (com.thoughtworks.xstream.XStreamException)7 MarshallingContext (com.thoughtworks.xstream.converters.MarshallingContext)7 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)7 FileNotFoundException (java.io.FileNotFoundException)7 ArrayList (java.util.ArrayList)7 ForbiddenClassException (com.thoughtworks.xstream.security.ForbiddenClassException)6 FileWriter (java.io.FileWriter)6