Search in sources :

Example 1 with ApplicationException

use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.

the class HTTPClient method getMetaData.

private Struct getMetaData(PageContext pc) {
    if (meta == null) {
        pc = ThreadLocalPageContext.get(pc);
        InputStream is = null;
        HTTPResponse rsp = null;
        try {
            rsp = HTTPEngine.get(metaURL, username, password, -1, false, "UTF-8", createUserAgent(pc), proxyData, null);
            MimeType mt = getMimeType(rsp, null);
            int format = MimeType.toFormat(mt, -1);
            if (format == -1)
                throw new ApplicationException("cannot convert response with mime type [" + mt + "] to a CFML Object");
            is = rsp.getContentAsStream();
            Struct data = Caster.toStruct(ReqRspUtil.toObject(pc, IOUtil.toBytes(is, false), format, mt.getCharset(), null));
            Object oUDF = data.get(KeyConstants._functions, null);
            Object oAACF = data.get(ComponentPageImpl.ACCEPT_ARG_COLL_FORMATS, null);
            if (oUDF != null && oAACF != null) {
                meta = Caster.toStruct(oUDF);
                String[] strFormats = ListUtil.listToStringArray(Caster.toString(oAACF), ',');
                argumentsCollectionFormat = UDFUtil.toReturnFormat(strFormats, UDF.RETURN_FORMAT_JSON);
            } else {
                meta = data;
            }
        } catch (Throwable t) {
            ExceptionUtil.rethrowIfNecessary(t);
            throw new PageRuntimeException(Caster.toPageException(t));
        } finally {
            IOUtil.closeEL(is);
            HTTPEngine.closeEL(rsp);
        }
    }
    return meta;
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) InputStream(java.io.InputStream) HTTPResponse(lucee.commons.net.http.HTTPResponse) PageRuntimeException(lucee.runtime.exp.PageRuntimeException) MimeType(lucee.commons.lang.mimetype.MimeType) Struct(lucee.runtime.type.Struct)

Example 2 with ApplicationException

use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.

the class HTTPClient method _callWithNamedValues.

private Object _callWithNamedValues(PageContext pc, Key methodName, Struct args) throws PageException {
    // prepare request
    Map<String, String> formfields = new HashMap<String, String>();
    formfields.put("method", methodName.getString());
    formfields.put("returnformat", "cfml");
    String str;
    try {
        if (UDF.RETURN_FORMAT_JSON == argumentsCollectionFormat) {
            Charset cs = pc.getWebCharset();
            str = new JSONConverter(true, cs).serialize(pc, args, false);
            formfields.put("argumentCollectionFormat", "json");
        } else if (UDF.RETURN_FORMAT_SERIALIZE == argumentsCollectionFormat) {
            str = new ScriptConverter().serialize(args);
            formfields.put("argumentCollectionFormat", "cfml");
        } else {
            // Json interpreter also accepts cfscript
            str = new ScriptConverter().serialize(args);
        }
    } catch (ConverterException e) {
        throw Caster.toPageException(e);
    }
    // add aparams to request
    formfields.put("argumentCollection", str);
    /*
		Iterator<Entry<Key, Object>> it = args.entryIterator();
		Entry<Key, Object> e;
		while(it.hasNext()){
			e = it.next();
			formfields.put(e.getKey().getString(), Caster.toString(e.getValue()));
		}*/
    Map<String, String> headers = new HashMap<String, String>();
    // application/java disabled for the moment, it is not working when we have different lucee versions
    headers.put("accept", "application/cfml,application/json");
    HTTPResponse rsp = null;
    InputStream is = null;
    try {
        // call remote cfc
        rsp = HTTPEngine.post(url, username, password, -1, false, "UTF-8", createUserAgent(pc), proxyData, headers, formfields);
        // read result
        Header[] rspHeaders = rsp.getAllHeaders();
        MimeType mt = getMimeType(rspHeaders, null);
        int format = MimeType.toFormat(mt, -1);
        if (format == -1) {
            if (rsp.getStatusCode() != 200) {
                boolean hasMsg = false;
                String msg = rsp.getStatusText();
                for (int i = 0; i < rspHeaders.length; i++) {
                    if (rspHeaders[i].getName().equalsIgnoreCase("exception-message")) {
                        msg = rspHeaders[i].getValue();
                        hasMsg = true;
                    }
                }
                is = rsp.getContentAsStream();
                ApplicationException ae = new ApplicationException("remote component throws the following error:" + msg);
                if (!hasMsg)
                    ae.setAdditional(KeyImpl.init("respone-body"), IOUtil.toString(is, mt.getCharset()));
                throw ae;
            }
            throw new ApplicationException("cannot convert response with mime type [" + mt + "] to a CFML Object");
        }
        is = rsp.getContentAsStream();
        return ReqRspUtil.toObject(pc, IOUtil.toBytes(is, false), format, mt.getCharset(), null);
    } catch (IOException ioe) {
        throw Caster.toPageException(ioe);
    } finally {
        IOUtil.closeEL(is);
        HTTPEngine.closeEL(rsp);
    }
}
Also used : ConverterException(lucee.runtime.converter.ConverterException) HashMap(java.util.HashMap) InputStream(java.io.InputStream) HTTPResponse(lucee.commons.net.http.HTTPResponse) Charset(java.nio.charset.Charset) IOException(java.io.IOException) MimeType(lucee.commons.lang.mimetype.MimeType) ApplicationException(lucee.runtime.exp.ApplicationException) Header(lucee.commons.net.http.Header) ScriptConverter(lucee.runtime.converter.ScriptConverter) JSONConverter(lucee.runtime.converter.JSONConverter)

Example 3 with ApplicationException

use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.

the class MailClient method renameFolder.

public void renameFolder(String srcFolderName, String trgFolderName) throws MessagingException, ApplicationException {
    if (srcFolderName.equalsIgnoreCase("INBOX") || srcFolderName.equalsIgnoreCase("OUTBOX"))
        throw new ApplicationException("cannot rename folder [" + srcFolderName + "], this folder is protected.");
    if (trgFolderName.equalsIgnoreCase("INBOX") || trgFolderName.equalsIgnoreCase("OUTBOX"))
        throw new ApplicationException("cannot rename folder to [" + trgFolderName + "], this folder name is protected.");
    Folder src = getFolder(srcFolderName, true, true, false);
    Folder trg = getFolder(trgFolderName, null, false, true);
    if (!src.renameTo(trg))
        throw new ApplicationException("cannot rename folder [" + srcFolderName + "] to [" + trgFolderName + "].");
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) Folder(javax.mail.Folder)

Example 4 with ApplicationException

use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.

the class MailClient method getInstance.

public static MailClient getInstance(int type, String server, int port, String username, String password, boolean secure, String name, String id) throws Exception {
    String uid;
    if (StringUtil.isEmpty(name))
        uid = createName(type, server, port, username, password, secure);
    else
        uid = name;
    uid = type + ";" + uid + ";" + id;
    PoolItem item = pool.get(uid);
    if (item == null) {
        if (StringUtil.isEmpty(server)) {
            if (StringUtil.isEmpty(name))
                throw new ApplicationException("missing server information");
            else
                throw new ApplicationException("there is no connection available with name [" + name + "]");
        }
        if (TYPE_POP3 == type)
            pool.put(uid, item = new PopClient(server, port, username, password, secure));
        if (TYPE_IMAP == type)
            pool.put(uid, item = new ImapClient(server, port, username, password, secure));
    }
    return (MailClient) item;
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) ImapClient(lucee.runtime.net.imap.ImapClient) PopClient(lucee.runtime.net.pop.PopClient) PoolItem(lucee.runtime.pool.PoolItem)

Example 5 with ApplicationException

use of lucee.runtime.exp.ApplicationException in project Lucee by lucee.

the class HtmlHeadBodyBase method processTag.

protected void processTag() throws PageException {
    try {
        if (StringUtil.isEmpty(action, true) || action.equals("append")) {
            required(getTagName(), "text", text);
            if (isValid())
                actionAppend();
        } else if (action.equals("reset")) {
            resetIdMap();
            actionReset();
        } else if (action.equals("write")) {
            required(getTagName(), "text", text);
            resetIdMap();
            if (// call isValid() to register the id if set
            isValid())
                actionWrite();
        } else if (action.equals("read"))
            actionRead();
        else if (action.equals("flush"))
            actionFlush();
        else
            throw new ApplicationException("invalid value [" + action + "] for attribute action", "values for attribute action are:append,read,reset,write");
    } catch (IOException e) {
        throw Caster.toPageException(e);
    }
}
Also used : ApplicationException(lucee.runtime.exp.ApplicationException) IOException(java.io.IOException)

Aggregations

ApplicationException (lucee.runtime.exp.ApplicationException)173 IOException (java.io.IOException)41 Resource (lucee.commons.io.res.Resource)36 PageException (lucee.runtime.exp.PageException)30 Struct (lucee.runtime.type.Struct)25 SecurityException (lucee.runtime.exp.SecurityException)17 BundleException (org.osgi.framework.BundleException)16 StructImpl (lucee.runtime.type.StructImpl)15 MalformedURLException (java.net.MalformedURLException)13 Element (org.w3c.dom.Element)13 Array (lucee.runtime.type.Array)12 Key (lucee.runtime.type.Collection.Key)12 Iterator (java.util.Iterator)11 InputStream (java.io.InputStream)10 Query (lucee.runtime.type.Query)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 ExpressionException (lucee.runtime.exp.ExpressionException)9 Entry (java.util.Map.Entry)8 PageContextImpl (lucee.runtime.PageContextImpl)8 ClassDefinition (lucee.runtime.db.ClassDefinition)8