Search in sources :

Example 1 with IContainerProxy

use of eu.openanalytics.shinyproxy.container.IContainerProxy in project shinyproxy by openanalytics.

the class LogService method sendSupportMail.

public void sendSupportMail(IssueForm form) {
    if (!isReportingEnabled()) {
        log.error("Cannot send support mail: no address is configured");
        return;
    }
    try {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);
        // Headers
        helper.setFrom(environment.getProperty("shiny.proxy.support.mail-from-address", "issues@shinyproxy.io"));
        helper.addTo(supportAddress);
        helper.setSubject("ShinyProxy Error Report");
        // Body
        StringBuilder body = new StringBuilder();
        String lineSep = System.getProperty("line.separator");
        body.append(String.format("This is an error report generated by ShinyProxy%s", lineSep));
        body.append(String.format("User: %s%s", form.userName, lineSep));
        if (form.appName != null)
            body.append(String.format("App: %s%s", form.appName, lineSep));
        if (form.currentLocation != null)
            body.append(String.format("Location: %s%s", form.currentLocation, lineSep));
        if (form.customMessage != null)
            body.append(String.format("Message: %s%s", form.customMessage, lineSep));
        helper.setText(body.toString());
        if (isContainerLoggingEnabled()) {
            IContainerProxy activeProxy = null;
            for (IContainerProxy proxy : proxyService.listProxies()) {
                if (proxy.getUserId().equals(form.getUserName()) && proxy.getApp().getName().equals(form.getAppName())) {
                    activeProxy = proxy;
                    break;
                }
            }
            if (activeProxy != null) {
                Path[] filePaths = getLogFilePaths(activeProxy.getContainerId());
                for (Path p : filePaths) {
                    if (Files.exists(p))
                        helper.addAttachment(p.toFile().getName(), p.toFile());
                }
            }
        }
        mailSender.send(message);
    } catch (Exception e) {
        throw new RuntimeException("Failed to send email", e);
    }
}
Also used : Path(java.nio.file.Path) IContainerProxy(eu.openanalytics.shinyproxy.container.IContainerProxy) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) IOException(java.io.IOException)

Example 2 with IContainerProxy

use of eu.openanalytics.shinyproxy.container.IContainerProxy in project shinyproxy by openanalytics.

the class ProxyService method getMapping.

public String getMapping(HttpServletRequest request, String userName, String appName, boolean startNew) {
    waitForLaunchingProxy(userName, appName);
    IContainerProxy proxy = findProxy(userName, appName);
    if (proxy == null && startNew) {
        // The user has no proxy yet.
        proxy = startProxy(userName, appName);
    }
    if (proxy == null) {
        return null;
    } else {
        Set<String> sessionIds = proxySessionIds.get(proxy);
        if (sessionIds == null) {
            sessionIds = new HashSet<>();
            proxySessionIds.put(proxy, sessionIds);
        }
        sessionIds.add(getCurrentSessionId(request));
        return proxy.getName();
    }
}
Also used : IContainerProxy(eu.openanalytics.shinyproxy.container.IContainerProxy)

Example 3 with IContainerProxy

use of eu.openanalytics.shinyproxy.container.IContainerProxy in project shinyproxy by openanalytics.

the class ProxyService method startProxy.

private IContainerProxy startProxy(String userName, String appName) {
    ShinyApp app = appService.getApp(appName);
    if (app == null) {
        throw new ShinyProxyException("Cannot start container: unknown application: " + appName);
    }
    if (findProxy(userName, appName) != null) {
        throw new ShinyProxyException("Cannot start container: user " + userName + " already has an active proxy for " + appName);
    }
    ContainerProxyRequest request = new ContainerProxyRequest();
    request.userId = userName;
    request.app = app;
    IContainerProxy proxy = backend.createProxy(request);
    activeProxies.add(proxy);
    try {
        backend.startProxy(proxy);
    } finally {
        if (proxy.getStatus() != ContainerProxyStatus.Up)
            activeProxies.remove(proxy);
    }
    try {
        URI target = new URI(proxy.getTarget());
        synchronized (mappingListeners) {
            for (MappingListener listener : mappingListeners) {
                listener.mappingAdded(proxy.getName(), target);
            }
        }
    } catch (URISyntaxException ignore) {
    }
    if (logService.isContainerLoggingEnabled()) {
        BiConsumer<File, File> outputAttacher = backend.getOutputAttacher(proxy);
        if (outputAttacher == null) {
            log.warn("Cannot log container output: " + backend.getClass() + " does not support output attaching.");
        } else {
            logService.attachToOutput(proxy, outputAttacher);
        }
    }
    log.info(String.format("Proxy activated [user: %s] [app: %s]", userName, appName));
    eventService.post(EventType.AppStart.toString(), userName, appName);
    return proxy;
}
Also used : ContainerProxyRequest(eu.openanalytics.shinyproxy.container.ContainerProxyRequest) IContainerProxy(eu.openanalytics.shinyproxy.container.IContainerProxy) ShinyProxyException(eu.openanalytics.shinyproxy.ShinyProxyException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) File(java.io.File) ShinyApp(eu.openanalytics.shinyproxy.services.AppService.ShinyApp)

Aggregations

IContainerProxy (eu.openanalytics.shinyproxy.container.IContainerProxy)3 ShinyProxyException (eu.openanalytics.shinyproxy.ShinyProxyException)1 ContainerProxyRequest (eu.openanalytics.shinyproxy.container.ContainerProxyRequest)1 ShinyApp (eu.openanalytics.shinyproxy.services.AppService.ShinyApp)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 Path (java.nio.file.Path)1 MimeMessage (javax.mail.internet.MimeMessage)1 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)1