Search in sources :

Example 1 with JspException

use of jakarta.servlet.jsp.JspException in project spring-security by spring-projects.

the class AuthenticationTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    Object result = null;
    // determine the value by...
    if (this.property != null) {
        if ((SecurityContextHolder.getContext() == null) || !(SecurityContextHolder.getContext() instanceof SecurityContext) || (SecurityContextHolder.getContext().getAuthentication() == null)) {
            return Tag.EVAL_PAGE;
        }
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth.getPrincipal() == null) {
            return Tag.EVAL_PAGE;
        }
        try {
            BeanWrapperImpl wrapper = new BeanWrapperImpl(auth);
            result = wrapper.getPropertyValue(this.property);
        } catch (BeansException ex) {
            throw new JspException(ex);
        }
    }
    if (this.var != null) {
        /*
			 * Store the result, letting an IllegalArgumentException propagate back if the
			 * scope is invalid (e.g., if an attempt is made to store something in the
			 * session without any HttpSession existing).
			 */
        if (result != null) {
            this.pageContext.setAttribute(this.var, result, this.scope);
        } else {
            if (this.scopeSpecified) {
                this.pageContext.removeAttribute(this.var, this.scope);
            } else {
                this.pageContext.removeAttribute(this.var);
            }
        }
    } else {
        if (this.htmlEscape) {
            writeMessage(TextEscapeUtils.escapeEntities(String.valueOf(result)));
        } else {
            writeMessage(String.valueOf(result));
        }
    }
    return EVAL_PAGE;
}
Also used : JspException(jakarta.servlet.jsp.JspException) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) BeansException(org.springframework.beans.BeansException)

Example 2 with JspException

use of jakarta.servlet.jsp.JspException in project spring-framework by spring-projects.

the class UrlTag method createQueryString.

/**
 * Build the query string from available parameters that have not already
 * been applied as template params.
 * <p>The names and values of parameters are URL encoded.
 * @param params the parameters to build the query string from
 * @param usedParams set of parameter names that have been applied as
 * template params
 * @param includeQueryStringDelimiter true if the query string should start
 * with a '?' instead of '&amp;'
 * @return the query string
 */
protected String createQueryString(List<Param> params, Set<String> usedParams, boolean includeQueryStringDelimiter) throws JspException {
    String encoding = this.pageContext.getResponse().getCharacterEncoding();
    StringBuilder qs = new StringBuilder();
    for (Param param : params) {
        if (!usedParams.contains(param.getName()) && StringUtils.hasLength(param.getName())) {
            if (includeQueryStringDelimiter && qs.length() == 0) {
                qs.append('?');
            } else {
                qs.append('&');
            }
            try {
                qs.append(UriUtils.encodeQueryParam(param.getName(), encoding));
                if (param.getValue() != null) {
                    qs.append('=');
                    qs.append(UriUtils.encodeQueryParam(param.getValue(), encoding));
                }
            } catch (UnsupportedCharsetException ex) {
                throw new JspException(ex);
            }
        }
    }
    return qs.toString();
}
Also used : JspException(jakarta.servlet.jsp.JspException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException)

Example 3 with JspException

use of jakarta.servlet.jsp.JspException in project spring-framework by spring-projects.

the class EscapeBodyTag method doAfterBody.

@Override
public int doAfterBody() throws JspException {
    try {
        String content = readBodyContent();
        // HTML and/or JavaScript escape, if demanded
        content = htmlEscape(content);
        content = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content);
        writeBodyContent(content);
    } catch (IOException ex) {
        throw new JspException("Could not write escaped body", ex);
    }
    return (SKIP_BODY);
}
Also used : JspException(jakarta.servlet.jsp.JspException) IOException(java.io.IOException)

Example 4 with JspException

use of jakarta.servlet.jsp.JspException in project tomcat by apache.

the class TagHandlerPool method get.

/**
 * Gets the next available tag handler from this tag handler pool,
 * instantiating one if this tag handler pool is empty.
 *
 * @param handlerClass
 *            Tag handler class
 * @return Reused or newly instantiated tag handler
 * @throws JspException
 *             if a tag handler cannot be instantiated
 */
public Tag get(Class<? extends Tag> handlerClass) throws JspException {
    Tag handler;
    synchronized (this) {
        if (current >= 0) {
            handler = handlers[current--];
            return handler;
        }
    }
    // wait for us to construct a tag for this thread.
    try {
        if (useInstanceManagerForTags) {
            return (Tag) instanceManager.newInstance(handlerClass.getName(), handlerClass.getClassLoader());
        } else {
            Tag instance = handlerClass.getConstructor().newInstance();
            instanceManager.newInstance(instance);
            return instance;
        }
    } catch (Exception e) {
        Throwable t = ExceptionUtils.unwrapInvocationTargetException(e);
        ExceptionUtils.handleThrowable(t);
        throw new JspException(e.getMessage(), t);
    }
}
Also used : JspException(jakarta.servlet.jsp.JspException) Tag(jakarta.servlet.jsp.tagext.Tag) JspException(jakarta.servlet.jsp.JspException)

Example 5 with JspException

use of jakarta.servlet.jsp.JspException in project spring-framework by spring-projects.

the class UrlTag method doEndTag.

@Override
public int doEndTag() throws JspException {
    String url = createUrl();
    RequestDataValueProcessor processor = getRequestContext().getRequestDataValueProcessor();
    ServletRequest request = this.pageContext.getRequest();
    if ((processor != null) && (request instanceof HttpServletRequest)) {
        url = processor.processUrl((HttpServletRequest) request, url);
    }
    if (this.var == null) {
        // print the url to the writer
        try {
            this.pageContext.getOut().print(url);
        } catch (IOException ex) {
            throw new JspException(ex);
        }
    } else {
        // store the url as a variable
        this.pageContext.setAttribute(this.var, url, this.scope);
    }
    return EVAL_PAGE;
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) ServletRequest(jakarta.servlet.ServletRequest) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) JspException(jakarta.servlet.jsp.JspException) RequestDataValueProcessor(org.springframework.web.servlet.support.RequestDataValueProcessor) IOException(java.io.IOException)

Aggregations

JspException (jakarta.servlet.jsp.JspException)10 IOException (java.io.IOException)4 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)2 ServletRequest (jakarta.servlet.ServletRequest)1 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)1 JspTagException (jakarta.servlet.jsp.JspTagException)1 PageContext (jakarta.servlet.jsp.PageContext)1 Tag (jakarta.servlet.jsp.tagext.Tag)1 PropertyEditor (java.beans.PropertyEditor)1 Test (org.junit.jupiter.api.Test)1 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)1 BeansException (org.springframework.beans.BeansException)1 EvaluationContext (org.springframework.expression.EvaluationContext)1 StandardEvaluationContext (org.springframework.expression.spel.support.StandardEvaluationContext)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 JspAwareRequestContext (org.springframework.web.servlet.support.JspAwareRequestContext)1 RequestDataValueProcessor (org.springframework.web.servlet.support.RequestDataValueProcessor)1