Search in sources :

Example 1 with JsonHelper

use of org.platformlayer.xml.JsonHelper 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)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ConnectException (java.net.ConnectException)1 JAXBException (javax.xml.bind.JAXBException)1 UnmarshalException (javax.xml.bind.UnmarshalException)1 Utf8StringByteSource (org.platformlayer.rest.Utf8StringByteSource)1 JaxbHelper (org.platformlayer.xml.JaxbHelper)1 JsonHelper (org.platformlayer.xml.JsonHelper)1