Search in sources :

Example 1 with DownloadWrapper

use of org.xdi.util.io.DownloadWrapper in project oxTrust by GluuFederation.

the class LogoImageServlet method doGet.

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse response) {
    log.debug("Starting organization logo upload");
    try {
        GluuOrganization organization = organizationService.getOrganization();
        GluuImage image = imageService.getGluuImageFromXML(organization.getLogoImage());
        if (image != null) {
            image.setLogo(true);
        }
        OutputStream os = null;
        InputStream is = null;
        try {
            DownloadWrapper downloadWrapper = null;
            // Send customized organization logo
            if (image != null) {
                File file = imageService.getSourceFile(image);
                try {
                    is = FileUtils.openInputStream(file);
                    downloadWrapper = new DownloadWrapper(is, image.getSourceName(), image.getSourceContentType(), image.getCreationDate(), (int) file.length());
                } catch (IOException ex) {
                    log.error("Organization logo image doesn't exist", ex);
                    FileDownloader.sendError(response);
                    return;
                }
            } else {
                // If customized logo doesn't exist then send default
                // organization logo
                String defaultLogoFileName = "/WEB-INF/static/images/default_logo.png";
                is = getServletContext().getResourceAsStream(defaultLogoFileName);
                if (is == null) {
                    log.error("Default organization logo image doesn't exist");
                    FileDownloader.sendError(response);
                    return;
                }
                // Calculate default logo size
                long contentLength;
                try {
                    contentLength = is.skip(Long.MAX_VALUE);
                } catch (IOException ex) {
                    log.error("Failed to calculate default organization logo image size", ex);
                    FileDownloader.sendError(response);
                    return;
                } finally {
                    IOUtils.closeQuietly(is);
                }
                is = getServletContext().getResourceAsStream(defaultLogoFileName);
                downloadWrapper = new DownloadWrapper(is, "default_logo.png", "image/png", new Date(), (int) contentLength);
            }
            try {
                int logoSize = FileDownloader.writeOutput(downloadWrapper, ContentDisposition.INLINE, response);
                response.getOutputStream().flush();
                log.debug("Successfully send organization logo with size", logoSize);
            } catch (IOException ex) {
                log.error("Failed to send organization logo", ex);
                FileDownloader.sendError(response);
            }
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    } catch (Exception ex) {
        log.error("Failed to send organization logo", ex);
    }
}
Also used : DownloadWrapper(org.xdi.util.io.DownloadWrapper) InputStream(java.io.InputStream) GluuImage(org.xdi.model.GluuImage) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GluuOrganization(org.gluu.oxtrust.model.GluuOrganization) File(java.io.File) Date(java.util.Date) IOException(java.io.IOException)

Example 2 with DownloadWrapper

use of org.xdi.util.io.DownloadWrapper in project oxTrust by GluuFederation.

the class FaviconImageServlet method doGet.

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse response) throws ServletException, IOException {
    log.debug("Starting organization favicon upload");
    String preview = httpServletRequest.getParameter("preview");
    GluuOrganization organization = null;
    try {
        organization = organizationService.getOrganization();
    } catch (Exception ex) {
        log.error("an Error Occured", ex);
    }
    GluuImage image = null;
    if ("true".equals(preview)) {
        image = imageService.getGluuImageFromXML(organization.getTempFaviconImage());
        if (image != null) {
            image.setStoreTemporary(true);
        }
    }
    if (!"true".equals(preview) || image == null) {
        image = imageService.getGluuImageFromXML(organization.getFaviconImage());
    }
    if (image != null) {
        image.setLogo(false);
    }
    OutputStream os = null;
    InputStream is = null;
    try {
        DownloadWrapper downloadWrapper = null;
        // Send customized organization logo
        if (image != null) {
            File file = null;
            try {
                file = imageService.getSourceFile(image);
            } catch (Exception ex) {
                log.error("an Error Occured", ex);
            }
            try {
                is = FileUtils.openInputStream(file);
                if (is != null && file != null) {
                    downloadWrapper = new DownloadWrapper(is, image.getSourceName(), image.getSourceContentType(), image.getCreationDate(), (int) file.length());
                }
            } catch (IOException ex) {
                log.error("Organization favicon image doesn't exist", ex);
                FileDownloader.sendError(response);
                return;
            }
        } else {
            // If customized logo doesn't exist then send default
            // organization logo
            String defaultFaviconFileName = "/WEB-INF/static/images/favicon_ic.ico";
            is = getServletContext().getResourceAsStream(defaultFaviconFileName);
            if (is == null) {
                log.error("Default organization favicon image doesn't exist");
                FileDownloader.sendError(response);
                return;
            }
            // Calculate default logo size
            long contentLength;
            try {
                contentLength = is.skip(Long.MAX_VALUE);
            } catch (IOException ex) {
                log.error("Failed to calculate default organization favicon image size", ex);
                FileDownloader.sendError(response);
                return;
            } finally {
                IOUtils.closeQuietly(is);
            }
            is = getServletContext().getResourceAsStream(defaultFaviconFileName);
            downloadWrapper = new DownloadWrapper(is, "favicon_ic.ico", "image/x-icon", new Date(), (int) contentLength);
        }
        try {
            int logoSize = FileDownloader.writeOutput(downloadWrapper, ContentDisposition.INLINE, response);
            response.getOutputStream().flush();
            log.debug("Successfully send organization favicon with size", logoSize);
        } catch (IOException ex) {
            log.error("Failed to send organization favicon", ex);
            FileDownloader.sendError(response);
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(os);
    }
}
Also used : DownloadWrapper(org.xdi.util.io.DownloadWrapper) InputStream(java.io.InputStream) GluuImage(org.xdi.model.GluuImage) OutputStream(java.io.OutputStream) IOException(java.io.IOException) GluuOrganization(org.gluu.oxtrust.model.GluuOrganization) File(java.io.File) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 Date (java.util.Date)2 GluuOrganization (org.gluu.oxtrust.model.GluuOrganization)2 GluuImage (org.xdi.model.GluuImage)2 DownloadWrapper (org.xdi.util.io.DownloadWrapper)2 ServletException (javax.servlet.ServletException)1