Search in sources :

Example 1 with RecordModel

use of com.developmentontheedge.be5.databasemodel.RecordModel in project be5 by DevelopmentOnTheEdge.

the class DownloadComponent method generate.

@Override
public void generate(Request req, Response res, Injector injector) {
    String entity = req.getNonEmpty("_t_");
    String ID = req.get("ID");
    String typeColumn = req.get("_typeColumn_");
    String filenameColumn = req.get("_filenameColumn_");
    String dataColumn = req.getNonEmpty("_dataColumn_");
    String charsetColumn = req.get("_charsetColumn_");
    // String encoding       = req.get("_enc_");
    boolean download = "yes".equals(req.get("_download_"));
    RecordModel record = injector.get(DatabaseModel.class).getEntity(entity).get(ID);
    String filename = record.getValueAsString(filenameColumn);
    String contentType = record.getValueAsString(typeColumn);
    String charset = MoreObjects.firstNonNull(record.getValueAsString(charsetColumn), Charsets.UTF_8.name());
    Object data = record.getValue(dataColumn);
    InputStream in;
    if (data instanceof byte[]) {
        in = new ByteArrayInputStream((byte[]) data);
    } else // else if (data instanceof Blob)
    // {
    // in = ((Blob) data).getBinaryStream();
    // }
    // else if (data instanceof String)
    // {
    // in = new ByteArrayInputStream(((String) data).getBytes(charset));
    // }
    {
        throw Be5Exception.internal("Unknown data type");
    }
    HttpServletResponse response = res.getRawResponse();
    response.setContentType(contentType + "; charset=" + charset);
    if (download) {
        response.setHeader("Content-disposition", "attachment; filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
    } else {
        response.setHeader("Content-disposition", "filename=" + UrlEscapers.urlFormParameterEscaper().escape(filename));
    }
    try {
        ByteStreams.copy(in, response.getOutputStream());
    } catch (IOException e) {
        throw Be5Exception.internal(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) RecordModel(com.developmentontheedge.be5.databasemodel.RecordModel) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException)

Aggregations

RecordModel (com.developmentontheedge.be5.databasemodel.RecordModel)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1