Exemplo n.º 1
0
  public static MimeType get(String typeOrExtension) {
    for (MimeType m : EnumSet.allOf(MimeType.class)) {
      if (m.getType().equalsIgnoreCase(typeOrExtension)
          || m.getExtension().equalsIgnoreCase(typeOrExtension)) {
        return m;
      }
    }

    return MimeType.PNG;

    /*Iterator it = lookup.entrySet().iterator();
    while (it.hasNext()) {
    	Map.Entry pair = (Map.Entry)it.next();
    	if
    }

    // Check first if we give in the name of the mime, like svg, png, pfd
    MimeType mime = MimeType.valueOf(type.toUpperCase());
    if (mime != null) {
    	return mime;
    }

    // if not matched above, check if we can lookup the mime based on the content-type
    mime = lookup.get(type);
    if (mime != null) {
    	return mime;
    }
    // else return default
    return MimeType.PNG;*/
  }
 private static MimeType getMime(String mime) {
   MimeType type = MimeType.get(mime);
   if (type != null) {
     return type;
   }
   return MimeType.PNG;
 }
  public static void writeFileContentToHttpResponse(
      String svg, String filename, Float width, MimeType mime, HttpServletResponse response)
      throws IOException, ServletException {

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    if (!MimeType.SVG.equals(mime)) {
      try {
        stream = SVGRasterizer.getInstance().transcode(stream, svg, mime, width);
      } catch (SVGRasterizerException sre) {
        logger.error("Error while transcoding svg file to an image", sre);
        stream.close();
        throw new ServletException("Error while transcoding svg file to an image");
      } catch (TranscoderException te) {
        logger.error("Error while transcoding svg file to an image", te);
        stream.close();
        throw new ServletException("Error while transcoding svg file to an image");
      }
    } else {
      stream.write(svg.getBytes());
    }

    // prepare response
    response.reset();
    response.setContentLength(stream.size());
    response.setCharacterEncoding("utf-8");
    response.setHeader(
        "Content-disposition",
        "attachment; filename=" + filename + "." + mime.name().toLowerCase());
    response.setHeader("Content-type", mime.getType());
    // set encoding before writing to out, check this
    ServletOutputStream out = response.getOutputStream();
    // Send content to Browser
    out.write(stream.toByteArray());
    out.flush();
  }