| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package net.sf.arestc.configuration; |
| 21 |
|
|
| 22 |
|
import java.util.Collection; |
| 23 |
|
import java.util.Collections; |
| 24 |
|
import java.util.EnumMap; |
| 25 |
|
import java.util.HashMap; |
| 26 |
|
import java.util.HashSet; |
| 27 |
|
import java.util.List; |
| 28 |
|
import java.util.Map; |
| 29 |
|
|
| 30 |
|
import net.sf.arestc.core.services.BaseService; |
| 31 |
|
import net.sf.arestc.core.services.HTTPMethods; |
| 32 |
|
import net.sf.arestc.core.services.RESTService; |
| 33 |
|
import net.sf.arestc.core.services.ReturnedTypes; |
| 34 |
|
import net.sf.arestc.core.services.ServiceAuthenticationData; |
| 35 |
|
import net.sf.arestc.core.services.ServiceConnectionData; |
| 36 |
|
import net.sf.arestc.core.services.ServiceConnectorData; |
| 37 |
|
import net.sf.arestc.core.services.ServiceParameterTypes; |
| 38 |
|
|
| 39 |
|
import org.apache.commons.configuration.ConfigurationException; |
| 40 |
|
import org.apache.commons.configuration.XMLConfiguration; |
| 41 |
|
import org.apache.commons.httpclient.Credentials; |
| 42 |
|
import org.apache.commons.httpclient.UsernamePasswordCredentials; |
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
@author |
| 48 |
|
|
|
|
|
| 97.8% |
Uncovered Elements: 1 (46) |
Complexity: 9 |
Complexity Density: 0.26 |
|
| 49 |
|
public class ServicesConfigurationFactory { |
| 50 |
|
|
| 51 |
|
|
| 52 |
|
private static final String SERVICES_FILE = "services.xml"; |
| 53 |
|
|
| 54 |
|
|
| 55 |
|
public static final ServicesConfigurationFactory _instance = new ServicesConfigurationFactory(); |
| 56 |
|
|
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 60 |
1
|
private ServicesConfigurationFactory() {... |
| 61 |
|
|
| 62 |
|
} |
| 63 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (22) |
Complexity: 3 |
Complexity Density: 0.17 |
|
| 64 |
266
|
private RESTService configService(final Credentials credentials,... |
| 65 |
|
final XMLConfiguration config, final String server, final int port, |
| 66 |
|
final int i) { |
| 67 |
266
|
final String prefix = "services.service(" + i + ")"; |
| 68 |
266
|
final String name = config.getString(prefix + ".[@name]"); |
| 69 |
|
|
| 70 |
|
|
| 71 |
266
|
final String contextPath = config.getString(prefix + "[@context-path]"); |
| 72 |
266
|
final HTTPMethods method = HTTPMethods.valueOf(config.getString(prefix |
| 73 |
|
+ "[@method]")); |
| 74 |
266
|
final ServiceConnectionData connectionData = new ServiceConnectionData( |
| 75 |
|
server, port, contextPath, method); |
| 76 |
|
|
| 77 |
|
|
| 78 |
266
|
final Boolean authReq = config.getBoolean(prefix |
| 79 |
|
+ ".authentication[@required]"); |
| 80 |
266
|
final String user = config.getString(prefix + ".authentication"); |
| 81 |
266
|
final ServiceAuthenticationData authData = new ServiceAuthenticationData( |
| 82 |
266
|
authReq, user, "admin".equalsIgnoreCase(user) ? credentials |
| 83 |
|
: null); |
| 84 |
|
|
| 85 |
|
|
| 86 |
266
|
final ReturnedTypes retType = ReturnedTypes.valueOf(config.getString( |
| 87 |
|
prefix + "[@returns]").toUpperCase()); |
| 88 |
266
|
final ServiceConnectorData connectorData = new ServiceConnectorData( |
| 89 |
|
config.getString(prefix + "[@content-type]"), retType); |
| 90 |
|
|
| 91 |
|
|
| 92 |
266
|
final List<String> paramsNames = config.getList(prefix |
| 93 |
|
+ ".params.param[@name]"); |
| 94 |
|
|
| 95 |
266
|
final EnumMap<ServiceParameterTypes, Collection<String>> params1 = new EnumMap<ServiceParameterTypes, Collection<String>>( |
| 96 |
|
ServiceParameterTypes.class); |
| 97 |
266
|
for (final ServiceParameterTypes type : ServiceParameterTypes.values()) { |
| 98 |
798
|
params1.put(type, new HashSet<String>()); |
| 99 |
|
} |
| 100 |
1026
|
for (int j = 0; j < paramsNames.size(); j++) { |
| 101 |
760
|
final String paramPrefix = prefix + ".params.param(" + j + ")"; |
| 102 |
760
|
params1.get( |
| 103 |
|
ServiceParameterTypes.valueOf(config.getString( |
| 104 |
|
paramPrefix + "[@value-type]").toUpperCase())).add( |
| 105 |
|
paramsNames.get(j)); |
| 106 |
|
} |
| 107 |
|
|
| 108 |
266
|
return new BaseService(name, params1, connectionData, authData, |
| 109 |
|
connectorData); |
| 110 |
|
} |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
@return |
| 116 |
|
@throws |
| 117 |
|
|
| 118 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 119 |
37
|
public Map<String, RESTService> configure() throws ConfigurationException {... |
| 120 |
|
|
| 121 |
37
|
final XMLConfiguration config = new XMLConfiguration(SERVICES_FILE); |
| 122 |
|
|
| 123 |
37
|
return control(config); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
|
| 127 |
|
|
| 128 |
|
|
| 129 |
|
|
| 130 |
|
@param |
| 131 |
|
|
| 132 |
|
@return |
| 133 |
|
@throws |
| 134 |
|
|
| 135 |
|
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
| 136 |
1
|
public Map<String, RESTService> configure(final String configFileName)... |
| 137 |
|
throws ConfigurationException { |
| 138 |
1
|
XMLConfiguration config; |
| 139 |
1
|
try { |
| 140 |
1
|
config = new XMLConfiguration(configFileName); |
| 141 |
|
|
| 142 |
1
|
return control(config); |
| 143 |
|
} catch (final ConfigurationException e) { |
| 144 |
0
|
return configure(); |
| 145 |
|
} finally { |
| 146 |
1
|
config = null; |
| 147 |
|
} |
| 148 |
|
} |
| 149 |
|
|
| 150 |
|
|
| 151 |
|
|
| 152 |
|
|
| 153 |
|
@param |
| 154 |
|
|
| 155 |
|
@return |
| 156 |
|
@throws |
| 157 |
|
|
| 158 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 159 |
38
|
protected Map<String, RESTService> control(final XMLConfiguration config)... |
| 160 |
|
throws ConfigurationException { |
| 161 |
38
|
final String server = config.getString("server[@url]"); |
| 162 |
38
|
final int port = config.getInt("server[@port]"); |
| 163 |
38
|
final Credentials credentials = new UsernamePasswordCredentials( |
| 164 |
|
config.getString("admin[@username]"), |
| 165 |
|
config.getString("admin[@password]")); |
| 166 |
38
|
final Map<String, RESTService> services = new HashMap<String, RESTService>(); |
| 167 |
38
|
final List<String> names = config.getList("services.service[@name]"); |
| 168 |
304
|
for (int i = 0; i < names.size(); i++) { |
| 169 |
|
|
| 170 |
266
|
final RESTService service = configService(credentials, config, |
| 171 |
|
server, port, i); |
| 172 |
266
|
services.put(service.getName(), service); |
| 173 |
|
|
| 174 |
|
} |
| 175 |
38
|
return Collections.unmodifiableMap(services); |
| 176 |
|
} |
| 177 |
|
} |