use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class ExceptionStatement method execute.
public boolean execute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.execute(javascriptContext, scope)) {
if (evaluated != null) {
String message = "A statement exception has been raised: ";
Throwable t = new Throwable(evaluated.toString());
EngineException ee = new EngineException(message, t);
throw ee;
}
return true;
}
}
return false;
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class AbstractEventStatement method configure.
/* (non-Javadoc)
* @see com.twinsoft.convertigo.beans.core.Statement#configure(org.w3c.dom.Element)
*/
@Override
public void configure(Element element) throws Exception {
super.configure(element);
String version = element.getAttribute("version");
if (version == null) {
String s = XMLUtils.prettyPrintDOM(element);
EngineException ee = new EngineException("Unable to find version number for the database object \"" + getName() + "\".\n" + "XML data: " + s);
throw ee;
}
if (VersionUtils.compare(version, "4.0.4") < 0) {
setXpath("'" + getXpath() + "'");
hasChanged = true;
Engine.logBeans.warn("[HttpStatement] The object \"" + getName() + "\" has been updated to version 4.0.4");
}
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class BasicAuthentication method handleAuthRequest.
@Override
public String handleAuthRequest(HttpServletRequest request, HttpServletResponse response) throws EngineException {
String authRequestableQName = getAuthRequestable();
if (authRequestableQName.isEmpty()) {
throw new EngineException("Authentication \"" + getName() + "\" has no auth requestable defined");
}
HttpSession httpSession = request.getSession();
StringTokenizer st = new StringTokenizer(authRequestableQName, ".");
int count = st.countTokens();
String projectName = st.nextToken();
String sequenceName = count == 2 ? st.nextToken() : "";
String connectorName = count == 3 ? st.nextToken() : "";
String transactionName = count == 3 ? st.nextToken() : "";
String contextName = request.getParameter(Parameter.Context.getName());
try {
String authorization = request.getHeader(HeaderName.Authorization.value());
if (authorization != null) {
Engine.logEngine.debug("(BasicAuthentication) Authorization header found: " + authorization);
// Retrieve credentials
String credentials = authorization.split("\\s")[1];
String[] decoded = Base64.decodeToString(credentials).split(":");
String user = decoded.length > 0 ? decoded[0] : null;
String password = decoded.length > 1 ? decoded[1] : null;
// Check user is authenticated with same credentials
String authenticatedUser = SessionAttribute.authenticatedUser.string(httpSession);
if (authenticatedUser != null && authenticatedUser.equals(user)) {
if (authorization.equals(httpSession.getAttribute("basic-authorization"))) {
Engine.logEngine.debug("(BasicAuthentication) User already authenticated");
return null;
}
}
// Prepare Auth requestable
Map<String, Object> map = new HashMap<String, Object>();
map.put(Parameter.Context.getName(), new String[] { contextName });
map.put(Parameter.Project.getName(), new String[] { projectName });
if (sequenceName.isEmpty()) {
map.put(Parameter.Connector.getName(), new String[] { connectorName });
map.put(Parameter.Transaction.getName(), new String[] { transactionName });
} else {
map.put(Parameter.Sequence.getName(), new String[] { sequenceName });
}
if (user != null) {
map.put("user", user);
}
if (password != null) {
map.put("password", password);
}
// Execute Auth requestable
Engine.logBeans.debug("(BasicAuthentication) Executing requestable \"" + authRequestableQName + "\"");
InternalRequester internalRequester = new InternalRequester(map, request);
request.setAttribute("convertigo.requester", internalRequester);
internalRequester.processRequest();
// Authentication failed
String authUser = SessionAttribute.authenticatedUser.string(httpSession);
if (authUser == null) {
// custom status
Map<Integer, String> status = RequestAttribute.responseStatus.get(request);
if (status.isEmpty()) {
// Set response status code
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
}
} else // Store Authorization
{
if (!authUser.equals(user)) {
Engine.logEngine.warn("(BasicAuthentication) Session " + httpSession.getId() + " has been authenticated with " + authUser + " instead of " + user);
}
httpSession.setAttribute("basic-authorization", authorization);
}
} else {
Engine.logEngine.debug("(BasicAuthentication) Authorization header NOT found.");
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setHeader(HeaderName.Authenticate.value(), "Basic realm=\"" + projectName + " access\"");
}
return null;
} catch (Throwable t) {
request.setAttribute("convertigo.requireEndOfContext", true);
throw new EngineException("Authentication \"" + getName() + "\" failed to retrieve data", t);
}
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class AdminServlet method doRequest.
private void doRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
boolean show_error = false;
try {
show_error = !EnginePropertiesManager.getProperty(PropertyName.HIDING_ERROR_INFORMATION).equals("true");
} catch (Exception e) {
Engine.logAdmin.debug("Failed to retrieve property HIDING_ERROR_INFORMATION: " + e.getClass() + " (" + e.getMessage() + ")");
}
try {
String serviceName = "";
String isAdmin = "";
try {
response.addHeader("Expires", "-1");
response.addHeader("Pragma", "no-cache");
request.setCharacterEncoding("UTF-8");
String requestURL = request.getRequestURL().toString();
int i = requestURL.lastIndexOf('/');
isAdmin = requestURL.substring(0, i).endsWith("/admin/services") ? "admin " : "";
serviceName = requestURL.substring(i + 1);
if (serviceName != null && !serviceName.equals("logs.Get")) {
Engine.logAdmin.info("Service name: " + serviceName);
}
String myPackage = this.getClass().getPackage().getName();
Class<?> serviceClass = Class.forName(myPackage + ".services." + serviceName);
// Check for authentication and roles
ServiceDefinition serviceDefinition = serviceClass.getAnnotation(ServiceDefinition.class);
if (serviceDefinition == null)
throw new IllegalArgumentException("The service '" + serviceName + "' has no service definition!");
if (Engine.isCloudMode()) {
boolean cloud_forbidden = serviceDefinition.cloud_forbidden();
Engine.logAdmin.debug("Is service forbidden for Cloud ? : " + cloud_forbidden);
if (cloud_forbidden) {
throw new EngineException("The service '" + serviceName + "' cannot be acceded on Cloud.");
}
}
if (isAdmin.isEmpty() && serviceDefinition.admin()) {
throw new ClassNotFoundException();
}
String corsOrigin = HttpUtils.applyCorsHeaders(request, response);
if (corsOrigin != null) {
Engine.logAdmin.trace("Add CORS header for: " + corsOrigin);
}
boolean needsAuthentication = !AuthenticatedSessionManager.hasRole(serviceDefinition.roles(), Role.ANONYMOUS);
Engine.logAdmin.debug("Needs authentication: " + needsAuthentication);
if (needsAuthentication) {
Engine.authenticatedSessionManager.checkRoles(request.getSession(false), serviceDefinition.roles());
}
Service service = (Service) serviceClass.getConstructor().newInstance();
try {
boolean xsrfAdmin = EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_ADMIN);
if (xsrfAdmin) {
if (!serviceDefinition.allow_cors() || EnginePropertiesManager.getPropertyAsBoolean(PropertyName.XSRF_API)) {
HttpUtils.checkXSRF(request, response);
}
}
} catch (IllegalStateException e) {
Engine.logAdmin.warn("Cannot retrieve properties for XSRF, Engine probably stopped.");
}
service.run(serviceName, request, response);
} catch (ClassNotFoundException e) {
String message = "Unknown " + isAdmin + "service '" + serviceName + "'";
Engine.logAdmin.error(message);
if (show_error) {
ServiceUtils.handleError(message, request, response);
}
} catch (NoClassDefFoundError e) {
String message = "Unknown " + isAdmin + "service '" + serviceName + "'";
Engine.logAdmin.error(message);
if (show_error) {
ServiceUtils.handleError(message, request, response);
}
} catch (AuthenticationException e) {
String authMessage = e.getMessage();
Engine.logAdmin.warn(authMessage);
if (show_error) {
ServiceUtils.handleError(authMessage, request, response);
}
} catch (Exception e) {
Engine.logAdmin.error("Unable to execute the service '" + serviceName + "'", e);
if (show_error) {
ServiceUtils.handleError(e, request, response);
}
} finally {
response.flushBuffer();
}
} catch (Throwable e) {
if (show_error) {
throw new ServletException(e);
}
}
}
use of com.twinsoft.convertigo.engine.EngineException in project convertigo by convertigo.
the class Export method writeResponseResult.
@Override
protected void writeResponseResult(HttpServletRequest request, HttpServletResponse response) throws IOException, EngineException {
String projectName = request.getParameter("projectName");
String exportOptionsTxt = request.getParameter("exportOptions");
HeaderName.ContentDisposition.setHeader(response, "attachment; filename=\"" + projectName + ".car\"");
response.setContentType(MimeType.Zip.value());
// if any, backup existing CAR file
File f = new File(Engine.projectDir(projectName) + ".car");
long lastDate = -1;
if (f.exists()) {
lastDate = f.lastModified();
File oldFile = new File(Engine.projectDir(projectName) + ".car.old");
FileUtils.deleteQuietly(oldFile);
f.renameTo(oldFile);
}
if (!Engine.theApp.databaseObjectsManager.existsProject(projectName)) {
throw new IllegalArgumentException("The project '" + projectName + "' does not exist!");
}
Set<ArchiveExportOption> exportOptions = ArchiveExportOption.all;
if (exportOptionsTxt != null) {
try {
JSONObject exportOptionsJson = new JSONObject(exportOptionsTxt);
exportOptions = new HashSet<ArchiveExportOption>(exportOptions);
for (Iterator<?> i = exportOptionsJson.keys(); i.hasNext(); ) {
String key = (String) i.next();
try {
ArchiveExportOption opt = ArchiveExportOption.valueOf(key);
if (!exportOptionsJson.getBoolean(key)) {
exportOptions.remove(opt);
}
} catch (Exception e) {
Engine.logAdmin.warn("Export cannot find ArchiveExportOption:" + key);
}
}
} catch (JSONException e) {
throw new EngineException("Export unable to parse JSON exportOptions: " + exportOptionsTxt);
}
}
// build a new CAR file from project directory
f = CarUtils.makeArchive(projectName, exportOptions);
// upload CAR file to admin
HeaderName.ContentLength.setHeader(response, "" + f.length());
if (f.exists()) {
if (lastDate > 0) {
f.setLastModified(lastDate);
}
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[1024];
int nbReadBytes;
while ((nbReadBytes = bis.read(buffer, 0, 1024)) > 0) {
outStream.write(buffer, 0, nbReadBytes);
}
bis.close();
}
Engine.logAdmin.info("The project '" + projectName + "' has been exported");
}
Aggregations