Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfabrix101/56ebfb168a0d6190b7a67af1efd69558 to your computer and use it in GitHub Desktop.
Save jfabrix101/56ebfb168a0d6190b7a67af1efd69558 to your computer and use it in GitHub Desktop.
Ewaf Download Action
package it.jfabrix101.wso2.stat.action;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import jfabrix101.ewaf.servlet.EwafAction;
import jfabrix101.ewaf.util.StringHelper;
public abstract class EwafDownloadAction extends EwafAction {
public static final class DownloadableContent {
private static Map<String, String> contentTypeMap = null;
private static Map<String, String> getContentTypeMap() {
if (contentTypeMap == null) {
contentTypeMap = new HashMap<String, String>();
contentTypeMap.put("JPEG", "image/jpeg"); contentTypeMap.put("JPG", "image/jpeg");
contentTypeMap.put("PNG", "image/png");
contentTypeMap.put("PDF", "application/pdf");
contentTypeMap.put("TXT", "text/plain");
contentTypeMap.put("HTML", "text/html"); contentTypeMap.put("HTM", "text/html");
contentTypeMap.put("ZIP", " application/zip, application/octet-stream");
contentTypeMap.put("PDF", "application/pdf");
}
return contentTypeMap;
}
private InputStream contentInputStream = null;
private String contentFileName = null;
private String contentType = null;
private int contentLenght = -1;
public void addContentType(String ext, String contentType) { getContentTypeMap().put(ext.toUpperCase(), contentType); }
public InputStream getContentInputStream() { return contentInputStream; }
public void setContentInputStream(InputStream contentInputStream) { this.contentInputStream = contentInputStream; }
public String getContentFileName() { return contentFileName; }
public void setContentFileName(String contentFileName) { this.contentFileName = contentFileName; }
public void setContentType(String contentType) { this.contentType = contentType; }
public int getContentLenght() { return contentLenght; }
public void setContentLenght(int contentLenght) { this.contentLenght = contentLenght; }
public String getContentType() {
if (contentType != null) return contentType;
if (contentFileName == null) return null;
String fileExt = contentFileName.substring(contentFileName.lastIndexOf(".") + 1);
contentType = getContentTypeMap().get(fileExt.toUpperCase());
return contentType;
}
}
@Override
public String executeAction(Map<String, String> config,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DownloadableContent content = getDownloadableContent(config, request, response);
if (content == null) return ERROR;
InputStream contentInputStream = content.getContentInputStream();
String contentFileName = content.getContentFileName();
String contentType = content.getContentType();
int contentLength = content.getContentLenght();
if (!StringHelper.isEmpty(contentFileName)) response.setHeader("Content-Disposition", "attachment; filename=" + contentFileName);
if (!StringHelper.isEmpty(contentType)) response.setHeader("Content-type",contentType);
if (contentLength > 0) response.setContentLength(contentLength);
OutputStream out = response.getOutputStream();
IOUtils.copy(contentInputStream, out);
contentInputStream.close();
return null;
}
public abstract DownloadableContent getDownloadableContent(
Map<String, String> config,
HttpServletRequest request, HttpServletResponse response);
@Override
protected String validate(Map<String, String> config,
HttpServletRequest request, HttpServletResponse response) {
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment