1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.pdf.impl

File PdfURLFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

2
19
6
1
131
71
7
0.37
3.17
6
1.17

Classes

Class Line # Actions
PdfURLFactory 41 19 0% 7 27
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.pdf.impl;
21   
22    import java.net.URL;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xwiki.model.reference.AttachmentReference;
27    import org.xwiki.model.reference.DocumentReference;
28   
29    import com.xpn.xwiki.XWikiContext;
30    import com.xpn.xwiki.internal.model.LegacySpaceResolver;
31    import com.xpn.xwiki.web.Utils;
32    import com.xpn.xwiki.web.XWikiServletURLFactory;
33   
34    /**
35    * Computes Image and Link URLs for attachments by using an absolute URLs but stores a Map in the XWiki Context to
36    * associate attachment URLs to Attachment Entity References so that when executing a PDF export the custom URI Resolver
37    * we use can stream image attachment content so that they are embedded in the PDF.
38    *
39    * @version $Id: 8d289e918c949b3c80e3ae7b10409d0ce34b93dd $
40    */
 
41    public class PdfURLFactory extends FileSystemURLFactory
42    {
43    private LegacySpaceResolver legacySpaceResolver = Utils.getComponent(LegacySpaceResolver.class);
44   
45    /**
46    * We delegate to the XWiki Servlet URL Factory for the creation of attachment URLs since we want links to
47    * attachments to be absolute. Image URLs are resolved by the {@link PDFResourceResolver}.
48    */
49    private XWikiServletURLFactory servletURLFactory = new XWikiServletURLFactory();
50   
 
51  0 toggle @Override
52    public void init(XWikiContext context)
53    {
54  0 super.init(context);
55  0 this.servletURLFactory.init(context);
56    }
57   
58    /**
59    * Key used to save image attachment data.
60    *
61    * @see #saveAttachmentReference(java.net.URL, String, String, String, String, com.xpn.xwiki.XWikiContext)
62    */
63    static final String PDF_EXPORT_CONTEXT_KEY = "pdfExportImageURLMap";
64   
 
65  0 toggle @Override
66    public URL createAttachmentURL(String filename, String spaces, String name, String action, String querystring,
67    String wiki, XWikiContext context)
68    {
69  0 URL url =
70    this.servletURLFactory.createAttachmentURL(filename, spaces, name, action, querystring, wiki, context);
71  0 saveAttachmentReference(url, wiki, spaces, name, filename, context);
72  0 return url;
73    }
74   
 
75  0 toggle @Override
76    public URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision, String wiki,
77    XWikiContext context)
78    {
79  0 URL url = this.servletURLFactory.createAttachmentURL(filename, spaces, name, revision, wiki, context);
80  0 saveAttachmentReference(url, wiki, spaces, name, filename, context);
81  0 return url;
82    }
83   
 
84  0 toggle @Override
85    public URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision,
86    String querystring, String xwikidb, XWikiContext context)
87    {
88  0 URL url =
89    this.servletURLFactory.createAttachmentRevisionURL(filename, spaces, name, revision, querystring, xwikidb,
90    context);
91  0 saveAttachmentReference(url, xwikidb, spaces, name, filename, context);
92  0 return url;
93    }
94   
 
95  0 toggle @Override
96    public URL createAttachmentRevisionURL(String filename, String spaces, String name, String revision, long recycleId,
97    String querystring, String xwikidb, XWikiContext context)
98    {
99  0 URL url =
100    this.servletURLFactory.createAttachmentRevisionURL(filename, spaces, name, revision, recycleId, querystring,
101    xwikidb, context);
102  0 saveAttachmentReference(url, xwikidb, spaces, name, filename, context);
103  0 return url;
104    }
105   
106    /**
107    * @param url the URL to save in the attachment map
108    * @param wiki the wiki where the attachment is located
109    * @param spaces a serialized space reference which can contain one or several spaces (e.g. "space1.space2"). If
110    * a space name contains a dot (".") it must be passed escaped as in "space1\.with\.dot.space2"
111    * @param page the page where the attachment is located
112    * @param fileName the name of the attachment file
113    * @param context the XWiki Context where to find the attachment map that we add to
114    * @see PDFResourceResolver
115    */
 
116  0 toggle private void saveAttachmentReference(URL url, String wiki, String spaces, String page, String fileName,
117    XWikiContext context)
118    {
119    // Save Entity Reference pointed to by this URL in the Context so that it can be used at export time in the
120    // PDF URIResolver code.
121  0 Map<String, AttachmentReference> attachmentMap =
122    (Map<String, AttachmentReference>) context.get(PDF_EXPORT_CONTEXT_KEY);
123  0 if (attachmentMap == null) {
124  0 attachmentMap = new HashMap<>();
125  0 context.put(PDF_EXPORT_CONTEXT_KEY, attachmentMap);
126    }
127   
128  0 attachmentMap.put(url.toString(), new AttachmentReference(fileName,
129    new DocumentReference(wiki, this.legacySpaceResolver.resolve(spaces), page)));
130    }
131    }