Search in sources :

Example 1 with RestRequestListener

use of lucee.runtime.rest.RestRequestListener in project Lucee by lucee.

the class PageContextImpl method executeRest.

@Override
public void executeRest(String realPath, boolean throwExcpetion) throws PageException {
    initallog();
    // config.get ApplicationListener();
    ApplicationListener listener = null;
    try {
        String pathInfo = req.getPathInfo();
        // charset
        try {
            String charset = HTTPUtil.splitMimeTypeAndCharset(req.getContentType(), new String[] { "", "" })[1];
            if (StringUtil.isEmpty(charset))
                charset = getWebCharset().name();
            java.net.URL reqURL = new java.net.URL(req.getRequestURL().toString());
            String path = ReqRspUtil.decode(reqURL.getPath(), charset, true);
            String srvPath = req.getServletPath();
            if (path.startsWith(srvPath)) {
                pathInfo = path.substring(srvPath.length());
            }
        } catch (Exception e) {
        }
        // Service mapping
        if (StringUtil.isEmpty(pathInfo) || pathInfo.equals("/")) {
            // list available services (if enabled in admin)
            if (config.getRestList()) {
                try {
                    HttpServletRequest _req = getHttpServletRequest();
                    write("Available sevice mappings are:<ul>");
                    lucee.runtime.rest.Mapping[] mappings = config.getRestMappings();
                    lucee.runtime.rest.Mapping _mapping;
                    String path;
                    for (int i = 0; i < mappings.length; i++) {
                        _mapping = mappings[i];
                        Resource p = _mapping.getPhysical();
                        path = _req.getContextPath() + ReqRspUtil.getScriptName(this, _req) + _mapping.getVirtual();
                        write("<li " + (p == null || !p.isDirectory() ? " style=\"color:red\"" : "") + ">" + path + "</li>");
                    }
                    write("</ul>");
                } catch (IOException e) {
                    throw Caster.toPageException(e);
                }
            } else
                RestUtil.setStatus(this, 404, null);
            return;
        }
        // check for matrix
        int index;
        String entry;
        Struct matrix = new StructImpl();
        while ((index = pathInfo.lastIndexOf(';')) != -1) {
            entry = pathInfo.substring(index + 1);
            pathInfo = pathInfo.substring(0, index);
            if (StringUtil.isEmpty(entry, true))
                continue;
            index = entry.indexOf('=');
            if (index != -1)
                matrix.setEL(KeyImpl.init(entry.substring(0, index).trim()), entry.substring(index + 1).trim());
            else
                matrix.setEL(KeyImpl.init(entry.trim()), "");
        }
        // get accept
        List<MimeType> accept = ReqRspUtil.getAccept(this);
        MimeType contentType = ReqRspUtil.getContentType(this);
        // check for format extension
        // int format = getApplicationContext().getRestSettings().getReturnFormat();
        int format;
        boolean hasFormatExtension = false;
        if (StringUtil.endsWithIgnoreCase(pathInfo, ".json")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 5);
            format = UDF.RETURN_FORMAT_JSON;
            accept.clear();
            accept.add(MimeType.APPLICATION_JSON);
            hasFormatExtension = true;
        } else if (StringUtil.endsWithIgnoreCase(pathInfo, ".wddx")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 5);
            format = UDF.RETURN_FORMAT_WDDX;
            accept.clear();
            accept.add(MimeType.APPLICATION_WDDX);
            hasFormatExtension = true;
        } else if (StringUtil.endsWithIgnoreCase(pathInfo, ".cfml")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 5);
            format = UDF.RETURN_FORMAT_SERIALIZE;
            accept.clear();
            accept.add(MimeType.APPLICATION_CFML);
            hasFormatExtension = true;
        } else if (StringUtil.endsWithIgnoreCase(pathInfo, ".serialize")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 10);
            format = UDF.RETURN_FORMAT_SERIALIZE;
            accept.clear();
            accept.add(MimeType.APPLICATION_CFML);
            hasFormatExtension = true;
        } else if (StringUtil.endsWithIgnoreCase(pathInfo, ".xml")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 4);
            format = UDF.RETURN_FORMAT_XML;
            accept.clear();
            accept.add(MimeType.APPLICATION_XML);
            hasFormatExtension = true;
        } else if (StringUtil.endsWithIgnoreCase(pathInfo, ".java")) {
            pathInfo = pathInfo.substring(0, pathInfo.length() - 5);
            format = UDFPlus.RETURN_FORMAT_JAVA;
            accept.clear();
            accept.add(MimeType.APPLICATION_JAVA);
            hasFormatExtension = true;
        } else {
            format = getApplicationContext() == null ? null : getApplicationContext().getRestSettings().getReturnFormat();
        // MimeType mt=MimeType.toMimetype(format);
        // if(mt!=null)accept.add(mt);
        }
        if (accept.size() == 0)
            accept.add(MimeType.ALL);
        // loop all mappings
        // lucee.runtime.rest.Result result = null;//config.getRestSource(pathInfo, null);
        RestRequestListener rl = null;
        lucee.runtime.rest.Mapping[] restMappings = config.getRestMappings();
        lucee.runtime.rest.Mapping m, mapping = null, defaultMapping = null;
        // String callerPath=null;
        if (restMappings != null)
            for (int i = 0; i < restMappings.length; i++) {
                m = restMappings[i];
                if (m.isDefault())
                    defaultMapping = m;
                if (pathInfo.startsWith(m.getVirtualWithSlash(), 0) && m.getPhysical() != null) {
                    mapping = m;
                    // result = m.getResult(this,callerPath=pathInfo.substring(m.getVirtual().length()),format,matrix,null);
                    rl = new RestRequestListener(m, pathInfo.substring(m.getVirtual().length()), matrix, format, hasFormatExtension, accept, contentType, null);
                    break;
                }
            }
        // default mapping
        if (mapping == null && defaultMapping != null && defaultMapping.getPhysical() != null) {
            mapping = defaultMapping;
            // result = mapping.getResult(this,callerPath=pathInfo,format,matrix,null);
            rl = new RestRequestListener(mapping, pathInfo, matrix, format, hasFormatExtension, accept, contentType, null);
        }
        if (mapping == null || mapping.getPhysical() == null) {
            RestUtil.setStatus(this, 404, "no rest service for [" + pathInfo + "] found");
            getConfig().getLog("rest").error("REST", "no rest service for [" + pathInfo + "] found");
        } else {
            base = config.toPageSource(null, mapping.getPhysical(), null);
            listener = ((MappingImpl) base.getMapping()).getApplicationListener();
            listener.onRequest(this, base, rl);
        }
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        PageException pe = Caster.toPageException(t);
        if (!Abort.isSilentAbort(pe)) {
            log(true);
            if (fdEnabled) {
                FDSignal.signal(pe, false);
            }
            if (listener == null) {
                if (base == null)
                    listener = config.getApplicationListener();
                else
                    listener = ((MappingImpl) base.getMapping()).getApplicationListener();
            }
            listener.onError(this, pe);
        } else
            log(false);
        if (throwExcpetion)
            throw pe;
    } finally {
        if (enablecfoutputonly > 0) {
            setCFOutputOnly((short) 0);
        }
        base = null;
    }
}
Also used : URL(lucee.runtime.type.scope.URL) MimeType(lucee.commons.lang.mimetype.MimeType) Struct(lucee.runtime.type.Struct) HttpServletRequest(javax.servlet.http.HttpServletRequest) PageException(lucee.runtime.exp.PageException) RestRequestListener(lucee.runtime.rest.RestRequestListener) Resource(lucee.commons.io.res.Resource) IOException(java.io.IOException) PageException(lucee.runtime.exp.PageException) CasterException(lucee.runtime.exp.CasterException) MissingIncludeException(lucee.runtime.exp.MissingIncludeException) JspException(javax.servlet.jsp.JspException) IOException(java.io.IOException) CacheException(lucee.commons.io.cache.exp.CacheException) DatabaseException(lucee.runtime.exp.DatabaseException) ServletException(javax.servlet.ServletException) ModernAppListenerException(lucee.runtime.listener.ModernAppListenerException) RequestTimeoutException(lucee.runtime.exp.RequestTimeoutException) ExpressionException(lucee.runtime.exp.ExpressionException) ApplicationException(lucee.runtime.exp.ApplicationException) StructImpl(lucee.runtime.type.StructImpl) ApplicationListener(lucee.runtime.listener.ApplicationListener)

Aggregations

IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JspException (javax.servlet.jsp.JspException)1 CacheException (lucee.commons.io.cache.exp.CacheException)1 Resource (lucee.commons.io.res.Resource)1 MimeType (lucee.commons.lang.mimetype.MimeType)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 CasterException (lucee.runtime.exp.CasterException)1 DatabaseException (lucee.runtime.exp.DatabaseException)1 ExpressionException (lucee.runtime.exp.ExpressionException)1 MissingIncludeException (lucee.runtime.exp.MissingIncludeException)1 PageException (lucee.runtime.exp.PageException)1 RequestTimeoutException (lucee.runtime.exp.RequestTimeoutException)1 ApplicationListener (lucee.runtime.listener.ApplicationListener)1 ModernAppListenerException (lucee.runtime.listener.ModernAppListenerException)1 RestRequestListener (lucee.runtime.rest.RestRequestListener)1 Struct (lucee.runtime.type.Struct)1 StructImpl (lucee.runtime.type.StructImpl)1 URL (lucee.runtime.type.scope.URL)1