Search in sources :

Example 86 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 87 with XStream

use of com.thoughtworks.xstream.XStream in project audit4j-core by audit4j.

the class XMLConfigProvider method generateConfig.

/**
 * {@inheritDoc}
 */
@Override
public void generateConfig(T config, String filePath) throws ConfigurationException {
    XStream xstream = new XStream(new StaxDriver());
    xstream.alias("configuration", clazz);
    BufferedOutputStream stdout = null;
    try {
        stdout = new BufferedOutputStream(new FileOutputStream(filePath));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    xstream.marshal(config, new PrettyPrintWriter(new OutputStreamWriter(stdout)));
}
Also used : StaxDriver(com.thoughtworks.xstream.io.xml.StaxDriver) XStream(com.thoughtworks.xstream.XStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) PrettyPrintWriter(com.thoughtworks.xstream.io.xml.PrettyPrintWriter) OutputStreamWriter(java.io.OutputStreamWriter) BufferedOutputStream(java.io.BufferedOutputStream)

Example 88 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 89 with XStream

use of com.thoughtworks.xstream.XStream in project drools by kiegroup.

the class XmlBifParser method loadBif.

public static Bif loadBif(URL url) {
    XStream xstream = createTrustingXStream();
    initXStream(xstream);
    Bif bif = (Bif) xstream.fromXML(url);
    return bif;
}
Also used : XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream)

Example 90 with XStream

use of com.thoughtworks.xstream.XStream in project drools by kiegroup.

the class XmlBifParser method loadBif.

public static Bif loadBif(Resource resource, KnowledgeBuilderErrors errors) {
    InputStream is = null;
    try {
        is = resource.getInputStream();
    } catch (IOException e) {
        errors.add(new ParserError(resource, "Exception opening Stream:\n" + e.toString(), 0, 0));
        return null;
    }
    try {
        String encoding = resource instanceof InternalResource ? ((InternalResource) resource).getEncoding() : null;
        XStream xstream = encoding != null ? createTrustingXStream(new DomDriver(encoding)) : createTrustingXStream();
        initXStream(xstream);
        Bif bif = (Bif) xstream.fromXML(is);
        return bif;
    } catch (Exception e) {
        errors.add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return null;
    }
}
Also used : InternalResource(org.drools.core.io.internal.InternalResource) DomDriver(com.thoughtworks.xstream.io.xml.DomDriver) ParserError(org.drools.compiler.compiler.ParserError) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) XStreamUtils.createTrustingXStream(org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream) BayesNetworkAssemblerError(org.drools.beliefs.bayes.assembler.BayesNetworkAssemblerError) IOException(java.io.IOException) IOException(java.io.IOException)

Aggregations

XStream (com.thoughtworks.xstream.XStream)199 Test (org.junit.Test)55 IOException (java.io.IOException)33 InputStream (java.io.InputStream)29 WstxDriver (com.thoughtworks.xstream.io.xml.WstxDriver)22 Metacard (ddf.catalog.data.Metacard)21 DomDriver (com.thoughtworks.xstream.io.xml.DomDriver)17 HashMap (java.util.HashMap)14 ByteArrayInputStream (java.io.ByteArrayInputStream)13 StaxDriver (com.thoughtworks.xstream.io.xml.StaxDriver)12 XStreamUtils.createTrustingXStream (org.kie.soup.commons.xstream.XStreamUtils.createTrustingXStream)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 FileNotFoundException (java.io.FileNotFoundException)9 Writer (java.io.Writer)8 ArrayList (java.util.ArrayList)8 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