Search in sources :

Example 11 with Request

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));
}
Also used : ServletContext(javax.servlet.ServletContext) Context(org.thymeleaf.context.Context) UserAwareMeta(com.developmentontheedge.be5.api.helpers.UserAwareMeta)

Example 12 with Request

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);
}
Also used : UserInfo(com.developmentontheedge.be5.model.UserInfo) Session(com.developmentontheedge.be5.api.Session)

Example 13 with Request

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);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Example 14 with Request

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());
    }
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel) ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData)

Example 15 with Request

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());
    }
}
Also used : ResourceData(com.developmentontheedge.be5.model.jsonapi.ResourceData) StaticPagePresentation(com.developmentontheedge.be5.model.StaticPagePresentation) ErrorModel(com.developmentontheedge.be5.model.jsonapi.ErrorModel)

Aggregations

Request (com.developmentontheedge.be5.api.Request)8 Response (com.developmentontheedge.be5.api.Response)6 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)5 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)5 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)5 Test (org.junit.Test)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)3 Query (com.developmentontheedge.be5.metadata.model.Query)3 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)3 Map (java.util.Map)3 Session (com.developmentontheedge.be5.api.Session)2 UserHelper (com.developmentontheedge.be5.api.helpers.UserHelper)2 RequestImpl (com.developmentontheedge.be5.api.impl.RequestImpl)2 StaticPagePresentation (com.developmentontheedge.be5.model.StaticPagePresentation)2 UserInfo (com.developmentontheedge.be5.model.UserInfo)2 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2