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);
}
}
Aggregations