Search in sources :

Example 1 with IApplicationServer

use of com.servoy.j2db.server.shared.IApplicationServer in project servoy-client by Servoy.

the class HeadlessClientFactoryInternal method createImportHookClient.

public static ISessionClient createImportHookClient(final Solution importHookModule, final IXMLImportUserChannel channel) throws Exception {
    final String[] loadException = new String[1];
    // assuming no login and no method args for import hooks
    SessionClient sc = new SessionClient(null, null, null, null, null, importHookModule.getName()) {

        @Override
        protected IActiveSolutionHandler createActiveSolutionHandler() {
            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
            return new LocalActiveSolutionHandler(as, this) {

                @Override
                protected Solution loadSolution(RootObjectMetaData solutionDef) throws RemoteException, RepositoryException {
                    // grab the latest version (-1) not the active one, because the hook was not yet activated.
                    return (Solution) ((IDeveloperRepository) getRepository()).getRootObject(solutionDef.getRootObjectId(), -1);
                }
            };
        }

        @Override
        protected IExecutingEnviroment createScriptEngine() {
            return new ScriptEngine(this) {

                @Override
                public Object executeFunction(Function f, Scriptable scope, Scriptable thisObject, Object[] args, boolean focusEvent, boolean throwException) throws Exception {
                    // always throw exception
                    return super.executeFunction(f, scope, thisObject, args, focusEvent, true);
                }
            };
        }

        @Override
        public void reportError(String msg, Object detail) {
            super.reportError(msg, detail);
            loadException[0] = msg;
            if (detail instanceof JavaScriptException && ((JavaScriptException) detail).getValue() instanceof Scriptable) {
                loadException[0] += " " + Utils.getScriptableString((Scriptable) ((JavaScriptException) detail).getValue());
            }
            if (detail instanceof Exception) {
                loadException[0] += " " + ((Exception) detail).getMessage();
            }
        }
    };
    sc.setUseLoginSolution(false);
    String userName = channel.getImporterUsername();
    if (userName != null) {
        // let the import hook client run with credentials from the logged in user from the admin page.
        sc.getClientInfo().setUserUid(ApplicationServerRegistry.get().getUserManager().getUserUID(sc.getClientID(), userName));
        sc.getClientInfo().setUserName(userName);
    }
    sc.setOutputChannel(channel);
    sc.loadSolution(importHookModule.getName());
    if (loadException[0] != null) {
        sc.shutDown(true);
        throw new RepositoryException(loadException[0]);
    }
    return sc;
}
Also used : RootObjectMetaData(com.servoy.j2db.persistence.RootObjectMetaData) ISessionClient(com.servoy.j2db.ISessionClient) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) LocalActiveSolutionHandler(com.servoy.j2db.LocalActiveSolutionHandler) Scriptable(org.mozilla.javascript.Scriptable) ScriptEngine(com.servoy.j2db.scripting.ScriptEngine) JavaScriptException(org.mozilla.javascript.JavaScriptException) RemoteException(java.rmi.RemoteException) RepositoryException(com.servoy.j2db.persistence.RepositoryException) JavaScriptException(org.mozilla.javascript.JavaScriptException) Function(org.mozilla.javascript.Function) Solution(com.servoy.j2db.persistence.Solution)

Example 2 with IApplicationServer

use of com.servoy.j2db.server.shared.IApplicationServer in project servoy-client by Servoy.

the class TemplateGenerator method getFormHTMLAndCSS.

public static Pair<String, String> getFormHTMLAndCSS(Solution solution, Form form, IServiceProvider sp, String formInstanceName) throws RepositoryException, RemoteException {
    if (form == null)
        return null;
    final IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
    boolean enableAnchoring = sp != null ? Utils.getAsBoolean(sp.getRuntimeProperties().get("enableAnchors")) : Utils.getAsBoolean(Settings.getInstance().getProperty("servoy.webclient.enableAnchors", Boolean.TRUE.toString()));
    String overriddenStyleName = null;
    Pair<String, ArrayList<Pair<String, String>>> retval = formCache.getFormAndCssPair(form, formInstanceName, overriddenStyleName, repository);
    Form f = form;
    FlattenedSolution fsToClose = null;
    try {
        if (retval == null) {
            if (f.getExtendsID() > 0) {
                FlattenedSolution fs = sp == null ? null : sp.getFlattenedSolution();
                if (fs == null) {
                    try {
                        IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                        fsToClose = fs = new FlattenedSolution(solution.getSolutionMetaData(), new AbstractActiveSolutionHandler(as) {

                            @Override
                            public IRepository getRepository() {
                                return repository;
                            }
                        });
                    } catch (RepositoryException e) {
                        Debug.error("Couldn't create flattened form for the template generator", e);
                    }
                }
                f = fs.getFlattenedForm(f);
                if (f == null) {
                    Debug.log("TemplateGenerator couldn't get a FlattenedForm for " + form + ", solution closed?");
                    f = form;
                }
            }
            StringBuffer html = new StringBuffer();
            TextualCSS css = new TextualCSS();
            IFormLayoutProvider layoutProvider = FormLayoutProviderFactory.getFormLayoutProvider(sp, solution, f, formInstanceName);
            int viewType = layoutProvider.getViewType();
            layoutProvider.renderOpenFormHTML(html, css);
            int startY = 0;
            Iterator<Part> parts = f.getParts();
            while (parts.hasNext()) {
                Part part = parts.next();
                int endY = part.getHeight();
                if (Part.rendersOnlyInPrint(part.getPartType())) {
                    startY = part.getHeight();
                    // is never shown (=printing only)
                    continue;
                }
                Color bgColor = ComponentFactory.getPartBackground(sp, part, f);
                if (part.getPartType() == Part.BODY && (viewType == FormController.TABLE_VIEW || viewType == FormController.LOCKED_TABLE_VIEW || viewType == IForm.LIST_VIEW || viewType == FormController.LOCKED_LIST_VIEW)) {
                    layoutProvider.renderOpenTableViewHTML(html, css, part);
                    // tableview == bodypart
                    createCellBasedView(f, f, html, css, layoutProvider.needsHeaders(), startY, endY, bgColor, sp, viewType, enableAnchoring, startY, endY);
                    layoutProvider.renderCloseTableViewHTML(html);
                } else {
                    layoutProvider.renderOpenPartHTML(html, css, part);
                    placePartElements(f, startY, endY, html, css, bgColor, enableAnchoring, sp);
                    layoutProvider.renderClosePartHTML(html, part);
                }
                startY = part.getHeight();
            }
            layoutProvider.renderCloseFormHTML(html);
            retval = new Pair<String, ArrayList<Pair<String, String>>>(html.toString(), css.getAsSelectorValuePairs());
            formCache.putFormAndCssPair(form, formInstanceName, overriddenStyleName, repository, retval);
        }
        Map<String, String> formIDToMarkupIDMap = null;
        if (sp instanceof IApplication) {
            Map runtimeProps = sp.getRuntimeProperties();
            Map<WebForm, Map<String, String>> clientFormsIDToMarkupIDMap = (Map<WebForm, Map<String, String>>) runtimeProps.get("WebFormIDToMarkupIDCache");
            if (clientFormsIDToMarkupIDMap == null) {
                clientFormsIDToMarkupIDMap = new WeakHashMap<WebForm, Map<String, String>>();
                runtimeProps.put("WebFormIDToMarkupIDCache", clientFormsIDToMarkupIDMap);
            }
            IForm wfc = ((IApplication) sp).getFormManager().getForm(formInstanceName);
            if (wfc instanceof FormController) {
                IFormUIInternal wf = ((FormController) wfc).getFormUI();
                if (wf instanceof WebForm) {
                    if (!((WebForm) wf).isUIRecreated())
                        formIDToMarkupIDMap = clientFormsIDToMarkupIDMap.get(wf);
                    if (formIDToMarkupIDMap == null) {
                        ArrayList<Pair<String, String>> formCSS = retval.getRight();
                        ArrayList<String> selectors = new ArrayList<String>(formCSS.size());
                        for (Pair<String, String> formCSSEntry : formCSS) selectors.add(formCSSEntry.getLeft());
                        formIDToMarkupIDMap = getWebFormIDToMarkupIDMap((WebForm) wf, selectors);
                        clientFormsIDToMarkupIDMap.put((WebForm) wf, formIDToMarkupIDMap);
                    }
                }
            }
        }
        String webFormCSS = getWebFormCSS(retval.getRight(), formIDToMarkupIDMap);
        // string the formcss/solutionname/ out of the url.
        webFormCSS = StripHTMLTagsConverter.convertMediaReferences(webFormCSS, solution.getName(), new ResourceReference("media"), "", false).toString();
        return new Pair<String, String>(retval.getLeft(), webFormCSS);
    } finally {
        if (fsToClose != null) {
            fsToClose.close(null);
        }
    }
}
Also used : IForm(com.servoy.j2db.IForm) Form(com.servoy.j2db.persistence.Form) WebForm(com.servoy.j2db.server.headlessclient.WebForm) WebForm(com.servoy.j2db.server.headlessclient.WebForm) ArrayList(java.util.ArrayList) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IForm(com.servoy.j2db.IForm) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) ResourceReference(org.apache.wicket.ResourceReference) IFormUIInternal(com.servoy.j2db.IFormUIInternal) Pair(com.servoy.j2db.util.Pair) FormController(com.servoy.j2db.FormController) Color(java.awt.Color) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) RepositoryException(com.servoy.j2db.persistence.RepositoryException) Point(java.awt.Point) IApplication(com.servoy.j2db.IApplication) Part(com.servoy.j2db.persistence.Part) IRepository(com.servoy.j2db.persistence.IRepository) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) TreeMap(java.util.TreeMap)

Example 3 with IApplicationServer

use of com.servoy.j2db.server.shared.IApplicationServer in project servoy-client by Servoy.

the class SharedMediaResource method getResource.

private ResourceState getResource(final String iconId, final String solutionName) {
    return new ResourceState() {

        private String contentType;

        private int length;

        byte[] array = null;

        @Override
        public Time lastModifiedTime() {
            try {
                IRootObject solution = ApplicationServerRegistry.get().getLocalRepository().getActiveRootObject(solutionName, IRepository.SOLUTIONS);
                if (solution != null)
                    return Time.valueOf(solution.getLastModifiedTime());
            } catch (Exception e) {
                Debug.trace(e);
            }
            return time;
        }

        @Override
        public byte[] getData() {
            if (array == null) {
                boolean closeFS = false;
                try {
                    final IRepository repository = ApplicationServerRegistry.get().getLocalRepository();
                    FlattenedSolution fs = null;
                    try {
                        if (Session.exists() && ((WebClientSession) Session.get()).getWebClient() != null) {
                            fs = ((WebClientSession) Session.get()).getWebClient().getFlattenedSolution();
                        }
                        if (fs == null) {
                            SolutionMetaData solutionMetaData = (SolutionMetaData) repository.getRootObjectMetaData(solutionName, IRepository.SOLUTIONS);
                            if (solutionMetaData == null)
                                return new byte[0];
                            closeFS = true;
                            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                            fs = new FlattenedSolution(solutionMetaData, new AbstractActiveSolutionHandler(as) {

                                @Override
                                public IRepository getRepository() {
                                    return repository;
                                }
                            });
                        }
                        Media m = fs.getMedia(iconId);
                        if (m == null) {
                            try {
                                Integer iIconID = new Integer(iconId);
                                m = fs.getMedia(iIconID.intValue());
                            } catch (NumberFormatException ex) {
                                Debug.error("no media found for: " + iconId);
                            }
                        }
                        if (m != null) {
                            array = m.getMediaData();
                            contentType = m.getMimeType();
                        }
                    } finally {
                        if (closeFS && fs != null) {
                            fs.close(null);
                        }
                    }
                    if (array != null) {
                        if (contentType == null) {
                            contentType = MimeTypes.getContentType(array);
                        }
                        length = array.length;
                    }
                } catch (Exception ex) {
                    Debug.error(ex);
                }
            }
            return array == null ? new byte[0] : array;
        }

        /**
         * @see wicket.markup.html.DynamicWebResource.ResourceState#getLength()
         */
        @Override
        public int getLength() {
            return length;
        }

        @Override
        public String getContentType() {
            return contentType;
        }
    };
}
Also used : Media(com.servoy.j2db.persistence.Media) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) IRootObject(com.servoy.j2db.persistence.IRootObject) StringValueConversionException(org.apache.wicket.util.string.StringValueConversionException) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) IRepository(com.servoy.j2db.persistence.IRepository)

Example 4 with IApplicationServer

use of com.servoy.j2db.server.shared.IApplicationServer in project servoy-client by Servoy.

the class FormTemplateGenerator method isSingleValueComponent.

/**
 * @return false if the persist has no valuelist or at most one value in the valuelist, true otherwise
 */
public static boolean isSingleValueComponent(IFormElement persist) {
    Field field = (Field) persist;
    if (field.getValuelistID() > 0) {
        try {
            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
            FlattenedSolution fs = new FlattenedSolution((SolutionMetaData) ApplicationServerRegistry.get().getLocalRepository().getRootObjectMetaData(((Solution) persist.getRootObject()).getName(), IRepository.SOLUTIONS), new AbstractActiveSolutionHandler(as) {

                @Override
                public IRepository getRepository() {
                    return ApplicationServerRegistry.get().getLocalRepository();
                }
            });
            ValueList valuelist = fs.getValueList(field.getValuelistID());
            return isSingleValue(valuelist);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    return true;
}
Also used : Field(com.servoy.j2db.persistence.Field) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) ValueList(com.servoy.j2db.persistence.ValueList) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IRepository(com.servoy.j2db.persistence.IRepository) TemplateException(freemarker.template.TemplateException) IOException(java.io.IOException)

Example 5 with IApplicationServer

use of com.servoy.j2db.server.shared.IApplicationServer in project servoy-client by Servoy.

the class NGClientEntryFilter method doFilter.

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    try {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        if ("GET".equalsIgnoreCase(request.getMethod())) {
            if (request.getCharacterEncoding() == null)
                request.setCharacterEncoding("UTF8");
            String uri = request.getRequestURI();
            if (handleShortSolutionRequest(request, response)) {
                return;
            }
            if (handleDeeplink(request, response)) {
                return;
            }
            if (handleRecording(request, response)) {
                return;
            }
            if (uri != null && (uri.endsWith(".html") || uri.endsWith(".js"))) {
                String solutionName = getSolutionNameFromURI(uri);
                if (solutionName != null) {
                    String clientnr = AngularIndexPageWriter.getClientNr(uri, request);
                    INGClientWebsocketSession wsSession = null;
                    HttpSession httpSession = request.getSession(false);
                    if (clientnr != null && httpSession != null) {
                        wsSession = (INGClientWebsocketSession) WebsocketSessionManager.getSession(CLIENT_ENDPOINT, httpSession, Integer.parseInt(clientnr));
                    }
                    FlattenedSolution fs = null;
                    boolean closeFS = false;
                    if (wsSession != null) {
                        fs = wsSession.getClient().getFlattenedSolution();
                    }
                    if (fs == null) {
                        try {
                            closeFS = true;
                            IApplicationServer as = ApplicationServerRegistry.getService(IApplicationServer.class);
                            if (AngularIndexPageWriter.applicationServerUnavailable(response, as)) {
                                return;
                            }
                            SolutionMetaData solutionMetaData = (SolutionMetaData) ApplicationServerRegistry.get().getLocalRepository().getRootObjectMetaData(solutionName, SOLUTIONS);
                            if (AngularIndexPageWriter.solutionMissing(response, solutionName, solutionMetaData)) {
                                return;
                            }
                            fs = new FlattenedSolution(solutionMetaData, new AbstractActiveSolutionHandler(as) {

                                @Override
                                public IRepository getRepository() {
                                    return ApplicationServerRegistry.get().getLocalRepository();
                                }
                            });
                        } catch (Exception e) {
                            Debug.error("error loading solution: " + solutionName + " for clientnr: " + clientnr, e);
                        }
                    }
                    if (fs != null) {
                        try {
                            if (handleMainJs(request, response, fs)) {
                                return;
                            }
                            if (handleForm(request, response, wsSession, fs)) {
                                return;
                            }
                            if (AngularIndexPageWriter.handleMaintenanceMode(request, response, wsSession)) {
                                return;
                            }
                            // prepare for possible index.html lookup
                            Map<String, Object> variableSubstitution = getSubstitutions(request, solutionName, clientnr, fs);
                            List<String> extraMeta = new ArrayList<String>();
                            addManifest(fs, extraMeta);
                            addHeadIndexContributions(fs, extraMeta);
                            ContentSecurityPolicyConfig contentSecurityPolicyConfig = addcontentSecurityPolicyHeader(request, response, true);
                            super.doFilter(servletRequest, servletResponse, filterChain, asList(SERVOY_CSS), new ArrayList<String>(getFormScriptReferences(fs)), extraMeta, variableSubstitution, contentSecurityPolicyConfig == null ? null : contentSecurityPolicyConfig.getNonce());
                            return;
                        } finally {
                            if (closeFS) {
                                fs.close(null);
                            }
                        }
                    }
                }
            }
            if (!uri.contains(ModifiablePropertiesGenerator.PUSH_TO_SERVER_BINDINGS_LIST))
                Debug.log("No solution found for this request, calling the default filter: " + uri);
        }
        super.doFilter(servletRequest, servletResponse, filterChain, null, null, null, null, null);
    } catch (RuntimeException | Error e) {
        Debug.error(e);
        throw e;
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) ArrayList(java.util.ArrayList) HttpServletResponse(javax.servlet.http.HttpServletResponse) FlattenedSolution(com.servoy.j2db.FlattenedSolution) IApplicationServer(com.servoy.j2db.server.shared.IApplicationServer) SolutionMetaData(com.servoy.j2db.persistence.SolutionMetaData) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ContentSecurityPolicyConfig(org.sablo.security.ContentSecurityPolicyConfig) HttpServletRequest(javax.servlet.http.HttpServletRequest) AbstractActiveSolutionHandler(com.servoy.j2db.AbstractActiveSolutionHandler) JSONObject(org.json.JSONObject)

Aggregations

IApplicationServer (com.servoy.j2db.server.shared.IApplicationServer)7 AbstractActiveSolutionHandler (com.servoy.j2db.AbstractActiveSolutionHandler)6 FlattenedSolution (com.servoy.j2db.FlattenedSolution)6 SolutionMetaData (com.servoy.j2db.persistence.SolutionMetaData)4 IRepository (com.servoy.j2db.persistence.IRepository)3 RepositoryException (com.servoy.j2db.persistence.RepositoryException)3 IOException (java.io.IOException)3 Pair (com.servoy.j2db.util.Pair)2 ArrayList (java.util.ArrayList)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 FormController (com.servoy.j2db.FormController)1 IApplication (com.servoy.j2db.IApplication)1 IForm (com.servoy.j2db.IForm)1 IFormUIInternal (com.servoy.j2db.IFormUIInternal)1 ISessionClient (com.servoy.j2db.ISessionClient)1 LocalActiveSolutionHandler (com.servoy.j2db.LocalActiveSolutionHandler)1 Field (com.servoy.j2db.persistence.Field)1 Form (com.servoy.j2db.persistence.Form)1 IRootObject (com.servoy.j2db.persistence.IRootObject)1