Search in sources :

Example 11 with JaxbHelper

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;
}
Also used : JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) Document(org.w3c.dom.Document)

Example 12 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 13 with JaxbHelper

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

Example 14 with JaxbHelper

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);
    }
}
Also used : Utf8StringByteSource(org.platformlayer.rest.Utf8StringByteSource) JsonHelper(org.platformlayer.xml.JsonHelper) InputStream(java.io.InputStream) UnmarshalException(javax.xml.bind.UnmarshalException) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper) IOException(java.io.IOException) ConnectException(java.net.ConnectException)

Example 15 with JaxbHelper

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;
}
Also used : OpsException(org.platformlayer.ops.OpsException) Element(org.w3c.dom.Element) JAXBException(javax.xml.bind.JAXBException) JaxbHelper(org.platformlayer.xml.JaxbHelper)

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