Java Code Examples for org.apache.jasper.compiler.Localizer#getMessage()

The following examples show how to use org.apache.jasper.compiler.Localizer#getMessage() . 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: JspCompilationContext.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
protected void createOutputDir() {
    String path = null;
    if (isTagFile()) {
        String tagName = tagInfo.getTagClassName();
        path = tagName.replace('.', File.separatorChar);
        path = path.substring(0, path.lastIndexOf(File.separatorChar));
    } else {
        path = getServletPackageName().replace('.',File.separatorChar);
    }

        // Append servlet or tag handler path to scratch dir
        try {
            File base = options.getScratchDir();
            baseUrl = base.toURI().toURL();
            outputDir = base.getAbsolutePath() + File.separator + path +
                File.separator;
            if (!makeOutputDir()) {
                throw new IllegalStateException(Localizer.getMessage("jsp.error.outputfolder"));
            }
        } catch (MalformedURLException e) {
            throw new IllegalStateException(Localizer.getMessage("jsp.error.outputfolder"), e);
        }
}
 
Example 2
Source File: PageContextImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
private Object doGetAttribute(String name, int scope) {
    switch (scope) {
    case PAGE_SCOPE:
        return attributes.get(name);

    case REQUEST_SCOPE:
        return request.getAttribute(name);

    case SESSION_SCOPE:
        if (session == null) {
            throw new IllegalStateException(Localizer
                    .getMessage("jsp.error.page.noSession"));
        }
        return session.getAttribute(name);

    case APPLICATION_SCOPE:
        return context.getAttribute(name);

    default:
        throw new IllegalArgumentException("Invalid scope");
    }
}
 
Example 3
Source File: JspRuntimeLibrary.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
public static Object handleGetProperty(Object o, String prop)
throws JasperException {
    if (o == null) {
        throw new JasperException(
                Localizer.getMessage("jsp.error.beans.nullbean"));
    }
    Object value = null;
    try {
        Method method = getReadMethod(o.getClass(), prop);
        value = method.invoke(o, (Object[]) null);
    } catch (Exception ex) {
        Throwable thr = ExceptionUtils.unwrapInvocationTargetException(ex);
        ExceptionUtils.handleThrowable(thr);
        throw new JasperException (ex);
    }
    return value;
}
 
Example 4
Source File: PageContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void setAttribute(final String name, final Object attribute) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (SecurityUtil.isPackageProtectionEnabled()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                doSetAttribute(name, attribute);
                return null;
            }
        });
    } else {
        doSetAttribute(name, attribute);
    }
}
 
Example 5
Source File: JspContextWrapper.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public Object findAttribute(String name) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    Object o = pageAttributes.get(name);
    if (o == null) {
        o = rootJspCtxt.getAttribute(name, REQUEST_SCOPE);
        if (o == null) {
            if (getSession() != null) {
                o = rootJspCtxt.getAttribute(name, SESSION_SCOPE);
            }
            if (o == null) {
                o = rootJspCtxt.getAttribute(name, APPLICATION_SCOPE);
            }
        }
    }

    return o;
}
 
Example 6
Source File: PageContextImpl.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Override
public int getAttributesScope(final String name) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (SecurityUtil.isPackageProtectionEnabled()) {
        return (AccessController
                .doPrivileged(new PrivilegedAction<Integer>() {
                    @Override
                    public Integer run() {
                        return Integer.valueOf(doGetAttributeScope(name));
                    }
                })).intValue();
    } else {
        return doGetAttributeScope(name);
    }
}
 
Example 7
Source File: PageContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void doRemoveAttribute(String name, int scope) {
    switch (scope) {
    case PAGE_SCOPE:
        attributes.remove(name);
        break;

    case REQUEST_SCOPE:
        request.removeAttribute(name);
        break;

    case SESSION_SCOPE:
        if (session == null) {
            throw new IllegalStateException(Localizer
                    .getMessage("jsp.error.page.noSession"));
        }
        session.removeAttribute(name);
        break;

    case APPLICATION_SCOPE:
        context.removeAttribute(name);
        break;

    default:
        throw new IllegalArgumentException("Invalid scope");
    }
}
 
Example 8
Source File: JspServlet.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
private void handleMissingResource(HttpServletRequest request,
        HttpServletResponse response, String jspUri)
        throws ServletException, IOException {

    String includeRequestUri =
        (String)request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI);

    if (includeRequestUri != null) {
        // This file was included. Throw an exception as
        // a response.sendError() will be ignored
        String msg =
            Localizer.getMessage("jsp.error.file.not.found",jspUri);
        // Strictly, filtering this is an application
        // responsibility but just in case...
        throw new ServletException(SecurityUtil.filter(msg));
    } else {
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND,
                    request.getRequestURI());
        } catch (IllegalStateException ise) {
            log.error(Localizer.getMessage("jsp.error.file.not.found",
                    jspUri));
        }
    }
    return;
}
 
Example 9
Source File: PageContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public Object getAttribute(final String name, final int scope) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (SecurityUtil.isPackageProtectionEnabled()) {
        return AccessController.doPrivileged(
                new PrivilegedAction<Object>() {
            @Override
            public Object run() {
                return doGetAttribute(name, scope);
            }
        });
    } else {
        return doGetAttribute(name, scope);
    }

}
 
Example 10
Source File: PageContextImpl.java    From Tomcat7.0.67 with Apache License 2.0 6 votes vote down vote up
@Override
public void removeAttribute(final String name) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (SecurityUtil.isPackageProtectionEnabled()) {
        AccessController.doPrivileged(new PrivilegedAction<Void>() {
            @Override
            public Void run() {
                doRemoveAttribute(name);
                return null;
            }
        });
    } else {
        doRemoveAttribute(name);
    }
}
 
Example 11
Source File: UTF8Reader.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
/** Throws an exception for expected byte. */
private void expectedByte(int position, int count)
    throws UTFDataFormatException {

    throw new UTFDataFormatException(
            Localizer.getMessage("jsp.error.xml.expectedByte",
                                 Integer.toString(position),
                                 Integer.toString(count)));

}
 
Example 12
Source File: PageContextImpl.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private void doSetAttribute(String name, Object o, int scope) {
    if (o != null) {
        switch (scope) {
        case PAGE_SCOPE:
            attributes.put(name, o);
            break;

        case REQUEST_SCOPE:
            request.setAttribute(name, o);
            break;

        case SESSION_SCOPE:
            if (session == null) {
                throw new IllegalStateException(Localizer
                        .getMessage("jsp.error.page.noSession"));
            }
            session.setAttribute(name, o);
            break;

        case APPLICATION_SCOPE:
            context.setAttribute(name, o);
            break;

        default:
            throw new IllegalArgumentException("Invalid scope");
        }
    } else {
        removeAttribute(name, scope);
    }
}
 
Example 13
Source File: JspContextWrapper.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Override
public void removeAttribute(String name, int scope) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (scope == PAGE_SCOPE) {
        pageAttributes.remove(name);
    } else {
        rootJspCtxt.removeAttribute(name, scope);
    }
}
 
Example 14
Source File: JspContextWrapper.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void removeAttribute(String name) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    pageAttributes.remove(name);
    rootJspCtxt.removeAttribute(name, REQUEST_SCOPE);
    if (getSession() != null) {
        rootJspCtxt.removeAttribute(name, SESSION_SCOPE);
    }
    rootJspCtxt.removeAttribute(name, APPLICATION_SCOPE);
}
 
Example 15
Source File: JspContextWrapper.java    From Tomcat7.0.67 with Apache License 2.0 5 votes vote down vote up
@Override
public void removeAttribute(String name, int scope) {

    if (name == null) {
        throw new NullPointerException(Localizer
                .getMessage("jsp.error.attribute.null_name"));
    }

    if (scope == PAGE_SCOPE) {
        pageAttributes.remove(name);
    } else {
        rootJspCtxt.removeAttribute(name, scope);
    }
}
 
Example 16
Source File: JspCServletContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
private WebXml buildMergedWebXml(boolean validate, boolean blockExternal)
        throws JasperException {
    WebXml webXml = new WebXml();
    WebXmlParser webXmlParser = new WebXmlParser(validate, validate, blockExternal);
    // Use this class's classloader as Ant will have set the TCCL to its own
    webXmlParser.setClassLoader(getClass().getClassLoader());

    try {
        URL url = getResource(
                org.apache.tomcat.util.descriptor.web.Constants.WEB_XML_LOCATION);
        if (!webXmlParser.parseWebXml(url, webXml, false)) {
            throw new JasperException(Localizer.getMessage("jspc.error.invalidWebXml"));
        }
    } catch (IOException e) {
        throw new JasperException(e);
    }

    // if the application is metadata-complete then we can skip fragment processing
    if (webXml.isMetadataComplete()) {
        return webXml;
    }

    // If an empty absolute ordering element is present, fragment processing
    // may be skipped.
    Set<String> absoluteOrdering = webXml.getAbsoluteOrdering();
    if (absoluteOrdering != null && absoluteOrdering.isEmpty()) {
        return webXml;
    }

    Map<String, WebXml> fragments = scanForFragments(webXmlParser);
    Set<WebXml> orderedFragments = WebXml.orderWebFragments(webXml, fragments, this);

    // Find resource JARs
    this.resourceJARs = scanForResourceJARs(orderedFragments, fragments.values());

    // JspC is not affected by annotations so skip that processing, proceed to merge
    webXml.merge(orderedFragments);
    return webXml;
}
 
Example 17
Source File: JspC.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * Include the generated web.xml inside the webapp's web.xml.
 * @throws IOException An IO error occurred
 */
protected void mergeIntoWebXml() throws IOException {

    File webappBase = new File(uriRoot);
    File webXml = new File(webappBase, "WEB-INF/web.xml");
    File webXml2 = new File(webappBase, "WEB-INF/web2.xml");
    String insertStartMarker =
        Localizer.getMessage("jspc.webinc.insertStart");
    String insertEndMarker =
        Localizer.getMessage("jspc.webinc.insertEnd");

    try (BufferedReader reader = new BufferedReader(openWebxmlReader(webXml));
            BufferedReader fragmentReader =
                    new BufferedReader(openWebxmlReader(new File(webxmlFile)));
            PrintWriter writer = new PrintWriter(openWebxmlWriter(webXml2))) {

        // Insert the <servlet> and <servlet-mapping> declarations
        boolean inserted = false;
        int current = reader.read();
        while (current > -1) {
            if (current == '<') {
                String element = getElement(reader);
                if (!inserted && insertBefore.contains(element)) {
                    // Insert generated content here
                    writer.println(insertStartMarker);
                    while (true) {
                        String line = fragmentReader.readLine();
                        if (line == null) {
                            writer.println();
                            break;
                        }
                        writer.println(line);
                    }
                    writer.println(insertEndMarker);
                    writer.println();
                    writer.write(element);
                    inserted = true;
                } else if (element.equals(insertStartMarker)) {
                    // Skip the previous auto-generated content
                    while (true) {
                        current = reader.read();
                        if (current < 0) {
                            throw new EOFException();
                        }
                        if (current == '<') {
                            element = getElement(reader);
                            if (element.equals(insertEndMarker)) {
                                break;
                            }
                        }
                    }
                    current = reader.read();
                    while (current == '\n' || current == '\r') {
                        current = reader.read();
                    }
                    continue;
                } else {
                    writer.write(element);
                }
            } else {
                writer.write(current);
            }
            current = reader.read();
        }
    }

    try (FileInputStream fis = new FileInputStream(webXml2);
            FileOutputStream fos = new FileOutputStream(webXml)) {

        byte buf[] = new byte[512];
        while (true) {
            int n = fis.read(buf);
            if (n < 0) {
                break;
            }
            fos.write(buf, 0, n);
        }
    }

    if(!webXml2.delete() && log.isDebugEnabled())
        log.debug(Localizer.getMessage("jspc.delete.fail",
                webXml2.toString()));

    if (!(new File(webxmlFile)).delete() && log.isDebugEnabled())
        log.debug(Localizer.getMessage("jspc.delete.fail", webxmlFile));

}
 
Example 18
Source File: JspServletWrapper.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
/**
 * <p>Attempts to construct a JasperException that contains helpful information
 * about what went wrong. Uses the JSP compiler system to translate the line
 * number in the generated servlet that originated the exception to a line
 * number in the JSP.  Then constructs an exception containing that
 * information, and a snippet of the JSP to help debugging.
 * Please see https://bz.apache.org/bugzilla/show_bug.cgi?id=37062 and
 * http://www.tfenne.com/jasper/ for more details.
 *</p>
 *
 * @param ex the exception that was the cause of the problem.
 * @return a JasperException with more detailed information
 */
protected JasperException handleJspException(Exception ex) {
    try {
        Throwable realException = ex;
        if (ex instanceof ServletException) {
            realException = ((ServletException) ex).getRootCause();
        }

        // First identify the stack frame in the trace that represents the JSP
        StackTraceElement[] frames = realException.getStackTrace();
        StackTraceElement jspFrame = null;

        for (int i=0; i<frames.length; ++i) {
            if ( frames[i].getClassName().equals(this.getServlet().getClass().getName()) ) {
                jspFrame = frames[i];
                break;
            }
        }


        if (jspFrame == null ||
                this.ctxt.getCompiler().getPageNodes() == null) {
            // If we couldn't find a frame in the stack trace corresponding
            // to the generated servlet class or we don't have a copy of the
            // parsed JSP to hand, we can't really add anything
            return new JasperException(ex);
        }

        int javaLineNumber = jspFrame.getLineNumber();
        JavacErrorDetail detail = ErrorDispatcher.createJavacError(
                jspFrame.getMethodName(),
                this.ctxt.getCompiler().getPageNodes(),
                null,
                javaLineNumber,
                ctxt);

        // If the line number is less than one we couldn't find out
        // where in the JSP things went wrong
        int jspLineNumber = detail.getJspBeginLineNumber();
        if (jspLineNumber < 1) {
            throw new JasperException(ex);
        }

        if (options.getDisplaySourceFragment()) {
            return new JasperException(Localizer.getMessage
                    ("jsp.exception", detail.getJspFileName(),
                            "" + jspLineNumber) + System.lineSeparator() +
                            System.lineSeparator() + detail.getJspExtract() +
                            System.lineSeparator() + System.lineSeparator() +
                            "Stacktrace:", ex);

        }

        return new JasperException(Localizer.getMessage
                ("jsp.exception", detail.getJspFileName(),
                        "" + jspLineNumber), ex);
    } catch (Exception je) {
        // If anything goes wrong, just revert to the original behaviour
        if (ex instanceof JasperException) {
            return (JasperException) ex;
        }
        return new JasperException(ex);
    }
}
 
Example 19
Source File: HttpJspBase.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
@Override
public String getServletInfo() {
    return Localizer.getMessage("jsp.engine.info");
}
 
Example 20
Source File: UTF8Reader.java    From Tomcat8-Source-Read with MIT License 3 votes vote down vote up
/**
 * Mark the present position in the stream.  Subsequent calls to reset()
 * will attempt to reposition the stream to this point.  Not all
 * character-input streams support the mark() operation.
 *
 * @param  readAheadLimit  Limit on the number of characters that may be
 *                         read while still preserving the mark.  After
 *                         reading this many characters, attempting to
 *                         reset the stream may fail.
 *
 * @exception  IOException  If the stream does not support mark(),
 *                          or if some other I/O error occurs
 */
@Override
public void mark(int readAheadLimit) throws IOException {
    throw new IOException(
            Localizer.getMessage("jsp.error.xml.operationNotSupported",
                                 "mark()", "UTF-8"));
}