use of org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException in project copper-cms by PogeyanOSS.
the class POSTHttpServletRequestWrapper method parseFormUrlEncodedData.
/**
* Parses a form data request
*
* @param request
* the request
* @return {@code true} if the body contained data, {@code false} otherwise
*/
protected boolean parseFormUrlEncodedData(HttpServletRequest request) throws IOException {
byte[] data = new byte[BUFFER_SIZE];
int dataPos = 0;
InputStream stream = request.getInputStream();
int b;
byte[] buffer = new byte[BUFFER_SIZE];
// read stream
while ((b = stream.read(buffer)) != -1) {
if (dataPos + b > MAX_CONTENT_BYTES) {
throw new CmisInvalidArgumentException("Limit exceeded!");
}
if (data.length - dataPos < b) {
// expand buffer
int newSize = ((data.length + b) * 2 < MAX_CONTENT_BYTES ? (data.length + b * 2) : MAX_CONTENT_BYTES);
byte[] newbuf = new byte[newSize];
System.arraycopy(data, 0, newbuf, 0, dataPos);
data = newbuf;
}
System.arraycopy(buffer, 0, data, dataPos, b);
dataPos += b;
}
if (dataPos == 0) {
// empty stream
return false;
}
// parse parameters
boolean parseName = true;
boolean parseCharset = false;
int startPos = 0;
List<String[]> rawParameters = new ArrayList<String[]>();
String rawName = null;
String rawValue = null;
String charset = null;
for (int i = 0; i < dataPos; i++) {
switch(data[i]) {
case '=':
if (startPos < i) {
rawName = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
if (CHARSET_FIELD.equalsIgnoreCase(rawName)) {
parseCharset = true;
}
}
parseName = false;
startPos = i + 1;
break;
case '&':
if (parseName) {
if (startPos < i) {
rawName = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
rawParameters.add(new String[] { rawName, null });
}
} else {
if (rawName != null) {
rawValue = new String(data, startPos, i - startPos, IOUtils.ISO_8859_1);
rawParameters.add(new String[] { rawName, rawValue });
if (parseCharset) {
charset = rawValue;
}
}
}
rawName = null;
rawValue = null;
parseName = true;
parseCharset = false;
startPos = i + 1;
break;
default:
break;
}
}
if (startPos < dataPos) {
// there is a final parameter after the last '&'
if (parseName) {
rawName = new String(data, startPos, dataPos - startPos, IOUtils.ISO_8859_1);
rawParameters.add(new String[] { rawName, null });
} else {
if (rawName != null) {
rawValue = new String(data, startPos, dataPos - startPos, IOUtils.ISO_8859_1);
rawParameters.add(new String[] { rawName, rawValue });
if (parseCharset) {
charset = rawValue;
}
}
}
} else if (!parseName) {
// the stream ended with '='
rawParameters.add(new String[] { rawName, "" });
}
data = null;
// find charset
if (charset == null) {
// check charset in content type
String contentType = request.getContentType();
if (contentType != null) {
String[] parts = contentType.split(";");
for (int i = 1; i < parts.length; i++) {
String part = parts[i].trim().toLowerCase(Locale.ENGLISH);
if (part.startsWith("charset")) {
int x = part.indexOf('=');
charset = part.substring(x + 1).trim();
break;
}
}
}
}
if (charset == null) {
// set default charset
charset = IOUtils.UTF8;
}
// decode parameters
for (String[] rawParameter : rawParameters) {
String name = URLDecoder.decode(rawParameter[0], charset);
String value = null;
if (rawParameter[1] != null) {
value = URLDecoder.decode(rawParameter[1], charset);
}
addParameter(name, value);
}
return true;
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException in project copper-cms by PogeyanOSS.
the class CreateInvalidTypeTest method run.
@Override
public void run(Session session) {
// create a test folder
Folder testFolder = createTestFolder(session);
try {
// test document creation
try {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, "never.txt");
properties.put(PropertyIds.OBJECT_TYPE_ID, getFolderTestTypeId());
byte[] contentBytes = IOUtils.toUTF8Bytes("nothing");
ContentStream contentStream = new ContentStreamImpl("never.txt", BigInteger.valueOf(contentBytes.length), "text/plain", new ByteArrayInputStream(contentBytes));
testFolder.createDocument(properties, contentStream, null);
addResult(createResult(FAILURE, "Creation of a document with a folder type shouldn't work!"));
} catch (Exception e) {
if (!(e instanceof CmisInvalidArgumentException) && !(e instanceof CmisConstraintException)) {
addResult(createResult(WARNING, "Creation of a document with a folder type threw an unexcpeted exception: " + e.toString()));
}
}
// test folder creation
try {
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(PropertyIds.NAME, "never");
properties.put(PropertyIds.OBJECT_TYPE_ID, getDocumentTestTypeId());
testFolder.createFolder(properties);
addResult(createResult(FAILURE, "Creation of a folder with a document type shouldn't work!"));
} catch (Exception e) {
if (!(e instanceof CmisInvalidArgumentException) && !(e instanceof CmisConstraintException)) {
addResult(createResult(WARNING, "Creation of a folder with a document type threw an unexcpeted exception: " + e.toString()));
}
}
} finally {
// delete the test folder
deleteTestFolder();
}
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException in project copper-cms by PogeyanOSS.
the class VersionDeleteTest method deleteVersion.
private void deleteVersion(Document versionDoc, Document previousDoc, int version) {
CmisTestResult f;
// check Allowable Action
if (!versionDoc.hasAllowableAction(Action.CAN_DELETE_OBJECT)) {
addResult(createResult(WARNING, "Version " + version + " does not have the Allowable Action 'canDeleteObject'."));
return;
}
// get version history before delete
List<Document> versionsBefore = versionDoc.getAllVersions();
// delete and check
try {
versionDoc.delete(false);
} catch (CmisInvalidArgumentException iae) {
addResult(createResult(WARNING, "Deletion of version " + version + " failed with an invalidArgument exception. " + "Removing just one version doesn't seem to be supported."));
return;
} catch (CmisConstraintException ce) {
addResult(createResult(WARNING, "Deletion of version " + version + " failed with an constraint exception. " + "Removing just one version doesn't seem to be supported."));
return;
}
f = createResult(FAILURE, "Deleted version " + version + " still exists!");
addResult(assertIsFalse(exists(versionDoc), null, f));
// check version history after delete
if (previousDoc != null) {
List<Document> versionsAfter = previousDoc.getAllVersions();
f = createResult(FAILURE, "After version " + version + " has been deleted, the version history should consist of " + (versionsBefore.size() - 1) + " documents but is has " + versionsAfter.size() + " !");
addResult(assertEquals(versionsBefore.size() - 1, versionsAfter.size(), null, f));
}
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException in project copper-cms by PogeyanOSS.
the class VersioningStateCreateTest method run.
@Override
public void run(Session session) {
CmisTestResult f;
try {
// create folder and document
Folder testFolder = createTestFolder(session);
DocumentTypeDefinition docType = (DocumentTypeDefinition) session.getTypeDefinition(getDocumentTestTypeId());
if (!docType.isVersionable()) {
addResult(createResult(SKIPPED, "Test type is not versionable. Test skipped!"));
return;
}
// major version
Document docMajor = testFolder.createDocument(getProperties("major.txt"), getContentStream(), VersioningState.MAJOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
addResult(checkObject(session, docMajor, getAllProperties(docMajor), "Major version compliance"));
f = createResult(FAILURE, "Document should be major version.");
addResult(assertIsTrue(docMajor.isMajorVersion(), null, f));
List<Document> versions = docMajor.getAllVersions();
f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
addResult(assertEquals(1, versions.size(), null, f));
deleteObject(docMajor);
// minor version
try {
Document docMinor = testFolder.createDocument(getProperties("minor.txt"), getContentStream(), VersioningState.MINOR, null, null, null, SELECT_ALL_NO_CACHE_OC);
addResult(checkObject(session, docMinor, getAllProperties(docMinor), "Minor version compliance"));
f = createResult(FAILURE, "Document should be minor version.");
addResult(assertIsFalse(docMinor.isMajorVersion(), null, f));
versions = docMinor.getAllVersions();
f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
addResult(assertEquals(1, versions.size(), null, f));
deleteObject(docMinor);
} catch (CmisConstraintException ce) {
addResult(createResult(WARNING, "Creating a minor version failed! " + "The repository might not support minor versions. Exception: " + ce, ce, false));
} catch (CmisInvalidArgumentException iae) {
addResult(createResult(WARNING, "Creating a minor version failed! " + "The repository might not support minor versions. Exception: " + iae, iae, false));
}
// checked out version
try {
Document docCheckedOut = testFolder.createDocument(getProperties("checkout.txt"), getContentStream(), VersioningState.CHECKEDOUT, null, null, null, SELECT_ALL_NO_CACHE_OC);
addResult(checkObject(session, docCheckedOut, getAllProperties(docCheckedOut), "Checked out version compliance"));
f = createResult(FAILURE, "Version series should be checked out.");
addResult(assertIsTrue(docCheckedOut.isVersionSeriesCheckedOut(), null, f));
versions = docCheckedOut.getAllVersions();
f = createResult(FAILURE, "Version series should have one version but has " + versions.size() + ".");
addResult(assertEquals(1, versions.size(), null, f));
docCheckedOut.cancelCheckOut();
} catch (CmisConstraintException ce) {
addResult(createResult(WARNING, "Creating a checked out version failed! " + "The repository might not support creating checked out versions. Exception: " + ce, ce, false));
} catch (CmisInvalidArgumentException iae) {
addResult(createResult(WARNING, "Creating a checked out version failed! " + "The repository might not support creating checked out versions. Exception: " + iae, iae, false));
}
} finally {
deleteTestFolder();
}
}
use of org.apache.chemistry.opencmis.commons.exceptions.CmisInvalidArgumentException in project alfresco-repository by Alfresco.
the class CMISConnector method setProperties.
/**
* Sets property values.
*/
@SuppressWarnings({ "rawtypes" })
public void setProperties(NodeRef nodeRef, TypeDefinitionWrapper type, Properties properties, String... exclude) {
if (properties == null) {
return;
}
Map<String, PropertyData<?>> incomingPropsMap = properties.getProperties();
if (incomingPropsMap == null) {
return;
}
// extract property data into an easier to use form
Map<String, Pair<TypeDefinitionWrapper, Serializable>> propsMap = new HashMap<String, Pair<TypeDefinitionWrapper, Serializable>>();
for (String propertyId : incomingPropsMap.keySet()) {
PropertyData<?> property = incomingPropsMap.get(propertyId);
PropertyDefinitionWrapper propDef = type.getPropertyById(property.getId());
if (propDef == null) {
propDef = getOpenCMISDictionaryService().findProperty(propertyId);
if (propDef == null) {
throw new CmisInvalidArgumentException("Property " + property.getId() + " is unknown!");
}
}
Boolean isOnWorkingCopy = checkOutCheckInService.isWorkingCopy(nodeRef);
Updatability updatability = propDef.getPropertyDefinition().getUpdatability();
if (!isUpdatable(updatability, isOnWorkingCopy)) {
throw new CmisInvalidArgumentException("Property " + propertyId + " is read-only!");
}
TypeDefinitionWrapper propType = propDef.getOwningType();
Serializable value = getValue(property, propDef.getPropertyDefinition().getCardinality() == Cardinality.MULTI);
Pair<TypeDefinitionWrapper, Serializable> pair = new Pair<TypeDefinitionWrapper, Serializable>(propType, value);
propsMap.put(propertyId, pair);
}
// Need to do deal with secondary types first
Pair<TypeDefinitionWrapper, Serializable> pair = propsMap.get(PropertyIds.SECONDARY_OBJECT_TYPE_IDS);
Serializable secondaryTypesProperty = (pair != null ? pair.getSecond() : null);
if (secondaryTypesProperty != null) {
if (!(secondaryTypesProperty instanceof List)) {
throw new CmisInvalidArgumentException("Secondary types must be a list!");
}
List secondaryTypes = (List) secondaryTypesProperty;
if (secondaryTypes != null && secondaryTypes.size() > 0) {
// add/remove secondary types/aspects
processSecondaryTypes(nodeRef, secondaryTypes, propsMap);
}
}
for (String propertyId : propsMap.keySet()) {
if (propertyId.equals(PropertyIds.SECONDARY_OBJECT_TYPE_IDS)) {
// already handled above
continue;
}
pair = propsMap.get(propertyId);
TypeDefinitionWrapper propType = pair.getFirst();
Serializable value = pair.getSecond();
if (Arrays.binarySearch(exclude, propertyId) < 0) {
setProperty(nodeRef, propType, propertyId, value);
}
}
List<CmisExtensionElement> extensions = properties.getExtensions();
if (extensions != null) {
boolean isNameChanging = properties.getProperties().containsKey(PropertyIds.NAME);
for (CmisExtensionElement extension : extensions) {
if (ALFRESCO_EXTENSION_NAMESPACE.equals(extension.getNamespace()) && SET_ASPECTS.equals(extension.getName())) {
setAspectProperties(nodeRef, isNameChanging, extension);
break;
}
}
}
}
Aggregations