Search in sources :

Example 1 with WebContext

use of org.directwebremoting.WebContext in project ma-modules-public by infiniteautomation.

the class ReportsDwr method saveReport.

@DwrPermission(custom = ReportPermissionDefinition.PERMISSION)
public ProcessResult saveReport(int id, String name, String xid, List<ReportPointVO> points, String template, int includeEvents, boolean includeUserComments, int dateRangeType, int relativeDateType, int previousPeriodCount, int previousPeriodType, int pastPeriodCount, int pastPeriodType, boolean fromNone, int fromYear, int fromMonth, int fromDay, int fromHour, int fromMinute, boolean toNone, int toYear, int toMonth, int toDay, int toHour, int toMinute, boolean schedule, int schedulePeriod, int runDelayMinutes, String scheduleCron, boolean email, boolean includeData, boolean zipData, List<RecipientListEntryBean> recipients) {
    ProcessResult response = new ProcessResult();
    // Basic validation
    validateData(response, name, points, dateRangeType, relativeDateType, previousPeriodCount, pastPeriodCount);
    // Validate XID
    if (StringUtils.isBlank(xid))
        response.addContextualMessage("xid", "validate.required");
    else if (StringValidation.isLengthGreaterThan(xid, 50))
        response.addMessage("xid", new TranslatableMessage("validate.notLongerThan", 50));
    else if (!ReportDao.instance.isXidUnique(xid, id))
        response.addContextualMessage("xid", "validate.xidUsed");
    if (schedule) {
        if (schedulePeriod == ReportVO.SCHEDULE_CRON) {
            // Check the cron pattern.
            try {
                new CronTimerTrigger(scheduleCron);
            } catch (Exception e) {
                response.addContextualMessage("scheduleCron", "reports.validate.cron", e.getMessage());
            }
        } else {
            if (runDelayMinutes < 0)
                response.addContextualMessage("runDelayMinutes", "reports.validate.lessThan0");
            else if (runDelayMinutes > 59)
                response.addContextualMessage("runDelayMinutes", "reports.validate.greaterThan59");
        }
    }
    if (email && recipients.isEmpty())
        response.addContextualMessage("recipients", "reports.validate.needRecip");
    if (response.getHasMessages())
        return response;
    User user = Common.getUser();
    ReportDao reportDao = ReportDao.instance;
    ReportVO report;
    if (id == Common.NEW_ID) {
        report = new ReportVO();
        report.setUserId(user.getId());
    } else
        report = reportDao.getReport(id);
    ReportCommon.ensureReportPermission(user, report);
    // Update the new values.
    report.setXid(xid);
    report.setName(name);
    report.setPoints(points);
    report.setTemplate(template);
    report.setIncludeEvents(includeEvents);
    report.setIncludeUserComments(includeUserComments);
    report.setDateRangeType(dateRangeType);
    report.setRelativeDateType(relativeDateType);
    report.setPreviousPeriodCount(previousPeriodCount);
    report.setPreviousPeriodType(previousPeriodType);
    report.setPastPeriodCount(pastPeriodCount);
    report.setPastPeriodType(pastPeriodType);
    report.setFromNone(fromNone);
    report.setFromYear(fromYear);
    report.setFromMonth(fromMonth);
    report.setFromDay(fromDay);
    report.setFromHour(fromHour);
    report.setFromMinute(fromMinute);
    report.setToNone(toNone);
    report.setToYear(toYear);
    report.setToMonth(toMonth);
    report.setToDay(toDay);
    report.setToHour(toHour);
    report.setToMinute(toMinute);
    report.setSchedule(schedule);
    report.setSchedulePeriod(schedulePeriod);
    report.setRunDelayMinutes(runDelayMinutes);
    report.setScheduleCron(scheduleCron);
    report.setEmail(email);
    report.setIncludeData(includeData);
    report.setZipData(zipData);
    report.setRecipients(recipients);
    // Save the report
    reportDao.saveReport(report);
    // Conditionally schedule the report.
    String host = "";
    WebContext webContext = WebContextFactory.get();
    int port;
    if (webContext != null) {
        HttpServletRequest req = webContext.getHttpServletRequest();
        host = req.getServerName();
        port = req.getLocalPort();
    } else {
        port = Common.envProps.getInt("web.port", 8080);
    }
    ReportJob.scheduleReportJob(host, port, report);
    // Send back the report id in case this was new.
    response.addData("reportId", report.getId());
    response.addData("report", report);
    return response;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.serotonin.m2m2.vo.User) WebContext(org.directwebremoting.WebContext) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) CronTimerTrigger(com.serotonin.timer.CronTimerTrigger) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) ReportVO(com.serotonin.m2m2.reports.vo.ReportVO) ReportDao(com.serotonin.m2m2.reports.ReportDao) InvalidArgumentException(com.serotonin.InvalidArgumentException) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 2 with WebContext

use of org.directwebremoting.WebContext in project ma-core-public by infiniteautomation.

the class MiscDwr method setLocale.

@DwrPermission(anonymous = true)
public void setLocale(String locale) {
    WebContext webContext = WebContextFactory.get();
    LocaleResolver localeResolver = new SessionLocaleResolver();
    LocaleEditor localeEditor = new LocaleEditor();
    localeEditor.setAsText(locale);
    localeResolver.setLocale(webContext.getHttpServletRequest(), webContext.getHttpServletResponse(), (Locale) localeEditor.getValue());
}
Also used : LocaleResolver(org.springframework.web.servlet.LocaleResolver) SessionLocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) SessionLocaleResolver(org.springframework.web.servlet.i18n.SessionLocaleResolver) WebContext(org.directwebremoting.WebContext) LocaleEditor(org.springframework.beans.propertyeditors.LocaleEditor) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 3 with WebContext

use of org.directwebremoting.WebContext in project ma-core-public by infiniteautomation.

the class StartupDwr method getStartupProgress.

@DwrPermission(anonymous = true)
public ProcessResult getStartupProgress(long since) {
    ProcessResult result = new ProcessResult();
    IMangoLifecycle lifecycle = Providers.get(IMangoLifecycle.class);
    float progress = lifecycle.getStartupProgress();
    float shutdownProgress = lifecycle.getShutdownProgress();
    List<String> messages = LoggingConsoleRT.instance.getMessagesSince(since);
    StringBuilder builder = new StringBuilder();
    for (String message : messages) {
        builder.append(message);
        builder.append("</br>");
    }
    result.addData("message", builder.toString());
    result.addData("startupProgress", progress);
    result.addData("shutdownProgress", shutdownProgress);
    result.addData("state", getLifecycleStateMessage(lifecycle.getLifecycleState()));
    if ((progress >= 100) && (shutdownProgress == 0)) {
        WebContext ctx = WebContextFactory.get();
        result.addData("startupUri", DefaultPagesDefinition.getLoginUri(ctx.getHttpServletRequest(), ctx.getHttpServletResponse()));
    }
    // Add the message to describe what process we are in
    if ((progress < 100) && (shutdownProgress == 0)) {
        result.addData("processMessage", this.translations.translate("startup.startingUp"));
    } else if ((shutdownProgress > 0) && (lifecycle.isRestarting())) {
        result.addData("processMessage", this.translations.translate("shutdown.restarting"));
    } else if (shutdownProgress > 0) {
        result.addData("processMessage", this.translations.translate("shutdown.shuttingDown"));
    }
    if ((progress == 100) && (shutdownProgress == 0)) {
        result.addData("processMessage", this.translations.translate("startup.state.running"));
    }
    return result;
}
Also used : WebContext(org.directwebremoting.WebContext) ProcessResult(com.serotonin.m2m2.i18n.ProcessResult) IMangoLifecycle(com.serotonin.m2m2.IMangoLifecycle) DwrPermission(com.serotonin.m2m2.web.dwr.util.DwrPermission)

Example 4 with WebContext

use of org.directwebremoting.WebContext in project ma-core-public by infiniteautomation.

the class StrutsCreator method getType.

/* (non-Javadoc)
     * @see org.directwebremoting.Creator#getType()
     */
public Class getType() {
    synchronized (this) {
        if (moduleConfig == null) {
            WebContext wc = WebContextFactory.get();
            if (getInstanceMethod != null) {
                try {
                    // ModuleUtils utils = ModuleUtils.getInstance();
                    Object utils = getInstanceMethod.invoke(null, new Object[0]);
                    // String moduleName = utils.getModuleName("/", wc.getServletContext());
                    String moduleName = (String) getModuleNameMethod.invoke(utils, new Object[] { "/", wc.getServletContext() });
                    // moduleConfig = utils.getModuleConfig(moduleName, wc.getServletContext());
                    moduleConfig = (ModuleConfig) getModuleConfigMethod.invoke(utils, new Object[] { moduleName, wc.getServletContext() });
                } catch (Exception ex) {
                    throw new IllegalArgumentException(ex.getMessage());
                }
            } else {
                HttpServletRequest request = wc.getHttpServletRequest();
                if (request == null) {
                    log.warn("Using a FakeHttpServletRequest as part of setup");
                    request = new FakeHttpServletRequest();
                }
                moduleConfig = RequestUtils.getModuleConfig(request, wc.getServletContext());
            }
        }
    }
    try {
        return LocalUtil.classForName(moduleConfig.findFormBeanConfig(formBean).getType());
    } catch (ClassNotFoundException ex) {
        throw new IllegalArgumentException(Messages.getString("Creator.ClassNotFound", moduleConfig.findFormBeanConfig(formBean).getType()));
    }
}
Also used : FakeHttpServletRequest(org.directwebremoting.util.FakeHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) FakeHttpServletRequest(org.directwebremoting.util.FakeHttpServletRequest) WebContext(org.directwebremoting.WebContext)

Example 5 with WebContext

use of org.directwebremoting.WebContext in project ma-core-public by infiniteautomation.

the class DefaultPageNormalizer method normalizePage.

/* (non-Javadoc)
     * @see org.directwebremoting.extend.PageNormalizer#normalizePage(java.lang.String)
     */
public String normalizePage(String unnormalized) {
    synchronized (initLock) {
        if (welcomeFiles == null) {
            if (servletContext != null) {
                welcomeFiles = getWebXmlWelcomeFileList(servletContext);
            } else {
                WebContext webContext = WebContextFactory.get();
                if (webContext == null) {
                    log.warn("Can't find ServletContext to check for <welcome-file-list> in web.xml. Assuming defaults.");
                    log.warn(" - To prevent this message from happening, either call the PageNormalizer from a DWR thread");
                    log.warn(" - Or seed the PageNormalizer with a ServletContext before access from outside a DWR thread");
                } else {
                    ServletContext threadServletContext = webContext.getServletContext();
                    welcomeFiles = getWebXmlWelcomeFileList(threadServletContext);
                }
            }
        }
        if (welcomeFiles == null) {
            log.debug("Using default welcome file list (index.[jsp|htm[l]])");
            welcomeFiles = getDefaultWelcomeFileList();
        }
    }
    if (unnormalized == null) {
        return null;
    }
    String normalized = unnormalized;
    if (!normalizeIncludesQueryString) {
        int queryPos = normalized.indexOf('?');
        if (queryPos != -1) {
            normalized = normalized.substring(0, queryPos);
        }
    }
    for (Iterator it = welcomeFiles.iterator(); it.hasNext(); ) {
        String welcomeFile = (String) it.next();
        if (normalized.endsWith(welcomeFile)) {
            normalized = normalized.substring(0, normalized.length() - welcomeFile.length());
            break;
        }
    }
    return normalized;
}
Also used : WebContext(org.directwebremoting.WebContext) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext)

Aggregations

WebContext (org.directwebremoting.WebContext)18 HttpServletRequest (javax.servlet.http.HttpServletRequest)5 DwrPermission (com.serotonin.m2m2.web.dwr.util.DwrPermission)4 ProcessResult (com.serotonin.m2m2.i18n.ProcessResult)3 Iterator (java.util.Iterator)3 Creator (org.directwebremoting.extend.Creator)3 ReportVO (com.serotonin.m2m2.reports.vo.ReportVO)2 Method (java.lang.reflect.Method)2 MarshallException (org.directwebremoting.extend.MarshallException)2 InvalidArgumentException (com.serotonin.InvalidArgumentException)1 IMangoLifecycle (com.serotonin.m2m2.IMangoLifecycle)1 CompoundChild (com.serotonin.m2m2.gviews.component.CompoundChild)1 CompoundComponent (com.serotonin.m2m2.gviews.component.CompoundComponent)1 ImageChartComponent (com.serotonin.m2m2.gviews.component.ImageChartComponent)1 PointComponent (com.serotonin.m2m2.gviews.component.PointComponent)1 SimpleCompoundComponent (com.serotonin.m2m2.gviews.component.SimpleCompoundComponent)1 SimplePointComponent (com.serotonin.m2m2.gviews.component.SimplePointComponent)1 ViewComponent (com.serotonin.m2m2.gviews.component.ViewComponent)1 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)1 ReportDao (com.serotonin.m2m2.reports.ReportDao)1