Search in sources :

Example 1 with ServiceException

use of com.twinsoft.convertigo.engine.admin.services.ServiceException in project convertigo by convertigo.

the class CreateScheduledElements method getServiceResult.

protected void getServiceResult(HttpServletRequest request, Document document) throws Exception {
    SchedulerManager schedulerManager = Engine.theApp.schedulerManager;
    SchedulerXML schedulerXML = schedulerManager.getSchedulerXML();
    boolean edit = (request.getParameter("edit") != null);
    boolean del = (request.getParameter("del") != null);
    Type type = null;
    try {
        type = Type.valueOf(request.getParameter("type"));
    } catch (Exception e) {
        throw new ServiceException("invalide \"type\" parameter");
    }
    Element rootElement = document.getDocumentElement();
    AbstractBase ab = null;
    Exception eProb = null;
    try {
        if (edit || del) {
            String exname = ServiceUtils.getRequiredParameter(request, "exname");
            if (ScheduledJob.class.isAssignableFrom(type.c)) {
                ab = schedulerXML.getScheduledJob(exname);
            } else if (AbstractSchedule.class.isAssignableFrom(type.c)) {
                ab = schedulerXML.getSchedule(exname);
            } else if (AbstractJob.class.isAssignableFrom(type.c)) {
                ab = schedulerXML.getJob(exname);
            }
            if (del) {
                schedulerXML.delAbstractBase(ab);
                ab = null;
            }
        } else {
            ab = type.c.getConstructor().newInstance();
        }
        if (ab != null) {
            String exName = ab.getName();
            String name = ServiceUtils.getRequiredParameter(request, "name");
            ab.setName(name);
            ab.setDescription(ServiceUtils.getRequiredParameter(request, "description"));
            ab.setEnable("true".equals(ServiceUtils.getRequiredParameter(request, "enabled")));
            if (ScheduledJob.class.isAssignableFrom(type.c)) {
                ScheduledJob sj = (ScheduledJob) ab;
                sj.setJob(schedulerXML.getJob(ServiceUtils.getRequiredParameter(request, "jobName")));
                sj.setSchedule(schedulerXML.getSchedule(ServiceUtils.getRequiredParameter(request, "scheduleName")));
            } else if (AbstractSchedule.class.isAssignableFrom(type.c)) {
                AbstractSchedule as = (AbstractSchedule) ab;
                if (ScheduleCron.class.isAssignableFrom(type.c)) {
                    ScheduleCron sc = (ScheduleCron) as;
                    sc.setCron(ServiceUtils.getRequiredParameter(request, "cron"));
                }
            } else if (AbstractJob.class.isAssignableFrom(type.c)) {
                AbstractJob aj = (AbstractJob) ab;
                if (JobGroupJob.class.isAssignableFrom(type.c)) {
                    JobGroupJob jgj = (JobGroupJob) aj;
                    jgj.setParallelJob(Integer.parseInt(ServiceUtils.getRequiredParameter(request, "parallelJob")));
                    jgj.delAllJobs();
                    for (String jobname : request.getParameterValues("jobsname")) {
                        AbstractJob jobToAdd = schedulerXML.getJob(jobname);
                        if (jobToAdd != null) {
                            jgj.addJob(jobToAdd);
                        }
                    }
                } else if (AbstractConvertigoJob.class.isAssignableFrom(type.c)) {
                    AbstractConvertigoJob acj = (AbstractConvertigoJob) aj;
                    acj.setContextName(ServiceUtils.getRequiredParameter(request, "context"));
                    acj.setProjectName(ServiceUtils.getRequiredParameter(request, "project"));
                    acj.setWriteOutput("true".equals(ServiceUtils.getRequiredParameter(request, "writeOutput")));
                    if (TransactionConvertigoJob.class.isAssignableFrom(type.c)) {
                        TransactionConvertigoJob tcj = (TransactionConvertigoJob) acj;
                        tcj.setConnectorName(ServiceUtils.getParameter(request, "connector", ""));
                        tcj.setTransactionName(ServiceUtils.getParameter(request, "transaction", ""));
                    } else if (SequenceConvertigoJob.class.isAssignableFrom(type.c)) {
                        SequenceConvertigoJob scj = (SequenceConvertigoJob) acj;
                        scj.setSequenceName(ServiceUtils.getParameter(request, "sequence", ""));
                    }
                    Map<String, String[]> parameters = new HashMap<String, String[]>();
                    Matcher prefix = prefixPattern.matcher("");
                    for (String pname : Collections.list(GenericUtils.<Enumeration<String>>cast(request.getParameterNames()))) {
                        prefix.reset(pname);
                        if (prefix.find()) {
                            String para_name = prefix.group(1);
                            String[] values = request.getParameterValues(pname);
                            // for (String value : values) {
                            // parameters.put(para_name, value);
                            parameters.put(para_name, values);
                        // }
                        }
                        if (pname.equals("parameters")) {
                            String value = request.getParameter(pname);
                            if (!value.equals("0")) {
                                parameters.put("__testcase", new String[] { value });
                            }
                        }
                    }
                    acj.setParameters(parameters);
                }
            }
            List<String> problems = schedulerXML.checkProblems(ab);
            if (exName.equals(name) && edit) {
                problems.remove(SchedulerXML.prob_alreadyExist);
            }
            if (problems.size() > 0) {
                for (String problem : problems) {
                    rootElement.appendChild(document.createElement("problem")).appendChild(document.createTextNode(problem));
                }
                throw (eProb = new Exception("problem!"));
            } else {
                if (!edit) {
                    schedulerXML.addAbstractBase(ab);
                }
            }
        }
        schedulerManager.save();
        schedulerManager.refreshJobs();
    } catch (Exception e) {
        schedulerManager.load();
        if (e != eProb) {
            throw e;
        }
    }
}
Also used : SchedulerXML(com.twinsoft.convertigo.beans.scheduler.SchedulerXML) Enumeration(java.util.Enumeration) Matcher(java.util.regex.Matcher) Element(org.w3c.dom.Element) ScheduledJob(com.twinsoft.convertigo.beans.scheduler.ScheduledJob) AbstractBase(com.twinsoft.convertigo.beans.scheduler.AbstractBase) SequenceConvertigoJob(com.twinsoft.convertigo.beans.scheduler.SequenceConvertigoJob) AbstractSchedule(com.twinsoft.convertigo.beans.scheduler.AbstractSchedule) ScheduleCron(com.twinsoft.convertigo.beans.scheduler.ScheduleCron) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) SchedulerManager(com.twinsoft.convertigo.engine.scheduler.SchedulerManager) AbstractJob(com.twinsoft.convertigo.beans.scheduler.AbstractJob) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) JobGroupJob(com.twinsoft.convertigo.beans.scheduler.JobGroupJob) AbstractConvertigoJob(com.twinsoft.convertigo.beans.scheduler.AbstractConvertigoJob) TransactionConvertigoJob(com.twinsoft.convertigo.beans.scheduler.TransactionConvertigoJob) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ServiceException

use of com.twinsoft.convertigo.engine.admin.services.ServiceException in project convertigo by convertigo.

the class ServiceUtils method handleError.

public static void handleError(Document document, HttpServletRequest request, HttpServletResponse response) throws ServiceException {
    try {
        HttpUtils.applyCorsHeaders(request, response);
        response.setStatus(500);
        // Bugfix IE #1622: do not forget to set the content type!
        response.setContentType(MimeType.TextXml.value());
        response.setCharacterEncoding("UTF-8");
        Writer writer;
        try {
            writer = response.getWriter();
        } catch (Exception e) {
            writer = new OutputStreamWriter(response.getOutputStream(), "UTF-8");
        }
        XMLUtils.prettyPrintDOMWithEncoding(document, "UTF-8", writer);
    } catch (Throwable t) {
        throw new ServiceException("Unable to handle error", t);
    }
}
Also used : ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 3 with ServiceException

use of com.twinsoft.convertigo.engine.admin.services.ServiceException in project convertigo by convertigo.

the class LaunchBuild method perform.

public static String perform(MobileResourceHelper mobileResourceHelper, HttpServletRequest request) throws Exception {
    MobilePlatform mobilePlatform = mobileResourceHelper.mobilePlatform;
    MobileApplication mobileApplication = mobileResourceHelper.mobileApplication;
    String finalApplicationName = mobileApplication.getComputedApplicationName();
    File mobileArchiveFile = mobileResourceHelper.makeZipPackage();
    // Login to the mobile builder platform
    String mobileBuilderPlatformURL = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_PLATFORM_URL);
    Map<String, String[]> params = new HashMap<String, String[]>();
    params.put("application", new String[] { finalApplicationName });
    params.put("platformName", new String[] { mobilePlatform.getName() });
    params.put("platformType", new String[] { mobilePlatform.getType() });
    params.put("auth_token", new String[] { mobileApplication.getComputedAuthenticationToken() });
    // revision and endpoint params
    params.put("revision", new String[] { mobileResourceHelper.getRevision() });
    params.put("endpoint", new String[] { mobileApplication.getComputedEndpoint(request) });
    params.put("appid", new String[] { mobileApplication.getComputedApplicationId() });
    // iOS
    if (mobilePlatform instanceof IOs) {
        IOs ios = (IOs) mobilePlatform;
        String pw, title = ios.getiOSCertificateTitle();
        if (!title.equals("")) {
            pw = ios.getiOSCertificatePw();
        } else {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_IOS_CERTIFICATE_TITLE);
            pw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_IOS_CERTIFICATE_PW);
        }
        params.put("iOSCertificateTitle", new String[] { title });
        params.put("iOSCertificatePw", new String[] { pw });
    }
    // Android
    if (mobilePlatform instanceof Android) {
        Android android = (Android) mobilePlatform;
        String certificatePw, keystorePw, title = android.getAndroidCertificateTitle();
        if (!title.equals("")) {
            certificatePw = android.getAndroidCertificatePw();
            keystorePw = android.getAndroidKeystorePw();
        } else {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_CERTIFICATE_TITLE);
            certificatePw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_CERTIFICATE_PW);
            keystorePw = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_ANDROID_KEYSTORE_PW);
        }
        params.put("androidCertificateTitle", new String[] { title });
        params.put("androidCertificatePw", new String[] { certificatePw });
        params.put("androidKeystorePw", new String[] { keystorePw });
    }
    // Windows Phone
    if (mobilePlatform instanceof WindowsPhoneKeyProvider) {
        WindowsPhoneKeyProvider windowsPhone = (WindowsPhoneKeyProvider) mobilePlatform;
        String title = windowsPhone.getWinphonePublisherIdTitle();
        if (title.equals("")) {
            title = EnginePropertiesManager.getProperty(PropertyName.MOBILE_BUILDER_WINDOWSPHONE_PUBLISHER_ID_TITLE);
        }
        params.put("winphonePublisherIdTitle", new String[] { title });
    }
    // Launch the mobile build
    URL url = new URL(mobileBuilderPlatformURL + "/build?" + URLUtils.mapToQuery(params));
    HostConfiguration hostConfiguration = new HostConfiguration();
    hostConfiguration.setHost(url.getHost());
    HttpState httpState = new HttpState();
    Engine.theApp.proxyManager.setProxy(hostConfiguration, httpState, url);
    PostMethod method = new PostMethod(url.toString());
    FileRequestEntity entity = new FileRequestEntity(mobileArchiveFile, null);
    method.setRequestEntity(entity);
    int methodStatusCode = Engine.theApp.httpClient.executeMethod(hostConfiguration, method, httpState);
    String sResult = IOUtils.toString(method.getResponseBodyAsStream(), "UTF-8");
    if (methodStatusCode != HttpStatus.SC_OK) {
        throw new ServiceException("Unable to build application '" + finalApplicationName + "'.\n" + sResult);
    }
    return sResult;
}
Also used : HashMap(java.util.HashMap) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpState(org.apache.commons.httpclient.HttpState) IOs(com.twinsoft.convertigo.beans.mobileplatforms.IOs) URL(java.net.URL) Android(com.twinsoft.convertigo.beans.mobileplatforms.Android) FileRequestEntity(org.apache.commons.httpclient.methods.FileRequestEntity) MobilePlatform(com.twinsoft.convertigo.beans.core.MobilePlatform) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) MobileApplication(com.twinsoft.convertigo.beans.core.MobileApplication) File(java.io.File) WindowsPhoneKeyProvider(com.twinsoft.convertigo.beans.mobileplatforms.WindowsPhoneKeyProvider)

Example 4 with ServiceException

use of com.twinsoft.convertigo.engine.admin.services.ServiceException in project convertigo by convertigo.

the class MobileResourceHelper method prepareFilesForFlashupdate.

public void prepareFilesForFlashupdate() throws ServiceException {
    boolean changed = false;
    final File lastEndpoint = new File(destDir, ".endpoint");
    fixMobileBuilderTimes();
    if (Engine.isStudioMode() && destDir.exists()) {
        try {
            for (File directory : mobileDir) {
                FileUtils.listFiles(directory, new IOFileFilter() {

                    public boolean accept(File file) {
                        if (MobileResourceHelper.defaultFilter.accept(file)) {
                            if (FileUtils.isFileNewer(file, destDir)) {
                                Engine.logEngine.info("(MobileResourceHelper) prepareFilesForFlashupdate, '" + file.lastModified() + "' newer than '" + destDir.lastModified() + "' for: " + file);
                                throw new RuntimeException();
                            }
                            return true;
                        } else {
                            return false;
                        }
                    }

                    public boolean accept(File file, String path) {
                        return accept(new File(file, path));
                    }
                }, MobileResourceHelper.defaultFilter);
            }
            changed = !endpoint.equals(FileUtils.readFileToString(lastEndpoint, StandardCharsets.UTF_8));
        } catch (Exception e) {
            changed = true;
        }
    }
    if (!destDir.exists() || changed) {
        prepareFiles(new FileFilter() {

            public boolean accept(File pathname) {
                boolean ok = MobileResourceHelper.defaultFilter.accept(pathname) && !new File(currentMobileDir, "plugins.txt").equals(pathname) && !new File(currentMobileDir, "config.xml").equals(pathname) && !new File(currentMobileDir, "res").equals(pathname) && !lastEndpoint.equals(pathname);
                return ok;
            }
        });
        try {
            write(lastEndpoint, endpoint);
        } catch (IOException e) {
            throw new ServiceException("Failed to write last endpoint", e);
        }
    }
}
Also used : ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) IOException(java.io.IOException) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) FileFilter(java.io.FileFilter) File(java.io.File) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Example 5 with ServiceException

use of com.twinsoft.convertigo.engine.admin.services.ServiceException in project convertigo by convertigo.

the class MobileResourceHelper method preparePackage.

public File preparePackage() throws Exception {
    FlashUpdateBuildMode buildMode = mobileApplication.getBuildMode();
    String finalApplicationName = mobileApplication.getComputedApplicationName();
    JSONObject json = new JSONObject();
    if (buildMode == FlashUpdateBuildMode.full) {
        fixMobileBuilderTimes();
        prepareFiles();
    } else if (buildMode == FlashUpdateBuildMode.light) {
        fixMobileBuilderTimes();
        prepareFiles(new FileFilter() {

            public boolean accept(File pathname) {
                try {
                    File dir;
                    boolean ok = MobileResourceHelper.defaultFilter.accept(pathname) && (new File(currentMobileDir, "index.html").equals(pathname) || new File(currentMobileDir, "config.xml").equals(pathname) || new File(currentMobileDir, "icon.png").equals(pathname) || (dir = new File(currentMobileDir, "res")).equals(pathname) || (dir.exists() && FileUtils.directoryContains(dir, pathname)) || (dir = new File(currentMobileDir, "flashupdate")).equals(pathname) || (dir.exists() && FileUtils.directoryContains(dir, pathname)));
                    return ok;
                } catch (Exception e) {
                    return false;
                }
            }
        });
        json.put("lightBuild", true);
    } else {
        throw new ServiceException("Unknow build mode: " + buildMode);
    }
    // Update config.xml
    File configFile = new File(destDir, "config.xml");
    String configText = FileUtils.readFileToString(configFile, StandardCharsets.UTF_8);
    long revision = destDir.lastModified();
    configText = configText.replace("$(ApplicationID)$", mobileApplication.getComputedApplicationId()).replace("$(ApplicationVersion)$", mobileApplication.getComputedApplicationVersion()).replace("$(ApplicationName)$", StringEscapeUtils.escapeXml11(finalApplicationName)).replace("$(ApplicationDescription)$", StringEscapeUtils.escapeXml11(mobileApplication.getApplicationDescription())).replace("$(ApplicationAuthorName)$", StringEscapeUtils.escapeXml11(mobileApplication.getApplicationAuthorName())).replace("$(ApplicationAuthorEmail)$", mobileApplication.getApplicationAuthorEmail()).replace("$(ApplicationAuthorWebsite)$", mobileApplication.getApplicationAuthorSite()).replace("$(PlatformName)$", mobilePlatform.getName()).replace("$(PlatformType)$", mobilePlatform.getType()).replace("$(CordovaPlatform)$", mobilePlatform.getCordovaPlatform());
    File pluginsFile = new File(destDir, "plugins.txt");
    if (pluginsFile.exists()) {
        String mandatoryPlugins = FileUtils.readFileToString(pluginsFile, StandardCharsets.UTF_8);
        if (!mandatoryPlugins.isEmpty()) {
            mandatoryPlugins = "<!-- Application mandatory plugins -->" + System.lineSeparator() + mandatoryPlugins;
            configText = configText.replace("<!-- Application mandatory plugins -->", mandatoryPlugins);
        }
        FileUtils.deleteQuietly(pluginsFile);
    }
    FileUtils.write(configFile, configText, StandardCharsets.UTF_8);
    configFile.setLastModified(revision);
    destDir.setLastModified(revision);
    listFiles(json);
    write("files.json", json.toString());
    String remoteBase = endpoint + "/projects/" + project.getName() + "/_private/mobile/flashupdate_" + this.mobilePlatform.getName();
    String env = read("env.json");
    try {
        json = new JSONObject(env);
    } catch (Exception e) {
        json = new JSONObject();
    }
    json.put("applicationAuthorName", mobileApplication.getApplicationAuthorName());
    json.put("applicationAuthorEmail", mobileApplication.getApplicationAuthorEmail());
    json.put("applicationAuthorWebsite", mobileApplication.getApplicationAuthorSite());
    json.put("applicationDescription", mobileApplication.getApplicationDescription());
    json.put("applicationId", mobileApplication.getComputedApplicationId());
    json.put("applicationName", finalApplicationName);
    json.put("builtRevision", revision);
    json.put("builtVersion", mobileApplication.getComputedApplicationVersion());
    json.put("currentRevision", revision);
    json.put("currentVersion", mobileApplication.getComputedApplicationVersion());
    json.put("endPoint", endpoint);
    json.put("platform", mobilePlatform.getCordovaPlatform());
    json.put("platformName", mobilePlatform.getName());
    json.put("projectName", project.getName());
    json.put("remoteBase", remoteBase);
    json.put("timeout", mobileApplication.getFlashUpdateTimeout());
    json.put("splashRemoveMode", mobileApplication.getSplashRemoveMode().name());
    write("env.json", json.toString(4));
    destDir.setLastModified(revision);
    return destDir;
}
Also used : JSONObject(org.codehaus.jettison.json.JSONObject) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) FlashUpdateBuildMode(com.twinsoft.convertigo.beans.core.MobileApplication.FlashUpdateBuildMode) IOFileFilter(org.apache.commons.io.filefilter.IOFileFilter) TrueFileFilter(org.apache.commons.io.filefilter.TrueFileFilter) FileFilter(java.io.FileFilter) File(java.io.File) ServiceException(com.twinsoft.convertigo.engine.admin.services.ServiceException) IOException(java.io.IOException) JSONException(org.codehaus.jettison.json.JSONException) EngineException(com.twinsoft.convertigo.engine.EngineException)

Aggregations

ServiceException (com.twinsoft.convertigo.engine.admin.services.ServiceException)21 File (java.io.File)8 Element (org.w3c.dom.Element)7 MobileApplication (com.twinsoft.convertigo.beans.core.MobileApplication)6 IOException (java.io.IOException)6 AuthenticationException (com.twinsoft.convertigo.engine.AuthenticationException)5 EngineException (com.twinsoft.convertigo.engine.EngineException)3 InputStream (java.io.InputStream)3 URL (java.net.URL)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 JSONException (org.codehaus.jettison.json.JSONException)3 JSONObject (org.codehaus.jettison.json.JSONObject)3 BufferedReader (java.io.BufferedReader)2 FileFilter (java.io.FileFilter)2 FileInputStream (java.io.FileInputStream)2 HashMap (java.util.HashMap)2 HostConfiguration (org.apache.commons.httpclient.HostConfiguration)2 HttpState (org.apache.commons.httpclient.HttpState)2 NameValuePair (org.apache.commons.httpclient.NameValuePair)2