1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.repository.internal.reference

File ExtensionResourceReference.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

14
41
10
1
239
111
21
0.51
4.1
10
2.1

Classes

Class Line # Actions
ExtensionResourceReference 37 41 0% 21 25
0.6153846461.5%
 

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 org.xwiki.repository.internal.reference;
21   
22    import java.io.UnsupportedEncodingException;
23    import java.net.URI;
24    import java.net.URISyntaxException;
25    import java.net.URLDecoder;
26    import java.net.URLEncoder;
27   
28    import org.apache.commons.lang3.StringUtils;
29    import org.xwiki.rendering.listener.reference.ResourceReference;
30    import org.xwiki.rendering.listener.reference.ResourceType;
31   
32    /**
33    * Represents a reference to an extension file.
34    *
35    * @version $Id: 1b4c3ba011003230ffa0fa1ba2adcba253fa0942 $
36    */
 
37    public class ExtensionResourceReference extends ResourceReference
38    {
39    /**
40    * The type for extension resources.
41    */
42    public static final ResourceType TYPE = new ResourceType("extension");
43   
44    /**
45    * The name of the parameter representing the id of the repository from were to get the extension file.
46    */
47    public static final String PARAM_REPOSITORYID = "rid";
48   
49    /**
50    * The name of the parameter representing the type of the repository from were to get the extension file.
51    */
52    public static final String PARAM_REPOSITORYTYPE = "rtype";
53   
54    /**
55    * The name of the parameter representing the uri of the repository from were to get the extension file.
56    */
57    public static final String PARAM_REPOSITORYURI = "ruri";
58   
59    /**
60    * The encoding used to encode/decode the reference.
61    */
62    private static final String URLENCODING = "UTF-8";
63   
64    /**
65    * @see #getExtensionId()
66    */
67    private String extensionId;
68   
69    /**
70    * @see #getExtensionId()
71    */
72    private String extensionVersion;
73   
74    /**
75    * @see #getRepositoryId()
76    */
77    private String repositoryId;
78   
79    /**
80    * @see #getRepositoryType()
81    */
82    private String repositoryType;
83   
84    /**
85    * @see #getRepositoryUri()
86    */
87    private URI repositoryURI;
88   
89    /**
90    * @param id the id of the extension
91    * @param version the version of the extension
92    * @param repositoryId the id of the repository from where to get the extension file
93    */
 
94  6 toggle public ExtensionResourceReference(String id, String version, String repositoryId)
95    {
96  6 super(encode(id) + '/' + encode(version)
97  6 + (repositoryId != null ? '?' + PARAM_REPOSITORYID + '=' + encode(repositoryId) : ""), TYPE);
98   
99  6 this.extensionId = id;
100  6 this.extensionVersion = version;
101   
102  6 this.repositoryId = repositoryId;
103    }
104   
105    /**
106    * @param id the id of the extension
107    * @param version the version of the extension
108    * @param repositoryType the type of the repository from where to get the extension file
109    * @param repositoryURI the URI of the repository from where to get the extension file
110    */
 
111  0 toggle public ExtensionResourceReference(String id, String version, String repositoryType, URI repositoryURI)
112    {
113  0 super(encode(id)
114    + '/'
115    + encode(version)
116  0 + (repositoryType != null && repositoryURI != null ? '?' + PARAM_REPOSITORYTYPE + '='
117    + encode(repositoryType) + '&' + PARAM_REPOSITORYURI + '=' + encode(repositoryURI.toString()) : ""),
118    TYPE);
119   
120  0 this.extensionId = id;
121  0 this.extensionVersion = version;
122   
123  0 this.repositoryType = repositoryType;
124  0 this.repositoryURI = repositoryURI;
125    }
126   
127    /**
128    * @param reference the reference
129    */
 
130  8 toggle public ExtensionResourceReference(String reference)
131    {
132  8 super(reference, TYPE);
133   
134    // Parameters
135  8 int queryStringIndex = reference.indexOf('?');
136   
137  8 if (queryStringIndex != -1) {
138  8 String[] parameters = StringUtils.split(reference.substring(queryStringIndex + 1), '&');
139  8 for (String parameter : parameters) {
140  8 int equalIndex = parameter.indexOf('=');
141   
142  8 String parameterName = parameter.substring(0, equalIndex);
143  8 String parameterValue = parameter.substring(equalIndex + 1);
144   
145  8 if (PARAM_REPOSITORYID.equals(parameterName)) {
146  8 this.repositoryId = parameterValue;
147  0 } else if (PARAM_REPOSITORYTYPE.equals(parameterName)) {
148  0 this.repositoryType = parameterValue;
149  0 } else if (PARAM_REPOSITORYURI.equals(parameterName)) {
150  0 try {
151  0 this.repositoryURI = new URI(parameterValue);
152    } catch (URISyntaxException e) {
153    // Ignore invalid repository URI
154    }
155    }
156    }
157    } else {
158  0 queryStringIndex = reference.length();
159    }
160   
161    // Id and version
162  8 int index = reference.indexOf('/');
163   
164  8 if (index == -1) {
165  0 this.extensionId = decode(reference.substring(0, queryStringIndex));
166    } else {
167  8 this.extensionId = decode(reference.substring(0, index));
168  8 this.extensionVersion = decode(reference.substring(index + 1, queryStringIndex));
169    }
170    }
171   
172    /**
173    * @param str the string to encode
174    * @return the encoded string
175    */
 
176  18 toggle private static String encode(String str)
177    {
178  18 try {
179  18 return URLEncoder.encode(str, URLENCODING);
180    } catch (UnsupportedEncodingException e) {
181    // Should never happen
182  0 throw new RuntimeException("UTF-8 encoding is not supported");
183    }
184    }
185   
186    /**
187    * @param str the string to decode
188    * @return the decoded string
189    */
 
190  16 toggle private static String decode(String str)
191    {
192  16 try {
193  16 return URLDecoder.decode(str, URLENCODING);
194    } catch (UnsupportedEncodingException e) {
195    // Should never happen
196  0 throw new RuntimeException("UTF-8 dencoding is not supported");
197    }
198    }
199   
200    /**
201    * @return the id of the extension
202    */
 
203  5 toggle public String getExtensionId()
204    {
205  5 return this.extensionId;
206    }
207   
208    /**
209    * @return the version of the extension
210    */
 
211  5 toggle public String getExtensionVersion()
212    {
213  5 return this.extensionVersion;
214    }
215   
216    /**
217    * @return the id of the repository from were to get the extension file
218    */
 
219  10 toggle public String getRepositoryId()
220    {
221  10 return this.repositoryId;
222    }
223   
224    /**
225    * @return the (optional) type of the repository from were to get the extension file
226    */
 
227  2 toggle public String getRepositoryType()
228    {
229  2 return this.repositoryType;
230    }
231   
232    /**
233    * @return the (optional) uri of the repository from were to get the extension file
234    */
 
235  2 toggle public URI getRepositoryURI()
236    {
237  2 return this.repositoryURI;
238    }
239    }