Example usage for org.apache.commons.io FileUtils openInputStream

List of usage examples for org.apache.commons.io FileUtils openInputStream

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils openInputStream.

Prototype

public static FileInputStream openInputStream(File file) throws IOException 

Source Link

Document

Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

Usage

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

private EntityIDHandler parseMetadata(File metadataFile) {
    if (!metadataFile.exists()) {
        log.error("Failed to get entityId from metadata file '{0}'", metadataFile.getAbsolutePath());
        return null;
    }/*from  w w  w .  j a  v  a  2  s .  c om*/

    InputStream is = null;
    InputStreamReader isr = null;
    EntityIDHandler handler = null;
    try {
        is = FileUtils.openInputStream(metadataFile);
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxParserFactory.newSAXParser();

        handler = new EntityIDHandler();
        is = FileUtils.openInputStream(metadataFile);
        saxParser.parse(is, handler);

    } catch (IOException ex) {
        log.error("Failed to read metadata file '{0}'", ex, metadataFile.getAbsolutePath());
    } catch (ParserConfigurationException e) {
        log.error("Failed to confugure SAX parser for file '{0}'", e, metadataFile.getAbsolutePath());
    } catch (SAXException e) {
        log.error("Failed to parse file '{0}'", e, metadataFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    return handler;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public String getIdpMetadataFilePath() {
    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        throw new InvalidConfigurationException(
                "Failed to find IDP metadata file due to undefined IDP root folder");
    }/*  www  .  ja  va 2s.  c o m*/

    String idpConfFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB2_IDP_CONF_FOLDER + File.separator;

    File relyingPartyFile = new File(idpConfFolder + SHIB2_IDP_RELYING_PARTY);
    if (!relyingPartyFile.exists()) {
        log.error("Failed to find IDP metadata file name because relaying party file '{0}' doesn't exist",
                relyingPartyFile.getAbsolutePath());
        return null;
    }

    InputStream is = null;
    InputStreamReader isr = null;
    Document xmlDocument = null;
    try {
        is = FileUtils.openInputStream(relyingPartyFile);
        isr = new InputStreamReader(is, "UTF-8");
        try {
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(isr));
        } catch (Exception ex) {
            log.error("Failed to parse relying party file '{0}'", ex, relyingPartyFile.getAbsolutePath());
        }
    } catch (IOException ex) {
        log.error("Failed to read relying party file '{0}'", ex, relyingPartyFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    if (xmlDocument == null) {
        return null;
    }

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    String filePath = null;
    try {
        filePath = xPath.compile(
                "/RelyingPartyGroup/MetadataProvider[@id='ShibbolethMetadata']/MetadataProvider[@id='IdPMD']/MetadataResource/@file")
                .evaluate(xmlDocument);
    } catch (XPathExpressionException ex) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'", ex,
                relyingPartyFile.getAbsolutePath());
    }

    if (filePath == null) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'",
                relyingPartyFile.getAbsolutePath());
    }

    return filePath;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth2ConfService.java

public boolean isFederationMetadata(String spMetaDataFN) {
    if (spMetaDataFN == null) {
        return false;
    }//from  w w  w  . jav  a2  s. co m
    File spMetaDataFile = new File(getSpMetadataFilePath(spMetaDataFN));
    InputStream is = null;
    InputStreamReader isr = null;
    Document xmlDocument = null;
    try {
        is = FileUtils.openInputStream(spMetaDataFile);
        isr = new InputStreamReader(is, "UTF-8");
        try {
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(isr));
        } catch (Exception ex) {
            log.error("Failed to parse metadata file '{0}'", ex, spMetaDataFile.getAbsolutePath());
        }
    } catch (IOException ex) {
        log.error("Failed to read metadata file '{0}'", ex, spMetaDataFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    if (xmlDocument == null) {
        return false;
    }

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    String federationTag = null;
    try {
        federationTag = xPath.compile("count(/EntitiesDescriptor)").evaluate(xmlDocument);
    } catch (XPathExpressionException ex) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'", ex,
                spMetaDataFile.getAbsolutePath());
    }

    return Integer.parseInt(federationTag) > 0;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth3ConfService.java

public String getIdpMetadataFilePath() {
    //TODO: change for IDP3

    if (applicationConfiguration.getShibboleth2IdpRootDir() == null) {
        throw new InvalidConfigurationException(
                "Failed to find IDP metadata file due to undefined IDP root folder");
    }/*  w w  w  .j av  a2s. c  om*/

    String idpConfFolder = applicationConfiguration.getShibboleth2IdpRootDir() + File.separator
            + SHIB3_IDP_CONF_FOLDER + File.separator;

    File relyingPartyFile = new File(idpConfFolder + SHIB3_IDP_RELYING_PARTY);
    if (!relyingPartyFile.exists()) {
        log.error("Failed to find IDP metadata file name because relaying party file '{0}' doesn't exist",
                relyingPartyFile.getAbsolutePath());
        return null;
    }

    InputStream is = null;
    InputStreamReader isr = null;
    Document xmlDocument = null;
    try {
        is = FileUtils.openInputStream(relyingPartyFile);
        isr = new InputStreamReader(is, "UTF-8");
        try {
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(isr));
        } catch (Exception ex) {
            log.error("Failed to parse relying party file '{0}'", ex, relyingPartyFile.getAbsolutePath());
        }
    } catch (IOException ex) {
        log.error("Failed to read relying party file '{0}'", ex, relyingPartyFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    if (xmlDocument == null) {
        return null;
    }

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    String filePath = null;
    try {
        filePath = xPath.compile(
                "/RelyingPartyGroup/MetadataProvider[@id='ShibbolethMetadata']/MetadataProvider[@id='IdPMD']/MetadataResource/@file")
                .evaluate(xmlDocument);
    } catch (XPathExpressionException ex) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'", ex,
                relyingPartyFile.getAbsolutePath());
    }

    if (filePath == null) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'",
                relyingPartyFile.getAbsolutePath());
    }

    return filePath;
}

From source file:org.gluu.oxtrust.ldap.service.Shibboleth3ConfService.java

public boolean isFederationMetadata(String spMetaDataFN) {
    //TODO: change for IDP3
    if (spMetaDataFN == null) {
        return false;
    }/*w  w  w.j a  v a2s  .  c  o m*/
    File spMetaDataFile = new File(getSpMetadataFilePath(spMetaDataFN));
    InputStream is = null;
    InputStreamReader isr = null;
    Document xmlDocument = null;
    try {
        is = FileUtils.openInputStream(spMetaDataFile);
        isr = new InputStreamReader(is, "UTF-8");
        try {
            xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource(isr));
        } catch (Exception ex) {
            log.error("Failed to parse metadata file '{0}'", ex, spMetaDataFile.getAbsolutePath());
        }
    } catch (IOException ex) {
        log.error("Failed to read metadata file '{0}'", ex, spMetaDataFile.getAbsolutePath());
    } finally {
        IOUtils.closeQuietly(isr);
        IOUtils.closeQuietly(is);
    }

    if (xmlDocument == null) {
        return false;
    }

    XPathFactory factory = XPathFactory.newInstance();
    XPath xPath = factory.newXPath();

    String federationTag = null;
    try {
        federationTag = xPath.compile("count(/EntitiesDescriptor)").evaluate(xmlDocument);
    } catch (XPathExpressionException ex) {
        log.error("Failed to find IDP metadata file in relaying party file '{0}'", ex,
                spMetaDataFile.getAbsolutePath());
    }

    return Integer.parseInt(federationTag) > 0;
}

From source file:org.gluu.oxtrust.servlet.FaviconImageServlet.java

@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 {/*from  w w w.  ja  v a2  s  .  c om*/
        organization = OrganizationService.instance().getOrganization();
    } catch (Exception ex) {
        log.error("an Error Occured", ex);
    }
    ImageService imageService = ImageService.instance();
    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);
    }
}

From source file:org.gluu.oxtrust.servlet.LogoImageServlet.java

@Override
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse response) {
    log.debug("Starting organization logo upload");
    try {//from  w  w w.j av a 2  s .  com
        GluuOrganization organization = OrganizationService.instance().getOrganization();
        ImageService imageService = ImageService.instance();
        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);
    }
}

From source file:org.gluu.saml.metadata.SAMLMetadataParser.java

public static EntityIDHandler parseMetadata(File metadataFile) {
    if (!metadataFile.exists()) {
        log.error("Failed to get entityId from metadata file: " + metadataFile.getAbsolutePath());
        return null;
    }/*  w  ww  .  j a v a 2s.  c o  m*/

    InputStream is = null;
    try {
        is = FileUtils.openInputStream(metadataFile);

        return parseMetadata(is);
    } catch (IOException ex) {
        log.error("Failed to read SAML metadata file: " + metadataFile.getAbsolutePath(), ex);
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:org.gradle.util.GFileUtils.java

public static FileInputStream openInputStream(File file) {
    try {/*ww  w .j av a  2s . c  om*/
        return FileUtils.openInputStream(file);
    } catch (IOException e) {
        throw new RuntimeException("Problems opening file input stream for file: " + file, e);
    }
}

From source file:org.hibernate.search.test.bridge.tika.TikaBridgeBlobSupportTest.java

private Blob getBlobData(String fileName, Session session) throws IOException {
    File file = new File(fileName);
    FileInputStream in = FileUtils.openInputStream(file);
    return session.getLobHelper().createBlob(in, file.length());
}