use of org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException in project copper-cms by PogeyanOSS.
the class ServletHelpers method printError.
static void printError(Exception ex, HttpServletRequest request, HttpServletResponse response) {
int statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
String exceptionName = CmisRuntimeException.EXCEPTION_NAME;
if (ex instanceof CmisRuntimeException) {
AkkaCmisBrowserBindingServlet.LOG.error(createLogMessage(ex, request), ex);
statusCode = getErrorCode((CmisRuntimeException) ex);
} else if (ex instanceof CmisStorageException) {
AkkaCmisBrowserBindingServlet.LOG.error(createLogMessage(ex, request), ex);
statusCode = getErrorCode((CmisStorageException) ex);
exceptionName = ((CmisStorageException) ex).getExceptionName();
} else if (ex instanceof CmisBaseException) {
statusCode = getErrorCode((CmisBaseException) ex);
exceptionName = ((CmisBaseException) ex).getExceptionName();
if (statusCode == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
AkkaCmisBrowserBindingServlet.LOG.error(createLogMessage(ex, request), ex);
}
} else if (ex instanceof IOException) {
AkkaCmisBrowserBindingServlet.LOG.warn(createLogMessage(ex, request), ex);
} else {
AkkaCmisBrowserBindingServlet.LOG.error(createLogMessage(ex, request), ex);
}
if (response.isCommitted()) {
AkkaCmisBrowserBindingServlet.LOG.warn("Failed to send error message to client. Response is already committed.", ex);
return;
}
String message = ex.getMessage();
/*
* if (!(ex instanceof CmisBaseException)) { message =
* "An error occurred!"; }
*/
response.resetBuffer();
response.setStatus(statusCode);
JSONObject jsonResponse = new JSONObject();
jsonResponse.put(BrowserConstants.ERROR_EXCEPTION, exceptionName);
jsonResponse.put(BrowserConstants.MESSAGE, message);
String st = ExceptionHelper.getStacktraceAsString(ex);
if (st != null) {
jsonResponse.put(BrowserConstants.ERROR_STACKTRACE, st);
}
if (ex instanceof CmisBaseException) {
Map<String, String> additionalData = ((CmisBaseException) ex).getAdditionalData();
if (additionalData != null && !additionalData.isEmpty()) {
for (Map.Entry<String, String> e : additionalData.entrySet()) {
if (BrowserConstants.ERROR_EXCEPTION.equalsIgnoreCase(e.getKey()) || BrowserConstants.MESSAGE.equalsIgnoreCase(e.getKey())) {
continue;
}
jsonResponse.put(e.getKey(), e.getValue());
}
}
}
try {
ServletHelpers.writeJSON(jsonResponse, request, response);
} catch (Exception e) {
AkkaCmisBrowserBindingServlet.LOG.error(createLogMessage(ex, request), e);
try {
response.sendError(statusCode, message);
} catch (Exception en) {
// there is nothing else we can do
}
}
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException in project copper-cms by PogeyanOSS.
the class AbstractSessionTest method createItem.
/**
* Creates a item.
*/
protected Item createItem(Session session, Folder parent, String name, String objectTypeId) {
if (parent == null) {
throw new IllegalArgumentException("Parent is not set!");
}
if (name == null) {
throw new IllegalArgumentException("Name is not set!");
}
if (objectTypeId == null) {
throw new IllegalArgumentException("Object Type ID is not set!");
}
// check type
ObjectType type;
try {
type = session.getTypeDefinition(objectTypeId);
} catch (CmisObjectNotFoundException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Item type '" + objectTypeId + "' is not available: " + e.getMessage(), e, true));
return null;
}
if (Boolean.FALSE.equals(type.isCreatable())) {
addResult(createResult(SKIPPED, "Item type '" + objectTypeId + "' is not creatable!", true));
return null;
}
// create
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, name);
properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
Item result = null;
try {
// create the item
result = parent.createItem(properties, null, null, null, SELECT_ALL_NO_CACHE_OC);
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Item could not be created! Exception: " + e.getMessage(), e, true));
return null;
}
try {
CmisTestResult f;
// check item name
f = createResult(FAILURE, "Item name does not match!", false);
addResult(assertEquals(name, result.getName(), null, f));
addResult(checkObject(session, result, getAllProperties(result), "New item object spec compliance"));
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Newly created item is invalid! Exception: " + e.getMessage(), e, true));
}
// check parents
List<Folder> parents = result.getParents(SELECT_ALL_NO_CACHE_OC);
boolean found = false;
for (Folder folder : parents) {
if (parent.getId().equals(folder.getId())) {
found = true;
break;
}
}
if (!found) {
addResult(createResult(FAILURE, "The folder the item has been created in is not in the list of the item parents!"));
}
return result;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException in project copper-cms by PogeyanOSS.
the class AbstractSessionTest method deleteObject.
/**
* Deletes an object and checks if it is deleted.
*/
protected void deleteObject(CmisObject object) {
if (object != null) {
if (object instanceof Folder) {
try {
((Folder) object).deleteTree(true, null, true);
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Folder could not be deleted! Exception: " + e.getMessage(), e, true));
}
} else {
try {
object.delete(true);
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Object could not be deleted! Exception: " + e.getMessage(), e, true));
}
}
CmisTestResult f = createResult(FAILURE, "Object should not exist anymore but it is still there! ID: " + object.getId(), true);
addResult(assertIsFalse(exists(object), null, f));
}
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException in project copper-cms by PogeyanOSS.
the class AbstractSessionTest method createRelationship.
/**
* Creates a relationship.
*/
protected Relationship createRelationship(Session session, String name, ObjectId source, ObjectId target, String objectTypeId) {
if (name == null) {
throw new IllegalArgumentException("Name is not set!");
}
if (objectTypeId == null) {
throw new IllegalArgumentException("Object Type ID is not set!");
}
// check type
ObjectType type;
try {
type = session.getTypeDefinition(objectTypeId);
} catch (CmisObjectNotFoundException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Relationship type '" + objectTypeId + "' is not available: " + e.getMessage(), e, true));
return null;
}
if (Boolean.FALSE.equals(type.isCreatable())) {
addResult(createResult(SKIPPED, "Relationship type '" + objectTypeId + "' is not creatable!", true));
return null;
}
// create
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, name);
properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
properties.put(PropertyIds.SOURCE_ID, source.getId());
properties.put(PropertyIds.TARGET_ID, target.getId());
ObjectId relId;
Relationship result = null;
try {
relId = session.createRelationship(properties);
result = (Relationship) session.getObject(relId, SELECT_ALL_NO_CACHE_OC);
} catch (Exception e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Relationship could not be created! Exception: " + e.getMessage(), e, true));
}
if (result != null) {
try {
// check the new relationship
addResult(checkObject(session, result, getAllProperties(result), "New document object spec compliance"));
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Newly created document is invalid! Exception: " + e.getMessage(), e, true));
}
}
return result;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException in project copper-cms by PogeyanOSS.
the class AbstractSessionTest method createDocument.
/**
* Creates a document.
*/
protected Document createDocument(Session session, Folder parent, String name, String objectTypeId, String[] secondaryTypeIds, String content) {
if (parent == null) {
throw new IllegalArgumentException("Parent is not set!");
}
if (name == null) {
throw new IllegalArgumentException("Name is not set!");
}
if (objectTypeId == null) {
throw new IllegalArgumentException("Object Type ID is not set!");
}
if (content == null) {
content = "";
}
// check type
ObjectType type;
try {
type = session.getTypeDefinition(objectTypeId);
} catch (CmisObjectNotFoundException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Document type '" + objectTypeId + "' is not available: " + e.getMessage(), e, true));
return null;
}
if (Boolean.FALSE.equals(type.isCreatable())) {
addResult(createResult(SKIPPED, "Document type '" + objectTypeId + "' is not creatable!", true));
return null;
}
// create
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, name);
properties.put(PropertyIds.OBJECT_TYPE_ID, objectTypeId);
if (secondaryTypeIds != null) {
properties.put(PropertyIds.SECONDARY_OBJECT_TYPE_IDS, Arrays.asList(secondaryTypeIds));
}
type = session.getTypeDefinition(objectTypeId);
if (!(type instanceof DocumentTypeDefinition)) {
addResult(createResult(FAILURE, "Type is not a document type! Type: " + objectTypeId, true));
return null;
}
DocumentTypeDefinition docType = (DocumentTypeDefinition) type;
VersioningState versioningState = (Boolean.TRUE.equals(docType.isVersionable()) ? VersioningState.MAJOR : VersioningState.NONE);
byte[] contentBytes = null;
Document result = null;
try {
contentBytes = IOUtils.toUTF8Bytes(content);
ContentStream contentStream = new ContentStreamImpl(name, BigInteger.valueOf(contentBytes.length), "text/plain", new ByteArrayInputStream(contentBytes));
// create the document
result = parent.createDocument(properties, contentStream, versioningState, null, null, null, SELECT_ALL_NO_CACHE_OC);
contentStream.getStream().close();
} catch (Exception e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Document could not be created! Exception: " + e.getMessage(), e, true));
return null;
}
try {
CmisTestResult f;
// check document name
f = createResult(FAILURE, "Document name does not match!", false);
addResult(assertEquals(name, result.getName(), null, f));
// check content length
f = createResult(WARNING, "Content length does not match!", false);
addResult(assertEquals((long) contentBytes.length, result.getContentStreamLength(), null, f));
// check the new document
addResult(checkObject(session, result, getAllProperties(result), "New document object spec compliance"));
// check content
try {
ContentStream contentStream = result.getContentStream();
f = createResult(WARNING, "Document filename and the filename of the content stream do not match!", false);
addResult(assertEquals(name, contentStream.getFileName(), null, f));
f = createResult(WARNING, "cmis:contentStreamFileName and the filename of the content stream do not match!", false);
addResult(assertEquals(result.getContentStreamFileName(), contentStream.getFileName(), null, f));
String fetchedContent = getStringFromContentStream(result.getContentStream());
if (!content.equals(fetchedContent)) {
addResult(createResult(FAILURE, "Content of newly created document doesn't match the orign content!"));
}
} catch (IOException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Content of newly created document couldn't be read! Exception: " + e.getMessage(), e, true));
}
} catch (CmisBaseException e) {
addResult(createResult(UNEXPECTED_EXCEPTION, "Newly created document is invalid! Exception: " + e.getMessage(), e, true));
}
// check parents
List<Folder> parents = result.getParents(SELECT_ALL_NO_CACHE_OC);
boolean found = false;
for (Folder folder : parents) {
if (parent.getId().equals(folder.getId())) {
found = true;
break;
}
}
if (!found) {
addResult(createResult(FAILURE, "The folder the document has been created in is not in the list of the document parents!"));
}
return result;
}
Aggregations