use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.
the class UntypedItemXml method setTags.
public void setTags(Tags tags) {
Document document;
JaxbHelper helper = JaxbHelper.get(Tags.class);
try {
document = helper.marshalToDom(tags);
} catch (JAXBException e) {
throw new IllegalStateException("Error parsing tags data", e);
}
replaceNode("tags", document.getDocumentElement());
// To avoid any possible state problems, we set to null rather than copying
this.tags = null;
}
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 HttpPlatformLayerClient method putItem.
@Override
public <T extends ItemBase> T putItem(T item) throws OpsException {
JaxbHelper jaxbHelper = PlatformLayerClientBase.toJaxbHelper(item);
String xml = PlatformLayerClientBase.serialize(jaxbHelper, item);
PlatformLayerKey key = PlatformLayerClientBase.toKey(jaxbHelper, item, listServices(true));
UntypedItem created = putItem(key, xml, Format.XML);
Class<T> itemClass = (Class<T>) item.getClass();
return promoteToTyped(created, itemClass);
}
use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.
the class PlatformLayerHttpRequest method doRequest.
public <T> T doRequest(Class<T> retvalClass, Format acceptFormat, Object sendData, Format sendDataFormat) throws PlatformLayerClientException {
try {
populateHttpRequest(acceptFormat, sendDataFormat);
if (debug != null) {
debug.println("Request: " + httpRequest);
}
if (sendData != null) {
if (sendData instanceof String) {
if (debug != null) {
debug.println("Data: " + sendData);
}
String sendDataString = (String) sendData;
httpRequest.setRequestContent(new Utf8StringByteSource(sendDataString));
} else {
switch(sendDataFormat) {
case XML:
if (debug != null) {
debug.println("Data: [XML Content]");
}
JaxbHelper jaxbHelper = JaxbHelper.get(sendData.getClass());
String xml = jaxbHelper.marshal(sendData, false);
httpRequest.setRequestContent(new Utf8StringByteSource(xml));
// jaxbHelper.marshal(sendData, false, getOutputStream());
break;
case JSON:
if (debug != null) {
debug.println("Data: [JSON Content]");
}
JsonHelper jsonHelper = JsonHelper.build(sendData.getClass());
String json = jsonHelper.marshal(sendData, false);
httpRequest.setRequestContent(new Utf8StringByteSource(json));
// jsonHelper.marshal(sendData, false, getOutputStream());
break;
default:
throw new IllegalStateException();
}
}
}
} catch (JAXBException e) {
throw new PlatformLayerClientException("Error while building request", e);
} catch (IOException e) {
throw new PlatformLayerClientException("Error while building request", e);
}
try {
processHttpResponseCode(getResponse());
if (retvalClass == null) {
return null;
} else if (String.class.equals(retvalClass)) {
InputStream is = getInputStream();
String text = null;
if (is != null) {
text = IoUtils.readAll(is);
}
if (debug != null) {
debug.println("Response: " + text);
}
return Casts.as(text, retvalClass);
} else if (StreamingResponse.class.equals(retvalClass)) {
return Casts.as(new StreamingResponse(getResponse()), retvalClass);
} else {
if (debug != null) {
debug.println("Response: XML/JSON content");
}
InputStream is = getInputStream();
return JaxbHelper.deserializeXmlObject(is, retvalClass, true);
}
} catch (ConnectException e) {
throw new PlatformLayerClientException("Error connecting to PlatformLayer service", e);
} catch (UnmarshalException e) {
throw new PlatformLayerClientException("Error while reading PlatformLayer response", e);
} catch (IOException e) {
throw new PlatformLayerClientException("Error communicating with PlatformLayer service", e);
}
}
use of org.platformlayer.xml.JaxbHelper in project platformlayer by platformlayer.
the class TypedItemMapper method promoteToTyped.
public <T> T promoteToTyped(UntypedItem untypedItem, Class<T> itemClass) throws OpsException {
JaxbHelper jaxbHelper = JaxbHelper.get(itemClass);
T typedItem;
try {
Element element = ((UntypedItemXml) untypedItem).getDataElement();
String xmlElementName = jaxbHelper.getXmlElementName();
String nodeName = element.getLocalName();
if (!Objects.equal(xmlElementName, nodeName)) {
String type = element.getAttribute("xsi:type");
if (type != null && type.endsWith(":" + xmlElementName)) {
// OK
} else {
throw new OpsException("Incorrect element type: " + xmlElementName + " vs " + nodeName);
}
}
T object = jaxbHelper.unmarshal(element, itemClass);
if (!(object.getClass().isAssignableFrom(itemClass))) {
System.out.println("XML = " + ((UntypedItemXml) untypedItem).serialize());
}
typedItem = Casts.checkedCast(object, itemClass);
} catch (JAXBException e) {
throw new OpsException("Error deserializing item", e);
}
return typedItem;
}
Aggregations