use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.
the class AkkaCmisBrowserBindingServlet method service.
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
final ActorSystem system = (ActorSystem) request.getServletContext().getAttribute("ActorSystem");
// CSRF token check
String method = request.getMethod();
if (!METHOD_GET.equals(method) && !METHOD_HEAD.equals(method)) {
checkCsrfToken(request, response, false, false);
}
// set default headers
response.addHeader("Cache-Control", "private, max-age=0");
response.addHeader("Server", ServerVersion.OPENCMIS_SERVER);
// split path
String[] pathFragments = HttpUtils.splitPath(request);
final AsyncContext ctx = request.startAsync(request, response);
if (Helpers.isPerfMode()) {
MetricsInputs.get().getCounter("counter_requests_total").inc();
}
if (pathFragments != null && pathFragments.length > 0 && StringUtils.isBlank(pathFragments[0])) {
BaseMessage bm = gettingBaseMessage(method, pathFragments, null, request, response);
if (bm != null) {
// create actor on-the-fly
ActorRef servletActor = system.actorOf(Props.create(ServletActor.class, ctx));
servletActor.tell(bm, ActorRef.noSender());
} else {
throw new CmisNotSupportedException("Unsupported method");
}
} else {
this.verifyLogin(request, pathFragments, system, (s) -> {
try {
IUserObject loginSession = (IUserObject) s;
BaseMessage bm = gettingBaseMessage(method, pathFragments, loginSession, request, response);
if (bm != null) {
// create actor on-the-fly
ActorRef servletActor = system.actorOf(Props.create(ServletActor.class, ctx));
servletActor.tell(bm, ActorRef.noSender());
} else {
throw new CmisNotSupportedException("Unsupported method");
}
} catch (Exception e1) {
MetricsInputs.markBindingServletErrorMeter();
LOG.error("Service execution exception: {}, stack: {}", e1.getMessage(), ExceptionUtils.getStackTrace(e1));
ServletHelpers.printError(e1, request, response);
}
}, (err) -> {
HttpServletResponse asyncResponse = (HttpServletResponse) ctx.getResponse();
asyncResponse.setHeader("WWW-Authenticate", "Basic realm=\"CMIS\", charset=\"UTF-8\"");
try {
asyncResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization Required");
} catch (Exception e1) {
MetricsInputs.markBindingServletErrorMeter();
ServletHelpers.printError(e1, (HttpServletRequest) ctx.getRequest(), asyncResponse);
}
ctx.complete();
});
}
} catch (Exception e) {
MetricsInputs.markBindingServletErrorMeter();
if (e instanceof CmisUnauthorizedException) {
response.setHeader("WWW-Authenticate", "Basic realm=\"CMIS\", charset=\"UTF-8\"");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization Required");
} else if (e instanceof CmisPermissionDeniedException) {
response.setHeader("WWW-Authenticate", "Basic realm=\"CMIS\", charset=\"UTF-8\"");
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Authorization Required");
} else {
ServletHelpers.printError(e, request, response);
}
} finally {
// in any case close the content stream if one has been provided
// if (request instanceof POSTHttpServletRequestWrapper) {
// InputStream stream = ((POSTHttpServletRequestWrapper)
// request).getStream();
// if (stream != null) {
// try {
// stream.close();
// } catch (IOException e) {
// LOG.error("Could not close POST stream: {}", e.toString(), e);
// }
// }
// }
// // we are done.
// try {
// response.flushBuffer();
// } catch (IOException ioe) {
// LOG.error("Could not flush resposne: {}", ioe.toString(), ioe);
// }
}
}
use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.
the class AkkaCmisBrowserBindingServlet method verifyLogin.
private void verifyLogin(HttpServletRequest request, String[] pathFragments, ActorSystem system, Action<Object> onSuccess, Action<Object> onError) {
// forwarding callback after verifying login response object
Action<BaseMessage> onLoginSuccess = (t) -> {
LoginResponse lr = (LoginResponse) t.getMessageAsType(LoginResponse.class);
if (lr.isSuccessfulLogin()) {
// To test different users and different password on test
// cases require to remove cache on 1 second
/*
* if (Helpers.isTestMode()) {
* //RedissonCacheFactory.get().put("login." + userName,
* lr.getLoginDetails(), 5, TimeUnit.SECONDS); } else { // set
* in map cache for 30 mins expiry
* //RedissonCacheFactory.get().put("login." + userName,
* lr.getLoginDetails(), 30, TimeUnit.MINUTES); }
*/
onSuccess.apply(lr.getLoginDetails());
} else {
onError.apply(null);
}
};
ActorRef genericActorRef = system.actorOf(Props.create(GenericActor.class, onLoginSuccess, onError));
LoginRequest loginRequest = new LoginRequest();
loginRequest.setHeaders(ServletHelpers.getHeadersInfo(request));
if (pathFragments.length > 0) {
loginRequest.setRepositoryId(pathFragments[0]);
}
BaseMessage loginMessage = BaseMessage.create("login", "authenticate", loginRequest);
genericActorRef.tell(loginMessage, ActorRef.noSender());
/*
* String userName = callContextMap.get(BrowserConstants.USERNAME);
* Object loginSession = RedissonCacheFactory.get().get("login." +
* userName); if (loginSession == "") { } else {
* onSuccess.apply(loginSession); }
*/
}
use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.
the class ServletHelpers method postToBaseMessage.
static BaseMessage postToBaseMessage(POSTHttpServletRequestWrapper request, String[] pathFragments, IUserObject userObject) {
PostRequest postRequest = new PostRequest();
ControlParser controlParser = new ControlParser(request);
if (controlParser != null) {
CmisRequestParameter requestParameter = new CmisRequestParameter();
postRequest.setPropertyData(controlParser.getProperties());
postRequest.setAddAcl(requestParameter.createAddAcl(controlParser, postRequest));
postRequest.setRemoveAcl(requestParameter.createRemoveAcl(controlParser, postRequest));
postRequest.setPolicies(requestParameter.createPolicies(controlParser, postRequest));
postRequest.setMultipart(request.isMultiPart());
postRequest.setContentStream(requestParameter.createContentStream(request));
postRequest.setObjectIds(requestParameter.getObjectIds(controlParser, postRequest));
postRequest.setChangeTokens(requestParameter.getChangeTokens(controlParser, postRequest));
postRequest.setAddSecondaryTypes(requestParameter.addSecondaryTypes(controlParser, postRequest));
postRequest.setRemoveSecondaryTypes(requestParameter.getChangeTokens(controlParser, postRequest));
postRequest.setPolicyId(requestParameter.getPolicyId(controlParser, postRequest));
postRequest.setAclPropagation(requestParameter.getAclPropagation(controlParser, postRequest));
}
postRequest.setParameterMap(request.getParameterMap());
postRequest.setBaseUrl((String) request.getAttribute(BrowserConstants.BASE_URL_ATTRIBUTE));
postRequest.setScheme(request.getScheme());
postRequest.setServerName(request.getServerName());
postRequest.setServerPort(request.getServerPort());
postRequest.setContextPath(request.getContextPath());
postRequest.setServletPath(request.getServletPath());
String cmisAction = HttpUtils.getStringParameter(request, BrowserConstants.CONTROL_CMISACTION);
postRequest.setCmisAction(cmisAction);
String objectId = HttpUtils.getStringParameter(request, BrowserConstants.CONTROL_OBJECT_ID);
postRequest.setObjectId(objectId);
if (pathFragments.length > 0) {
postRequest.setRepositoryId(pathFragments[0]);
if (objectId != null) {
ObjectData object = ServletHelpers.getObjectDataFor(pathFragments[0], objectId, pathFragments);
String typeId = getStringPropertyValue(object, PropertyIds.OBJECT_TYPE_ID);
postRequest.setTypeId(typeId);
BaseTypeId baseTypeId = BaseTypeId.fromValue(getStringPropertyValue(object, PropertyIds.BASE_TYPE_ID));
postRequest.setBaseTypeId(baseTypeId);
}
}
String token = HttpUtils.getStringParameter(request, BrowserConstants.CONTROL_TOKEN);
postRequest.setToken(token);
postRequest.setRequestBody(request.getRequestBody());
if (cmisAction == null || cmisAction.length() == 0) {
throw new CmisNotSupportedException("Unknown action");
}
postRequest.setPathFragments(pathFragments);
if (userObject != null) {
postRequest.setUserName(userObject.getUserDN());
postRequest.setUserObject(userObject);
}
BaseMessage bm = BaseMessage.create("", cmisAction, postRequest);
return bm;
}
use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.
the class AkkaCmisBrowserBindingServlet method gettingBaseMessage.
private BaseMessage gettingBaseMessage(String method, String[] pathFragments, IUserObject loginSession, HttpServletRequest request, HttpServletResponse response) {
BaseMessage bm = null;
try {
// check HTTP method
if (METHOD_GET.equals(method)) {
QueryStringHttpServletRequestWrapper qRequest = new QueryStringHttpServletRequestWrapper(request);
bm = ServletHelpers.queryHttpToBaseMessage((QueryStringHttpServletRequestWrapper) qRequest, pathFragments, loginSession);
} else if (METHOD_POST.equals(method)) {
POSTHttpServletRequestWrapper pRequest = new POSTHttpServletRequestWrapper(request);
bm = ServletHelpers.postToBaseMessage((POSTHttpServletRequestWrapper) pRequest, pathFragments, loginSession);
} else {
throw new CmisNotSupportedException("Unsupported method");
}
} catch (Exception e1) {
LOG.error("Service execution exception: {}, stack: {}", e1.getMessage(), ExceptionUtils.getStackTrace(e1));
ServletHelpers.printError(e1, request, response);
}
return bm;
}
use of com.pogeyan.cmis.api.BaseMessage in project copper-cms by PogeyanOSS.
the class ServletHelpers method queryHttpToBaseMessage.
static BaseMessage queryHttpToBaseMessage(QueryStringHttpServletRequestWrapper request, String[] pathFragments, IUserObject userObject) {
QueryGetRequest queryRequest = new QueryGetRequest();
queryRequest.setParameterMap(request.getParameterMap());
queryRequest.setBaseUrl((String) request.getAttribute(BrowserConstants.BASE_URL_ATTRIBUTE));
queryRequest.setScheme(request.getScheme());
queryRequest.setServerName(request.getServerName());
queryRequest.setServerPort(request.getServerPort());
queryRequest.setContextPath(request.getContextPath());
queryRequest.setServletPath(request.getServletPath());
queryRequest.setRange(request.getHeader("Range"));
String objectId = HttpUtils.getStringParameter(request, BrowserConstants.PARAM_OBJECT_ID);
queryRequest.setObjectId(objectId);
if (userObject != null) {
queryRequest.setUserName(userObject.getUserDN());
queryRequest.setUserObject(userObject);
}
if (pathFragments.length > 0) {
queryRequest.setRepositoryId(pathFragments[0]);
}
// selector is the actionName
String selector = HttpUtils.getStringParameter(request, BrowserConstants.PARAM_SELECTOR);
if (pathFragments.length > 1) {
queryRequest.setPathFragments(pathFragments);
// nested url
String repositoryId = pathFragments[0];
try {
ObjectData object = null;
// Example objectId format-typeID::primaryKey::primaryKeyValue
if (objectId != null && objectId.contains("::")) {
String[] inputs = objectId.split("::");
// getObjectForRestAPI method is used to get the object.
object = CmisObjectService.Impl.getObjectForRestAPI(repositoryId, inputs[0], inputs[1], inputs[2], "cmis:objectId,cmis:objectTypeId,cmis:baseTypeId", false, IncludeRelationships.NONE, "cmis:none", false, false, null, "");
} else {
object = ServletHelpers.getObjectDataFor(repositoryId, objectId, pathFragments);
}
// reset object id again here
objectId = object.getId();
queryRequest.setObjectId(objectId);
String typeId = getStringPropertyValue(object, PropertyIds.OBJECT_TYPE_ID);
queryRequest.setTypeId(typeId);
BaseTypeId baseTypeId = BaseTypeId.fromValue(getStringPropertyValue(object, PropertyIds.BASE_TYPE_ID));
queryRequest.setBaseTypeId(baseTypeId);
if (selector == null) {
switch(baseTypeId) {
case CMIS_DOCUMENT:
selector = BrowserConstants.SELECTOR_CONTENT;
break;
case CMIS_FOLDER:
selector = BrowserConstants.SELECTOR_CHILDREN;
break;
default:
selector = BrowserConstants.SELECTOR_OBJECT;
break;
}
}
} catch (Exception e) {
selector = BrowserConstants.SELECTOR_OBJECT;
}
}
selector = selector == null ? "GetRepositories".toLowerCase() : selector;
BaseMessage bm = BaseMessage.create("", selector, queryRequest);
return bm;
}
Aggregations