use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.
the class Marshaller method write.
public <T> void write(HttpServletRequest httpRequest, HttpServletResponse httpResponse, T response) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// // We do gzip compression directly (for now)
// String ce = httpRequest.getHeader("content-encoding");
// if (ce != null) {
// if (ce.equalsIgnoreCase("gzip")) {
// is = new GZIPInputStream(is);
// } else {
// httpRequest.sendError(415);
// return;
// }
// }
String accept = httpRequest.getHeader("accept");
if (accept == null) {
accept = "";
}
boolean json = true;
for (String acceptType : Splitter.on(',').split(accept)) {
int semiIndex = acceptType.indexOf(';');
if (semiIndex != -1) {
acceptType = acceptType.substring(0, semiIndex);
}
acceptType = acceptType.trim().toLowerCase();
if (accept.equals("application/xml")) {
json = false;
break;
} else if (accept.equals("application/json")) {
json = true;
break;
}
}
if (json) {
try {
jsonMapper.writeValue(baos, response);
} catch (Exception e) {
log.error("Error serializing value", e);
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
} else {
try {
JaxbHelper jaxb = JaxbHelper.get(response.getClass());
boolean formatted = false;
jaxb.marshal(response, formatted, baos);
} catch (Exception e) {
log.error("Error serializing value", e);
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}
}
byte[] data = baos.toByteArray();
if (json) {
httpResponse.setContentType("application/json");
} else {
httpResponse.setContentType("application/xml");
}
httpResponse.setContentLength(data.length);
try {
ServletOutputStream os = httpResponse.getOutputStream();
os.write(data);
os.flush();
} catch (IOException e) {
// Not a lot we can do here ... we've already started sending data
log.error("Error flushing data", e);
// Try setting an error response
try {
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (Exception e2) {
log.warn("Unable to set error status", e2);
}
return;
}
}
use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.
the class FederatedPlatformLayerClient method listItems.
@Override
public <T> List<T> listItems(final Class<T> clazz) throws PlatformLayerClientException {
JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(clazz, ManagedItemCollection.class);
PlatformLayerKey path = PlatformLayerClientBase.toKey(jaxbHelper, null, listServices(true));
return doListConcatenationTyped(getChildClients(path), AddHostTyped.wrap(new ListItemsTyped<T>(clazz)));
}
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);
}
Aggregations