简体   繁体   English

在使用PDFBox和Java Filter Servlet下载PDF文件之前,将其添加到PDF文件中

[英]Adding a Header to a PDF file before it is downloaded using PDFBox and Java Filter Servlet

I am trying to add a Header to a PDF file just before displaying to the client. 我正试图在显示给客户端之前将标题添加到PDF文件。 I have a filter servlet which will be intercepting the request/response before the app asks for Save. 我有一个过滤器Servlet,它将在应用程序请求保存之前拦截请求/响应。 I have tested my PDFBox class on a standalone PDF and that works. 我已经在独立的PDF上测试了我的PDFBox类,并且可以正常工作。 I am having a difficulty with the Filter Servlet. 我在使用Filter Servlet时遇到了困难。 I have tried the HttpServletRequestWrapper but not able to get eh inputstream. 我已经尝试过HttpServletRequestWrapper,但无法获得eh inputstream。 It seems to be empty. 它似乎是空的。 Here is my filter code: 这是我的过滤器代码:

public class AddFOUOFilterServlet implements Filter {
/** The log. */
private static Log log = LogFactory.getLog(AddFOUOFilterServlet.class);

private String loginUrl = null;

/**
 * @desc - This function initializes the global variables.
 * @param filterConfig - FilterConfig settings that come from the web.xml
 */
public void init(FilterConfig filterConfig) throws ServletException {
    log.debug("Inside AddFOUOFilterServlet init ");
    ServletContext context = filterConfig.getServletContext();
} 

/**
 * @desc - the method called by the server before each server request
 * @param req - the servlet request
 * @param res - the servlet response
 * @param chain - a FilterChain object that lists all filters to be called by the server
 * @throws IOException, ServletException
 */
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { 
    //log.debug("Inside doFilter");

    // cast the HTTP request and response objects and get the url path info
    HttpServletRequest request = (HttpServletRequest)req; 
    HttpServletResponse response = (HttpServletResponse)res;  
    HttpServletRequestWrapper requestWrapper = new HttpServletRequestWrapper(request);
    InputStream is = request.getInputStream();
        log.debug("is.available() "+is.available());
        byte[] buf = new byte[1024];
        while((is.read(buf))>0) {
            log.debug("Reading the InputStream from the wrapper ");
    }
    if (request.getParameter("DocumentName") != null && request.getParameter("BlobID") != null)
    {
        log.debug("Processing the PDF URIs... ");
    try 
    {   
            AddFOUOToReport addfouo = new AddFOUOToReport();
            OutputStream os = null;
            os = addfouo.doIt(is, "For Official Use Only");
            chain.doFilter(requestWrapper, res);
    }
    catch (IOException ex)
    {
        log.error("AddFOUOToReport Filter error: " + ex.getMessage());
        response.sendRedirect(loginUrl); 
    } catch (COSVisitorException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    } else {
        chain.doFilter(req, res);
    }

}

public void destroy() {log.info("Inside destroy");  }

} }

Web.xml entry: Web.xml条目:

    <!-- BEGIN AddFOUOFilter Changes  -->
<filter>
    <filter-name>AddFOUO Filter</filter-name>
    <filter-class>com.jmar.bo.controller.AddFOUOFilterServlet</filter-class>
</filter>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/cdz/*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/cdzServlet?getBlob*</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>AddFOUO Filter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<!-- END AddFOUOFilter Changes -->

There is no unique pattern to catch this event that is the reason I have as /* as pattern. 没有捕获此事件的唯一模式,这就是我将/ *作为模式的原因。

If the InputStream is empty that simply means the browser is not sending the PDF. 如果InputStream为空,则仅表示浏览器未发送PDF。

I'm guessing the PDF does not come from the browser, but from the server... 我猜PDF不是来自浏览器,而是来自服务器...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM