Search in sources :

Example 1 with ApplicationStore

use of com.nabalive.data.core.model.ApplicationStore 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)

Aggregations

ApplicationLogo (com.nabalive.data.core.model.ApplicationLogo)1 ApplicationStore (com.nabalive.data.core.model.ApplicationStore)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