Search in sources :

Example 1 with Credentials

use of lucee.commons.security.Credentials in project Lucee by lucee.

the class Schedule method doUpdate.

/**
 * @throws PageException
 */
private void doUpdate() throws PageException {
    String message = "missing attribute for tag schedule with action update";
    String detail = "required attributes are [startDate, startTime, URL, interval, operation]";
    Resource file = null;
    // if(publish) {
    if (!StringUtil.isEmpty(strFile) && !StringUtil.isEmpty(strPath)) {
        file = ResourceUtil.toResourceNotExisting(pageContext, strPath);
        file = file.getRealResource(strFile);
    } else if (!StringUtil.isEmpty(strFile)) {
        file = ResourceUtil.toResourceNotExisting(pageContext, strFile);
    } else if (!StringUtil.isEmpty(strPath)) {
        file = ResourceUtil.toResourceNotExisting(pageContext, strPath);
    }
    if (file != null)
        pageContext.getConfig().getSecurityManager().checkFileLocation(pageContext.getConfig(), file, serverPassword);
    // missing attributes
    if (startdate == null || starttime == null || url == null || interval == null)
        throw new ApplicationException(message, detail);
    // timeout
    if (requesttimeout < 0)
        requesttimeout = pageContext.getRequestTimeout();
    // username/password
    Credentials cr = null;
    if (username != null)
        cr = CredentialsImpl.toCredentials(username, password);
    try {
        ScheduleTask st = new ScheduleTaskImpl(task, file, startdate, starttime, enddate, endtime, url, port, interval, requesttimeout, cr, ProxyDataImpl.getInstance(proxyserver, proxyport, proxyuser, proxypassword), resolveurl, publish, hidden, readonly, paused, autoDelete);
        scheduler.addScheduleTask(st, true);
    } catch (Exception e) {
        throw Caster.toPageException(e);
    }
// 
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) ScheduleTaskImpl(lucee.runtime.schedule.ScheduleTaskImpl) Resource(lucee.commons.io.res.Resource) ScheduleTask(lucee.runtime.schedule.ScheduleTask) Credentials(lucee.commons.security.Credentials) DatabaseException(lucee.runtime.exp.DatabaseException) ApplicationException(lucee.runtime.exp.ApplicationException) PageException(lucee.runtime.exp.PageException)

Example 2 with Credentials

use of lucee.commons.security.Credentials in project Lucee by lucee.

the class ExecutionThread method execute.

public static void execute(Config config, ScheduleTask task, String charset) {
    Log log = getLog(config);
    boolean hasError = false;
    String logName = "schedule task:" + task.getTask();
    // init
    // HttpClient client = new HttpClient();
    // client.setStrictMode(false);
    // HttpState state = client.getState();
    String url;
    if (task.getUrl().getQuery() == null)
        url = task.getUrl().toExternalForm() + "?RequestTimeout=" + (task.getTimeout() / 1000);
    else if (StringUtil.isEmpty(task.getUrl().getQuery()))
        url = task.getUrl().toExternalForm() + "RequestTimeout=" + (task.getTimeout() / 1000);
    else {
        if (StringUtil.indexOfIgnoreCase(task.getUrl().getQuery() + "", "RequestTimeout") != -1)
            url = task.getUrl().toExternalForm();
        else
            url = task.getUrl().toExternalForm() + "&RequestTimeout=" + (task.getTimeout() / 1000);
    }
    // HttpMethod method = new GetMethod(url);
    // HostConfiguration hostConfiguration = client.getHostConfiguration();
    Header[] headers = new Header[] { HTTPEngine.header("User-Agent", "CFSCHEDULE") };
    // method.setRequestHeader("User-Agent","CFSCHEDULE");
    // Userame / Password
    Credentials credentials = task.getCredentials();
    String user = null, pass = null;
    if (credentials != null) {
        user = credentials.getUsername();
        pass = credentials.getPassword();
    // get.addRequestHeader("Authorization","Basic admin:spwwn1p");
    }
    // Proxy
    ProxyData proxy = task.getProxyData();
    if (!ProxyDataImpl.isValid(proxy) && config.isProxyEnableFor(task.getUrl().getHost())) {
        proxy = config.getProxyData();
    }
    HTTPResponse rsp = null;
    // execute
    try {
        rsp = HTTPEngine.get(new URL(url), user, pass, task.getTimeout(), true, charset, null, proxy, headers);
    } catch (Exception e) {
        LogUtil.log(log, Log.LEVEL_ERROR, logName, e);
        hasError = true;
    }
    // write file
    Resource file = task.getResource();
    if (!hasError && file != null && task.isPublish()) {
        String n = file.getName();
        if (n.indexOf("{id}") != -1) {
            n = StringUtil.replace(n, "{id}", CreateUUID.invoke(), false);
            file = file.getParentResource().getRealResource(n);
        }
        if (isText(rsp) && task.isResolveURL()) {
            String str;
            try {
                InputStream stream = rsp.getContentAsStream();
                str = stream == null ? "" : IOUtil.toString(stream, (Charset) null);
                if (str == null)
                    str = "";
            } catch (IOException e) {
                str = e.getMessage();
            }
            try {
                str = new URLResolver().transform(str, task.getUrl(), false);
            } catch (PageException e) {
                LogUtil.log(log, Log.LEVEL_ERROR, logName, e);
                hasError = true;
            }
            try {
                IOUtil.write(file, str, charset, false);
            } catch (IOException e) {
                LogUtil.log(log, Log.LEVEL_ERROR, logName, e);
                hasError = true;
            }
        } else {
            try {
                IOUtil.copy(rsp.getContentAsStream(), file, true);
            } catch (IOException e) {
                LogUtil.log(log, Log.LEVEL_ERROR, logName, e);
                hasError = true;
            }
        }
        HTTPEngine.closeEL(rsp);
    }
    if (!hasError)
        log.log(Log.LEVEL_INFO, logName, "executed");
}
Also used : PageException(lucee.runtime.exp.PageException) Log(lucee.commons.io.log.Log) InputStream(java.io.InputStream) HTTPResponse(lucee.commons.net.http.HTTPResponse) Resource(lucee.commons.io.res.Resource) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) PageException(lucee.runtime.exp.PageException) ProxyData(lucee.runtime.net.proxy.ProxyData) Header(lucee.commons.net.http.Header) URLResolver(lucee.runtime.util.URLResolver) Credentials(lucee.commons.security.Credentials)

Aggregations

Resource (lucee.commons.io.res.Resource)2 Credentials (lucee.commons.security.Credentials)2 PageException (lucee.runtime.exp.PageException)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 Log (lucee.commons.io.log.Log)1 HTTPResponse (lucee.commons.net.http.HTTPResponse)1 Header (lucee.commons.net.http.Header)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 ProxyData (lucee.runtime.net.proxy.ProxyData)1 ScheduleTask (lucee.runtime.schedule.ScheduleTask)1 ScheduleTaskImpl (lucee.runtime.schedule.ScheduleTaskImpl)1 URLResolver (lucee.runtime.util.URLResolver)1