Clover Coverage Report - ARESTC 0.1.7-SNAPSHOT
Coverage timestamp: Fri Aug 27 2010 19:12:04 CEST
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
33   232   17   2.36
4   97   0.52   14
14     1.21  
1    
 
  BaseService       Line # 40 33 0% 17 2 96.1% 0.9607843
 
  (8)
 
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.core.services;
20   
21    import java.net.MalformedURLException;
22    import java.net.URL;
23    import java.util.Collection;
24    import java.util.EnumMap;
25    import java.util.HashSet;
26    import java.util.Iterator;
27    import java.util.Map;
28   
29    import org.apache.commons.collections.CollectionUtils;
30    import org.apache.commons.configuration.ConfigurationException;
31    import org.apache.commons.httpclient.Credentials;
32    import org.apache.log4j.Logger;
33   
34    // TODO: Auto-generated Javadoc
35    /**
36    * The Class BaseService.
37    *
38    * @author georgosn
39    */
 
40    public class BaseService implements RESTService {
41   
42    /** The Constant LOGGER. */
43    private static final Logger LOGGER = Logger.getLogger(BaseService.class
44    .getName());
45   
46    /** The name. */
47    private final String name;
48   
49    /** The parameters. */
50    private final EnumMap<ServiceParameterTypes, Collection<String>> serviceParameters;
51   
52    /** The connection data. */
53    private final ServiceConnectionData connectionData;
54   
55    /** The authentication data. */
56    private final ServiceAuthenticationData authenticationData;
57   
58    /** The connector data. */
59    private final ServiceConnectorData connectorData;
60   
61    /**
62    * Instantiates a new base service.
63    *
64    * @param name the name
65    * @param inParams the in params
66    * @param connectionData the connection data
67    * @param authenticationData the authentication data
68    * @param connectorData the connector data
69    */
 
70  280 toggle public BaseService(final String name,
71    final EnumMap<ServiceParameterTypes, Collection<String>> inParams,
72    final ServiceConnectionData connectionData,
73    final ServiceAuthenticationData authenticationData,
74    final ServiceConnectorData connectorData) {
75  280 this.name = name;
76   
77  280 serviceParameters = inParams;
78  280 this.connectionData = connectionData;
79  280 this.authenticationData = authenticationData;
80  280 this.connectorData = connectorData;
81    }
82   
83    /*
84    * (non-Javadoc)
85    *
86    * @see
87    * net.sf.arestc.configuration.services.RESTService#addParameter(java.lang
88    * .String, java.lang.Object)
89    */
 
90  1 toggle public Object addParameter(final String key,
91    final ServiceParameterTypes value) {
92  1 return serviceParameters.get(value).add(key);
93    }
94   
95    /**
96    * Gets the admin credentials.
97    *
98    * @return the adminCredentials
99    */
 
100  23 toggle public Credentials getAdminCredentials() {
101  23 return authenticationData.getAdminCredentials();
102    }
103   
104    /**
105    * Gets the authenticating user.
106    *
107    * @return the authenticatingUser
108    */
 
109  66 toggle public String getAuthenticatingUser() {
110  66 return authenticationData.getAuthenticatingUser();
111    }
112   
113    /*
114    * (non-Javadoc)
115    *
116    * @see net.sf.arestc.core.services.RESTService#getContentType()
117    */
 
118  2 toggle public String getContentType() {
119  2 return connectorData.getContentType();
120    }
121   
122    /*
123    * (non-Javadoc)
124    *
125    * @see net.sf.arestc.configuration.services.RESTService#getContextPath()
126    */
 
127  3 toggle public String getContextPath() {
128  3 return connectionData.getContextPath();
129    }
130   
131    /*
132    * (non-Javadoc)
133    *
134    * @see net.sf.arestc.configuration.services.RESTService#getMethod()
135    */
 
136  40 toggle public HTTPMethods getMethod() {
137  40 return connectionData.getMethod();
138    }
139   
140    /*
141    * (non-Javadoc)
142    *
143    * @see net.sf.arestc.configuration.services.RESTService#getName()
144    */
 
145  269 toggle public String getName() {
146  269 return name;
147    }
148   
149    /*
150    * (non-Javadoc)
151    *
152    * @see net.sf.arestc.configuration.services.RESTService#getParameters()
153    */
 
154  5 toggle public EnumMap<ServiceParameterTypes, Collection<String>> getParameters() {
155  5 return serviceParameters;
156    }
157   
158    /**
159    * Gets the returned type.
160    *
161    * @return the returnedType
162    */
 
163  10 toggle public ReturnedTypes getReturnedType() {
164  10 return connectorData.getReturnedType();
165    }
166   
167    /*
168    * (non-Javadoc)
169    *
170    * @see
171    * net.sf.arestc.configuration.services.RESTService#getServiceURL(java.util
172    * .Map)
173    */
 
174  30 toggle public URL getServiceURL(final Map<String, Object> params)
175    throws ConfigurationException {
176   
177  30 URL ret = null;
178  30 try {
179  30 String path = connectionData.getRealPath();
180  30 final Collection<String> urlParameters = serviceParameters
181    .get(ServiceParameterTypes.URL);
182  30 final Iterator<String> nameIter = CollectionUtils.intersection(
183    params.keySet(), urlParameters).iterator();
184  75 while (nameIter.hasNext()) {
185  45 final String name = nameIter.next();
186  45 path = path.replaceAll("\\{" + name + "\\}",
187    (String) params.get(name));
188    }
189  30 ret = new URL(path);
190    } catch (final MalformedURLException e) {
191  0 LOGGER.error("Malformed service url:", e);
192  0 throw new ConfigurationException(e);
193    }
194   
195  30 return ret;
196    }
197   
198    /* (non-Javadoc)
199    * @see net.sf.arestc.core.services.RESTService#getUnionOfParameters()
200    */
 
201  16 toggle public Collection<String> getUnionOfParameters() {
202  16 Collection<String> union = new HashSet<String>();
203  16 for (final ServiceParameterTypes type : ServiceParameterTypes.values()) {
204  48 if (null != serviceParameters.get(type)) {
205  36 union = CollectionUtils.union(union,
206    serviceParameters.get(type));
207    }
208    }
209  16 return union;
210    }
211   
212    /*
213    * (non-Javadoc)
214    *
215    * @see net.sf.arestc.configuration.services.RESTService#getURLParameters()
216    */
 
217  14 toggle public Collection<String> getURLParameters() {
218  14 return serviceParameters.get(ServiceParameterTypes.URL);
219    }
220   
221    /*
222    * (non-Javadoc)
223    *
224    * @see
225    * net.sf.arestc.configuration.services.RESTService#isAuthenticationRequired
226    * ()
227    */
 
228  31 toggle public boolean isAuthenticationRequired() {
229  31 return authenticationData.isAuthenticationRequired();
230    }
231   
232    }