use of com.nabalive.data.core.model.ApplicationLogo in project NabAlive by jcheype.
the class ApplicationController method init.
@PostConstruct
void init() {
restHandler.get(new Route("/applications") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
List<ApplicationStore> applicationStores = applicationStoreDAO.find().asList();
Iterables.filter(applicationStores, new Predicate<ApplicationStore>() {
@Override
public boolean apply(@Nullable ApplicationStore applicationStore) {
return applicationManager.getApplication(applicationStore.getApikey()) != null;
}
});
response.writeJSON(applicationStores);
}
}).get(new Route("/applications/:apikey/logo.png") {
@Override
public void handle(Request request, Response response, Map<String, String> map) throws Exception {
ChannelBuffer buffer;
String apikey = checkNotNull(map.get("apikey"));
ApplicationLogo applicationLogo = applicationLogoDAO.get(apikey);
if (applicationLogo != null)
buffer = ChannelBuffers.copiedBuffer(applicationLogo.getData());
else {
byte[] bytes = ByteStreams.toByteArray(getClass().getResourceAsStream("/app/default.png"));
buffer = ChannelBuffers.copiedBuffer(bytes);
}
DefaultHttpResponse defaultHttpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
defaultHttpResponse.setHeader(HttpHeaders.Names.CONTENT_TYPE, "image/png");
defaultHttpResponse.setContent(buffer);
response.write(defaultHttpResponse);
}
});
// .post(new Route("/applications/:apikey/logo.png") {
// @Override
// public void handle(Request request, Response response, Map<String, String> map) throws Exception {
// String apikey = checkNotNull(map.get("apikey"));
// String contentType = request.getHeader("Content-Type");
// Matcher matcher = boundaryPattern.matcher(contentType);
// if (!matcher.matches())
// throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "bad content type");
//
// String boundary = matcher.group(1);
//
// ChannelBuffer content = request.request.getContent();
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// MultipartStream multipartStream = new MultipartStream(new ChannelBufferInputStream(content), boundary.getBytes(CharsetUtil.UTF_8));
// String contentTypeLogo = null;
// boolean nextPart = multipartStream.skipPreamble();
// while (nextPart) {
// String header = multipartStream.readHeaders();
// if(header.contains("form-data; name=\"logo\"")){
// contentTypeLogo = "application/octet-stream"; // TODO parse contentType
// multipartStream.readBodyData(outputStream);
// nextPart=false;
// }
// else
// nextPart = multipartStream.readBoundary();
// }
// if(contentTypeLogo == null)
// throw new HttpException(HttpResponseStatus.INTERNAL_SERVER_ERROR, "cannot parse data");
//
// ApplicationLogo applicationLogo = new ApplicationLogo();
// applicationLogo.setApikey(apikey);
// applicationLogo.setContentType(contentTypeLogo);
// applicationLogo.setFilename("logo.png");
// applicationLogo.setData(outputStream.toByteArray());
//
// applicationLogoDAO.save(applicationLogo);
//
// response.writeJSON(applicationLogo);
// }
// })
}
use of com.nabalive.data.core.model.ApplicationLogo in project NabAlive by jcheype.
the class ApplicationManager method registerApp.
public void registerApp(Application application, String name, String jsonString, byte[] logo) throws IOException {
logger.debug("registering: {}", name);
DBObject dbObject = (DBObject) JSON.parse(jsonString);
dbObject.put("className", ApplicationStore.class.getName());
dbObject.put("name", name);
dbObject.put("apikey", application.getApikey());
BasicDBObject query = new BasicDBObject("apikey", application.getApikey());
logger.debug("upsert: {}", dbObject);
applicationStoreDAO.getCollection().update(query, dbObject, true, false, WriteConcern.SAFE);
applicationLogoDAO.deleteById(application.getApikey());
ApplicationLogo applicationLogo = new ApplicationLogo();
applicationLogo.setData(logo);
applicationLogo.setApikey(application.getApikey());
applicationLogo.setContentType("application/octet-stream");
applicationLogo.setFilename("logo.png");
applicationLogoDAO.save(applicationLogo);
applicationMap.put(application.getApikey(), application);
}
Aggregations