1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.wiki

File DefaultLinkService.java

 

Coverage histogram

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

Code metrics

8
61
6
1
255
147
14
0.23
10.17
6
2.33

Classes

Class Line # Actions
DefaultLinkService 50 61 0% 14 75
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 org.xwiki.wysiwyg.server.internal.wiki;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.xwiki.bridge.DocumentAccessBridge;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.gwt.wysiwyg.client.wiki.EntityConfig;
29    import org.xwiki.gwt.wysiwyg.client.wiki.URIReference;
30    import org.xwiki.model.reference.AttachmentReference;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.model.reference.DocumentReferenceResolver;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.rendering.listener.reference.ResourceReference;
37    import org.xwiki.rendering.listener.reference.ResourceType;
38    import org.xwiki.rendering.parser.ResourceReferenceParser;
39    import org.xwiki.rendering.renderer.reference.ResourceReferenceSerializer;
40    import org.xwiki.wysiwyg.server.wiki.EntityReferenceConverter;
41    import org.xwiki.wysiwyg.server.wiki.LinkService;
42   
43    /**
44    * The service used to create links.
45    *
46    * @version $Id: 0d3ba8fda06728b1a248a82e90e854499c1ee0ca $
47    */
48    @Component
49    @Singleton
 
50    public class DefaultLinkService implements LinkService
51    {
52    /**
53    * The component used to access documents. This is temporary till XWiki model is moved into components.
54    */
55    @Inject
56    private DocumentAccessBridge documentAccessBridge;
57   
58    /**
59    * The component used to serialize XWiki document references.
60    */
61    @Inject
62    @Named("compact")
63    private EntityReferenceSerializer<String> entityReferenceSerializer;
64   
65    /**
66    * The component used to resolve an entity reference relative to another entity reference.
67    */
68    @Inject
69    @Named("explicit")
70    private EntityReferenceResolver<EntityReference> explicitReferenceEntityReferenceResolver;
71   
72    /**
73    * The component used to serialize link references.
74    * <p>
75    * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
76    * replaced with a generic one to avoid confusion.
77    */
78    @Inject
79    @Named("xhtmlmarker")
80    private ResourceReferenceSerializer linkReferenceSerializer;
81   
82    /**
83    * The component used to parser link references.
84    * <p>
85    * Note: The link reference syntax is independent of the syntax of the edited document. The current hint should be
86    * replaced with a generic one to avoid confusion.
87    */
88    @Inject
89    @Named("xhtmlmarker")
90    private ResourceReferenceParser linkReferenceParser;
91   
92    /**
93    * The object used to convert between client and server entity reference.
94    */
95    @Inject
96    private EntityReferenceConverter entityReferenceConverter;
97   
98    /**
99    * Used to resolve a DocumentReference from a SpaceReference, i.e. the space's homepage.
100    */
101    @Inject
102    private DocumentReferenceResolver<EntityReference> defaultReferenceDocumentReferenceResolver;
103   
104    @Inject
105    private EntityReferenceResolver<ResourceReference> resourceReferenceResolver;
106   
 
107  0 toggle @Override
108    public EntityConfig getEntityConfig(org.xwiki.gwt.wysiwyg.client.wiki.EntityReference origin,
109    org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference destination)
110    {
111  0 String url;
112  0 String destRelativeStrRef;
113   
114  0 if (org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.EXTERNAL == destination.getEntityReference()
115    .getType()) {
116  0 url = new URIReference(destination.getEntityReference()).getURI();
117  0 destRelativeStrRef = url;
118    } else {
119  0 EntityReference originRef = entityReferenceConverter.convert(origin);
120  0 EntityReference destRef = entityReferenceConverter.convert(destination.getEntityReference());
121  0 destRef = explicitReferenceEntityReferenceResolver.resolve(destRef, destRef.getType(), originRef);
122  0 destRelativeStrRef = entityReferenceSerializer.serialize(destRef, originRef);
123  0 url = getEntityURL(destRef);
124    }
125   
126  0 EntityConfig entityConfig = new EntityConfig();
127  0 entityConfig.setUrl(url);
128  0 entityConfig.setReference(getLinkReference(destination.getType(), destination.isTyped(), destRelativeStrRef));
129  0 return entityConfig;
130    }
131   
132    /**
133    * @param entityReference an entity reference
134    * @return the URL to access the specified entity
135    */
 
136  0 toggle private String getEntityURL(EntityReference entityReference)
137    {
138  0 switch (entityReference.getType()) {
139  0 case DOCUMENT:
140  0 DocumentReference documentReference = new DocumentReference(entityReference);
141  0 return documentAccessBridge.getDocumentURL(documentReference, "view", null, null);
142  0 case ATTACHMENT:
143  0 AttachmentReference attachmentReference = new AttachmentReference(entityReference);
144  0 return documentAccessBridge.getAttachmentURL(attachmentReference, false);
145  0 default:
146  0 return null;
147    }
148    }
149   
150    /**
151    * @param clientResourceType the type of linked resource
152    * @param typed {@code true} to include the resource scheme in the link reference serialization, {@code false}
153    * otherwise
154    * @param relativeStringEntityReference a relative string entity reference
155    * @return a link reference that can be used to insert a link to the specified entity
156    */
 
157  0 toggle private String getLinkReference(
158    org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference.ResourceType clientResourceType, boolean typed,
159    String relativeStringEntityReference)
160    {
161  0 ResourceType resourceType = new ResourceType(clientResourceType.getScheme());
162  0 ResourceReference linkReference = new ResourceReference(relativeStringEntityReference, resourceType);
163  0 linkReference.setTyped(typed);
164  0 return linkReferenceSerializer.serialize(linkReference);
165    }
166   
 
167  0 toggle @Override
168    public org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference parseLinkReference(String linkReferenceAsString,
169    org.xwiki.gwt.wysiwyg.client.wiki.EntityReference baseReference)
170    {
171  0 ResourceReference linkReference = linkReferenceParser.parse(linkReferenceAsString);
172   
173  0 ResourceType linkResourceType = linkReference.getType();
174  0 if (ResourceType.SPACE.equals(linkResourceType)) {
175    // Treat space resources the same as documents in order to reuse the UI.
176  0 linkResourceType = ResourceType.DOCUMENT;
177    }
178   
179  0 org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference clientLinkReference =
180    new org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference();
181  0 clientLinkReference.setType(org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference.ResourceType
182    .forScheme(linkResourceType.getScheme()));
183  0 clientLinkReference.setTyped(linkReference.isTyped());
184  0 clientLinkReference.getParameters().putAll(linkReference.getParameters());
185  0 clientLinkReference.setEntityReference(parseEntityReferenceFromResourceReference(linkReference,
186    clientLinkReference.getType(), baseReference));
187   
188  0 return clientLinkReference;
189    }
190   
191    /**
192    * Parses a client entity reference from a link/resource reference.
193    *
194    * @param resourceReference the resource reference to parse
195    * @param clientResourceType the previously resolved client-side type of the passed resource reference
196    * @param baseReference the client entity reference that is used to resolve the parsed entity reference relative to
197    * @return an untyped client entity reference
198    */
 
199  0 toggle private org.xwiki.gwt.wysiwyg.client.wiki.EntityReference parseEntityReferenceFromResourceReference(
200    ResourceReference resourceReference,
201    org.xwiki.gwt.wysiwyg.client.wiki.ResourceReference.ResourceType clientResourceType,
202    org.xwiki.gwt.wysiwyg.client.wiki.EntityReference baseReference)
203    {
204  0 org.xwiki.gwt.wysiwyg.client.wiki.EntityReference result = null;
205   
206  0 org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType clientEntityType;
207  0 switch (clientResourceType) {
208  0 case DOCUMENT:
209  0 clientEntityType = org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.DOCUMENT;
210  0 break;
211  0 case ATTACHMENT:
212  0 clientEntityType = org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.ATTACHMENT;
213  0 break;
214  0 default:
215  0 clientEntityType = org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.EXTERNAL;
216  0 break;
217    }
218   
219  0 if (clientEntityType == org.xwiki.gwt.wysiwyg.client.wiki.EntityReference.EntityType.EXTERNAL) {
220  0 result = new URIReference(resourceReference.getReference()).getEntityReference();
221    } else {
222  0 EntityReference serverEntityReference =
223    parseServerEntityReferenceFromResourceReference(resourceReference, baseReference);
224   
225  0 result = entityReferenceConverter.convert(serverEntityReference);
226    }
227   
228  0 return result;
229    }
230   
231    /**
232    * @param resourceReference the reference to resolve
233    * @param baseReference the base reference to use when resolving
234    * @return the resolved {@link EntityReference}
235    */
 
236  0 toggle private EntityReference parseServerEntityReferenceFromResourceReference(ResourceReference resourceReference,
237    org.xwiki.gwt.wysiwyg.client.wiki.EntityReference baseReference)
238    {
239  0 EntityReference result = null;
240   
241  0 EntityReference baseServerEntityReference = entityReferenceConverter.convert(baseReference);
242   
243  0 result = resourceReferenceResolver.resolve(resourceReference, null, baseServerEntityReference);
244   
245  0 ResourceType resourceType = resourceReference.getType();
246   
247    // Depending on the resource type, additional work might be needed to resolve the reference.
248  0 if (ResourceType.SPACE.equals(resourceType)) {
249    // Make sure to return the space's WebHome since space links are mapped to documents for now.
250  0 result = defaultReferenceDocumentReferenceResolver.resolve(result);
251    }
252   
253  0 return result;
254    }
255    }