use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class SoapJSProtocol method soapFault.
public Element soapFault(ServiceException e) {
String reason = e.getMessage();
if (reason == null)
reason = e.toString();
QName code = e.isReceiversFault() ? RECEIVER_CODE : SENDER_CODE;
Element eFault = mFactory.createElement(mFaultQName);
// FIXME: should really be a qualified "attribute"
eFault.addUniqueElement(CODE).addAttribute(VALUE, code.getQualifiedName());
// FIXME: should really be a qualified "attribute"
eFault.addUniqueElement(REASON).addAttribute(TEXT, reason);
// FIXME: should really be a qualified "attribute"
Element eError = eFault.addUniqueElement(DETAIL).addUniqueElement(ZimbraNamespace.E_ERROR);
eError.addAttribute(ZimbraNamespace.E_CODE.getName(), e.getCode());
if (LC.soap_fault_include_stack_trace.booleanValue())
eError.addAttribute(ZimbraNamespace.E_TRACE.getName(), ExceptionToString.ToString(e));
else
eError.addAttribute(ZimbraNamespace.E_TRACE.getName(), e.getThreadName());
for (ServiceException.Argument arg : e.getArgs()) {
if (arg.externalVisible()) {
Element val = eError.addElement(ZimbraNamespace.E_ARGUMENT);
val.addAttribute(ZimbraNamespace.A_ARG_NAME, arg.name);
val.addAttribute(ZimbraNamespace.A_ARG_TYPE, arg.type.toString());
val.setText(arg.value);
}
}
return eFault;
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class Soap11Protocol method soapFault.
/* (non-Javadoc)
* @see com.zimbra.common.soap.SoapProtocol#soapFault(com.zimbra.cs.service.ServiceException)
*/
public Element soapFault(ServiceException e) {
String reason = e.getMessage();
if (reason == null)
reason = e.toString();
QName code = e.isReceiversFault() ? RECEIVER_CODE : SENDER_CODE;
Element eFault = mFactory.createElement(mFaultQName);
eFault.addUniqueElement(FAULTCODE).setText(code.getQualifiedName());
eFault.addUniqueElement(FAULTSTRING).setText(reason);
Element eDetail = eFault.addUniqueElement(DETAIL);
Element error = eDetail.addUniqueElement(ZimbraNamespace.E_ERROR);
// FIXME: should really be a qualified "attribute"
error.addUniqueElement(ZimbraNamespace.E_CODE).setText(e.getCode());
if (LC.soap_fault_include_stack_trace.booleanValue())
error.addUniqueElement(ZimbraNamespace.E_TRACE).setText(ExceptionToString.ToString(e));
else
error.addUniqueElement(ZimbraNamespace.E_TRACE).setText(e.getThreadName());
for (ServiceException.Argument arg : e.getArgs()) {
if (arg.externalVisible()) {
Element val = error.addElement(ZimbraNamespace.E_ARGUMENT);
val.addAttribute(ZimbraNamespace.A_ARG_NAME, arg.name);
val.addAttribute(ZimbraNamespace.A_ARG_TYPE, arg.type.toString());
val.setText(arg.value);
}
}
return eFault;
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class ZimbraMemcachedClient method putBigByteArray.
/**
* Puts the key/value pair for a big byte array.
*
* A big byte array value is a byte array whose length can be greater than memcached limit of 1MB.
* If the data is smaller than MAX_CHUNK_SIZE (1MB - 1KB) it is set in a single key/value pair in
* memcached, with the value suffixed with a "V". (V for value) If the data is bigger, the data is
* split into MAX_CHUNK_SIZE chunks and set as individual cache entries. The cache keys for the
* chunks are the combination of the original key, the data "fingerprint" (for some uniqueness),
* and chunk number. The cache value for the original key is set to a table of contents containing
* the number of chunks, the fingerprint, and length and checksum of each chunk, followed by a "T".
* (T for table of contents)
*
* During retrieval, the value for the main key is examined to see if the last byte is a V or T. If
* it's a V, the preceding bytes constitute the entire cache value. If it's a T, the preceding bytes
* are interpreted as the table of contents. The information in the table of contents can be used
* to then fetch the chunks and reassemble them. All chunks must exist and must match the length and
* checksum in the table of contents. Otherwise the whole get operation is considered a cache miss.
*
* When the main key is updated or removed, the chunk values become orphaned because the table of contents
* will no longer contain the same fingerprint. (Some collision risk exists.) These entries will age
* out of the cache eventually.
*
* Example of a short value:
*
* key = "foo", value = ['b', 'a', 'r']
* Memcached will have "foo" = ['b', 'a', 'r', 'V'].
*
* Example of a big value:
*
* key = "foo", value = <1.5MB byte array>
* Assume the fingerprint computed is 1234567890.
* Memcached will have:
* "foo" = <table of contents> 'T'
* "foo:1234567890.0" = <1st chunk of ~1MB>
* "foo:1234567890.1" = <2nd chunk of ~0.5MB>
*
* @param key
* @param value
* @param expirySec expiry in seconds
* @param timeout in millis
* @param waitForAck if true, block until ack'd or timeout; if false, return immediately
* @return
*/
public boolean putBigByteArray(String key, byte[] value, int expirySec, long timeout, boolean waitForAck) {
MemcachedClient client;
synchronized (this) {
client = mMCDClient;
if (expirySec == DEFAULT_EXPIRY)
expirySec = mDefaultExpiry;
if (timeout == DEFAULT_TIMEOUT)
timeout = mDefaultTimeout;
}
if (client == null)
return false;
ByteArrayTranscoder bat = new ByteArrayTranscoder();
ByteArray mainValue;
if (value.length < MAX_CHUNK_SIZE) {
// Value is short enough. Set it directly, with a prefix. Requires 1 memcached set operation.
byte[] prefixed = new byte[value.length + 1];
System.arraycopy(value, 0, prefixed, 0, value.length);
prefixed[value.length] = BBA_PREFIX_VALUE;
mainValue = new ByteArray(prefixed);
} else {
// Value can't fit in a single memcached entry. Split into chunks and use table of contents.
// Requires N+1 memcached set operations.
ByteArrayChunks chunks;
try {
chunks = new ByteArrayChunks(value);
} catch (ServiceException e) {
ZimbraLog.misc.warn("Unable to split byte array into chunks", e);
return false;
}
ByteArrayChunksTOC toc = chunks.makeTOC();
// Add chunks to the cache.
String chunkKeyPrefix = key + ":" + toc.getFingerprint() + ".";
int numChunks = chunks.getNumChunks();
for (int i = 0; i < numChunks; ++i) {
String chunkKey = chunkKeyPrefix + i;
ByteArray byteArray = chunks.getChunk(i);
Future<Boolean> future = client.set(chunkKey, expirySec, byteArray, bat);
if (waitForAck) {
Boolean success = null;
try {
success = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached set timed out after " + timeout + "ms", e);
future.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached set operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached set operation", e);
}
if (success == null || !success.booleanValue())
return false;
}
}
// Put the table of contents as the main value. Do this after all chunks have been
// added successfully.
byte[] tocBytes;
try {
tocBytes = toc.encode().getBytes("utf-8");
} catch (UnsupportedEncodingException e) {
ZimbraLog.misc.warn("Unable to get bytes for BBA table of contents", e);
return false;
}
byte[] prefixed = new byte[tocBytes.length + 1];
System.arraycopy(tocBytes, 0, prefixed, 0, tocBytes.length);
prefixed[tocBytes.length] = BBA_PREFIX_TOC;
mainValue = new ByteArray(prefixed);
}
// Put the main value.
Future<Boolean> future = client.set(key, expirySec, mainValue, bat);
if (waitForAck) {
Boolean success = null;
try {
success = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
ZimbraLog.misc.warn("memcached set timed out after " + timeout + "ms", e);
future.cancel(false);
} catch (InterruptedException e) {
ZimbraLog.misc.warn("InterruptedException during memcached set operation", e);
} catch (ExecutionException e) {
ZimbraLog.misc.warn("ExecutionException during memcached set operation", e);
}
return success != null && success.booleanValue();
} else {
return true;
}
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class JaxbToJsonTest method missingRequiredStringElem.
/** Permissive handling - if required things are not present, users need to handle this */
@Test
public void missingRequiredStringElem() throws Exception {
final String attr1Val = "My attribute ONE";
StringAttrStringElem tstr = new StringAttrStringElem();
tstr.setAttr1(attr1Val);
// tstr.setElem1(elem1Val);
Element jsonJaxbElem = JacksonUtil.jaxbToJSONElement(tstr);
logDebug("JSONElement from JAXB ---> prettyPrint\n%1$s", jsonJaxbElem.prettyPrint());
StringAttrStringElem roundtripped = JaxbUtil.elementToJaxb(jsonJaxbElem, StringAttrStringElem.class);
Assert.assertEquals("JSONElement attr1", attr1Val, jsonJaxbElem.getAttribute("attribute-1"));
try {
jsonJaxbElem.getElement("element1");
} catch (ServiceException svcE) {
Assert.assertEquals("JSONElement exception when getting missing item:", ServiceException.INVALID_REQUEST, svcE.getCode());
}
Assert.assertEquals("roundtripped attr1", attr1Val, roundtripped.getAttr1());
Assert.assertEquals("roundtripped elem1", null, roundtripped.getElem1());
}
use of com.zimbra.common.service.ServiceException in project zm-mailbox by Zimbra.
the class JaxbUtil method addChildElementFromJaxb.
public static Element addChildElementFromJaxb(Element parent, String name, String namespace, Object o) {
Element.ElementFactory factory;
if (parent instanceof XMLElement) {
factory = XMLElement.mFactory;
} else {
factory = JSONElement.mFactory;
}
Element child = null;
try {
child = jaxbToNamedElement(name, namespace, o, factory);
} catch (ServiceException e) {
ZimbraLog.misc.info("JAXB Problem making " + name + " element", e);
}
parent.addElement(child);
return child;
}
Aggregations