Clover Coverage Report - ARESTC 0.1.1-SNAPSHOT
Coverage timestamp: Sat Jul 24 2010 18:26:04 CEST
../../../../../img/srcFileCovDistChart9.png 2% of files have more coverage
40   360   28   1.54
2   136   0.7   13
26     1.08  
2    
This report was generated with an evaluation server license. Purchase Clover or configure your license.
 
  BaseService       Line # 42 37 0% 26 8 87.3% 0.8730159
  BaseService.ParamsPredicate       Line # 47 3 0% 2 0 100% 1.0
 
  (24)
 
1    /*
2    *
3    * (C)opyright 2010, Nikolaos Georgosopoulos
4    *
5    * This file is part of ARESTC.
6    *
7    * ARESTC is free software: you can redistribute it and/or modify it under the
8    * terms of the Lesser General Public License as published by the Free Software
9    * Foundation, either version 3 of the License, or (at your option) any later
10    * version.
11    *
12    * ARESTC is distributed in the hope that it will be useful, but WITHOUT ANY
13    * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14    * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15    *
16    * You should have received a copy of the Lesser General Public License along
17    * with ARESTC. If not, see <http://www.gnu.org/licenses/>.
18    */
19    package net.sf.arestc.configuration.services;
20   
21    import java.net.MalformedURLException;
22    import java.net.URL;
23    import java.text.MessageFormat;
24    import java.util.HashMap;
25    import java.util.Iterator;
26    import java.util.Map;
27    import java.util.Map.Entry;
28   
29    import net.sf.arestc.configuration.HTTPMethods;
30   
31    import org.apache.commons.collections.IteratorUtils;
32    import org.apache.commons.collections.Predicate;
33    import org.apache.commons.configuration.ConfigurationException;
34    import org.apache.commons.httpclient.UsernamePasswordCredentials;
35    import org.apache.log4j.Logger;
36   
37    /**
38    * The Class BaseService.
39    *
40    * @author georgosn
41    */
 
42    public class BaseService implements RESTService {
43   
44    /**
45    * The Class ParamsPredicate.
46    */
 
47    private class ParamsPredicate implements Predicate {
48   
49    /** The type. */
50    private final String type;
51   
52    /**
53    * Instantiates a new params predicate.
54    *
55    * @param execType
56    * the exec type
57    */
 
58  13 toggle public ParamsPredicate(final ServiceParameterTypes execType) {
59  13 type = execType.name().toLowerCase();
60    }
61   
62    /*
63    * (non-Javadoc)
64    *
65    * @see
66    * org.apache.commons.collections.Predicate#evaluate(java.lang.Object)
67    */
 
68  12 toggle public boolean evaluate(final Object arg0) {
69  12 final Entry<String, Object> entry = (Entry<String, Object>) arg0;
70  12 return type.equals(parameters.get(entry.getKey()));
71    }
72   
73    };
74   
75    /** The Constant LOGGER. */
76    private static final Logger LOGGER = Logger.getLogger(BaseService.class
77    .getName());
78   
79    /** The name. */
80    private String name;
81   
82    /** The context path. */
83    private String contextPath;
84   
85    /** The method. */
86    private HTTPMethods method;
87   
88    /** The authentication required. */
89    private boolean authenticationRequired;
90   
91    /** The parameters. */
92    private Map<String, Object> parameters;
93   
94    /** The authenticating user. */
95    private String authenticatingUser;
96   
97    /** The server. */
98    private final String server;
99   
100    /** The port. */
101    private final int port;
102   
103    private UsernamePasswordCredentials adminCredentials;
104   
105    private String contentType;
106   
107    private ReturnedTypes returnedType;
108   
109    /**
110    * Instantiates a new base service.
111    *
112    * @param server
113    * the server
114    * @param port
115    * the port
116    */
 
117  110 toggle public BaseService(final String server, final int port) {
118  110 parameters = new HashMap<String, Object>();
119  110 this.server = server;
120  110 this.port = port;
121    }
122   
123    /*
124    * (non-Javadoc)
125    *
126    * @see
127    * net.sf.arestc.configuration.services.RESTService#addParameter(java.lang
128    * .String, java.lang.Object)
129    */
 
130  288 toggle public Object addParameter(final String key, final Object value) {
131  288 return parameters.put(key, value);
132    }
133   
134    /**
135    * @return the adminCredentials
136    */
 
137  6 toggle public UsernamePasswordCredentials getAdminCredentials() {
138  6 return adminCredentials;
139    }
140   
141    /**
142    * Gets the authenticating user.
143    *
144    * @return the authenticatingUser
145    */
 
146  17 toggle public String getAuthenticatingUser() {
147  17 return authenticatingUser;
148    }
149   
150    /**
151    * Gets the body parameters.
152    *
153    * @return the body parameters
154    */
 
155  0 toggle public Iterator<Entry<String, Object>> getBobyParameters() {
156  0 return IteratorUtils.filteredIterator(parameters.entrySet().iterator(),
157    new ParamsPredicate(ServiceParameterTypes.BODY));
158    }
159   
 
160  0 toggle public String getContentType() {
161  0 return contentType;
162    }
163   
164    /*
165    * (non-Javadoc)
166    *
167    * @see net.sf.arestc.configuration.services.RESTService#getContextPath()
168    */
 
169  3 toggle public String getContextPath() {
170  3 return contextPath;
171    }
172   
173    /*
174    * (non-Javadoc)
175    *
176    * @see net.sf.arestc.configuration.services.RESTService#getMethod()
177    */
 
178  3 toggle public HTTPMethods getMethod() {
179  3 return method;
180    }
181   
182    /*
183    * (non-Javadoc)
184    *
185    * @see net.sf.arestc.configuration.services.RESTService#getName()
186    */
 
187  103 toggle public String getName() {
188  103 return name;
189    }
190   
191    /*
192    * (non-Javadoc)
193    *
194    * @see net.sf.arestc.configuration.services.RESTService#getParameters()
195    */
 
196  11 toggle public Map<String, Object> getParameters() {
197  11 return parameters;
198    }
199   
200    /*
201    * (non-Javadoc)
202    *
203    * @see
204    * net.sf.arestc.configuration.services.RESTService#getQueryParameters()
205    */
 
206  1 toggle public Iterator<Entry<String, Object>> getQueryParameters() {
207   
208  1 return IteratorUtils.filteredIterator(parameters.entrySet().iterator(),
209    new ParamsPredicate(ServiceParameterTypes.QUERY));
210    }
211   
212    /**
213    * @return the returnedType
214    */
 
215  0 toggle public ReturnedTypes getReturnedType() {
216  0 return returnedType;
217    }
218   
219    /*
220    * (non-Javadoc)
221    *
222    * @see
223    * net.sf.arestc.configuration.services.RESTService#getServiceURL(java.util
224    * .Map)
225    */
 
226  10 toggle public URL getServiceURL(final Map<String, Object> params)
227    throws ConfigurationException {
228   
229  10 URL ret = null;
230  10 try {
231  10 String path = MessageFormat.format("{0}:{1,number,####}/{2}",
232    new Object[] { server, port, contextPath });
233   
234  10 final Iterator<Entry<String, Object>> entriesIter = IteratorUtils
235    .filteredIterator(params.entrySet().iterator(),
236    new ParamsPredicate(ServiceParameterTypes.URL));
237  10 ;
238  19 while (entriesIter.hasNext()) {
239  9 final Entry<String, Object> entry = entriesIter.next();
240  9 path = path.replaceAll("\\{" + entry.getKey() + "\\}", entry
241    .getValue().toString());
242   
243    }
244  10 ret = new URL(path);
245    } catch (final MalformedURLException e) {
246  1 LOGGER.error("Malformed service url:", e);
247  1 throw new ConfigurationException(e);
248    }
249   
250  9 return ret;
251    }
252   
253    /*
254    * (non-Javadoc)
255    *
256    * @see net.sf.arestc.configuration.services.RESTService#getURLParameters()
257    */
 
258  2 toggle public Iterator<Entry<String, Object>> getURLParameters() {
259  2 return IteratorUtils.filteredIterator(parameters.entrySet().iterator(),
260    new ParamsPredicate(ServiceParameterTypes.URL));
261    }
262   
263    /*
264    * (non-Javadoc)
265    *
266    * @see
267    * net.sf.arestc.configuration.services.RESTService#isAuthenticationRequired
268    * ()
269    */
 
270  10 toggle public boolean isAuthenticationRequired() {
271  10 return authenticationRequired;
272    }
273   
274    /**
275    * @param adminCredentials
276    * the adminCredentials to set
277    */
 
278  81 toggle public void setAdminCredentials(
279    final UsernamePasswordCredentials adminCredentials) {
280  81 this.adminCredentials = adminCredentials;
281    }
282   
283    /**
284    * Sets the authenticating user.
285    *
286    * @param authenticatingUser
287    * the authenticatingUser to set
288    */
 
289  104 toggle public void setAuthenticatingUser(final String authenticatingUser) {
290  104 this.authenticatingUser = authenticatingUser;
291    }
292   
293    /*
294    * (non-Javadoc)
295    *
296    * @see
297    * net.sf.arestc.configuration.services.RESTService#setAuthenticationRequired
298    * (boolean)
299    */
 
300  105 toggle public void setAuthenticationRequired(final boolean authenticationRequired) {
301  105 this.authenticationRequired = authenticationRequired;
302    }
303   
 
304  100 toggle public void setContentType(final String contentType) {
305  100 this.contentType = contentType;
306    }
307   
308    /*
309    * (non-Javadoc)
310    *
311    * @see
312    * net.sf.arestc.configuration.services.RESTService#setContextPath(java.
313    * lang.String)
314    */
 
315  106 toggle public void setContextPath(final String contextPath) {
316  106 this.contextPath = contextPath;
317    }
318   
319    /*
320    * (non-Javadoc)
321    *
322    * @see
323    * net.sf.arestc.configuration.services.RESTService#setMethod(net.sf.arestc
324    * .configuration.HTTPMethods)
325    */
 
326  100 toggle public void setMethod(final HTTPMethods method) {
327  100 this.method = method;
328    }
329   
330    /*
331    * (non-Javadoc)
332    *
333    * @see
334    * net.sf.arestc.configuration.services.RESTService#setName(java.lang.String
335    * )
336    */
 
337  100 toggle public void setName(final String name) {
338  100 this.name = name;
339    }
340   
341    /*
342    * (non-Javadoc)
343    *
344    * @see
345    * net.sf.arestc.configuration.services.RESTService#setParameters(java.util
346    * .Map)
347    */
 
348  0 toggle public void setParameters(final Map<String, Object> parameters) {
349  0 this.parameters = parameters;
350    }
351   
352    /**
353    * @param returnedType
354    * the returnedType to set
355    */
 
356  100 toggle public void setReturnedType(final ReturnedTypes returnedType) {
357  100 this.returnedType = returnedType;
358    }
359   
360    }