Search in sources :

Example 1 with ApplicationLogo

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);
//                    }
//                })
}
Also used : Request(com.nabalive.framework.web.Request) ApplicationLogo(com.nabalive.data.core.model.ApplicationLogo) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Response(com.nabalive.framework.web.Response) ApplicationStore(com.nabalive.data.core.model.ApplicationStore) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 2 with 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);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ApplicationStore(com.nabalive.data.core.model.ApplicationStore) ApplicationLogo(com.nabalive.data.core.model.ApplicationLogo) BasicDBObject(com.mongodb.BasicDBObject) DBObject(com.mongodb.DBObject)

Aggregations

ApplicationLogo (com.nabalive.data.core.model.ApplicationLogo)2 ApplicationStore (com.nabalive.data.core.model.ApplicationStore)2 BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 Request (com.nabalive.framework.web.Request)1 Response (com.nabalive.framework.web.Response)1 Route (com.nabalive.framework.web.Route)1 Map (java.util.Map)1 PostConstruct (javax.annotation.PostConstruct)1 ChannelBuffer (org.jboss.netty.buffer.ChannelBuffer)1 DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)1