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);
}
}
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)));
}
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)));
}
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);
}
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());
}
}
Aggregations