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

File OfficeServerScriptService.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart1.png
82% of files have more coverage

Code metrics

10
27
7
1
190
90
14
0.52
3.86
7
2

Classes

Class Line # Actions
OfficeServerScriptService 45 27 0% 14 42
0.0454545474.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.officeimporter.server.script;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24    import javax.inject.Singleton;
25   
26    import org.slf4j.Logger;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.model.ModelContext;
31    import org.xwiki.officeimporter.server.OfficeServer;
32    import org.xwiki.officeimporter.server.OfficeServerConfiguration;
33    import org.xwiki.officeimporter.server.OfficeServerException;
34    import org.xwiki.script.service.ScriptService;
35   
36    /**
37    * Exposes the office manager APIs to server-side scripts.
38    *
39    * @version $Id: 5ab7da44940059bb68022a46529bd548146fd03c $
40    * @since 4.1M1
41    */
42    @Component
43    @Named("officemanager")
44    @Singleton
 
45    public class OfficeServerScriptService implements ScriptService
46    {
47    /**
48    * The key used to place any error messages while trying to control the office server instance.
49    */
50    public static final String OFFICE_MANAGER_ERROR = "OFFICE_MANAGER_ERROR";
51   
52    /**
53    * Error message used to indicate that office server administration is restricted for main wiki.
54    */
55    private static final String ERROR_FORBIDDEN = "Office server administration is forbidden for sub-wikis.";
56   
57    /**
58    * Error message used to indicate that the current user does not have enough rights to perform the requested action.
59    */
60    private static final String ERROR_PRIVILEGES = "Inadequate privileges.";
61   
62    /**
63    * The object used to log messages.
64    */
65    @Inject
66    private Logger logger;
67   
68    /**
69    * Provides access to the request context.
70    */
71    @Inject
72    private Execution execution;
73   
74    /**
75    * The component used to access the current wiki.
76    */
77    @Inject
78    private ModelContext modelContext;
79   
80    /**
81    * The office server.
82    */
83    @Inject
84    private OfficeServer officeServer;
85   
86    /**
87    * The {@link DocumentAccessBridge} component.
88    */
89    @Inject
90    private DocumentAccessBridge docBridge;
91   
92    /**
93    * The office server configuration.
94    */
95    @Inject
96    private OfficeServerConfiguration officeServerConfig;
97   
98    /**
99    * Tries to start the office server process.
100    *
101    * @return true if the operation succeeds, false otherwise
102    */
 
103  0 toggle public boolean startServer()
104    {
105  0 if (!isMainXWiki()) {
106  0 setErrorMessage(ERROR_FORBIDDEN);
107  0 } else if (!this.docBridge.hasProgrammingRights()) {
108  0 setErrorMessage(ERROR_PRIVILEGES);
109    } else {
110  0 try {
111  0 this.officeServer.start();
112  0 return true;
113    } catch (OfficeServerException ex) {
114  0 logger.error(ex.getMessage(), ex);
115  0 setErrorMessage(ex.getMessage());
116    }
117    }
118  0 return false;
119    }
120   
121    /**
122    * Tries to stop the office server process.
123    *
124    * @return true if the operation succeeds, false otherwise
125    */
 
126  0 toggle public boolean stopServer()
127    {
128  0 if (!isMainXWiki()) {
129  0 setErrorMessage(ERROR_FORBIDDEN);
130  0 } else if (!this.docBridge.hasProgrammingRights()) {
131  0 setErrorMessage(ERROR_PRIVILEGES);
132    } else {
133  0 try {
134  0 this.officeServer.stop();
135  0 return true;
136    } catch (OfficeServerException ex) {
137  0 logger.error(ex.getMessage(), ex);
138  0 setErrorMessage(ex.getMessage());
139    }
140    }
141  0 return false;
142    }
143   
144    /**
145    * @return current status of the office server process as a string
146    */
 
147  437 toggle public String getServerState()
148    {
149  437 return this.officeServer.getState().toString();
150    }
151   
152    /**
153    * @return the office server configuration
154    */
 
155  0 toggle public OfficeServerConfiguration getConfig()
156    {
157  0 return officeServerConfig;
158    }
159   
160    /**
161    * @return any error messages encountered
162    */
 
163  0 toggle public String getLastErrorMessage()
164    {
165  0 Object error = this.execution.getContext().getProperty(OFFICE_MANAGER_ERROR);
166  0 return (error != null) ? (String) error : null;
167    }
168   
169    /**
170    * Sets an error message inside the execution context.
171    *
172    * @param message error message
173    */
 
174  0 toggle private void setErrorMessage(String message)
175    {
176  0 this.execution.getContext().setProperty(OFFICE_MANAGER_ERROR, message);
177    }
178   
179    /**
180    * Utility method for checking if current context document is from main wiki.
181    *
182    * @return true if the current context document is from main wiki
183    */
 
184  0 toggle private boolean isMainXWiki()
185    {
186  0 String currentWiki = this.modelContext.getCurrentEntityReference().getName();
187    // TODO: Remove the hard-coded main wiki name when a fix becomes available.
188  0 return (currentWiki != null) && currentWiki.equals("xwiki");
189    }
190    }