Kawai Wiki

Rev

Rev 146 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.kawai.tag.kcode;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

/**
 * KCode class Download.
 *
 * @author Sergio Oliveira
 *
 */

public class Download implements KCode {

    /**
     * {@inheritDoc}.
     */

    @Override
    public String getTag() {
        return "download";
    }

    /**
     * {@inheritDoc}.
     */

    @Override
    public boolean hasBody() {
        return false;
    }

    /**
     * {@inheritDoc}.
     */

    @Override
    public String process(PageContext pageContext, Map<String, String> attributes, String body) throws JspException {

        if (body != null) throw new JspException("contextPath tag cannot have a body!");

        String filename = attributes.get("file");

        if (filename == null) throw new JspException("file attribute is mandatory!");

        HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
        String cp = req.getContextPath();

        return "<a href=\"" + cp + "/uploads/archive/" + filename + "\">" + filename + "</a>";
    }
}