Search in sources :

Example 1 with JaxbHelper

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;
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) JaxbHelper(org.platformlayer.xml.JaxbHelper) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException)

Example 2 with JaxbHelper

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)));
}
Also used : JaxbHelper(org.platformlayer.xml.JaxbHelper) PlatformLayerKey(org.platformlayer.core.model.PlatformLayerKey)

Example 3 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 4 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 5 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)

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