Search in sources :

Example 16 with Response

use of com.developmentontheedge.be5.api.Response 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 17 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class UserInfoComponentTest method generate.

@Test
public void generate() throws Exception {
    Response response = mock(Response.class);
    component.generate(getMockRequest(""), response, injector);
    verify(response).sendAsRawJson(new UserInfoComponent.State(true, TEST_USER, Collections.singletonList(RoleType.ROLE_ADMINISTRATOR), Collections.singletonList(RoleType.ROLE_ADMINISTRATOR), any(Date.class)));
}
Also used : Response(com.developmentontheedge.be5.api.Response) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

Example 18 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class UserInfoComponentTest method testGuest.

@Test
public void testGuest() throws Exception {
    userHelper.initGuest(null);
    Response response = mock(Response.class);
    component.generate(getMockRequest(""), response, injector);
    verify(response).sendAsRawJson(new UserInfoComponent.State(false, RoleType.ROLE_GUEST, Collections.singletonList(RoleType.ROLE_GUEST), Collections.singletonList(RoleType.ROLE_GUEST), any(Date.class)));
}
Also used : Response(com.developmentontheedge.be5.api.Response) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

Example 19 with Response

use of com.developmentontheedge.be5.api.Response in project be5 by DevelopmentOnTheEdge.

the class UserInfoComponentTest method generateSelectRolesAndSendNewState.

@Test
public void generateSelectRolesAndSendNewState() throws Exception {
    Response response = mock(Response.class);
    component.generate(getSpyMockRequest("selectRoles", ImmutableMap.of("roles", RoleType.ROLE_ADMINISTRATOR)), response, injector);
    verify(response).sendAsRawJson(eq(ImmutableList.of(RoleType.ROLE_ADMINISTRATOR)));
    verify(SqlServiceMock.mock).update("UPDATE user_prefs SET pref_value = ? WHERE pref_name = ? AND user_name = ?", "('Administrator')", "current-role-list", "testUser");
    verify(SqlServiceMock.mock).insert("INSERT INTO user_prefs VALUES( ?, ?, ? )", "testUser", "current-role-list", "('Administrator')");
    initUserWithRoles(RoleType.ROLE_GUEST);
}
Also used : Response(com.developmentontheedge.be5.api.Response) Test(org.junit.Test) Be5ProjectTest(com.developmentontheedge.be5.test.Be5ProjectTest)

Example 20 with Response

use of com.developmentontheedge.be5.api.Response 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)

Aggregations

Response (com.developmentontheedge.be5.api.Response)23 Test (org.junit.Test)22 Be5ProjectTest (com.developmentontheedge.be5.test.Be5ProjectTest)20 Request (com.developmentontheedge.be5.api.Request)6 Be5Exception (com.developmentontheedge.be5.api.exceptions.Be5Exception)5 ErrorModel (com.developmentontheedge.be5.model.jsonapi.ErrorModel)5 ResourceData (com.developmentontheedge.be5.model.jsonapi.ResourceData)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 Action (com.developmentontheedge.be5.model.Action)4 JsonApiModel (com.developmentontheedge.be5.model.jsonapi.JsonApiModel)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 UserAwareMeta (com.developmentontheedge.be5.api.helpers.UserAwareMeta)2 UserHelper (com.developmentontheedge.be5.api.helpers.UserHelper)2 ResponseImpl (com.developmentontheedge.be5.api.impl.ResponseImpl)2 DocumentGenerator (com.developmentontheedge.be5.query.DocumentGenerator)2 SqlMockOperationTest (com.developmentontheedge.be5.test.SqlMockOperationTest)2 HashUrl (com.developmentontheedge.be5.util.HashUrl)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 Date (java.util.Date)2