Search in sources :

Example 26 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class ResourceTag method doStartTag.

// --------------------------------------------------------- Public Methods
/**
     * Retrieve the required property and expose it as a scripting variable.
     *
     * @throws JspException if a JSP exception has occurred
     */
public int doStartTag() throws JspException {
    // Acquire an input stream to the specified resource
    InputStream stream = pageContext.getServletContext().getResourceAsStream(name);
    if (stream == null) {
        JspException e = new JspException(messages.getMessage("resource.get", name));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    // If we are returning an InputStream, do so and return
    if (input != null) {
        pageContext.setAttribute(id, stream);
        return (SKIP_BODY);
    }
    // Accumulate the contents of this resource into a StringBuffer
    try {
        StringBuffer sb = new StringBuffer();
        InputStreamReader reader = new InputStreamReader(stream);
        char[] buffer = new char[BUFFER_SIZE];
        int n = 0;
        while (true) {
            n = reader.read(buffer);
            if (n < 1) {
                break;
            }
            sb.append(buffer, 0, n);
        }
        reader.close();
        pageContext.setAttribute(id, sb.toString());
    } catch (IOException e) {
        TagUtils.getInstance().saveException(pageContext, e);
        throw new JspException(messages.getMessage("resource.get", name));
    }
    return (SKIP_BODY);
}
Also used : JspException(javax.servlet.jsp.JspException) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 27 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class SizeTag method doStartTag.

// --------------------------------------------------------- Public Methods
/**
     * Retrieve the required property and expose it as a scripting variable.
     *
     * @throws JspException if a JSP exception has occurred
     */
public int doStartTag() throws JspException {
    // Retrieve the required property value
    Object value = this.collection;
    if (value == null) {
        if (name == null) {
            // Must specify either a collection attribute or a name
            // attribute.
            JspException e = new JspException(messages.getMessage("size.noCollectionOrName"));
            TagUtils.getInstance().saveException(pageContext, e);
            throw e;
        }
        value = TagUtils.getInstance().lookup(pageContext, name, property, scope);
    }
    // Identify the number of elements, based on the collection type
    int size = 0;
    if (value == null) {
        JspException e = new JspException(messages.getMessage("size.collection"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    } else if (value.getClass().isArray()) {
        size = Array.getLength(value);
    } else if (value instanceof Collection) {
        size = ((Collection) value).size();
    } else if (value instanceof Map) {
        size = ((Map) value).size();
    } else {
        JspException e = new JspException(messages.getMessage("size.collection"));
        TagUtils.getInstance().saveException(pageContext, e);
        throw e;
    }
    // Expose this size as a scripting variable
    pageContext.setAttribute(this.id, new Integer(size), PageContext.PAGE_SCOPE);
    return (SKIP_BODY);
}
Also used : JspException(javax.servlet.jsp.JspException) Collection(java.util.Collection) Map(java.util.Map)

Example 28 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class TestTagUtils method skiptestComputeParametersParamIdAsStringArray.

public void skiptestComputeParametersParamIdAsStringArray() {
    Map params = new HashMap();
    //        String[] vals = new String[]{"test0, test1"};
    params.put("fooParamId", "fooParamValue");
    request.getSession().setAttribute("SomeBean", params);
    Map map = null;
    try {
        map = tagutils.computeParameters(pageContext, "fooParamId", "SomeBean", null, null, "SomeBean", null, null, false);
        //            map = tagutils.computeParameters(
        //                    page, "paramId", "SomeBean", "stringArray", null,
        //                    null, null, null, false);
        assertNotNull("map is null", map);
        String val = (String) map.get("key0");
        assertTrue("paramId should be \"test\"", "1.0".equals(val));
    } catch (JspException e) {
        fail("JspException not thrown");
    }
}
Also used : JspException(javax.servlet.jsp.JspException) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class TestTagUtils method testComputeParameters2d.

// Provided map -- name with one key and two values
public void testComputeParameters2d() {
    Map map = new HashMap();
    map.put("foo", new String[] { "bar1", "bar2" });
    request.getSession().setAttribute("attr", map);
    try {
        map = tagutils.computeParameters(pageContext, null, null, null, null, "attr", null, null, false);
    } catch (JspException e) {
        fail("JspException: " + e);
    }
    assertNotNull("Map is not null", map);
    assertEquals("One parameter in the returned map", 1, map.size());
    assertTrue("Parameter foo present", map.containsKey("foo"));
    assertTrue("Parameter foo value type", map.get("foo") instanceof String[]);
    String[] values = (String[]) map.get("foo");
    assertEquals("Values count", 2, values.length);
}
Also used : JspException(javax.servlet.jsp.JspException) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 30 with JspException

use of javax.servlet.jsp.JspException in project sonarqube by SonarSource.

the class TestTagUtils method testComputeParameters1b.

// Single parameter -- scope + name
public void testComputeParameters1b() {
    request.setAttribute("attr", "bar");
    Map map = null;
    try {
        map = tagutils.computeParameters(pageContext, "foo", "attr", null, "request", null, null, null, false);
    } catch (JspException e) {
        fail("JspException: " + e);
    }
    assertNotNull("Map is not null", map);
    assertEquals("One parameter in the returned map", 1, map.size());
    assertTrue("Parameter present", map.containsKey("foo"));
    assertEquals("Parameter value", "bar", (String) map.get("foo"));
}
Also used : JspException(javax.servlet.jsp.JspException) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JspException (javax.servlet.jsp.JspException)151 IOException (java.io.IOException)34 Map (java.util.Map)30 HashMap (java.util.HashMap)21 HttpServletRequest (javax.servlet.http.HttpServletRequest)21 Iterator (java.util.Iterator)20 JspWriter (javax.servlet.jsp.JspWriter)20 ActionMessages (org.apache.struts.action.ActionMessages)15 MockFormBean (org.apache.struts.mock.MockFormBean)15 InvocationTargetException (java.lang.reflect.InvocationTargetException)11 ActionMessage (org.apache.struts.action.ActionMessage)11 ArrayList (java.util.ArrayList)7 Collection (java.util.Collection)7 List (java.util.List)7 PageExpiredException (org.mifos.framework.exceptions.PageExpiredException)6 MalformedURLException (java.net.MalformedURLException)5 Locale (java.util.Locale)5 ModuleConfig (org.apache.struts.config.ModuleConfig)5 UserContext (org.mifos.security.util.UserContext)5 JspTag (javax.servlet.jsp.tagext.JspTag)4