use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.
the class TemplateProcessor method generate.
@Override
public void generate(Request req, Response res, Injector injector) {
UserAwareMeta userAwareMeta = injector.get(UserAwareMeta.class);
String title = userAwareMeta.getColumnTitle("index", "page", "title");
String description = userAwareMeta.getColumnTitle("index", "page", "description");
Context context = new Context();
context.setVariable("lang", UserInfoHolder.getLanguage());
context.setVariable("title", title);
context.setVariable("description", description);
String reqWithoutContext = req.getRequestUri().replaceFirst(req.getContextPath(), "");
if (!reqWithoutContext.endsWith("/"))
reqWithoutContext += "/";
context.setVariable("baseUrl", req.getContextPath() + reqWithoutContext);
context.setVariable("baseUrlWithoutContext", reqWithoutContext);
res.sendHtml(templateEngine.process(reqWithoutContext + "index", context));
}
use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.
the class LoginServiceImpl method saveUser.
@Override
public void saveUser(String username, Request req) {
List<String> availableRoles = selectAvailableRoles(username);
if (ModuleLoader2.getDevRoles().size() > 0) {
availableRoles.addAll(ModuleLoader2.getDevRoles());
}
String savedRoles = coreUtils.getUserSetting(username, DatabaseConstants.CURRENT_ROLE_LIST);
List<String> currentRoles;
if (savedRoles != null) {
currentRoles = parseRoles(savedRoles);
} else {
currentRoles = availableRoles;
}
UserInfo ui = userHelper.saveUser(username, availableRoles, currentRoles, req.getLocale(), req.getRemoteAddr(), req.getSession());
Session session = req.getSession();
session.set("remoteAddr", req.getRemoteAddr());
session.set(SessionConstants.USER_INFO, ui);
session.set(SessionConstants.CURRENT_USER, ui.getUserName());
log.fine("Login user: " + username);
}
use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.
the class DownloadComponent method generate.
@Override
public void generate(Request req, Response res, Injector injector) {
String entity = req.getNonEmpty("_t_");
String ID = req.get("ID");
String typeColumn = req.get("_typeColumn_");
String filenameColumn = req.get("_filenameColumn_");
String dataColumn = req.getNonEmpty("_dataColumn_");
String charsetColumn = req.get("_charsetColumn_");
// String encoding = req.get("_enc_");
boolean download = "yes".equals(req.get("_download_"));
RecordModel record = injector.get(DatabaseModel.class).getEntity(entity).get(ID);
String filename = record.getValueAsString(filenameColumn);
String contentType = record.getValueAsString(typeColumn);
String charset = MoreObjects.firstNonNull(record.getValueAsString(charsetColumn), Charsets.UTF_8.name());
Object data = record.getValue(dataColumn);
InputStream in;
if (data instanceof byte[]) {
in = new ByteArrayInputStream((byte[]) data);
} else // else if (data instanceof Blob)
// {
// in = ((Blob) data).getBinaryStream();
// }
// else if (data instanceof String)
// {
// in = new ByteArrayInputStream(((String) data).getBytes(charset));
// }
{
throw Be5Exception.internal("Unknown data type");
}
HttpServletResponse response = res.getRawResponse();
response.setContentType(contentType + "; charset=" + charset);
if (download) {
response.setHeader("Content-disposition", "attachment; filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
} else {
response.setHeader("Content-disposition", "filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
}
try {
ByteStreams.copy(in, response.getOutputStream());
} catch (IOException e) {
throw Be5Exception.internal(e);
}
}
use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.
the class QueryBuilder method generate.
@Override
public void generate(Request req, Response res, Injector injector) {
if (UserInfoHolder.isSystemDeveloper()) {
String sql = req.get("sql");
boolean execute = sql != null;
List<String> history;
if (req.getAttribute(QUERY_BUILDER_HISTORY) != null) {
history = (List<String>) req.getAttribute(QUERY_BUILDER_HISTORY);
} else {
history = new ArrayList<>();
}
if (sql == null) {
if (!history.isEmpty()) {
sql = history.get(history.size() - 1);
} else {
sql = "select * from users";
}
} else {
if (history.isEmpty() || !history.get(history.size() - 1).equals(sql)) {
history.add(sql);
req.setAttribute(QUERY_BUILDER_HISTORY, history);
}
}
ResourceData resourceData = new ResourceData("queryBuilder", new Data(sql, history), Collections.singletonMap(SELF_LINK, "queryBuilder"));
try {
SqlType type = getSqlType(sql);
if (type == SqlType.SELECT) {
select(sql, req, injector);
} else {
if (execute) {
switch(type) {
case INSERT:
insert(sql, injector);
break;
case UPDATE:
update(sql, injector);
break;
case DELETE:
update(sql, injector);
break;
default:
res.sendUnknownActionError();
return;
}
}
}
} catch (Throwable e) {
errorModelList.add(new ErrorModel(Be5Exception.internal(e)));
}
res.sendAsJson(JsonApiModel.data(resourceData, errorModelList.toArray(new ErrorModel[0]), resourceDataList.toArray(new ResourceData[0]), req.getDefaultMeta(), null));
} else {
res.sendErrorAsJson(new ErrorModel(Be5Exception.accessDenied(), "Role " + RoleType.ROLE_SYSTEM_DEVELOPER + " required.", Collections.singletonMap(SELF_LINK, "queryBuilder")), req.getDefaultMeta());
}
}
use of com.developmentontheedge.be5.api.Request in project be5 by DevelopmentOnTheEdge.
the class StaticPageComponent method generate.
@Override
public void generate(Request req, Response res, Injector injector) {
String language = UserInfoHolder.getLanguage();
String page = req.getRequestUri();
String staticPageContent = injector.getProject().getStaticPageContent(language, page);
if (staticPageContent == null) {
// todo localize
res.sendErrorAsJson(new ErrorModel("500", ErrorTitles.formatTitle(Be5ErrorCode.NOT_FOUND, page), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
} else {
res.sendAsJson(new ResourceData(STATIC_ACTION, new StaticPagePresentation("", staticPageContent), Collections.singletonMap(SELF_LINK, "static/" + page)), req.getDefaultMeta());
}
}
Aggregations