1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.classloader.internal.protocol.attachmentjar

File AttachmentURLStreamHandler.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
11
3
1
114
55
5
0.45
3.67
3
1.67

Classes

Class Line # Actions
AttachmentURLStreamHandler 48 11 0% 5 1
0.937593.8%
 

Contributing tests

This file is covered by 8 tests. .

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 org.xwiki.classloader.internal.protocol.attachmentjar;
21   
22    import java.io.IOException;
23    import java.io.UnsupportedEncodingException;
24    import java.net.URL;
25    import java.net.URLConnection;
26    import java.net.URLDecoder;
27    import java.net.URLStreamHandler;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.xwiki.bridge.DocumentAccessBridge;
34    import org.xwiki.classloader.ExtendedURLStreamHandler;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.model.reference.AttachmentReference;
37    import org.xwiki.model.reference.AttachmentReferenceResolver;
38   
39    /**
40    * Special handler that allows building URLs that have their contents in a wiki document's attachment.
41    *
42    * @version $Id: c8f2bf1359ad4f7ded04d5500e33dd33a4e567ae $
43    * @since 2.0.1
44    */
45    @Component
46    @Named("attachmentjar")
47    @Singleton
 
48    public class AttachmentURLStreamHandler extends URLStreamHandler implements ExtendedURLStreamHandler
49    {
50    private static final String ATTACHMENT_JAR_PROTOCOL = "attachmentjar";
51   
52    private static final String ATTACHMENT_JAR_PREFIX = ATTACHMENT_JAR_PROTOCOL + "://";
53   
54    /**
55    * Create attachment name from a string reference.
56    */
57    @Inject
58    @Named("current")
59    private AttachmentReferenceResolver<String> attachmentReferenceResolver;
60   
61    /**
62    * Used to get the current document name and document URLs.
63    */
64    @Inject
65    private DocumentAccessBridge documentAccessBridge;
66   
 
67  7 toggle @Override
68    public String getProtocol()
69    {
70  7 return ATTACHMENT_JAR_PROTOCOL;
71    }
72   
73    /**
74    * {@inheritDoc}
75    *
76    * Parse the attachment URL which is in the format {@code attachmentjar://(wiki):(space).(page)@(filename)}.
77    *
78    * @see URLStreamHandler#openConnection(URL)
79    */
 
80  7 toggle @Override
81    protected URLConnection openConnection(URL url) throws IOException
82    {
83    // Get the attachment reference from the URL and transform it into an AttachmentReference object
84  7 AttachmentReference attachmentReference = this.attachmentReferenceResolver.resolve(
85    getAttachmentReference(url));
86   
87  6 return new AttachmentURLConnection(url, attachmentReference, this.documentAccessBridge);
88    }
89   
 
90  7 toggle private String getAttachmentReference(URL url)
91    {
92    // If the URL doesn't start with the Attachment JAR scheme prefix something is wrong
93  7 String urlAsString = url.toString();
94  7 if (!urlAsString.startsWith(ATTACHMENT_JAR_PREFIX)) {
95  1 throw new RuntimeException("An attachment JAR URL should start with ["
96    + ATTACHMENT_JAR_PREFIX + "], got [" + urlAsString + "]");
97    }
98   
99  6 String attachmentReference = urlAsString.substring(ATTACHMENT_JAR_PREFIX.length());
100  6 try {
101    // Note: we decode using UTF8 since it's the W3C recommendation.
102    // See http://www.w3.org/TR/html40/appendix/notes.html#non-ascii-chars
103    // TODO: Once the xwiki-url module is usable, refactor this code to use it and remove the need to
104    // perform explicit decoding here.
105  6 attachmentReference = URLDecoder.decode(attachmentReference, "UTF-8");
106    } catch (UnsupportedEncodingException e) {
107    // Not supporting UTF-8 as a valid encoding for some reasons. We consider XWiki cannot work
108    // without that encoding.
109  0 throw new RuntimeException("Failed to URL decode [" + attachmentReference + "] using UTF-8.", e);
110    }
111   
112  6 return attachmentReference;
113    }
114    }