Search in sources :

Example 6 with JaxbHelper

use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.

the class SmartDeserialization method deserialize.

public static <T> T deserialize(Class<T> c, InputStream is) throws OpsException {
    // TODO: Auto-detect XML, JSON, others?
    String data;
    try {
        data = IoUtils.readAll(is);
    } catch (IOException e) {
        throw new OpsException("Error reading data", e);
    }
    Format format = null;
    for (int i = 0; i < data.length(); i++) {
        char firstChar = data.charAt(i);
        switch(firstChar) {
            case ' ':
                continue;
            case '<':
                format = Format.XML;
                break;
            case '{':
                format = Format.JSON;
                break;
            default:
                {
                    if (Character.isLetter(firstChar)) {
                        format = Format.PROPERTIES;
                    } else {
                        throw new IllegalArgumentException("Unhandled character: " + ((int) firstChar));
                    }
                    break;
                }
        }
        if (format != null) {
            break;
        }
    }
    if (format == null) {
        throw new IllegalStateException("Could not determine format");
    }
    if (format == Format.XML) {
        JaxbHelper jaxb = JaxbHelper.get(c);
        try {
            return jaxb.deserialize(new StringReader(data), c);
        } catch (UnmarshalException e) {
            throw new OpsException("Error deserializing item", e);
        }
    } else {
        throw new UnsupportedOperationException();
    }
}
Also used : OpsException(org.platformlayer.ops.OpsException) UnmarshalException(javax.xml.bind.UnmarshalException) JaxbHelper(org.platformlayer.xml.JaxbHelper) StringReader(java.io.StringReader) IOException(java.io.IOException)

Example 7 with JaxbHelper

use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.

the class PlatformLayerClientBase method listItems.

@Override
public <T> List<T> listItems(Class<T> clazz) throws OpsException {
    JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(clazz, ManagedItemCollection.class);
    PlatformLayerKey path = PlatformLayerClientBase.toKey(jaxbHelper, null, listServices(true));
    UntypedItemCollection untypedItems = listItemsUntyped(path);
    List<T> items = Lists.newArrayList();
    for (UntypedItem untypedItem : untypedItems.getItems()) {
        T item = promoteToTyped(untypedItem, clazz);
        items.add(item);
    }
    return items;
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) JaxbHelper(org.platformlayer.xml.JaxbHelper) UntypedItemCollection(org.platformlayer.common.UntypedItemCollection) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 8 with JaxbHelper

use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.

the class PlatformLayerClientBase method putItemByTag.

@Override
public <T extends ItemBase> T putItemByTag(T item, Tag uniqueTag) throws OpsException {
    JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(item);
    String xml = PlatformLayerClientBase.serialize(jaxbHelper, item);
    PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, item, listServices(true));
    UntypedItem ret = putItemByTag(key, uniqueTag, xml, Format.XML);
    Class<T> itemClass = (Class<T>) item.getClass();
    return promoteToTyped(ret, itemClass);
}
Also used : UntypedItem(org.platformlayer.common.UntypedItem) JaxbHelper(org.platformlayer.xml.JaxbHelper) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 9 with JaxbHelper

use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.

the class TypedPlatformLayerClient method toKey.

public <T> PlatformLayerKey toKey(Class<T> itemClass, String id) throws PlatformLayerClientException {
    JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(itemClass, new Class[0]);
    ManagedItemId itemId = new ManagedItemId(id);
    PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, itemId, itemClass, platformLayerClient.listServices(true));
    return key;
}
Also used : JaxbHelper(org.platformlayer.xml.JaxbHelper) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey) ManagedItemId(org.platformlayer.ids.ManagedItemId)

Example 10 with JaxbHelper

use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.

the class UntypedItemXml method setLinks.

public void setLinks(Links links) {
    ItemBase item = new ItemBase();
    item.links = links;
    Document document;
    JaxbHelper helper = JaxbHelper.get(ItemBase.class);
    try {
        document = helper.marshalToDom(item);
    } catch (JAXBException e) {
        throw new IllegalStateException("Error serializing data", e);
    }
    replaceNode("links", XmlHelper.findUniqueChild(document.getDocumentElement(), "links", false));
    // To avoid any possible state problems, we set to null rather than copying
    this.links = null;
}
Also used : ItemBase(org.platformlayer.core.model.ItemBase) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) Document(org.w3c.dom.Document)

Aggregations

JaxbHelper (org.platformlayer.xml.JaxbHelper)19 JAXBException (javax.xml.bind.JAXBException)8 PlatformLayerKey (org.platformlayer.core.model.PlatformLayerKey)6 IOException (java.io.IOException)4 UnmarshalException (javax.xml.bind.UnmarshalException)3 UntypedItem (org.platformlayer.common.UntypedItem)3 OpsException (org.platformlayer.ops.OpsException)3 StringReader (java.io.StringReader)2 ItemBase (org.platformlayer.core.model.ItemBase)2 ItemType (org.platformlayer.ids.ItemType)2 ManagedItemId (org.platformlayer.ids.ManagedItemId)2 ServiceType (org.platformlayer.ids.ServiceType)2 ServiceProvider (org.platformlayer.xaas.services.ServiceProvider)2 Document (org.w3c.dom.Document)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 StringWriter (java.io.StringWriter)1 ConnectException (java.net.ConnectException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 Consumes (javax.ws.rs.Consumes)1