Search in sources :

Example 1 with BaseMessage

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);
    // }
    }
}
Also used : ActorSystem(akka.actor.ActorSystem) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) ActorRef(akka.actor.ActorRef) IUserObject(com.pogeyan.cmis.api.auth.IUserObject) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) ServletException(javax.servlet.ServletException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HttpServletRequest(javax.servlet.http.HttpServletRequest) BaseMessage(com.pogeyan.cmis.api.BaseMessage) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException)

Example 2 with BaseMessage

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); }
		 */
}
Also used : LoginRequest(com.pogeyan.cmis.api.messages.LoginRequest) LoginResponse(com.pogeyan.cmis.api.messages.LoginResponse) ServletException(javax.servlet.ServletException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) BaseMessage(com.pogeyan.cmis.api.BaseMessage) LoggerFactory(org.slf4j.LoggerFactory) IUserObject(com.pogeyan.cmis.api.auth.IUserObject) StringUtils(org.apache.commons.lang3.StringUtils) CmisBaseResponse(com.pogeyan.cmis.api.messages.CmisBaseResponse) ActorSelection(akka.actor.ActorSelection) AsyncContext(javax.servlet.AsyncContext) HttpServletRequest(javax.servlet.http.HttpServletRequest) CmisErrorResponse(com.pogeyan.cmis.api.messages.CmisErrorResponse) Action(com.pogeyan.cmis.api.Action) ActorRef(akka.actor.ActorRef) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) GenericActor(com.pogeyan.cmis.api.GenericActor) QueryStringHttpServletRequestWrapper(com.pogeyan.cmis.browser.shared.QueryStringHttpServletRequestWrapper) UntypedActor(akka.actor.UntypedActor) Helpers(com.pogeyan.cmis.api.utils.Helpers) ContentStream(org.apache.chemistry.opencmis.commons.data.ContentStream) MimeHelper(org.apache.chemistry.opencmis.commons.impl.MimeHelper) OutputStream(java.io.OutputStream) ServletConfig(javax.servlet.ServletConfig) Logger(org.slf4j.Logger) HttpServlet(javax.servlet.http.HttpServlet) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) MetricsInputs(com.pogeyan.cmis.api.utils.MetricsInputs) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) QueryGetRequest(com.pogeyan.cmis.api.messages.QueryGetRequest) WebServlet(javax.servlet.annotation.WebServlet) PostFileResponse(com.pogeyan.cmis.api.messages.PostFileResponse) POSTHttpServletRequestWrapper(com.pogeyan.cmis.browser.shared.POSTHttpServletRequestWrapper) IOUtils(org.apache.chemistry.opencmis.commons.impl.IOUtils) ActorSystem(akka.actor.ActorSystem) HttpUtils(com.pogeyan.cmis.browser.shared.HttpUtils) MessageType(com.pogeyan.cmis.api.MessageType) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Props(akka.actor.Props) InputStream(java.io.InputStream) ExceptionUtils(org.apache.commons.lang3.exception.ExceptionUtils) LoginResponse(com.pogeyan.cmis.api.messages.LoginResponse) BaseMessage(com.pogeyan.cmis.api.BaseMessage) ActorRef(akka.actor.ActorRef) GenericActor(com.pogeyan.cmis.api.GenericActor) LoginRequest(com.pogeyan.cmis.api.messages.LoginRequest)

Example 3 with BaseMessage

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;
}
Also used : CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) PostRequest(com.pogeyan.cmis.api.messages.PostRequest) BaseMessage(com.pogeyan.cmis.api.BaseMessage) ControlParser(com.pogeyan.cmis.browser.shared.ControlParser) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) BaseTypeId(org.apache.chemistry.opencmis.commons.enums.BaseTypeId) CmisRequestParameter(com.pogeyan.cmis.browser.shared.CmisRequestParameter)

Example 4 with BaseMessage

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;
}
Also used : CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) QueryStringHttpServletRequestWrapper(com.pogeyan.cmis.browser.shared.QueryStringHttpServletRequestWrapper) BaseMessage(com.pogeyan.cmis.api.BaseMessage) POSTHttpServletRequestWrapper(com.pogeyan.cmis.browser.shared.POSTHttpServletRequestWrapper) ServletException(javax.servlet.ServletException) CmisPermissionDeniedException(org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException) CmisUnauthorizedException(org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException) CmisNotSupportedException(org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with BaseMessage

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;
}
Also used : BaseMessage(com.pogeyan.cmis.api.BaseMessage) ObjectData(org.apache.chemistry.opencmis.commons.data.ObjectData) BaseTypeId(org.apache.chemistry.opencmis.commons.enums.BaseTypeId) QueryGetRequest(com.pogeyan.cmis.api.messages.QueryGetRequest) 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)

Aggregations

BaseMessage (com.pogeyan.cmis.api.BaseMessage)6 CmisNotSupportedException (org.apache.chemistry.opencmis.commons.exceptions.CmisNotSupportedException)5 IOException (java.io.IOException)4 CmisPermissionDeniedException (org.apache.chemistry.opencmis.commons.exceptions.CmisPermissionDeniedException)4 ActorRef (akka.actor.ActorRef)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 ServletException (javax.servlet.ServletException)3 CmisUnauthorizedException (org.apache.chemistry.opencmis.commons.exceptions.CmisUnauthorizedException)3 ActorSystem (akka.actor.ActorSystem)2 IUserObject (com.pogeyan.cmis.api.auth.IUserObject)2 QueryGetRequest (com.pogeyan.cmis.api.messages.QueryGetRequest)2 POSTHttpServletRequestWrapper (com.pogeyan.cmis.browser.shared.POSTHttpServletRequestWrapper)2 QueryStringHttpServletRequestWrapper (com.pogeyan.cmis.browser.shared.QueryStringHttpServletRequestWrapper)2 AsyncContext (javax.servlet.AsyncContext)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 ObjectData (org.apache.chemistry.opencmis.commons.data.ObjectData)2 ActorSelection (akka.actor.ActorSelection)1 Props (akka.actor.Props)1 Terminated (akka.actor.Terminated)1