use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class CacheRegionNew method _call.
static String _call(PageContext pc, String cacheName, Struct properties, Boolean throwOnError, String strWebAdminPassword) throws PageException {
Password webAdminPassword = CacheUtil.getPassword(pc, strWebAdminPassword, false);
try {
// TODO why we have here EHCache?
XMLConfigAdmin adminConfig = XMLConfigAdmin.newInstance((ConfigWebImpl) pc.getConfig(), webAdminPassword);
adminConfig.updateCacheConnection(cacheName, new ClassDefinitionImpl("org.lucee.extension.cache.eh.EHCache", null, null, pc.getConfig().getIdentification()), Config.CACHE_TYPE_NONE, properties, false, false);
adminConfig.storeAndReload();
} catch (Exception e) {
if (throwOnError)
throw Caster.toPageException(e);
}
return null;
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class FDVariableComparator method getVariables.
private static List getVariables(FDStackFrameImpl frame, PageContextImpl pc, List list, String strScope) throws FDLanguageException {
Scope scope;
try {
scope = pc.scope(strScope, null);
if (scope != null)
return copyValues(frame, list, scope);
Object value = pc.undefinedScope().get(strScope, null);
if (value != null) {
if (value instanceof Struct)
return copyValues(frame, new ArrayList(), (Struct) value);
throw new FDLanguageException("[" + strScope + "] is not of type scope, type is [" + Caster.toTypeName(value) + "]");
}
throw new FDLanguageException("[" + strScope + "] does not exist in the current context");
} catch (PageException e) {
throw new FDLanguageException(e);
}
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class CFMLFactoryImpl method stopThread.
public void stopThread(String threadId, String stopType) {
// synchronized (runningPcs) {
Iterator<PageContextImpl> it = runningPcs.values().iterator();
PageContext pc;
while (it.hasNext()) {
pc = it.next();
Log log = ((ConfigImpl) pc.getConfig()).getLog("application");
try {
String id = Hash.call(pc, pc.getId() + ":" + pc.getStartTime());
if (id.equals(threadId)) {
stopType = stopType.trim();
// Throwable t;
if ("abort".equalsIgnoreCase(stopType) || "cfabort".equalsIgnoreCase(stopType))
throw new RuntimeException("type [" + stopType + "] is no longer supported");
// t=new Abort(Abort.SCOPE_REQUEST);
// else t=new RequestTimeoutException(pc.getThread(),"request has been forced to stop.");
SystemUtil.stop(pc, log, true);
SystemUtil.sleep(10);
break;
}
} catch (PageException e1) {
}
}
// }
}
use of lucee.runtime.exp.PageException in project Lucee by lucee.
the class Props method callRest.
private void callRest(PageContext pc, Component component, String path, Result result, boolean suppressContent) throws IOException, ConverterException {
String method = pc.getHttpServletRequest().getMethod();
String[] subPath = result.getPath();
Struct cMeta;
try {
cMeta = component.getMetaData(pc);
} catch (PageException pe) {
throw ExceptionUtil.toIOException(pe);
}
// Consumes
MimeType[] cConsumes = null;
String strMimeType = Caster.toString(cMeta.get(KeyConstants._consumes, null), null);
if (!StringUtil.isEmpty(strMimeType, true)) {
cConsumes = MimeType.getInstances(strMimeType, ',');
}
// Produces
MimeType[] cProduces = null;
strMimeType = Caster.toString(cMeta.get(KeyConstants._produces, null), null);
if (!StringUtil.isEmpty(strMimeType, true)) {
cProduces = MimeType.getInstances(strMimeType, ',');
}
Iterator<Entry<Key, Object>> it = component.entryIterator();
Entry<Key, Object> e;
Object value;
UDF udf;
Struct meta;
int status = 404;
MimeType bestP, bestC;
while (it.hasNext()) {
e = it.next();
value = e.getValue();
if (value instanceof UDF) {
udf = (UDF) value;
try {
meta = udf.getMetaData(pc);
// check if http method match
String httpMethod = Caster.toString(meta.get(KeyConstants._httpmethod, null), null);
if (StringUtil.isEmpty(httpMethod) || !httpMethod.equalsIgnoreCase(method))
continue;
// get consumes mimetype
MimeType[] consumes;
strMimeType = Caster.toString(meta.get(KeyConstants._consumes, null), null);
if (!StringUtil.isEmpty(strMimeType, true)) {
consumes = MimeType.getInstances(strMimeType, ',');
} else
consumes = cConsumes;
// get produces mimetype
MimeType[] produces;
strMimeType = Caster.toString(meta.get(KeyConstants._produces, null), null);
if (!StringUtil.isEmpty(strMimeType, true)) {
produces = MimeType.getInstances(strMimeType, ',');
} else
produces = cProduces;
String restPath = Caster.toString(meta.get(KeyConstants._restPath, null), null);
// no rest path
if (StringUtil.isEmpty(restPath)) {
if (ArrayUtil.isEmpty(subPath)) {
bestC = best(consumes, result.getContentType());
bestP = best(produces, result.getAccept());
if (bestC == null)
status = 405;
else if (bestP == null)
status = 406;
else {
status = 200;
_callRest(pc, component, udf, path, result.getVariables(), result, bestP, produces, suppressContent, e.getKey());
break;
}
}
} else {
Struct var = result.getVariables();
int index = RestUtil.matchPath(var, Path.init(restPath), /*TODO cache this*/
result.getPath());
if (index >= 0 && index + 1 == result.getPath().length) {
bestC = best(consumes, result.getContentType());
bestP = best(produces, result.getAccept());
if (bestC == null)
status = 405;
else if (bestP == null)
status = 406;
else {
status = 200;
_callRest(pc, component, udf, path, var, result, bestP, produces, suppressContent, e.getKey());
break;
}
}
}
} catch (PageException pe) {
pc.getConfig().getLog("rest").error("REST", pe);
}
}
}
if (status == 404) {
RestUtil.setStatus(pc, 404, "no rest service for [" + path + "] found");
pc.getConfig().getLog("rest").error("REST", "404; no rest service for [" + path + "] found");
} else if (status == 405) {
RestUtil.setStatus(pc, 405, "Unsupported Media Type");
pc.getConfig().getLog("rest").error("REST", "405; Unsupported Media Type");
} else if (status == 406) {
RestUtil.setStatus(pc, 406, "Not Acceptable");
pc.getConfig().getLog("rest").error("REST", "406; Not Acceptable");
}
}
use of lucee.runtime.exp.PageException 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;
}
}
Aggregations