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();
}
}
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;
}
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);
}
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;
}
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;
}
Aggregations