You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cocoon.apache.org by Russ White <ru...@earthlink.net> on 2000/09/15 16:44:10 UTC

Pesky FOP PDF problem on explorer fix

The problem (on explorer anyway) is the when ie sees a url that ends with
.xml it assumes text/xml mime type.

The following servlet fixes the fop pdf problem on explorer 5.x and
netscape.

to use it simple map it with a name like /pdf/

the invoke it like this /pdf/documentName (no .xml extension here!)

the sevlet then looks for a url documentName.xml to grab and return to the
client.

Hope this solves a lot of problems for you guys.

Servlet code follows:

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.net.*;

/**
 * A simple servlet that tests inderict loading of a PDF file generated by
 * Cocoon and FOP.
 *
 * This servlet is intended to address the problems many people have had
 * displaying their FOP genrated PDF files on Explorer 5.x and Netscape 4.7X
 */

public class PdfServlet extends HttpServlet {

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  public void doPost(HttpServletRequest request, HttpServletResponse
response)
      throws ServletException, IOException {
    doGet(request,response);
  }

  public void doGet(HttpServletRequest request, HttpServletResponse
response)
      throws ServletException, IOException {
    response.setHeader("Cache-Control", "no-cache, post-check=0,
pre-check=0");
    String agent = request.getHeader("User-Agent").toLowerCase();
    // netscape chokes on Pragma no-cahe so only send it to explorer
    if (agent.indexOf("explorer") > -1){
      response.setHeader("Pragma", "no-cache");
    }
    response.setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
    String spec = "";
    String protocol = "http://";
    if (request.isSecure()) protocol = "https://";
    String server = request.getServerName();
    int port = request.getServerPort();
    String doc = request.getParameter("doc");
    // I add the xml extension dynamically because Explorer gets confused by
    // xml extension because the crappy Microsoft developers neglected
proper
    // use of mime types.
    doc += ".xml";
    // making sure the servlet works with https.
    if (port != 80) spec = protocol+server+port+"/"+doc;
    else spec = protocol+server+"/"+doc;
    // hard coded content type for pdf to test this could be a property or
parm
    response.setContentType("application/pdf");
    URL url = new URL(spec);
    InputStream in = url.openStream();
    OutputStream out = response.getOutputStream();
    int i;
    while ((i = in.read()) > -1) {
      out.write(i);
    }
    in.close();
    out.flush();
    out.close();
  }
}>