@Override public <R extends Response> void enqueueRequest(Request aRequest, Scripts.Space aSpace, Consumer<R> onSuccess, Consumer<Exception> onFailure) throws Exception { if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount();
Scripts.LocalContext context = new Scripts.LocalContext(session.getPrincipal(), session); session.getSpace().process(context, () -> { handler.handle(session, (Response aResponse) -> { if (session != null) { ioSession.setAttribute(SESSION_ID, session.getId()); Scripts.LocalContext context = new Scripts.LocalContext(session.getPrincipal(), session); session.getSpace().process(context, () -> { handler.handle(session, (Response aResponse) -> {
Scripts.LocalContext context = new Scripts.LocalContext(aHttpRequest, aHttpResponse, httpRequestPrincipal(aHttpRequest), aPlatypusSession); aPlatypusSession.getSpace().process(context, () -> { handler.handle(aPlatypusSession, (Response resp) -> { assert Scripts.getSpace() == aPlatypusSession.getSpace(); PlatypusHttpResponseWriter writer = new PlatypusHttpResponseWriter(aHttpResponse, aHttpRequest, aPlatypusSession.getSpace(), ((Principal) Scripts.getContext().getPrincipal()).getName(), aAsync); try { resp.accept(writer);
targetSpace = statelessSpaces[rnd]; } else {// Statefull session module targetSession = (Session) callingContext.getSession(); targetSpace = targetSession.getSpace(); Scripts.LocalContext targetContext = new Scripts.LocalContext(callingContext.getRequest(), callingContext.getResponse(), callingContext.getPrincipal(), callingContext.getSession()); targetSpace.process(targetContext, () -> { assert Scripts.getSpace() == targetSpace; Scripts.getContext().initAsyncs(0); try { ServerModuleStructureRequestHandler.checkPrincipalPermission(config.getPropertyAllowedRoles().get(aMethodName), aModuleName + "." + aMethodName); Object result = ((JSObject) oFun).call(moduleInstance, arguments.toArray()); int asyncs = Scripts.getContext().getAsyncsCount(); if (!(result instanceof Undefined) || asyncs == 0) { if (!executed.get()) { Scripts.getContext().initAsyncs(null);
Consumer<com.eas.server.Session> withPlatypusSession = (com.eas.server.Session aSession) -> { Scripts.LocalContext context = new Scripts.LocalContext(platypusPrincipal(handshake, websocketSession), aSession); aSession.getSpace().process(context, () -> {
PlatypusPrincipal principal = (PlatypusPrincipal)Scripts.getContext().getPrincipal(); if (rolesAllowed != null && !principal.hasAnyRole(rolesAllowed)) { throw new AccessControlException(String.format(ACCESS_DENIED_MSG, query.getEntityName(), principal.getName()), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null);
((LocalQueriesProxy) serverCore.getQueries()).getQuery(change.entityName, Scripts.getSpace(), (SqlQuery aQuery) -> { if (aQuery.isPublicAccess()) { AccessControlException aex = checkWritePrincipalPermission((PlatypusPrincipal) Scripts.getContext().getPrincipal(), change.entityName, aQuery.getWriteRoles()); if (aex != null) { process.complete(null, null, aex, null);
PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (rolesAllowed != null && !principal.hasAnyRole(rolesAllowed)) { throw new AccessControlException(String.format(ACCESS_DENIED_MSG, query.getEntityName(), principal.getName()), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null);
public void send(String from, String to, String subject, String body, JSObject aOnSuccess, JSObject aOnFailure) throws NamingException, AddressException, MessagingException { InitialContext initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup(lookup); Session session = (Session) envCtx.lookup(sessionName); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(MimeMessage.RecipientType.TO, to); message.setSubject(subject); message.setContent(body, contentType); Scripts.Space callingSpace = Scripts.getSpace(); Scripts.LocalContext callingContext = Scripts.getContext(); callingContext.incAsyncsCount(); Scripts.startBIO(() -> { try { Transport.send(message); if (aOnSuccess != null) { callingSpace.process(callingContext, () -> { aOnSuccess.call(null, new Object[]{}); }); } } catch (MessagingException ex) { Logger.getLogger(Messages.class.getName()).log(Level.SEVERE, null, ex); if (aOnFailure != null) { callingSpace.process(callingContext, () -> { aOnFailure.call(null, new Object[]{ex}); }); } } }); }
/** * Checks module roles. * * @param anAllowedRoles * @param aSubjectName * @throws AccessControlException */ public static void checkPrincipalPermission(Set<String> anAllowedRoles, String aSubjectName) throws AccessControlException { if (anAllowedRoles != null && !anAllowedRoles.isEmpty()) { try { PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (principal == null || !principal.hasAnyRole(anAllowedRoles)) { throw new AccessControlException(String.format("Access denied to %s for '%s'.",//NOI18N aSubjectName, principal != null ? principal.getName() : null), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null); } } catch (Exception ex) { if (ex instanceof AccessControlException) { throw ex; } else { throw new AccessControlException(ex.getMessage()); } } } } }
@Override public <R extends Response> void enqueueRequest(Request aRequest, Scripts.Space aSpace, Consumer<R> onSuccess, Consumer<Exception> onFailure) { if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } Attempts attemps = new Attempts(); Consumer<Response> responseHandler = (Response response) -> { if (response instanceof ExceptionResponse) { if (onFailure != null) { Exception cause = handleErrorResponse((ExceptionResponse) response, aSpace); onFailure.accept(cause); } } else if (onSuccess != null) { if (aRequest instanceof LogoutRequest) { credentials = null; sessionTicket = null; } onSuccess.accept((R) response); } }; retry(aRequest, aSpace, responseHandler, attemps); }
@Override public Object received(Packet aPacket) throws Exception { Session session = serverCore.getSessionManager().getSystemSession(); Scripts.LocalContext context = new Scripts.LocalContext(session.getPrincipal(), session); session.getSpace().process(context, () -> { serverCore.executeMethod(moduleName, RECIEVER_METHOD_NAME, new Object[]{aPacket}, true, (Object result) -> { if (result != null) { assert result instanceof String; assert sender != null; sender.send(aPacket, (String) result); } }, (Exception ex) -> { Logger.getLogger(PositioningPacketReciever.class.getName()).log(Level.WARNING, null, ex); }); }); return null; } }
@Override public Object callServerModuleMethod(String aModuleName, String aMethodName, Scripts.Space aSpace, JSObject onSuccess, JSObject onFailure, Object... aArguments) throws Exception { if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } assert Scripts.getSpace() == aSpace; serverCore.executeMethod(aModuleName, aMethodName, aArguments, false, onSuccess != null ? (Object aResult) -> { onSuccess.call(null, new Object[]{aSpace.toJs(aResult)}); } : null, (Exception ex) -> { if (onFailure != null) { onFailure.call(null, new Object[]{ex instanceof JsObjectException ? ((JsObjectException) ex).getData() : ex.toString()}); } else { Logger.getLogger(LocalServerModulesProxy.class.getName()).log(Level.SEVERE, ex.toString()); } }); return null;// Only asynchronous form is allowed. }
public static void startBIO(Runnable aBioTask) { LocalContext context = getContext(); if (context != null) { context.incAsyncsCount(); } bio.submit(() -> { setContext(context); try { aBioTask.run(); } finally { setContext(null); } }); }
private void startJdbcTask(Runnable aTask) { Scripts.LocalContext context = Scripts.getContext(); if (context != null) { context.incAsyncsCount(); } jdbcProcessor.submit(() -> { Scripts.setContext(context); try { aTask.run(); } finally { Scripts.setContext(null); } }); }
private void checkModuleRoles(String aModuleName) throws Exception { ScriptDocument.ModuleDocument moduleDoc = serverCore.lookupModuleDocument(aModuleName); if (moduleDoc != null) { Set<String> rolesAllowed = moduleDoc.getAllowedRoles(); PlatypusPrincipal principal = (PlatypusPrincipal) Scripts.getContext().getPrincipal(); if (rolesAllowed != null && !principal.hasAnyRole(rolesAllowed)) { throw new AccessControlException(String.format(ACCESS_DENIED_MSG, aModuleName, getRequest().getModuleName(), principal.getName()), principal instanceof AnonymousPlatypusPrincipal ? new AuthPermission("*") : null); } } } }
@Override public void handle(Session aSession, Consumer<CredentialRequest.Response> onSuccess, Consumer<Exception> onFailure) { if (onSuccess != null) { onSuccess.accept(new CredentialRequest.Response(((PlatypusPrincipal) Scripts.getContext().getPrincipal()).getName())); } } }
public static void offerTask(Runnable aTask) { assert tasks != null : "Scripts tasks are not initialized"; if (Scripts.getContext() != null) { Scripts.getContext().incAsyncsCount(); } tasks.accept(aTask); }
@Override public String preparationContext() throws Exception { Scripts.LocalContext context = Scripts.getContext(); if (context != null && context.getPrincipal() != null) { return ((PlatypusPrincipal) context.getPrincipal()).getContext(); } else { return null; } }