Java Code Examples for org.apache.tomcat.Jar#getInputStream()

The following examples show how to use org.apache.tomcat.Jar#getInputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JspUtil.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public static InputStream getInputStream(String fname, Jar jar,
        JspCompilationContext ctxt) throws IOException {

    InputStream in = null;

    if (jar != null) {
        String jarEntryName = fname.substring(1, fname.length());
        in = jar.getInputStream(jarEntryName);
    } else {
        in = ctxt.getResourceAsStream(fname);
    }

    if (in == null) {
        throw new FileNotFoundException(Localizer.getMessage(
                "jsp.error.file.not.found", fname));
    }

    return in;
}
 
Example 2
Source File: TestAbstractInputStreamJar.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testNestedJarGetInputStream() throws Exception {
    File f = new File("test/webresources/war-url-connection.war");
    StringBuilder sb = new StringBuilder("war:");
    sb.append(f.toURI().toURL());
    sb.append("*/WEB-INF/lib/test.jar");

    Jar jar = JarFactory.newInstance(new URL(sb.toString()));

    InputStream is1 = jar.getInputStream("META-INF/resources/index.html");
    ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
    IOTools.flow(is1, baos1);

    InputStream is2 = jar.getInputStream("META-INF/resources/index.html");
    ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
    IOTools.flow(is2, baos2);

    Assert.assertArrayEquals(baos1.toByteArray(), baos2.toByteArray());
}
 
Example 3
Source File: FragmentJarScannerCallback.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void scan(Jar jar, String webappPath, boolean isWebapp) throws IOException {

    InputStream is = null;
    WebXml fragment = new WebXml();
    fragment.setWebappJar(isWebapp);
    fragment.setDelegate(delegate);

    try {
        // Only web application JARs are checked for web-fragment.xml
        // files.
        // web-fragment.xml files don't need to be parsed if they are never
        // going to be used.
        if (isWebapp && parseRequired) {
            is = jar.getInputStream(FRAGMENT_LOCATION);
        }

        if (is == null) {
            // If there is no web.xml, normal JAR no impact on
            // distributable
            fragment.setDistributable(true);
        } else {
            String fragmentUrl = jar.getURL(FRAGMENT_LOCATION);
            InputSource source = new InputSource(fragmentUrl);
            source.setByteStream(is);
            if (!webXmlParser.parseWebXml(source, fragment, true)) {
                ok = false;
            }
        }
    } finally {
        addFragment(fragment, jar.getJarFileURL());
    }
}
 
Example 4
Source File: JavacErrorDetail.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Constructor.
 *
 * @param javaFileName The name of the Java file in which the
 * compilation error occurred
 * @param javaLineNum The compilation error line number
 * @param jspFileName The name of the JSP file from which the Java source
 * file was generated
 * @param jspBeginLineNum The start line number of the JSP element
 * responsible for the compilation error
 * @param errMsg The compilation error message
 * @param ctxt The compilation context
 */
public JavacErrorDetail(String javaFileName,
        int javaLineNum,
        String jspFileName,
        int jspBeginLineNum,
        StringBuilder errMsg,
        JspCompilationContext ctxt) {

    this.javaFileName = javaFileName;
    this.javaLineNum = javaLineNum;
    this.errMsg = errMsg;
    this.jspFileName = jspFileName;
    // Note: this.jspBeginLineNum is set at the end of this method as it may
    //       be modified (corrected) during the execution of this method

    if (jspBeginLineNum > 0 && ctxt != null) {
        InputStream is = null;
        try {
            Jar tagJar = ctxt.getTagFileJar();
            if (tagJar != null) {
                // Strip leading '/'
                String entryName = jspFileName.substring(1);
                is = tagJar.getInputStream(entryName);
                this.jspFileName = tagJar.getURL(entryName);
            } else {
                is = ctxt.getResourceAsStream(jspFileName);
            }
            // Read both files in, so we can inspect them
            String[] jspLines = readFile(is);

            try (FileInputStream fis = new FileInputStream(ctxt.getServletJavaFileName())) {
                String[] javaLines = readFile(fis);

                if (jspLines.length < jspBeginLineNum) {
                    // Avoid ArrayIndexOutOfBoundsException
                    // Probably bug 48498 but could be some other cause
                    jspExtract = Localizer.getMessage("jsp.error.bug48498");
                    return;
                }

                // If the line contains the opening of a multi-line scriptlet
                // block, then the JSP line number we got back is probably
                // faulty.  Scan forward to match the java line...
                if (jspLines[jspBeginLineNum-1].lastIndexOf("<%") >
                    jspLines[jspBeginLineNum-1].lastIndexOf("%>")) {
                    String javaLine = javaLines[javaLineNum-1].trim();

                    for (int i=jspBeginLineNum-1; i<jspLines.length; i++) {
                        if (jspLines[i].indexOf(javaLine) != -1) {
                            // Update jsp line number
                            jspBeginLineNum = i+1;
                            break;
                        }
                    }
                }

                // copy out a fragment of JSP to display to the user
                StringBuilder fragment = new StringBuilder(1024);
                int startIndex = Math.max(0, jspBeginLineNum-1-3);
                int endIndex = Math.min(
                        jspLines.length-1, jspBeginLineNum-1+3);

                for (int i=startIndex;i<=endIndex; ++i) {
                    fragment.append(i+1);
                    fragment.append(": ");
                    fragment.append(jspLines[i]);
                    fragment.append(System.lineSeparator());
                }
                jspExtract = fragment.toString();
            }
        } catch (IOException ioe) {
            // Can't read files - ignore
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                    // Ignore
                }
            }
        }
    }
    this.jspBeginLineNum = jspBeginLineNum;
}