1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rest.script

File RESTScriptService.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

2
11
4
1
126
52
8
0.73
2.75
4
2

Classes

Class Line # Actions
RESTScriptService 46 11 0% 8 8
0.529411852.9%
 

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.rest.script;
21   
22    import java.net.URL;
23   
24    import javax.inject.Inject;
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.reference.EntityReference;
32    import org.xwiki.rest.XWikiRestException;
33    import org.xwiki.rest.url.RestURLGenerator;
34    import org.xwiki.script.service.ScriptService;
35    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
36   
37    /**
38    * Generate REST URLs for different types of resources.
39    *
40    * @version $Id: 466f46f19565ce6314fb59751750a32417198d4e $
41    * @since 7.2M1
42    */
43    @Component
44    @Named("rest")
45    @Singleton
 
46    public class RESTScriptService implements ScriptService
47    {
48    /**
49    * The key under which the last encountered error is stored in the current execution context.
50    */
51    private static final String ERROR_KEY = "scriptservice.rest.error";
52   
53    @Inject
54    private RestURLGenerator restURLGenerator;
55   
56    @Inject
57    private Execution execution;
58   
59    @Inject
60    private WikiDescriptorManager wikis;
61   
62    /**
63    * Create an REST URL representing the passed entity.
64    * <p>
65    * The URL path will be returned unless the reference wiki is different from the current wiki.
66    *
67    * @param reference an entity reference
68    * @return the REST URL corresponding to the referenced entity, or null if an error occurs
69    * @since 7.2M3
70    */
 
71  1083 toggle public String url(EntityReference reference)
72    {
73  1083 return url(reference, false);
74    }
75   
76    /**
77    * Create an REST URL representing the passed entity.
78    * <p>
79    * If <code>external</code> is false the URL path will be returned unless the reference wiki is different from the
80    * current wiki.
81    *
82    * @param reference an entity reference
83    * @param external indicate if the returned URL should be absolute (to be used by an external service usually)
84    * @return the REST URL corresponding to the referenced entity, or null if an error occurs
85    * @since 7.2M3
86    */
 
87  1083 toggle public String url(EntityReference reference, boolean external)
88    {
89  1083 EntityReference wikiReference = reference.extractReference(EntityType.WIKI);
90   
91  1083 try {
92  1083 URL externalURL = this.restURLGenerator.getURL(reference);
93   
94    // If forced or the reference wiki is different from the current wiki return external form URL
95  1083 if (external || (wikiReference != null && !wikiReference.getName().equals(this.wikis.getCurrentWikiId()))) {
96  0 return externalURL.toExternalForm();
97    }
98   
99  1083 return externalURL.getPath();
100    } catch (XWikiRestException e) {
101  0 setLastError(e);
102  0 return null;
103    }
104    }
105   
106    /**
107    * Get the error generated while performing the previously called action.
108    *
109    * @return an eventual exception or {@code null} if no exception was thrown
110    */
 
111  0 toggle public XWikiRestException getLastError()
112    {
113  0 return (XWikiRestException) this.execution.getContext().getProperty(ERROR_KEY);
114    }
115   
116    /**
117    * Store a caught exception in the context, so that it can be later retrieved using {@link #getLastError()}.
118    *
119    * @param e the exception to store, can be {@code null} to clear the previously stored exception
120    * @see #getLastError()
121    */
 
122  0 toggle private void setLastError(XWikiRestException e)
123    {
124  0 this.execution.getContext().setProperty(ERROR_KEY, e);
125    }
126    }