Search in sources :

Example 21 with Resource

use of org.apache.naming.resources.Resource in project Payara by payara.

the class DefaultServlet method findXsltInputStream.

/**
 * Return a Source for the xsl template (if possible)
 */
protected Source findXsltInputStream(DirContext directory) throws IOException, ServletException {
    if (localXsltFile != null) {
        try {
            Object obj = directory.lookup(localXsltFile);
            if (obj != null && obj instanceof Resource) {
                InputStream is = ((Resource) obj).streamContent();
                if (is != null) {
                    if (Globals.IS_SECURITY_ENABLED) {
                        return secureXslt(is);
                    } else {
                        return new StreamSource(is);
                    }
                }
            }
        } catch (Throwable e) {
            ;
            /* Should only be IOException or NamingException
                    * can be ignored
                    */
            if (debug > 10)
                log("localXsltFile '" + localXsltFile + "' not found", e);
            return null;
        }
    }
    if (contextXsltFile != null) {
        InputStream is = getServletContext().getResourceAsStream(contextXsltFile);
        if (is != null) {
            if (Globals.IS_SECURITY_ENABLED) {
                return secureXslt(is);
            } else {
                return new StreamSource(is);
            }
        }
        if (debug > 10)
            log("contextXsltFile '" + contextXsltFile + "' not found");
    }
    /*  Open and read in file in one fell swoop to reduce chance
         *  chance of leaving handle open.
         */
    if (globalXsltFile != null) {
        File f = validateGlobalXsltFile();
        if (f != null) {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(f);
                long len = f.length();
                byte[] b = new byte[(int) len];
                /* danger! */
                if (len != fis.read(b)) {
                    throw new IOException(MessageFormat.format(rb.getString(LogFacade.READ_FILE_EXCEPTION), f.getAbsolutePath()));
                }
                return new StreamSource(new ByteArrayInputStream(b));
            } finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException ioe) {
                        if (debug > 10) {
                            log(ioe.getMessage(), ioe);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) Resource(org.apache.naming.resources.Resource) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Resource (org.apache.naming.resources.Resource)21 NamingException (javax.naming.NamingException)19 InputStream (java.io.InputStream)10 File (java.io.File)9 IOException (java.io.IOException)9 BufferedInputStream (java.io.BufferedInputStream)8 FileInputStream (java.io.FileInputStream)8 DirContext (javax.naming.directory.DirContext)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 RandomAccessFile (java.io.RandomAccessFile)6 NameClassPair (javax.naming.NameClassPair)5 MalformedURLException (java.net.MalformedURLException)4 FileOutputStream (java.io.FileOutputStream)3 JarFile (java.util.jar.JarFile)3 Binding (javax.naming.Binding)3 LifecycleException (org.apache.catalina.LifecycleException)3 ResourceAttributes (org.apache.naming.resources.ResourceAttributes)3 InputStreamReader (java.io.InputStreamReader)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2