Search in sources :

Example 1 with CmisBaseException

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
        }
    }
}
Also used : CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) JSONObject(org.apache.chemistry.opencmis.commons.impl.json.JSONObject) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) IOException(java.io.IOException) CmisStorageException(org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException) HashMap(java.util.HashMap) Map(java.util.Map) CmisStreamNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisStreamNotSupportedException) CmisContentAlreadyExistsException(org.apache.chemistry.opencmis.commons.exceptions.CmisContentAlreadyExistsException) CmisConstraintException(org.apache.chemistry.opencmis.commons.exceptions.CmisConstraintException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisRuntimeException(org.apache.chemistry.opencmis.commons.exceptions.CmisRuntimeException) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisServiceUnavailableException(org.apache.chemistry.opencmis.commons.exceptions.CmisServiceUnavailableException) CmisNameConstraintViolationException(org.apache.chemistry.opencmis.commons.exceptions.CmisNameConstraintViolationException) CmisFilterNotValidException(org.apache.chemistry.opencmis.commons.exceptions.CmisFilterNotValidException) CmisTooManyRequestsException(org.apache.chemistry.opencmis.commons.exceptions.CmisTooManyRequestsException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisStorageException(org.apache.chemistry.opencmis.commons.exceptions.CmisStorageException) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) IOException(java.io.IOException) CmisUpdateConflictException(org.apache.chemistry.opencmis.commons.exceptions.CmisUpdateConflictException) CmisVersioningException(org.apache.chemistry.opencmis.commons.exceptions.CmisVersioningException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)

Example 2 with CmisBaseException

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;
}
Also used : HashMap(java.util.HashMap) Folder(org.apache.chemistry.opencmis.client.api.Folder) ObjectType(org.apache.chemistry.opencmis.client.api.ObjectType) Item(org.apache.chemistry.opencmis.client.api.Item) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject)

Example 3 with CmisBaseException

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));
    }
}
Also used : CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) Folder(org.apache.chemistry.opencmis.client.api.Folder)

Example 4 with CmisBaseException

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;
}
Also used : ObjectType(org.apache.chemistry.opencmis.client.api.ObjectType) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) HashMap(java.util.HashMap) ObjectId(org.apache.chemistry.opencmis.client.api.ObjectId) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) Relationship(org.apache.chemistry.opencmis.client.api.Relationship) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) IOException(java.io.IOException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)

Example 5 with CmisBaseException

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;
}
Also used : ContentStreamImpl(org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl) DocumentTypeDefinition(org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition) HashMap(java.util.HashMap) IOException(java.io.IOException) Document(org.apache.chemistry.opencmis.client.api.Document) Folder(org.apache.chemistry.opencmis.client.api.Folder) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) IOException(java.io.IOException) CmisInvalidArgumentException(org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException) ObjectType(org.apache.chemistry.opencmis.client.api.ObjectType) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) CmisObjectNotFoundException(org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException) ByteArrayInputStream(java.io.ByteArrayInputStream) CmisBaseException(org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException) CmisTestResult(org.apache.chemistry.opencmis.tck.CmisTestResult) FileableCmisObject(org.apache.chemistry.opencmis.client.api.FileableCmisObject) CmisObject(org.apache.chemistry.opencmis.client.api.CmisObject) VersioningState(org.apache.chemistry.opencmis.commons.enums.VersioningState)

Aggregations

CmisBaseException (org.apache.chemistry.opencmis.commons.exceptions.CmisBaseException)16 CmisTestResult (org.apache.chemistry.opencmis.tck.CmisTestResult)11 Folder (org.apache.chemistry.opencmis.client.api.Folder)8 CmisObjectNotFoundException (org.apache.chemistry.opencmis.commons.exceptions.CmisObjectNotFoundException)8 HashMap (java.util.HashMap)7 CmisObject (org.apache.chemistry.opencmis.client.api.CmisObject)7 FileableCmisObject (org.apache.chemistry.opencmis.client.api.FileableCmisObject)7 ObjectType (org.apache.chemistry.opencmis.client.api.ObjectType)7 CmisInvalidArgumentException (org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException)4 CmisNotSupportedException (org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException)4 IOException (java.io.IOException)3 Document (org.apache.chemistry.opencmis.client.api.Document)3 ObjectId (org.apache.chemistry.opencmis.client.api.ObjectId)3 DocumentTypeDefinition (org.apache.chemistry.opencmis.commons.definitions.DocumentTypeDefinition)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PrintWriter (java.io.PrintWriter)2 Policy (org.apache.chemistry.opencmis.client.api.Policy)2 Relationship (org.apache.chemistry.opencmis.client.api.Relationship)2 ContentStream (org.apache.chemistry.opencmis.commons.data.ContentStream)2 ContentStreamImpl (org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl)2