// --------------------------------------------------------
private void dispatch(CallContext context, HttpServletRequest request, HttpServletResponse response)
throws Exception {
CmisService service = null;
try {
// get services factory
CmisServiceFactory factory = (CmisServiceFactory) getServletContext().getAttribute(
CmisRepositoryContextListener.SERVICES_FACTORY);
if (factory == null) {
throw new CmisRuntimeException("Service factory not available! Configuration problem?");
}
// get the service
service = factory.getService(context);
// analyze the path
String[] pathFragments = HttpUtils.splitPath(request);
if (pathFragments.length < 1) {
// root -> repository infos
RepositoryService.getRepositories(context, service, request, response);
return;
}
// select dispatcher
CallUrl callUrl = null;
if (pathFragments.length == 1) {
callUrl = CallUrl.REPOSITORY;
} else if (BrowserBindingUtils.ROOT_PATH_FRAGMENT.equals(pathFragments[1])) {
callUrl = CallUrl.ROOT;
}
if (callUrl == null) {
throw new CmisNotSupportedException("Unknown operation");
}
String method = request.getMethod();
String repositoryId = pathFragments[0];
boolean methodFound = false;
if (METHOD_GET.equals(method)) {
String selector = getStringParameter(request, Constants.PARAM_SELECTOR);
String objectId = getStringParameter(request, PARAM_OBJECT_ID);
// add object id and object base type id to context
prepareContext(context, callUrl, service, repositoryId, objectId, null, request);
// dispatch
if (callUrl == CallUrl.REPOSITORY) {
if (selector == null) {
selector = "";
}
methodFound = repositoryDispatcher.dispatch(selector, method, context, service, repositoryId,
request, response);
} else if (callUrl == CallUrl.ROOT) {
// set default method if necessary
if (selector == null) {
try {
BaseTypeId basetype = BaseTypeId.fromValue((String) context.get(CONTEXT_BASETYPE_ID));
switch (basetype) {
case CMIS_DOCUMENT:
selector = SELECTOR_CONTENT;
break;
case CMIS_FOLDER:
selector = SELECTOR_CHILDREN;
break;
default:
selector = SELECTOR_OBJECT;
break;
}
} catch (Exception e) {
selector = SELECTOR_OBJECT;
}
}
methodFound = rootDispatcher.dispatch(selector, method, context, service, repositoryId, request,
response);
}
} else if (METHOD_POST.equals(method)) {
POSTHttpServletRequestWrapper postRequest = new POSTHttpServletRequestWrapper(request, tempDir,
memoryThreshold);
String cmisaction = getStringParameter(postRequest, Constants.CONTROL_CMISACTION);
String objectId = getStringParameter(postRequest, Constants.CONTROL_OBJECT_ID);
String transaction = getStringParameter(postRequest, Constants.CONTROL_TRANSACTION);
if (cmisaction == null || cmisaction.length() == 0) {
throw new CmisNotSupportedException("Unknown action");
}
// add object id and object base type id to context
prepareContext(context, callUrl, service, repositoryId, objectId, transaction, postRequest);
// dispatch
if (callUrl == CallUrl.REPOSITORY) {
methodFound = repositoryDispatcher.dispatch(cmisaction, method, context, service, repositoryId,
postRequest, response);
} else if (callUrl == CallUrl.ROOT) {
methodFound = rootDispatcher.dispatch(cmisaction, method, context, service, repositoryId,
postRequest, response);
}
}
// if the dispatcher couldn't find a matching method, return an
// error message
if (!methodFound) {
throw new CmisNotSupportedException("Unknown operation");
}
} finally {
if (service != null) {
service.close();
}
}
}