Search in sources :

Example 16 with PostConstruct

use of javax.annotation.PostConstruct in project NabAlive by jcheype.

the class RecordController method init.

@PostConstruct
void init() {
    restHandler.post(new Route("/vl/record.jsp") {

        @Override
        public void handle(Request request, Response response, Map<String, String> map) throws Exception {
            String mac = checkNotNull(request.getParam("sn")).toLowerCase();
            if (!connectionManager.containsKey(mac))
                throw new HttpException(HttpResponseStatus.NOT_FOUND, "sn is not connected");
            Nabaztag nabaztag = checkNotNull(nabaztagDAO.findOne("macAddress", mac));
            ChannelBuffer content = request.request.getContent();
            logger.debug("record orig size: {}", content.readableBytes());
            ChannelBufferInputStream inputStream = new ChannelBufferInputStream(content);
            TmpData sound = new TmpData();
            sound.setData(ByteStreams.toByteArray(inputStream));
            tmpDataDAO.save(sound, WriteConcern.SAFE);
            String host = request.request.getHeader("Host");
            String url = "http://" + host + "/record/" + sound.getId().toString();
            logger.debug("sound url: {}", url);
            final String command = "ST " + url + "\nMW\n";
            Query<Nabaztag> query = nabaztagDAO.createQuery();
            query.filter("subscribe.objectId", nabaztag.getId().toString());
            List<Nabaztag> nabaztags = nabaztagDAO.find(query).asList();
            logger.debug("sending to {} subscribers", nabaztags.size());
            for (Nabaztag nab : nabaztags) {
                if (connectionManager.containsKey(nab.getMacAddress())) {
                    final Nabaztag nabTmp = nab;
                    Runnable runnable = new Runnable() {

                        @Override
                        public void run() {
                            logger.debug("sending to {}", nabTmp.getMacAddress());
                            logger.debug("command {}", command);
                            messageService.sendMessage(nabTmp.getMacAddress(), command);
                        }
                    };
                    ses.schedule(runnable, 500, TimeUnit.MILLISECONDS);
                }
            }
            response.write("ok");
        }
    }).get(new Route("/record/:recordId") {

        @Override
        public void handle(Request request, Response response, Map<String, String> map) throws Exception {
            ObjectId recordId = new ObjectId(checkNotNull(map.get("recordId")));
            TmpData sound = checkNotNull(tmpDataDAO.get(recordId));
            response.write(sound.getData());
        }
    });
}
Also used : ObjectId(org.bson.types.ObjectId) Request(com.nabalive.framework.web.Request) HttpException(com.nabalive.framework.web.exception.HttpException) ExecutionException(java.util.concurrent.ExecutionException) ChannelBuffer(org.jboss.netty.buffer.ChannelBuffer) Response(com.nabalive.framework.web.Response) Nabaztag(com.nabalive.data.core.model.Nabaztag) TmpData(com.nabalive.data.core.model.TmpData) HttpException(com.nabalive.framework.web.exception.HttpException) ChannelBufferInputStream(org.jboss.netty.buffer.ChannelBufferInputStream) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 17 with PostConstruct

use of javax.annotation.PostConstruct in project NabAlive by jcheype.

the class TtsController method init.

@PostConstruct
void init() {
    restHandler.get(new Route("/tts/:apikey/:voice") {

        @Override
        public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
            String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
            String voice = checkNotNull(map.get("voice"));
            if (!text.startsWith("<s>")) {
                text = "<s>" + text + "</s>";
            }
            final String key = text + "|" + voice;
            byte[] bytes = ttsCache.asMap().get(key);
            if (bytes != null) {
                response.write(ChannelBuffers.copiedBuffer(bytes));
                return;
            }
            asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {

                @Override
                public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
                    InputStream inputStream = asyncResponse.getResponseBodyAsStream();
                    byte[] bytes = ByteStreams.toByteArray(inputStream);
                    ttsCache.asMap().put(key, bytes);
                    response.write(bytes);
                    return asyncResponse;
                }

                @Override
                public void onThrowable(Throwable t) {
                    logger.error("TTS error", t);
                    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    response.write(httpResponse);
                }
            });
        }
    }).get(new Route("/tts/:voice") {

        @Override
        public void handle(Request request, final Response response, Map<String, String> map) throws Exception {
            String text = StringEscapeUtils.escapeXml(checkNotNull(request.getParam("text")));
            String voice = checkNotNull(map.get("voice"));
            if (!text.startsWith("<s>")) {
                text = "<s>" + text + "</s>";
            }
            final String key = text + "|" + voice;
            byte[] bytes = ttsCache.asMap().get(key);
            if (bytes != null) {
                response.write(ChannelBuffers.copiedBuffer(bytes));
                return;
            }
            asyncHttpClient.preparePost(frenchTtsUrl).setBody(text).execute(new AsyncCompletionHandler<com.ning.http.client.Response>() {

                @Override
                public com.ning.http.client.Response onCompleted(com.ning.http.client.Response asyncResponse) throws Exception {
                    InputStream inputStream = asyncResponse.getResponseBodyAsStream();
                    byte[] bytes = ByteStreams.toByteArray(inputStream);
                    ttsCache.asMap().put(key, bytes);
                    response.write(bytes);
                    return asyncResponse;
                }

                @Override
                public void onThrowable(Throwable t) {
                    logger.error("TTS error", t);
                    HttpResponse httpResponse = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR);
                    response.write(httpResponse);
                }
            });
        }
    });
}
Also used : InputStream(java.io.InputStream) Request(com.nabalive.framework.web.Request) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Response(com.nabalive.framework.web.Response) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) AsyncCompletionHandler(com.ning.http.client.AsyncCompletionHandler) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) Map(java.util.Map) Route(com.nabalive.framework.web.Route) PostConstruct(javax.annotation.PostConstruct)

Example 18 with PostConstruct

use of javax.annotation.PostConstruct in project JFoenix by jfoenixadmin.

the class IconsController method init.

@PostConstruct
public void init() throws FlowException, VetoException {
    bindAction(burger1);
    bindAction(burger2);
    bindAction(burger3);
    bindAction(burger4);
    snackbar.registerSnackbarContainer(root);
    badge1.setOnMouseClicked((e) -> {
        int value = Integer.parseInt(badge1.getText());
        if (e.getButton() == MouseButton.PRIMARY) {
            value++;
        } else if (e.getButton() == MouseButton.SECONDARY) {
            value--;
        }
        if (value == 0) {
            badge1.setEnabled(false);
        } else {
            badge1.setEnabled(true);
        }
        badge1.setText(String.valueOf(value));
        // trigger snackbar
        if (count++ % 2 == 0) {
            snackbar.fireEvent(new SnackbarEvent("Toast Message " + count));
        } else {
            if (count % 4 == 0) {
                snackbar.fireEvent(new SnackbarEvent("Snackbar Message Persistant " + count, "CLOSE", 3000, true, (b) -> {
                    snackbar.close();
                }));
            } else {
                snackbar.fireEvent(new SnackbarEvent("Snackbar Message " + count, "UNDO", 3000, false, (b) -> {
                }));
            }
        }
    });
}
Also used : JFXSnackbar(com.jfoenix.controls.JFXSnackbar) VetoException(io.datafx.controller.util.VetoException) FXML(javafx.fxml.FXML) MouseButton(javafx.scene.input.MouseButton) JFXBadge(com.jfoenix.controls.JFXBadge) FlowException(io.datafx.controller.flow.FlowException) SnackbarEvent(com.jfoenix.controls.JFXSnackbar.SnackbarEvent) PostConstruct(javax.annotation.PostConstruct) StackPane(javafx.scene.layout.StackPane) FXMLController(io.datafx.controller.FXMLController) JFXHamburger(com.jfoenix.controls.JFXHamburger) SnackbarEvent(com.jfoenix.controls.JFXSnackbar.SnackbarEvent) PostConstruct(javax.annotation.PostConstruct)

Example 19 with PostConstruct

use of javax.annotation.PostConstruct in project JFoenix by jfoenixadmin.

the class SVGLoaderController method init.

@PostConstruct
public void init() throws FlowException, VetoException, Exception {
    final Stage stage = (Stage) context.getRegisteredObject("Stage");
    glyphDetailViewer = new GlyphDetailViewer();
    detailsContainer.getChildren().add(glyphDetailViewer);
    ScrollPane scrollableGlyphs = allGlyphs();
    scrollableGlyphs.setStyle("-fx-background-insets: 0;");
    iconsContainer.getChildren().add(scrollableGlyphs);
    browseFont.setOnAction((action) -> {
        FileChooser fileChooser = new FileChooser();
        FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("SVG files (*.svg)", "*.svg");
        fileChooser.getExtensionFilters().add(extFilter);
        File file = fileChooser.showOpenDialog(stage);
        if (file != null) {
            SVGGlyphLoader.clear();
            try {
                SVGGlyphLoader.loadGlyphsFont(new FileInputStream(file), file.getName());
                ScrollPane newglyphs = allGlyphs();
                newglyphs.setStyle("-fx-background-insets: 0;");
                iconsContainer.getChildren().clear();
                iconsContainer.getChildren().add(newglyphs);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}
Also used : FileChooser(javafx.stage.FileChooser) Stage(javafx.stage.Stage) File(java.io.File) FileInputStream(java.io.FileInputStream) FlowException(io.datafx.controller.flow.FlowException) IOException(java.io.IOException) VetoException(io.datafx.controller.util.VetoException) PostConstruct(javax.annotation.PostConstruct)

Example 20 with PostConstruct

use of javax.annotation.PostConstruct in project NabAlive by jcheype.

the class ApplicationGroovyLoader method init.

@PostConstruct
public void init() {
    File scriptFolder = new File(APPS_FOLDER);
    if (!scriptFolder.isDirectory()) {
        throw new IllegalStateException("parameter \"apps.folder\" must point to a directory: " + APPS_FOLDER);
    }
    Runnable folderWatcher = new FolderWatcher(scriptFolder) {

        @Override
        protected void onChange(File file) {
            logger.debug("FolderWatcher onChange: {}", file.getName());
            if (!file.exists()) {
                applicationManager.unRegisterByName(stripExtension(file.getName()));
                return;
            } else if (file.getName().endsWith(".zip")) {
                try {
                    registerZip(file);
                } catch (Exception e) {
                    logger.error("cannot load app: {}", file.getName(), e);
                }
            }
        }
    };
    new Thread(folderWatcher).start();
}
Also used : File(java.io.File) BeansException(org.springframework.beans.BeansException) IOException(java.io.IOException) FolderWatcher(com.nabalive.application.core.util.FolderWatcher) PostConstruct(javax.annotation.PostConstruct)

Aggregations

PostConstruct (javax.annotation.PostConstruct)248 IOException (java.io.IOException)28 File (java.io.File)18 SamlRegisteredService (org.apereo.cas.support.saml.services.SamlRegisteredService)17 Map (java.util.Map)15 ArrayList (java.util.ArrayList)12 ExternalResource (com.vaadin.server.ExternalResource)11 HashMap (java.util.HashMap)10 InitialContext (javax.naming.InitialContext)9 Label (com.vaadin.ui.Label)8 VerticalLayout (com.vaadin.ui.VerticalLayout)8 Request (com.nabalive.framework.web.Request)7 Response (com.nabalive.framework.web.Response)7 Route (com.nabalive.framework.web.Route)7 Link (com.vaadin.ui.Link)7 Properties (java.util.Properties)7 Method (java.lang.reflect.Method)5 InputStream (java.io.InputStream)4 URISyntaxException (java.net.URISyntaxException)4 Path (java.nio.file.Path)4