Search in sources :

Example 91 with Project

use of com.twinsoft.convertigo.beans.core.Project in project convertigo by convertigo.

the class FullSyncServlet method service.

@Override
protected void service(final HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    StringBuffer debug = new StringBuffer();
    HttpMethodType method;
    try {
        HttpUtils.checkCV(request);
        String corsOrigin = HttpUtils.applyCorsHeaders(request, response);
        if (corsOrigin != null) {
            debug.append("Added CORS header for: " + corsOrigin + "\n");
        }
        method = HttpMethodType.valueOf(request.getMethod());
        if (method == HttpMethodType.OPTIONS) {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            return;
        }
        if (method != HttpMethodType.HEAD) {
            HttpSessionListener.checkSession(request);
        }
    } catch (Throwable e) {
        throw new ServletException(e);
    }
    HttpSession httpSession = request.getSession();
    try {
        FullSyncClient fsClient = Engine.theApp.couchDbManager.getFullSyncClient();
        RequestParser requestParser = new RequestParser(request, fsClient.getPrefix());
        boolean isUtilsSession = "true".equals(httpSession.getAttribute("__isUtilsSession"));
        boolean isUtilsRequest = false;
        String referer = request.getHeader("Referer");
        if (isUtilsSession || (referer != null && referer.endsWith("/admin/_utils/"))) {
            Engine.authenticatedSessionManager.checkRoles(httpSession, Role.WEB_ADMIN, Role.FULLSYNC_CONFIG, Role.FULLSYNC_VIEW);
            httpSession.setAttribute("__isUtilsSession", "true");
            isUtilsRequest = !"_all_dbs".equals(requestParser.getSpecial());
        } else {
            Engine.theApp.couchDbManager.checkRequest(requestParser.getPath(), requestParser.getSpecial(), requestParser.getDocId());
        }
        synchronized (httpSession) {
            Set<HttpServletRequest> set = SessionAttribute.fullSyncRequests.get(httpSession);
            if (set == null) {
                SessionAttribute.fullSyncRequests.set(httpSession, set = new HashSet<HttpServletRequest>());
            }
            set.add(request);
        }
        LogParameters logParameters = GenericUtils.cast(httpSession.getAttribute(FullSyncServlet.class.getCanonicalName()));
        if (logParameters == null) {
            httpSession.setAttribute(FullSyncServlet.class.getCanonicalName(), logParameters = new LogParameters());
            logParameters.put(mdcKeys.ContextID.toString().toLowerCase(), httpSession.getId());
        }
        Log4jHelper.mdcSet(logParameters);
        logParameters.put(mdcKeys.ClientIP.toString().toLowerCase(), request.getRemoteAddr());
        if (EnginePropertiesManager.getProperty(PropertyName.NET_REVERSE_DNS).equalsIgnoreCase("true")) {
            Log4jHelper.mdcPut(mdcKeys.ClientHostName, request.getRemoteHost());
        }
        String dbName = requestParser.getDbName();
        FullSyncAuthentication fsAuth = Engine.theApp.couchDbManager.getFullSyncAuthentication(request.getSession());
        if (fsAuth == null) {
            Log4jHelper.mdcPut(mdcKeys.User, "(anonymous)");
            debug.append("Anonymous user\n");
            Boolean allowAnonymous = null;
            for (String projectName : Engine.theApp.databaseObjectsManager.getAllProjectNamesList()) {
                try {
                    Project project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName);
                    for (Connector connector : project.getConnectorsList()) {
                        if (connector instanceof FullSyncConnector) {
                            FullSyncConnector fullSyncConnector = (FullSyncConnector) connector;
                            if (fullSyncConnector.getDatabaseName().equals(dbName)) {
                                allowAnonymous = fullSyncConnector.getAnonymousReplication() == FullSyncAnonymousReplication.allow;
                                break;
                            }
                        }
                    }
                    if (allowAnonymous != null) {
                        break;
                    }
                } catch (Exception e) {
                // TODO: handle exception
                }
            }
            if (allowAnonymous == Boolean.FALSE) {
                throw new SecurityException("The '" + dbName + "' database deny pull synchronization for an anonymous session");
            }
        } else {
            Log4jHelper.mdcPut(mdcKeys.User, "'" + fsAuth.getAuthenticatedUser() + "'");
            debug.append("Authenticated user: ").append(fsAuth.getAuthenticatedUser()).append('\n').append("Authenticated groups: ").append(fsAuth.getGroups()).append('\n');
        }
        String url = Engine.theApp.couchDbManager.getFullSyncUrl() + requestParser.getPath();
        URIBuilder builder = new URIBuilder(url);
        String query = request.getQueryString();
        if (query != null) {
            try {
                // needed for PouchDB replication
                URI uri = URI.create(url + "?" + request.getQueryString());
                query = uri.getQuery();
            } catch (Exception e) {
                debug.append("parse query failed (" + e.getMessage() + "), use as it; query=" + query + "\n");
            }
            builder.setCustomQuery(query);
        }
        String special = requestParser.getSpecial();
        boolean isChanges = "_changes".equals(special);
        String version = fsClient.getServerVersion();
        boolean isCouchDB = "CouchDB".equals(fsClient.getServerName());
        if (isChanges && version.compareTo("2.") >= 0 && isCouchDB) {
            method = HttpMethodType.POST;
        }
        debug.append("dbName=" + dbName + " special=" + special + " couchdb=" + version + (requestParser.hasAttachment() ? " attachment=true" : "") + "\n");
        HttpRequestBase newRequest;
        switch(method) {
            case DELETE:
                if (isUtilsRequest) {
                    Engine.authenticatedSessionManager.checkRoles(httpSession, Role.WEB_ADMIN, Role.FULLSYNC_CONFIG);
                    if (requestParser.getDocId() == null && StringUtils.isNotBlank(requestParser.getDbName()) && DelegateServlet.canDelegate()) {
                        JSONObject instruction = new JSONObject();
                        JSONObject variables = new JSONObject();
                        try {
                            instruction.put("action", "deleteDatabase");
                            variables.put("db", fsClient.getPrefix() + requestParser.getDbName());
                            instruction.put("variables", variables);
                            JSONObject deleteResponse = DelegateServlet.delegate(instruction);
                            if (deleteResponse != null) {
                                JSONObject meta = CouchKey._c8oMeta.JSONObject(deleteResponse);
                                CouchKey._c8oMeta.remove(deleteResponse);
                                response.setStatus(meta.getInt("statusCode"));
                                JSONObject headers = meta.getJSONObject("headers");
                                for (java.util.Iterator<?> i = headers.keys(); i.hasNext(); ) {
                                    String key = (String) i.next();
                                    response.addHeader(key, headers.getString(key));
                                }
                                response.setCharacterEncoding("UTF-8");
                                try (Writer w = response.getWriter()) {
                                    w.write(deleteResponse.toString());
                                }
                                return;
                            }
                        } catch (Exception e) {
                        }
                    }
                    newRequest = new HttpDelete();
                } else {
                    // disabled to prevent db delete
                    throw new ServletException("Invalid HTTP method");
                }
                break;
            case GET:
                newRequest = new HttpGet();
                break;
            case HEAD:
                newRequest = new HttpHead();
                break;
            case OPTIONS:
                newRequest = new HttpOptions();
                break;
            case POST:
                if (isUtilsRequest) {
                    Engine.authenticatedSessionManager.checkRoles(httpSession, Role.WEB_ADMIN, Role.FULLSYNC_CONFIG);
                }
                newRequest = new HttpPost();
                break;
            case PUT:
                if (isUtilsRequest) {
                    Engine.authenticatedSessionManager.checkRoles(httpSession, Role.WEB_ADMIN, Role.FULLSYNC_CONFIG);
                }
                newRequest = new HttpPut();
                break;
            case TRACE:
                newRequest = new HttpTrace();
                break;
            default:
                throw new ServletException("Invalid HTTP method");
        }
        if (method.equals(HttpMethodType.POST) && "_bulk_docs".equals(special)) {
            int n = fsClient.getN();
            if (n > 1) {
                for (NameValuePair kv : builder.getQueryParams()) {
                    if ("w".equals(kv.getName())) {
                        n = 0;
                        break;
                    }
                }
                if (n > 1) {
                    builder.addParameter("w", Integer.toString(n));
                }
            }
        }
        URI uri = builder.build();
        newRequest.setURI(uri);
        debug.append(method.name() + " URI: " + uri.toString() + "\n");
        String requestStringEntity = null;
        HttpEntity httpEntity = null;
        JSONObject bulkDocsRequest = null;
        boolean isCBL = false;
        boolean isCBLiOS = false;
        {
            String agent = HeaderName.UserAgent.getHeader(request);
            isCBL = agent != null && agent.startsWith("CouchbaseLite/1.");
            if (isCBL) {
                isCBLiOS = agent.contains("iOS");
                isCBL = version != null && version.compareTo("1.7") >= 0;
            }
        }
        for (String headerName : Collections.list(request.getHeaderNames())) {
            if (!(HeaderName.TransferEncoding.is(headerName) || HeaderName.ContentLength.is(headerName) || HeaderName.UserAgent.is(headerName) || HeaderName.Expect.is(headerName) || HeaderName.Connection.is(headerName) || HeaderName.Host.is(headerName) || HeaderName.Cookie.is(headerName) || HeaderName.ContentEncoding.is(headerName) || HeaderName.Origin.is(headerName) || (isChanges && (HeaderName.IfNoneMatch.is(headerName) || HeaderName.IfModifiedSince.is(headerName) || HeaderName.CacheControl.is(headerName) || HeaderName.AcceptEncoding.is(headerName))))) {
                for (String headerValue : Collections.list(request.getHeaders(headerName))) {
                    debug.append("request Header: " + headerName + "=" + headerValue + "\n");
                    newRequest.addHeader(headerName, headerValue);
                }
            } else {
                debug.append("skip request Header: " + headerName + "=" + request.getHeader(headerName) + "\n");
            }
        }
        {
            Header authBasicHeader = fsClient.getAuthBasicHeader();
            if (authBasicHeader != null) {
                debug.append("request add BasicHeader");
                newRequest.addHeader(authBasicHeader);
            }
        }
        if (request.getInputStream() != null) {
            String reqContentType = request.getContentType();
            if (reqContentType != null && reqContentType.startsWith("multipart/related;")) {
                final MimeMultipart mp = new MimeMultipart(new ByteArrayDataSource(request.getInputStream(), reqContentType));
                final long[] size = { request.getIntHeader(HeaderName.ContentLength.value()) };
                final boolean chunked = size[0] == -1;
                int count = mp.getCount();
                debug.append("handle multipart/related: " + reqContentType + "; " + count + " parts; original size of " + size[0]);
                final File mpTmp;
                if (chunked) {
                    mpTmp = File.createTempFile("c8o", "mpTmp");
                    mpTmp.deleteOnExit();
                } else {
                    mpTmp = null;
                }
                try {
                    bulkDocsRequest = new JSONObject();
                    JSONArray bulkDocsArray = new JSONArray();
                    CouchKey.docs.put(bulkDocsRequest, bulkDocsArray);
                    for (int i = 0; i < count; i++) {
                        BodyPart part = mp.getBodyPart(i);
                        ContentTypeDecoder contentType = new ContentTypeDecoder(part.getContentType());
                        if (contentType.mimeType() == MimeType.Json) {
                            String charset = contentType.getCharset("UTF-8");
                            List<javax.mail.Header> headers = Collections.list(GenericUtils.<Enumeration<javax.mail.Header>>cast(part.getAllHeaders()));
                            byte[] buf = IOUtils.toByteArray(part.getInputStream());
                            if (!chunked) {
                                size[0] -= buf.length;
                            }
                            String json = new String(buf, charset);
                            try {
                                JSONObject docRequest = new JSONObject(json);
                                Engine.theApp.couchDbManager.handleDocRequest(dbName, docRequest, fsAuth);
                                bulkDocsArray.put(docRequest);
                                json = docRequest.toString();
                            } catch (JSONException e) {
                                debug.append("failed to parse [ " + e.getMessage() + "]: " + json);
                            }
                            part.setContent(buf = json.getBytes(charset), part.getContentType());
                            if (!chunked) {
                                size[0] += buf.length;
                            }
                            for (javax.mail.Header header : headers) {
                                String name = header.getName();
                                if (HeaderName.ContentLength.is(name)) {
                                    part.setHeader(name, Integer.toString(buf.length));
                                } else {
                                    part.setHeader(name, header.getValue());
                                }
                            }
                        }
                    }
                    if (chunked) {
                        try (FileOutputStream fos = new FileOutputStream(mpTmp)) {
                            mp.writeTo(fos);
                        }
                        size[0] = mpTmp.length();
                    }
                    debug.append("; new size of " + size[0] + "\n");
                    httpEntity = new AbstractHttpEntity() {

                        @Override
                        public void writeTo(OutputStream output) throws IOException {
                            if (chunked) {
                                try (FileInputStream fis = new FileInputStream(mpTmp)) {
                                    IOUtils.copyLarge(fis, output);
                                }
                            } else {
                                try {
                                    mp.writeTo(output);
                                } catch (MessagingException e) {
                                    new IOException(e);
                                }
                            }
                        }

                        @Override
                        public boolean isStreaming() {
                            return false;
                        }

                        @Override
                        public boolean isRepeatable() {
                            return true;
                        }

                        @Override
                        public long getContentLength() {
                            return size[0];
                        }

                        @Override
                        public InputStream getContent() throws IOException, IllegalStateException {
                            return null;
                        }
                    };
                } finally {
                    if (mpTmp != null) {
                        mpTmp.delete();
                    }
                }
            } else {
                InputStream is = null;
                try {
                    if ("gzip".equals(HeaderName.ContentEncoding.getHeader(request))) {
                        is = new GZIPInputStream(request.getInputStream());
                    } else {
                        is = request.getInputStream();
                    }
                    requestStringEntity = IOUtils.toString(is, "UTF-8");
                    debug.append("request Entity:\n" + requestStringEntity + "\n");
                } finally {
                    if (is != null) {
                        is.close();
                    }
                }
            }
        }
        boolean isNewStringEntity = false;
        if (method == HttpMethodType.POST && "_bulk_docs".equals(special)) {
            try {
                bulkDocsRequest = new JSONObject(requestStringEntity);
                Engine.theApp.couchDbManager.handleBulkDocsRequest(dbName, bulkDocsRequest, fsAuth);
                String newEntity = bulkDocsRequest.toString();
                if (!newEntity.equals(requestStringEntity)) {
                    requestStringEntity = newEntity;
                    isNewStringEntity = true;
                }
            } catch (JSONException e) {
                debug.append("failed to parse [ " + e.getMessage() + "]: " + requestStringEntity);
            }
        } else if (isChanges && newRequest instanceof HttpEntityEnclosingRequest) {
            requestStringEntity = Engine.theApp.couchDbManager.handleChangesUri(dbName, newRequest, requestStringEntity, fsAuth);
            if (requestStringEntity != null) {
                debug.append("request new Entity:\n" + requestStringEntity + "\n");
            }
            uri = newRequest.getURI();
            debug.append("Changed to " + newRequest.getMethod() + " URI: " + uri + "\n");
        }
        if (!isChanges && newRequest instanceof HttpEntityEnclosingRequest) {
            HttpEntityEnclosingRequest entityRequest = ((HttpEntityEnclosingRequest) newRequest);
            if (entityRequest.getEntity() == null) {
                if (httpEntity != null) {
                // already exists
                } else if (requestStringEntity != null) {
                    if (isNewStringEntity) {
                        debug.append("request new Entity:\n" + requestStringEntity + "\n");
                    }
                    httpEntity = new StringEntity(requestStringEntity, "UTF-8");
                } else {
                    httpEntity = new InputStreamEntity(request.getInputStream());
                }
                entityRequest.setEntity(httpEntity);
            }
        }
        Map<AbstractFullSyncListener, JSONArray> listeners = Engine.theApp.couchDbManager.handleBulkDocsRequest(dbName, bulkDocsRequest);
        long requestTime = System.currentTimeMillis();
        CloseableHttpResponse newResponse = null;
        try {
            newResponse = httpClient.get().execute(newRequest);
        } catch (IOException e) {
            debug.append("retry request because: " + e.getMessage());
            newResponse = httpClient.get().execute(newRequest);
        }
        requestTime = System.currentTimeMillis() - requestTime;
        int code = newResponse.getStatusLine().getStatusCode();
        debug.append("response Code: " + code + " in " + requestTime + " ms\n");
        if (isCBLiOS && code == 400) {
            code = 500;
            debug.append("response changed Code to: " + code + " (for iOS CBL)\n");
        }
        response.setStatus(code);
        boolean isCblBulkGet = isCBL && version.compareTo("2.3.") < 0 && isCouchDB && "_bulk_get".equals(special);
        if (!isCblBulkGet) {
            for (Header header : newResponse.getAllHeaders()) {
                if (isCBL && HeaderName.Server.is(header)) {
                    response.addHeader("Server", "Couchbase Sync Gateway/0.81");
                    debug.append("response Header: Server=Couchbase Sync Gateway/0.81\n");
                } else if (!(HeaderName.TransferEncoding.is(header) || HeaderName.ContentLength.is(header) || HeaderName.AccessControlAllowOrigin.is(header) || (isChanges && (HeaderName.ETag.is(header) || HeaderName.LastModified.is(header) || HeaderName.CacheControl.is(header))))) {
                    response.addHeader(header.getName(), header.getValue());
                    debug.append("response Header: " + header.getName() + "=" + header.getValue() + "\n");
                } else {
                    debug.append("skip response Header: " + header.getName() + "=" + header.getValue() + "\n");
                }
            }
            ServletUtils.applyCustomHeaders(request, response);
        }
        HttpEntity responseEntity = newResponse.getEntity();
        ContentTypeDecoder contentType = new ContentTypeDecoder(responseEntity == null || responseEntity.getContentType() == null ? "" : responseEntity.getContentType().getValue());
        debug.append("response ContentType charset=" + contentType.getCharset("n/a") + " mime=" + contentType.getMimeType() + "\n");
        OutputStream os = response.getOutputStream();
        String responseStringEntity = null;
        if (responseEntity != null) {
            // InputStream is = null;
            try (InputStream is = responseEntity.getContent()) {
                if (code >= 200 && code < 300 && !isUtilsRequest && contentType.mimeType().in(MimeType.Plain, MimeType.Json) && !"_design".equals(special) && !requestParser.hasAttachment() && ((isChanges && ((version.compareTo("2.") < 0 && isCouchDB) || !isCouchDB)) || "_bulk_get".equals(special) || "_all_docs".equals(special) || "_all_dbs".equals(special) || StringUtils.isNotEmpty(requestParser.getDocId()) || (bulkDocsRequest != null && listeners != null))) {
                    String charset = contentType.getCharset("UTF-8");
                    try (OutputStreamWriter writer = new OutputStreamWriter(os, charset);
                        BufferedInputStream bis = new BufferedInputStream(is)) {
                        if (isChanges) {
                            Engine.logCouchDbManager.info("(FullSyncServlet) Entering in continuous loop:\n" + debug);
                            try (BufferedReader br = new BufferedReader(new InputStreamReader(bis, charset))) {
                                Engine.theApp.couchDbManager.filterChanges(httpSession.getId(), dbName, uri, fsAuth, br, writer);
                            }
                        } else if (bulkDocsRequest != null) {
                            Engine.logCouchDbManager.info("(FullSyncServlet) Handle bulkDocsRequest:\n" + debug);
                            responseStringEntity = IOUtils.toString(bis, charset);
                            writer.write(responseStringEntity);
                            writer.flush();
                            if (listeners != null) {
                                Engine.theApp.couchDbManager.handleBulkDocsResponse(request, listeners, bulkDocsRequest, responseStringEntity);
                            }
                        } else if (isCblBulkGet) {
                            Engine.logCouchDbManager.info("(FullSyncServlet) Checking text response documents for CBL BulkGet:\n" + debug);
                            Engine.theApp.couchDbManager.checkCblBulkGetResponse(special, fsAuth, bis, charset, response);
                        } else {
                            Engine.logCouchDbManager.info("(FullSyncServlet) Checking response documents:\n" + debug);
                            Engine.theApp.couchDbManager.checkResponse(special, fsAuth, bis, charset, writer);
                        }
                    }
                } else if (code >= 200 && code < 300 && contentType.mimeType() == MimeType.MultiPartRelated && "_bulk_get".equals(special)) {
                    Engine.logCouchDbManager.info("(FullSyncServlet) Checking multipart response documents for CBL BulkGet:\n" + debug);
                    Engine.theApp.couchDbManager.checkCblBulkGetResponse(fsAuth, is, response);
                } else if (Pattern.matches(".*/bundle\\..*?\\.js", uri.getPath())) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(IOUtils.toString(is, "UTF-8").replace("json=function(e){var t", "json=function(e){e=e.replace('../../','../');var t"));
                    sb.append("\n$(\"#primary-navbar\").remove();");
                    byte[] b = sb.toString().getBytes("UTF-8");
                    HeaderName.ContentLength.addHeader(response, Integer.toString(b.length));
                    os.write(b);
                } else if (Pattern.matches(".*/bundle-.*?\\.js", uri.getPath())) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(IOUtils.toString(is, "UTF-8").replace("ajax:function(e,t){", "ajax:function(e,t){console.log('> ' + e.url);if(e.url.startsWith('/')){e.url='..'+e.url}else{e.url=e.url.replace('../..','..')}console.log('< ' + e.url);"));
                    // sb.append("\n$(\"#primary-navbar\").remove();");
                    byte[] b = sb.toString().getBytes("UTF-8");
                    HeaderName.ContentLength.addHeader(response, Integer.toString(b.length));
                    os.write(b);
                } else if (Pattern.matches(".*/styles\\..*?\\.css", uri.getPath())) {
                    StringBuilder sb = new StringBuilder();
                    sb.append(IOUtils.toString(is, "UTF-8"));
                    sb.append("\n.closeMenu #dashboard { left: 0px; }");
                    sb.append("\n.closeMenu .pusher { padding-right: 0px; }");
                    sb.append("\nbutton.add-new-database-btn { display: none; }");
                    sb.append("\n#notification-center-btn { display: none; }");
                    sb.append("\na.fonticon-replicate { display: none; }");
                    sb.append("\n.faux-header__doc-header-dropdown-itemwrapper a.faux-header__doc-header-dropdown-item[href*=\"replication\"] { display: none; }\n");
                    byte[] b = sb.toString().getBytes("UTF-8");
                    HeaderName.ContentLength.addHeader(response, Integer.toString(b.length));
                    os.write(b);
                } else if (requestParser.docId == null && requestParser.special == null && !fsClient.getPrefix().isEmpty()) {
                    String content = IOUtils.toString(is, "UTF-8");
                    content = content.replace("\"db_name\":\"" + fsClient.getPrefix(), "\"db_name\":\"");
                    byte[] bytes = content.getBytes("UTF-8");
                    HeaderName.ContentLength.addHeader(response, Integer.toString(bytes.length));
                    debug.append("response Header: " + HeaderName.ContentLength.value() + "=" + bytes.length + "\n");
                    Engine.logCouchDbManager.info("(FullSyncServlet) Remove prefix from response:\n" + debug);
                    os.write(bytes);
                } else {
                    String contentLength = HeaderName.ContentLength.getHeader(newResponse);
                    if (contentLength != null) {
                        HeaderName.ContentLength.addHeader(response, contentLength);
                        debug.append("response Header: " + HeaderName.ContentLength.value() + "=" + contentLength + "\n");
                    }
                    Engine.logCouchDbManager.info("(FullSyncServlet) Copying response stream:\n" + debug);
                    StreamUtils.copyAutoFlush(is, os);
                }
            } finally {
                newResponse.close();
            }
        }
    } catch (SecurityException e) {
        Engine.logCouchDbManager.warn("(FullSyncServlet) Failed to process request due to a security exception:\n" + e.getMessage() + "\n" + debug);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        HttpUtils.terminateNewSession(httpSession);
    } catch (EngineException e) {
        String message = e.getMessage();
        if (message != null && message.contains("anonymous user")) {
            Engine.logCouchDbManager.warn("(FullSyncServlet) Failed to process request: " + message + "\n" + debug);
        } else {
            Engine.logCouchDbManager.error("(FullSyncServlet) Failed to process request:\n" + debug, e);
        }
        HttpUtils.terminateNewSession(httpSession);
    } catch (Exception e) {
        if ("ClientAbortException".equals(e.getClass().getSimpleName())) {
            Engine.logCouchDbManager.info("(FullSyncServlet) Client disconnected:\n" + debug);
        } else {
            Engine.logCouchDbManager.error("(FullSyncServlet) Failed to process request:\n" + debug, e);
        }
        HttpUtils.terminateNewSession(httpSession);
    } finally {
        Log4jHelper.mdcClear();
        synchronized (httpSession) {
            Set<HttpServletRequest> set = SessionAttribute.fullSyncRequests.get(httpSession);
            if (set != null) {
                set.remove(request);
            }
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BodyPart(javax.mail.BodyPart) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpMethodType(com.twinsoft.convertigo.engine.enums.HttpMethodType) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) HttpOptions(org.apache.http.client.methods.HttpOptions) EngineException(com.twinsoft.convertigo.engine.EngineException) FullSyncClient(com.twinsoft.convertigo.engine.providers.couchdb.FullSyncClient) HttpPut(org.apache.http.client.methods.HttpPut) ServletException(javax.servlet.ServletException) HttpServletRequest(javax.servlet.http.HttpServletRequest) StringEntity(org.apache.http.entity.StringEntity) MimeMultipart(javax.mail.internet.MimeMultipart) BufferedInputStream(java.io.BufferedInputStream) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) AbstractFullSyncListener(com.twinsoft.convertigo.beans.couchdb.AbstractFullSyncListener) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) HashSet(java.util.HashSet) FileInputStream(java.io.FileInputStream) FullSyncAuthentication(com.twinsoft.convertigo.engine.providers.couchdb.CouchDbManager.FullSyncAuthentication) URIBuilder(org.apache.http.client.utils.URIBuilder) Project(com.twinsoft.convertigo.beans.core.Project) LogParameters(com.twinsoft.convertigo.engine.LogParameters) JSONObject(org.codehaus.jettison.json.JSONObject) Header(org.apache.http.Header) FileOutputStream(java.io.FileOutputStream) File(java.io.File) Connector(com.twinsoft.convertigo.beans.core.Connector) FullSyncConnector(com.twinsoft.convertigo.beans.connectors.FullSyncConnector) HttpDelete(org.apache.http.client.methods.HttpDelete) HttpEntity(org.apache.http.HttpEntity) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) FullSyncConnector(com.twinsoft.convertigo.beans.connectors.FullSyncConnector) URI(java.net.URI) HttpHead(org.apache.http.client.methods.HttpHead) GZIPInputStream(java.util.zip.GZIPInputStream) AbstractHttpEntity(org.apache.http.entity.AbstractHttpEntity) NameValuePair(org.apache.http.NameValuePair) InputStreamReader(java.io.InputStreamReader) MessagingException(javax.mail.MessagingException) HttpSession(javax.servlet.http.HttpSession) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) JSONArray(org.codehaus.jettison.json.JSONArray) JSONException(org.codehaus.jettison.json.JSONException) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) MessagingException(javax.mail.MessagingException) EngineException(com.twinsoft.convertigo.engine.EngineException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HttpTrace(org.apache.http.client.methods.HttpTrace) BufferedReader(java.io.BufferedReader) ContentTypeDecoder(com.twinsoft.convertigo.engine.util.ContentTypeDecoder) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 92 with Project

use of com.twinsoft.convertigo.beans.core.Project in project convertigo by convertigo.

the class GenericServlet method doRequest.

protected void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    HttpServletRequestTwsWrapper wrapped_request = new HttpServletRequestTwsWrapper(request);
    request = wrapped_request;
    String baseUrl = getServletBaseUrl(request);
    boolean isProject;
    if ((isProject = baseUrl.contains("/projects/") || baseUrl.contains("/system/")) || baseUrl.contains("/webclipper/")) {
        long t0 = System.currentTimeMillis();
        try {
            if (EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_API)) {
                HttpUtils.checkXSRF(request, response);
            }
            String encoded = request.getParameter(Parameter.RsaEncoded.getName());
            if (encoded != null) {
                String query = Engine.theApp.rsaManager.decrypt(encoded, request.getSession());
                wrapped_request.clearParameters();
                wrapped_request.addQuery(query);
            }
            if (isProject && request.getMethod().equalsIgnoreCase("OPTIONS") && Engine.isStarted) {
                Project project = null;
                String projectName = request.getParameter(Parameter.Project.getName());
                if (projectName == null) {
                    projectName = request.getRequestURI().replaceFirst(".*/projects/(.*?)/.*", "$1");
                }
                if (!projectName.contains("/")) {
                    try {
                        project = Engine.theApp.databaseObjectsManager.getOriginalProjectByName(projectName);
                    } catch (Exception e) {
                    }
                }
                if (project == null) {
                    response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                    return;
                }
                HttpUtils.applyFilterCorsHeaders(request, response, project.getCorsOrigin());
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                return;
            }
            Object result = processRequest(request);
            response.addHeader("Expires", "-1");
            if (getCacheControl(request).equals("false")) {
                HeaderName.CacheControl.addHeader(response, "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
            }
            HttpUtils.applyCorsHeaders(request, response);
            /**
             * Disabled since #253 : Too much HTML Connector cookies in
             * response header make a tomcat exception
             * http://sourceus.twinsoft.fr/ticket/253 cookies must be in xml
             * if wanted, not in headers
             *
             * Vector cookies = (Vector)
             * request.getAttribute("convertigo.cookies"); for (int i=0;
             * i<cookies.size(); i++) { String sCookie =
             * (String)cookies.elementAt(i);
             * response.addHeader("Set-Cookie", sCookie);
             * Engine.logContext.trace("[GenericServlet] Set-Cookie: " +
             * sCookie); }
             */
            String trSessionId = (String) request.getAttribute("sequence.transaction.sessionid");
            if ((trSessionId != null) && (!trSessionId.equals(""))) {
                response.setHeader("Transaction-JSessionId", trSessionId);
            }
            String requested_content_type = request.getParameter(Parameter.ContentType.getName());
            String content_type = getContentType(request);
            if (requested_content_type != null && !requested_content_type.equals(content_type)) {
                Engine.logEngine.debug("(GenericServlet) Override Content-Type requested to change : " + content_type + " to " + requested_content_type);
                content_type = requested_content_type;
            } else {
                requested_content_type = null;
            }
            response.setContentType(content_type);
            if (content_type.startsWith("text")) {
                String charset = (String) request.getAttribute("convertigo.charset");
                if (charset != null && charset.length() > 0) {
                    response.setCharacterEncoding(charset);
                }
            }
            try {
                if (result != null) {
                    Boolean b = (Boolean) request.getAttribute("convertigo.isErrorDocument");
                    if (b.booleanValue()) {
                        Requester requester = getRequester();
                        boolean bThrowHTTP500 = false;
                        if (requester instanceof WebServiceServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager.getProperty(EnginePropertiesManager.PropertyName.THROW_HTTP_500_SOAP_FAULT));
                        } else if (requester instanceof ServletRequester) {
                            bThrowHTTP500 = Boolean.parseBoolean(EnginePropertiesManager.getProperty(EnginePropertiesManager.PropertyName.THROW_HTTP_500));
                        }
                        if (bThrowHTTP500) {
                            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
                            Engine.logEngine.debug("(GenericServlet) Requested HTTP 500 status code");
                        }
                    } else {
                        applyCustomStatus(request, response);
                    }
                    if (result instanceof AttachmentDetails) {
                        AttachmentDetails attachment = (AttachmentDetails) result;
                        byte[] data = attachment.getData();
                        String contentType = attachment.getContentType();
                        if (requested_content_type != null) {
                            contentType = requested_content_type;
                        }
                        String name = attachment.getName();
                        HeaderName.ContentType.setHeader(response, contentType);
                        HeaderName.ContentLength.setHeader(response, "" + data.length);
                        HeaderName.ContentDisposition.setHeader(response, "attachment; filename=" + name);
                        applyCustomHeaders(request, response);
                        OutputStream out = response.getOutputStream();
                        out.write(data);
                        out.flush();
                    } else if (result instanceof byte[]) {
                        if (requested_content_type != null) {
                            response.setContentType(requested_content_type);
                        } else {
                            response.setContentType(getContentType(request));
                            response.setCharacterEncoding((String) request.getAttribute("convertigo.charset"));
                        }
                        HeaderName.ContentLength.addHeader(response, "" + ((byte[]) result).length);
                        applyCustomHeaders(request, response);
                        OutputStream out = response.getOutputStream();
                        out.write((byte[]) result);
                        out.flush();
                    } else {
                        String sResult = "";
                        if (result instanceof String) {
                            sResult = (String) result;
                        } else if (result instanceof Document) {
                            sResult = XMLUtils.prettyPrintDOM((Document) result);
                        } else if (result instanceof SOAPMessage) {
                            sResult = SOAPUtils.toString((SOAPMessage) result, (String) request.getAttribute("convertigo.charset"));
                        }
                        applyCustomHeaders(request, response);
                        Writer writer = response.getWriter();
                        writer.write(sResult);
                        writer.flush();
                    }
                } else {
                    applyCustomHeaders(request, response);
                    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
                }
            } catch (IOException e) {
                // The connection has probably been reset by peer
                Engine.logContext.warn("[GenericServlet] The connection has probably been reset by peer (IOException): " + e.getMessage());
            } finally {
                onFinally(request);
            }
        } catch (Exception e) {
            Engine.logContext.error("Unable to process the request!", e);
            processException(request, response, e);
        } finally {
            long t1 = System.currentTimeMillis();
            Engine.theApp.pluginsManager.fireHttpServletRequestEnd(request, t0, t1);
        }
    } else {
        // Not a valid Convertigo invocation URL, use retrieve as static
        // resource
        handleStaticData(request, response);
        return;
    }
}
Also used : OutputStream(java.io.OutputStream) IOException(java.io.IOException) Document(org.w3c.dom.Document) SOAPMessage(javax.xml.soap.SOAPMessage) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Project(com.twinsoft.convertigo.beans.core.Project) ServletRequester(com.twinsoft.convertigo.engine.requesters.ServletRequester) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester) ServletRequester(com.twinsoft.convertigo.engine.requesters.ServletRequester) Requester(com.twinsoft.convertigo.engine.requesters.Requester) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester) AttachmentDetails(com.twinsoft.convertigo.engine.AttachmentManager.AttachmentDetails) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) HttpServletRequestTwsWrapper(com.twinsoft.convertigo.engine.util.HttpServletRequestTwsWrapper) WebServiceServletRequester(com.twinsoft.convertigo.engine.requesters.WebServiceServletRequester)

Example 93 with Project

use of com.twinsoft.convertigo.beans.core.Project in project convertigo by convertigo.

the class ApplicationComponentEditor method launchBuilder.

public void launchBuilder(boolean forceInstall, boolean forceClean) {
    final MobileBuilderBuildMode buildMode = this.buildMode;
    final int buildCount = ++this.buildCount;
    final boolean isDark = SwtUtils.isDark();
    // Close editors (*.temp.ts) to avoid npm error at build launch
    ConvertigoPlugin.getDisplay().syncExec(new Runnable() {

        public void run() {
            try {
                ApplicationComponent mc = applicationEditorInput.application;
                IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
                if (activePage != null) {
                    IEditorReference[] editorRefs = activePage.getEditorReferences();
                    for (int i = 0; i < editorRefs.length; i++) {
                        IEditorReference editorRef = (IEditorReference) editorRefs[i];
                        try {
                            IEditorInput editorInput = editorRef.getEditorInput();
                            if (editorInput != null && editorInput instanceof ComponentFileEditorInput) {
                                if (((ComponentFileEditorInput) editorInput).is(mc) || ((ComponentFileEditorInput) editorInput).isChildOf(mc)) {
                                    activePage.closeEditor(editorRef.getEditor(false), false);
                                }
                            }
                        } catch (Exception e) {
                        }
                    }
                }
            } catch (Throwable t) {
            }
        }
    });
    // Launch build
    Engine.execute(() -> {
        try {
            String loader = IOUtils.toString(getClass().getResourceAsStream("loader.html"), "UTF-8");
            if (isDark) {
                loader = loader.replace("lightblue", "rgb(47,47,47); color: white");
            }
            c8oBrowser.setText(loader);
        } catch (Exception e1) {
            throw new RuntimeException(e1);
        }
        Project project = applicationEditorInput.application.getProject();
        File ionicDir = new File(project.getDirPath(), "_private/ionic");
        File nodeModules = new File(ionicDir, "node_modules");
        String nodeVersion = ProcessUtils.getNodeVersion(project);
        File nodeDir = ProcessUtils.getDefaultNodeDir();
        try {
            nodeDir = ProcessUtils.getNodeDir(nodeVersion, (r, t, x) -> {
                appendOutput("Downloading nodejs " + nodeVersion + ": " + Math.round((r * 100f) / t) + "%");
            });
        } catch (Exception e1) {
        }
        {
            String versions = "Will use nodejs " + ProcessUtils.getNodeVersion(nodeDir) + " and npm " + ProcessUtils.getNpmVersion(nodeDir);
            appendOutput(versions);
            Engine.logStudio.info(versions);
        }
        String path = nodeDir.getAbsolutePath();
        terminateNode();
        MobileBuilder mb = project.getMobileBuilder();
        if (forceInstall || !nodeModules.exists() || mb.getNeedPkgUpdate()) {
            boolean[] running = { true };
            try {
                new File(ionicDir, "package-lock.json").delete();
                if (forceClean) {
                    appendOutput("...", "...", "Removing existing node_modules... This can take several seconds...");
                    Engine.logStudio.info("Removing existing node_modules... This can take several seconds...");
                    com.twinsoft.convertigo.engine.util.FileUtils.deleteQuietly(nodeModules);
                }
                appendOutput("Installing node_modules... This can take several minutes depending on your network connection speed...");
                Engine.logStudio.info("Installing node_modules... This can take several minutes depending on your network connection speed...");
                long start = System.currentTimeMillis();
                ProcessBuilder pb = ProcessUtils.getNpmProcessBuilder(path, "npm", "install", ionicDir.toString(), "--no-shrinkwrap", "--no-package-lock");
                pb.redirectErrorStream(true);
                pb.directory(ionicDir);
                Process p = pb.start();
                Engine.execute(() -> {
                    try {
                        File staging = new File(nodeModules, ".staging");
                        while (running[0] && !staging.exists()) {
                            appendOutput("Resolving dependencies … (" + Math.round(System.currentTimeMillis() - start) / 1000 + " sec)");
                            Thread.sleep(1000);
                        }
                        while (running[0] && staging.exists()) {
                            appendOutput("Collecting node_modules: " + FileUtils.byteCountToDisplaySize(FileUtils.sizeOfAsBigInteger(nodeModules)) + " (" + Math.round(System.currentTimeMillis() - start) / 1000 + " sec)");
                            Engine.logStudio.info("Installing, node_module size is now : " + FileUtils.byteCountToDisplaySize(FileUtils.sizeOfAsBigInteger(nodeModules)));
                            Thread.sleep(1000);
                        }
                    } catch (Exception e) {
                        appendOutput("Something wrong during the install: " + e);
                    }
                });
                processes.add(p);
                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                String line;
                while ((line = br.readLine()) != null) {
                    line = pRemoveEchap.matcher(line).replaceAll("");
                    if (StringUtils.isNotBlank(line)) {
                        Engine.logStudio.info(line);
                        appendOutput(line);
                    }
                }
                Engine.logStudio.info(line);
                appendOutput("\\o/");
            } catch (Exception e) {
                appendOutput(":( " + e);
            }
            running[0] = false;
        }
        mb.setNeedPkgUpdate(false);
        Object mutex = new Object();
        mb.setBuildMutex(mutex);
        mb.setAppBuildMode(buildMode);
        try {
            ConvertigoPlugin.getDefault().getProjectPluginResource(project.getName()).refreshLocal(IResource.DEPTH_INFINITE, null);
        } catch (CoreException ce) {
        }
        try {
            mb.startBuild();
            File displayObjectsMobile = new File(project.getDirPath(), "DisplayObjects/mobile");
            displayObjectsMobile.mkdirs();
            appendOutput("removing previous build directory");
            for (File f : displayObjectsMobile.listFiles()) {
                if (!f.getName().equals("assets")) {
                    com.twinsoft.convertigo.engine.util.FileUtils.deleteQuietly(f);
                }
            }
            appendOutput("previous build directory removed");
            this.applicationEditorInput.application.checkFolder();
            try {
                File watchJS = new File(project.getDirPath(), "_private/ionic/node_modules/@ionic/app-scripts/dist/watch.js");
                if (watchJS.exists()) {
                    int ms = ConvertigoPlugin.getMobileBuilderThreshold();
                    String txt = FileUtils.readFileToString(watchJS, "UTF-8");
                    String ntxt = txt.replaceAll("var BUILD_UPDATE_DEBOUNCE_MS = \\d+;", "var BUILD_UPDATE_DEBOUNCE_MS = " + ms + ";");
                    if (!txt.equals(ntxt))
                        ;
                    {
                        FileUtils.writeStringToFile(watchJS, ntxt, "UTF-8");
                    }
                }
            } catch (Exception e) {
                Engine.logStudio.warn("Failed to update DEBOUNCE", e);
            }
            File assets = new File(displayObjectsMobile, "assets");
            if (assets.exists() && assets.isDirectory()) {
                appendOutput("Handle application assets");
                Engine.logStudio.info("Handle application assets");
                File privAssets = new File(ionicDir, "src/assets");
                FileUtils.deleteDirectory(privAssets);
                FileUtils.copyDirectory(assets, privAssets);
            }
            ProcessBuilder pb = ProcessUtils.getNpmProcessBuilder(path, "npm", "run", buildMode.command(), "--nobrowser");
            if (!MobileBuilderBuildMode.production.equals(buildMode)) {
                List<String> cmd = pb.command();
                synchronized (usedPort) {
                    cmd.add("--port");
                    cmd.add("" + (portNode = NetworkUtils.nextAvailable(8100, usedPort)));
                    cmd.add("--livereload-port");
                    cmd.add("" + (portReload = NetworkUtils.nextAvailable(35729, usedPort)));
                    cmd.add("--dev-logger-port");
                    cmd.add("" + (portLogger = NetworkUtils.nextAvailable(53703, usedPort)));
                }
            }
            pb.redirectErrorStream(true);
            pb.directory(ionicDir);
            Process p = pb.start();
            processes.add(p);
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                line = pRemoveEchap.matcher(line).replaceAll("");
                if (StringUtils.isNotBlank(line)) {
                    Engine.logStudio.info(line);
                    appendOutput(line);
                    if (line.matches(".*build .*finished.*")) {
                        synchronized (mutex) {
                            mutex.notify();
                        }
                        mb.buildFinished();
                    }
                    Matcher m = pIsServerRunning.matcher(line);
                    if (m.matches()) {
                        JSONObject envJSON = new JSONObject();
                        envJSON.put("remoteBase", EnginePropertiesManager.getProperty(PropertyName.APPLICATION_SERVER_CONVERTIGO_URL) + "/projects/" + project.getName() + "/_private");
                        FileUtils.write(new File(displayObjectsMobile, "env.json"), envJSON.toString(4), "UTF-8");
                        baseUrl = m.group(1);
                        synchronized (mutex) {
                            mutex.notify();
                        }
                        mb.buildFinished();
                        doLoad();
                    }
                }
            }
            if (buildCount == this.buildCount) {
                if (MobileBuilderBuildMode.production.equals(buildMode)) {
                    String SERVER_C8O_URL = EnginePropertiesManager.getProperty(PropertyName.APPLICATION_SERVER_CONVERTIGO_URL);
                    baseUrl = SERVER_C8O_URL + "/projects/" + project.getName() + "/DisplayObjects/mobile/index.html";
                    doLoad();
                    toast("Application in production mode");
                }
                appendOutput("\\o/");
            } else {
                appendOutput("previous build canceled !");
            }
        } catch (Exception e) {
            appendOutput(":( " + e);
        } finally {
            synchronized (mutex) {
                mutex.notify();
            }
            mb.setBuildMutex(null);
            mb.buildFinished();
            try {
                ConvertigoPlugin.getDefault().getProjectPluginResource(project.getName()).refreshLocal(IResource.DEPTH_INFINITE, null);
            } catch (CoreException ce) {
            }
        }
    });
}
Also used : Element(com.teamdev.jxbrowser.dom.Element) CoreException(org.eclipse.core.runtime.CoreException) FocusEvent(org.eclipse.swt.events.FocusEvent) StringUtils(org.apache.commons.lang3.StringUtils) Point(org.eclipse.swt.graphics.Point) Matcher(java.util.regex.Matcher) Composite(org.eclipse.swt.widgets.Composite) PartInitException(org.eclipse.ui.PartInitException) UIUseShared(com.twinsoft.convertigo.beans.mobile.components.UIUseShared) KeyEvent(org.eclipse.swt.events.KeyEvent) UIDynamicElement(com.twinsoft.convertigo.beans.mobile.components.UIDynamicElement) MessageDialog(org.eclipse.jface.dialogs.MessageDialog) KeyAdapter(org.eclipse.swt.events.KeyAdapter) IEditorPart(org.eclipse.ui.IEditorPart) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) Text(org.eclipse.swt.widgets.Text) IEditorInput(org.eclipse.ui.IEditorInput) PlatformUI(org.eclipse.ui.PlatformUI) Set(java.util.Set) ToolItem(org.eclipse.swt.widgets.ToolItem) IOUtils(org.apache.commons.io.IOUtils) MenuItem(org.eclipse.swt.widgets.MenuItem) SWT(org.eclipse.swt.SWT) UIActionStack(com.twinsoft.convertigo.beans.mobile.components.UIActionStack) JsAccessible(com.teamdev.jxbrowser.js.JsAccessible) MobileComponent(com.twinsoft.convertigo.beans.mobile.components.MobileComponent) EditorPart(org.eclipse.ui.part.EditorPart) SwtUtils(com.twinsoft.convertigo.eclipse.swt.SwtUtils) DatabaseObjectFoundException(com.twinsoft.convertigo.engine.DatabaseObjectFoundException) SelectionListener(org.eclipse.swt.events.SelectionListener) JFaceResources(org.eclipse.jface.resource.JFaceResources) IEditorSite(org.eclipse.ui.IEditorSite) RequestPermissionCallback(com.teamdev.jxbrowser.permission.callback.RequestPermissionCallback) GridData(org.eclipse.swt.layout.GridData) RowData(org.eclipse.swt.layout.RowData) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) UIDynamicInvoke(com.twinsoft.convertigo.beans.mobile.components.UIDynamicInvoke) Engine(com.twinsoft.convertigo.engine.Engine) JSONObject(org.codehaus.jettison.json.JSONObject) JSONArray(org.codehaus.jettison.json.JSONArray) MobileDebugView(com.twinsoft.convertigo.eclipse.views.mobile.MobileDebugView) MobileBuilderBuildMode(com.twinsoft.convertigo.engine.enums.MobileBuilderBuildMode) JsObject(com.teamdev.jxbrowser.js.JsObject) InputStreamReader(java.io.InputStreamReader) File(java.io.File) Node(com.teamdev.jxbrowser.dom.Node) Project(com.twinsoft.convertigo.beans.core.Project) UISharedComponent(com.twinsoft.convertigo.beans.mobile.components.UISharedComponent) JSONException(org.codehaus.jettison.json.JSONException) ModifyListener(org.eclipse.swt.events.ModifyListener) FileUtils(com.twinsoft.convertigo.engine.util.FileUtils) IEditorReference(org.eclipse.ui.IEditorReference) BufferedReader(java.io.BufferedReader) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) ProcessUtils(com.twinsoft.convertigo.engine.util.ProcessUtils) GridLayout(org.eclipse.swt.layout.GridLayout) WalkHelper(com.twinsoft.convertigo.engine.helpers.WalkHelper) ClipboardAction(com.twinsoft.convertigo.eclipse.popup.actions.ClipboardAction) ToolBar(org.eclipse.swt.widgets.ToolBar) VerifyListener(org.eclipse.swt.events.VerifyListener) MobileEventListener(com.twinsoft.convertigo.engine.mobile.MobileEventListener) EnginePropertiesManager(com.twinsoft.convertigo.engine.EnginePropertiesManager) InjectJsCallback(com.teamdev.jxbrowser.browser.callback.InjectJsCallback) NetworkUtils(com.twinsoft.convertigo.engine.util.NetworkUtils) ModifyEvent(org.eclipse.swt.events.ModifyEvent) Collection(java.util.Collection) UIComponent(com.twinsoft.convertigo.beans.mobile.components.UIComponent) UIControlEvent(com.twinsoft.convertigo.beans.mobile.components.UIControlEvent) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ConvertigoPlugin(com.twinsoft.convertigo.eclipse.ConvertigoPlugin) PropertyName(com.twinsoft.convertigo.engine.EnginePropertiesManager.PropertyName) List(java.util.List) Pattern(java.util.regex.Pattern) ShowContextMenuCallback(com.teamdev.jxbrowser.browser.callback.ShowContextMenuCallback) Label(org.eclipse.swt.widgets.Label) CompositeEvent(com.twinsoft.convertigo.eclipse.editors.CompositeEvent) Document(com.teamdev.jxbrowser.dom.Document) Image(org.eclipse.swt.graphics.Image) Rectangle(org.eclipse.swt.graphics.Rectangle) FocusListener(org.eclipse.swt.events.FocusListener) Event(org.eclipse.swt.widgets.Event) Frame(com.teamdev.jxbrowser.frame.Frame) MobileBuilder(com.twinsoft.convertigo.engine.mobile.MobileBuilder) HashSet(java.util.HashSet) PaletteSourceTransfer(com.twinsoft.convertigo.eclipse.dnd.PaletteSourceTransfer) BatchOperationHelper(com.twinsoft.convertigo.engine.helpers.BatchOperationHelper) Charset(java.nio.charset.Charset) Browser(com.teamdev.jxbrowser.browser.Browser) LinkedList(java.util.LinkedList) UIDynamicAction(com.twinsoft.convertigo.beans.mobile.components.UIDynamicAction) Response(com.teamdev.jxbrowser.browser.callback.InjectJsCallback.Response) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) RowLayout(org.eclipse.swt.layout.RowLayout) PageComponent(com.twinsoft.convertigo.beans.mobile.components.PageComponent) IResource(org.eclipse.core.resources.IResource) NumberUtils(org.apache.commons.lang3.math.NumberUtils) ApplicationComponent(com.twinsoft.convertigo.beans.mobile.components.ApplicationComponent) KeyListener(org.eclipse.swt.events.KeyListener) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Menu(org.eclipse.swt.widgets.Menu) FocusAdapter(org.eclipse.swt.events.FocusAdapter) C8oBrowser(com.twinsoft.convertigo.eclipse.swt.C8oBrowser) Control(org.eclipse.swt.widgets.Control) Matcher(java.util.regex.Matcher) IEditorReference(org.eclipse.ui.IEditorReference) InputStreamReader(java.io.InputStreamReader) ApplicationComponent(com.twinsoft.convertigo.beans.mobile.components.ApplicationComponent) MobileBuilderBuildMode(com.twinsoft.convertigo.engine.enums.MobileBuilderBuildMode) Point(org.eclipse.swt.graphics.Point) CoreException(org.eclipse.core.runtime.CoreException) PartInitException(org.eclipse.ui.PartInitException) DatabaseObjectFoundException(com.twinsoft.convertigo.engine.DatabaseObjectFoundException) JSONException(org.codehaus.jettison.json.JSONException) Project(com.twinsoft.convertigo.beans.core.Project) MobileBuilder(com.twinsoft.convertigo.engine.mobile.MobileBuilder) CoreException(org.eclipse.core.runtime.CoreException) JSONObject(org.codehaus.jettison.json.JSONObject) BufferedReader(java.io.BufferedReader) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) JSONObject(org.codehaus.jettison.json.JSONObject) JsObject(com.teamdev.jxbrowser.js.JsObject) TreeObject(com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject) File(java.io.File) IEditorInput(org.eclipse.ui.IEditorInput)

Example 94 with Project

use of com.twinsoft.convertigo.beans.core.Project in project convertigo by convertigo.

the class ObjectExplorerWizardPage method createBean.

private void createBean() {
    BeanInfo bi = getCurrentSelectedBeanInfo();
    if (bi != null) {
        try {
            newBean = (DatabaseObject) bi.getBeanDescriptor().getBeanClass().getConstructor().newInstance();
            if (parentObject instanceof DatabaseObject) {
                newBean.setParent((DatabaseObject) parentObject);
            }
            if (xpath != null) {
                if (newBean instanceof IXPathable) {
                    ((IXPathable) newBean).setXpath(xpath);
                }
                // case we create an "javascriptable" statement
                if (newBean instanceof XpathableStatement) {
                    ((XpathableStatement) newBean).setPureXpath(xpath);
                }
            }
            // case we create a javelinConnector
            if (newBean instanceof JavelinConnector) {
                ((JavelinConnector) newBean).setEmulatorTechnology(com.twinsoft.api.Session.AS400);
                ((JavelinConnector) newBean).emulatorID = ((JavelinConnector) newBean).findEmulatorId();
                ((JavelinConnector) newBean).setServiceCode(",DIR|localhost:23");
            }
            if (newBean instanceof FullSyncConnector && parentObject instanceof Project) {
                boolean bContinue = true;
                String name = ((Project) parentObject).getName().toLowerCase() + "_fullsync";
                while (bContinue) {
                    try {
                        newBean.setName(name);
                        bContinue = false;
                    } catch (ObjectWithSameNameException e) {
                        name = DatabaseObject.incrementName(name);
                    }
                }
            }
        } catch (Exception e) {
            newBean = null;
        }
    }
}
Also used : JavelinConnector(com.twinsoft.convertigo.beans.connectors.JavelinConnector) Project(com.twinsoft.convertigo.beans.core.Project) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) IXPathable(com.twinsoft.convertigo.beans.core.IXPathable) BeanInfo(java.beans.BeanInfo) DatabaseObject(com.twinsoft.convertigo.beans.core.DatabaseObject) FullSyncConnector(com.twinsoft.convertigo.beans.connectors.FullSyncConnector) EngineException(com.twinsoft.convertigo.engine.EngineException) ObjectWithSameNameException(com.twinsoft.convertigo.engine.ObjectWithSameNameException) XpathableStatement(com.twinsoft.convertigo.beans.statements.XpathableStatement)

Example 95 with Project

use of com.twinsoft.convertigo.beans.core.Project in project convertigo by convertigo.

the class ExportWizard method performFinish.

/* (non-Javadoc)
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
	 */
@Override
public boolean performFinish() {
    ProjectExplorerView explorerView = ConvertigoPlugin.getDefault().getProjectExplorerView();
    String filePath = fileChooserPage.getFilePath();
    try {
        if (explorerView != null) {
            if (filePath != null) {
                int index = filePath.lastIndexOf("/");
                String exportDirectoryPath = filePath.substring(0, index);
                String exportFileName = filePath.substring(index + 1);
                ConvertigoPlugin.logInfo("Export project to file \"" + exportFileName + "\"");
                String projectName = null;
                String fileName = exportFileName;
                int idx = fileName.lastIndexOf('.');
                if (idx != -1) {
                    fileName = fileName.substring(0, idx);
                    projectName = fileName;
                }
                if (projectName != null) {
                    Project project = null;
                    try {
                        project = explorerView.getProject(projectName);
                    } catch (EngineException e) {
                        project = Engine.theApp.databaseObjectsManager.getProjectByName(projectName);
                    }
                    if (project != null) {
                        if (exportFileName.endsWith(".xml")) {
                            Engine.theApp.databaseObjectsManager.exportProject(project);
                            return true;
                        } else if (exportFileName.endsWith(".car")) {
                            CarUtils.makeArchive(exportDirectoryPath, project);
                            return true;
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
    }
    return false;
}
Also used : Project(com.twinsoft.convertigo.beans.core.Project) ProjectExplorerView(com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView) EngineException(com.twinsoft.convertigo.engine.EngineException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

Project (com.twinsoft.convertigo.beans.core.Project)148 EngineException (com.twinsoft.convertigo.engine.EngineException)56 DatabaseObject (com.twinsoft.convertigo.beans.core.DatabaseObject)47 IOException (java.io.IOException)39 File (java.io.File)37 Sequence (com.twinsoft.convertigo.beans.core.Sequence)35 Connector (com.twinsoft.convertigo.beans.core.Connector)33 ArrayList (java.util.ArrayList)29 ProjectExplorerView (com.twinsoft.convertigo.eclipse.views.projectexplorer.ProjectExplorerView)26 JSONException (org.codehaus.jettison.json.JSONException)26 Transaction (com.twinsoft.convertigo.beans.core.Transaction)24 TreeObject (com.twinsoft.convertigo.eclipse.views.projectexplorer.model.TreeObject)22 SAXException (org.xml.sax.SAXException)21 CoreException (org.eclipse.core.runtime.CoreException)20 Step (com.twinsoft.convertigo.beans.core.Step)19 Element (org.w3c.dom.Element)19 Shell (org.eclipse.swt.widgets.Shell)18 JSONObject (org.codehaus.jettison.json.JSONObject)17 IProject (org.eclipse.core.resources.IProject)17 Cursor (org.eclipse.swt.graphics.Cursor)17