| 1 | /* WRS 2.0 |
|---|
| 2 | * Copyright (C) 2009 Anton Patrushev |
|---|
| 3 | * |
|---|
| 4 | * This program is free software; you can redistribute it and/or modify |
|---|
| 5 | * it under the terms of the GNU General Public License as published by |
|---|
| 6 | * the Free Software Foundation; either version 3 of the License, or |
|---|
| 7 | * (at your option) any later version. |
|---|
| 8 | * |
|---|
| 9 | * This program is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * GNU General Public License for more details. |
|---|
| 13 | * |
|---|
| 14 | * You should have received a copy of the GNU General Public License |
|---|
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|---|
| 16 | */ |
|---|
| 17 | |
|---|
| 18 | import handler.HandlerFactory; |
|---|
| 19 | import handler.ResourceHandler; |
|---|
| 20 | |
|---|
| 21 | import java.io.IOException; |
|---|
| 22 | import java.util.ArrayList; |
|---|
| 23 | import java.util.Enumeration; |
|---|
| 24 | import java.util.HashMap; |
|---|
| 25 | import java.util.Hashtable; |
|---|
| 26 | import java.util.Iterator; |
|---|
| 27 | import java.util.ListIterator; |
|---|
| 28 | |
|---|
| 29 | import javax.xml.bind.JAXBException; |
|---|
| 30 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 31 | |
|---|
| 32 | import model.Profile; |
|---|
| 33 | import model.Resource; |
|---|
| 34 | import model.Service; |
|---|
| 35 | import model.Template; |
|---|
| 36 | |
|---|
| 37 | import org.restlet.Component; |
|---|
| 38 | import org.restlet.Restlet; |
|---|
| 39 | import org.xml.sax.SAXException; |
|---|
| 40 | |
|---|
| 41 | import util.IOHelper; |
|---|
| 42 | import util.Log; |
|---|
| 43 | import util.Parameter; |
|---|
| 44 | import util.ServiceRequest; |
|---|
| 45 | import util.conf.Configuration; |
|---|
| 46 | import util.conf.Configuration.Includes.Include; |
|---|
| 47 | import util.format.FillerFactory; |
|---|
| 48 | import util.format.FormatParser; |
|---|
| 49 | import util.format.ParserFactory; |
|---|
| 50 | import util.format.TemplateFiller; |
|---|
| 51 | import util.profile.Profiles; |
|---|
| 52 | import util.profile.Profiles.Profile.Resources.Rref; |
|---|
| 53 | import util.resource.Resources; |
|---|
| 54 | import util.Sref; |
|---|
| 55 | |
|---|
| 56 | import org.restlet.data.CharacterSet; |
|---|
| 57 | import org.restlet.data.Form; |
|---|
| 58 | import org.restlet.data.MediaType; |
|---|
| 59 | import org.restlet.data.Method; |
|---|
| 60 | import org.restlet.data.Protocol; |
|---|
| 61 | import org.restlet.data.Request; |
|---|
| 62 | import org.restlet.data.Response; |
|---|
| 63 | import org.restlet.data.Status; |
|---|
| 64 | |
|---|
| 65 | import exception.InvalidProfileException; |
|---|
| 66 | import exception.InvalidServiceException; |
|---|
| 67 | import exception.InvalidTemplateException; |
|---|
| 68 | import exception.InvalidVersionException; |
|---|
| 69 | |
|---|
| 70 | public class WRS |
|---|
| 71 | { |
|---|
| 72 | private Log log; |
|---|
| 73 | private Configuration conf; |
|---|
| 74 | |
|---|
| 75 | private Hashtable<String, Service> services; |
|---|
| 76 | private Hashtable<String, Profile> profiles; |
|---|
| 77 | private Hashtable<String, Resource> resources; |
|---|
| 78 | |
|---|
| 79 | private String pUrl; |
|---|
| 80 | |
|---|
| 81 | @SuppressWarnings("unused") |
|---|
| 82 | private static final String DEFAULT_VERSION = "1.1.0"; |
|---|
| 83 | |
|---|
| 84 | private enum Includes |
|---|
| 85 | { |
|---|
| 86 | TEMPLATES, SERVICES, PROFILES, RESOURCES; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | private enum ParameterSources |
|---|
| 90 | { |
|---|
| 91 | URL, PROFILE, RESOURCE; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | private enum MIMETypes |
|---|
| 95 | { |
|---|
| 96 | TEXT |
|---|
| 97 | { |
|---|
| 98 | MediaType getType() |
|---|
| 99 | { |
|---|
| 100 | return MediaType.TEXT_ALL; |
|---|
| 101 | } |
|---|
| 102 | }, |
|---|
| 103 | JSON |
|---|
| 104 | { |
|---|
| 105 | MediaType getType() |
|---|
| 106 | { |
|---|
| 107 | return MediaType.TEXT_JAVASCRIPT; |
|---|
| 108 | } |
|---|
| 109 | }, |
|---|
| 110 | HTML |
|---|
| 111 | { |
|---|
| 112 | MediaType getType() |
|---|
| 113 | { |
|---|
| 114 | return MediaType.TEXT_HTML; |
|---|
| 115 | } |
|---|
| 116 | }, |
|---|
| 117 | XML |
|---|
| 118 | { |
|---|
| 119 | MediaType getType() |
|---|
| 120 | { |
|---|
| 121 | return MediaType.TEXT_XML; |
|---|
| 122 | } |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | abstract MediaType getType(); |
|---|
| 126 | |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | public WRS(String[] args) throws Exception |
|---|
| 130 | { |
|---|
| 131 | if (!checkParams(args)) |
|---|
| 132 | { |
|---|
| 133 | System.out.println("Wrong parameters"); |
|---|
| 134 | System.out.println("Usage: java WRS configuration.xml"); |
|---|
| 135 | System.exit(0); |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | this.resources = new Hashtable<String, Resource>(); |
|---|
| 139 | this.services = new Hashtable<String, Service>(); |
|---|
| 140 | this.profiles = new Hashtable<String, Profile>(); |
|---|
| 141 | |
|---|
| 142 | this.conf = readConfig(args[0]); |
|---|
| 143 | |
|---|
| 144 | if (this.conf == null) |
|---|
| 145 | { |
|---|
| 146 | System.out.println("Failed to configure service."); |
|---|
| 147 | System.exit(0); |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | log = new Log(this.conf.getLog().getUrl(), this.conf.getLog() |
|---|
| 151 | .getLevel().byteValue()); |
|---|
| 152 | log.logger.info("Hello world!"); |
|---|
| 153 | |
|---|
| 154 | Component component = new Component(); |
|---|
| 155 | |
|---|
| 156 | log.logger.finest("Starting service at port " + this.conf.getPort()); |
|---|
| 157 | component.getServers().add(Protocol.HTTP, this.conf.getPort()); |
|---|
| 158 | |
|---|
| 159 | Restlet restlet = new Restlet() |
|---|
| 160 | { |
|---|
| 161 | |
|---|
| 162 | synchronized public void handle(Request request, Response response) |
|---|
| 163 | { |
|---|
| 164 | log.logger.info("Handle request"); |
|---|
| 165 | ServiceRequest serviceRequest = null; |
|---|
| 166 | String result = ""; |
|---|
| 167 | |
|---|
| 168 | try |
|---|
| 169 | { |
|---|
| 170 | serviceRequest = parseURL(request); |
|---|
| 171 | |
|---|
| 172 | if (serviceRequest == null) |
|---|
| 173 | throw new ArrayIndexOutOfBoundsException(); |
|---|
| 174 | |
|---|
| 175 | Resource resource = serviceRequest.getResource(); |
|---|
| 176 | |
|---|
| 177 | if (resource == null) |
|---|
| 178 | { |
|---|
| 179 | // TODO process this case carefully |
|---|
| 180 | } |
|---|
| 181 | else |
|---|
| 182 | { |
|---|
| 183 | try |
|---|
| 184 | { |
|---|
| 185 | ResourceHandler handler = HandlerFactory.Handler |
|---|
| 186 | .valueOf(resource.getType().toUpperCase()) |
|---|
| 187 | .getInstance(); |
|---|
| 188 | handler.setResource(resource); |
|---|
| 189 | |
|---|
| 190 | ArrayList<HashMap<String, Parameter>> output = handler |
|---|
| 191 | .handle(serviceRequest, log); |
|---|
| 192 | |
|---|
| 193 | String templateName = serviceRequest.getTemplate() |
|---|
| 194 | .getName(); |
|---|
| 195 | |
|---|
| 196 | try |
|---|
| 197 | { |
|---|
| 198 | TemplateFiller filler = FillerFactory.Fillers |
|---|
| 199 | .valueOf(templateName.toUpperCase()) |
|---|
| 200 | .getInstance(); |
|---|
| 201 | result = filler.fillOutputTemplate( |
|---|
| 202 | serviceRequest, output, handler, log); |
|---|
| 203 | } |
|---|
| 204 | // TODO process the error more carefully |
|---|
| 205 | catch (InstantiationException e) |
|---|
| 206 | { |
|---|
| 207 | log.logger.warning("Wrong output format " |
|---|
| 208 | + templateName); |
|---|
| 209 | } |
|---|
| 210 | catch (IllegalAccessException e) |
|---|
| 211 | { |
|---|
| 212 | log.logger.warning("Wrong output format " |
|---|
| 213 | + templateName); |
|---|
| 214 | } |
|---|
| 215 | catch (NullPointerException e) |
|---|
| 216 | { |
|---|
| 217 | log.logger.warning("Output format " |
|---|
| 218 | + templateName |
|---|
| 219 | + " is not supported yet."); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | } |
|---|
| 223 | // TODO process the error more carefully |
|---|
| 224 | catch (InstantiationException e) |
|---|
| 225 | { |
|---|
| 226 | log.logger.warning("Wrong resource " |
|---|
| 227 | + resource.getName()); |
|---|
| 228 | } |
|---|
| 229 | catch (IllegalAccessException e) |
|---|
| 230 | { |
|---|
| 231 | log.logger.warning("Wrong resource " |
|---|
| 232 | + resource.getName()); |
|---|
| 233 | } |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | String templateType = serviceRequest.getTemplate() |
|---|
| 237 | .getFormat(); |
|---|
| 238 | response.setEntity(result, MIMETypes.valueOf( |
|---|
| 239 | templateType.toUpperCase()).getType()); |
|---|
| 240 | // Magical thing you need to do to set a proper encoding |
|---|
| 241 | response.getEntity().setCharacterSet( |
|---|
| 242 | CharacterSet.valueOf("UTF-8")); |
|---|
| 243 | } |
|---|
| 244 | catch (InvalidVersionException e) |
|---|
| 245 | { |
|---|
| 246 | log.logger.warning("Invalid version '" + e.getVersion() |
|---|
| 247 | + "'. Reqest can't be processed."); |
|---|
| 248 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 249 | } |
|---|
| 250 | catch (InvalidProfileException e) |
|---|
| 251 | { |
|---|
| 252 | log.logger.warning("Invalid profile '" + e.getProfileName() |
|---|
| 253 | + "'. Reqest can't be processed."); |
|---|
| 254 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 255 | } |
|---|
| 256 | catch (InvalidServiceException e) |
|---|
| 257 | { |
|---|
| 258 | log.logger.warning("Invalid service '" + e.getServiceName() |
|---|
| 259 | + "'. Reqest can't be processed."); |
|---|
| 260 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 261 | } |
|---|
| 262 | catch (InvalidTemplateException e) |
|---|
| 263 | { |
|---|
| 264 | log.logger.warning("Invalid format '" + e.getTemplateName() |
|---|
| 265 | + "'. Reqest can't be processed."); |
|---|
| 266 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 267 | } |
|---|
| 268 | catch (ArrayIndexOutOfBoundsException e) |
|---|
| 269 | { |
|---|
| 270 | log.logger |
|---|
| 271 | .warning("Invalid URL. Reqest can't be processed."); |
|---|
| 272 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 273 | } |
|---|
| 274 | catch (NullPointerException e) |
|---|
| 275 | { |
|---|
| 276 | log.logger |
|---|
| 277 | .warning("Invalid URL. Reqest can't be processed."); |
|---|
| 278 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 279 | } |
|---|
| 280 | catch (IOException e) |
|---|
| 281 | { |
|---|
| 282 | log.logger |
|---|
| 283 | .warning("Invalid profile. Reqest can't be processed."); |
|---|
| 284 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 285 | } |
|---|
| 286 | catch (ClassNotFoundException e) |
|---|
| 287 | { |
|---|
| 288 | log.logger |
|---|
| 289 | .warning("Invalid profile. Reqest can't be processed."); |
|---|
| 290 | response.setStatus(Status.CLIENT_ERROR_BAD_REQUEST); |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | } |
|---|
| 294 | }; |
|---|
| 295 | component.getDefaultHost().attach("/{version}/{profile}/{service}", |
|---|
| 296 | restlet); |
|---|
| 297 | component.start(); |
|---|
| 298 | |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | /** |
|---|
| 302 | * Parses the url. |
|---|
| 303 | * |
|---|
| 304 | * @param req |
|---|
| 305 | * the request to the restlet |
|---|
| 306 | * |
|---|
| 307 | * @return the service request |
|---|
| 308 | * |
|---|
| 309 | * @throws ArrayIndexOutOfBoundsException |
|---|
| 310 | * the array index out of bounds exception |
|---|
| 311 | * @throws InvalidVersionException |
|---|
| 312 | * the invalid version exception |
|---|
| 313 | * @throws InvalidProfileException |
|---|
| 314 | * the invalid profile exception |
|---|
| 315 | * @throws InvalidServiceException |
|---|
| 316 | * the invalid service exception |
|---|
| 317 | * @throws InvalidTemplateException |
|---|
| 318 | * the invalid template exception |
|---|
| 319 | * @throws ClassNotFoundException |
|---|
| 320 | * @throws IOException |
|---|
| 321 | */ |
|---|
| 322 | @SuppressWarnings("unchecked") |
|---|
| 323 | public ServiceRequest parseURL(Request req) |
|---|
| 324 | throws ArrayIndexOutOfBoundsException, InvalidVersionException, |
|---|
| 325 | InvalidProfileException, InvalidServiceException, |
|---|
| 326 | InvalidTemplateException, IOException, ClassNotFoundException |
|---|
| 327 | { |
|---|
| 328 | // http://<servicedomain>:<port>/<version>/<profile>/<service>.<format>?<input>={<data>} |
|---|
| 329 | |
|---|
| 330 | String cVersion = (String) req.getAttributes().get("version"); |
|---|
| 331 | |
|---|
| 332 | if (!this.conf.getVersion().equals(cVersion)) |
|---|
| 333 | { |
|---|
| 334 | throw new InvalidVersionException(cVersion); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | String pName = (String) req.getAttributes().get("profile"); |
|---|
| 338 | readProfiles(pUrl); |
|---|
| 339 | Profile cProfile = this.profiles.get(pName); |
|---|
| 340 | |
|---|
| 341 | if (cProfile == null) |
|---|
| 342 | { |
|---|
| 343 | throw new InvalidProfileException(pName); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | String sName = ((String) req.getAttributes().get("service")) |
|---|
| 347 | .split("\\.")[0]; |
|---|
| 348 | ArrayList<Service> csList = (ArrayList) cProfile.getServices().get( |
|---|
| 349 | sName); |
|---|
| 350 | // TODO implement a rule |
|---|
| 351 | Service cService = (Service) csList.get(0); |
|---|
| 352 | |
|---|
| 353 | if (cService == null) |
|---|
| 354 | { |
|---|
| 355 | throw new InvalidServiceException(sName); |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | String tName = ((String) req.getAttributes().get("service")) |
|---|
| 359 | .split("\\.")[1]; |
|---|
| 360 | |
|---|
| 361 | Template cTemplate = cService.getTemplates().get(tName); |
|---|
| 362 | |
|---|
| 363 | if (cTemplate == null) |
|---|
| 364 | { |
|---|
| 365 | throw new InvalidTemplateException(tName); |
|---|
| 366 | } |
|---|
| 367 | |
|---|
| 368 | ServiceRequest request = new ServiceRequest(cProfile, cService, |
|---|
| 369 | cTemplate); |
|---|
| 370 | |
|---|
| 371 | findResource(request); |
|---|
| 372 | |
|---|
| 373 | // Pass parameters from resource to service request |
|---|
| 374 | addParameters(request.getParameters(), request.getResource() |
|---|
| 375 | .getParameters(), ParameterSources.RESOURCE.toString()); |
|---|
| 376 | |
|---|
| 377 | // Pass parameters from profile to service request |
|---|
| 378 | addParameters(request.getParameters(), cProfile.getParameters(), |
|---|
| 379 | ParameterSources.PROFILE.toString()); |
|---|
| 380 | |
|---|
| 381 | Form form = new Form(); |
|---|
| 382 | // GET Request |
|---|
| 383 | if (req.getMethod() == Method.GET) |
|---|
| 384 | { |
|---|
| 385 | String paramString; |
|---|
| 386 | try |
|---|
| 387 | { |
|---|
| 388 | paramString = req.getResourceRef().getRemainingPart().split( |
|---|
| 389 | "\\?")[1]; |
|---|
| 390 | form = new Form(paramString); |
|---|
| 391 | } |
|---|
| 392 | catch (ArrayIndexOutOfBoundsException e) |
|---|
| 393 | { |
|---|
| 394 | // No parameters. It might happen. Do nothing. |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| 397 | // POST Request |
|---|
| 398 | else if (req.getMethod() == Method.POST) |
|---|
| 399 | { |
|---|
| 400 | form = req.getEntityAsForm(); |
|---|
| 401 | } |
|---|
| 402 | else |
|---|
| 403 | { |
|---|
| 404 | log.logger.finest("Method " + req.getMethod() |
|---|
| 405 | + " is not supported yet."); |
|---|
| 406 | return null; |
|---|
| 407 | } |
|---|
| 408 | |
|---|
| 409 | Iterator<String> ni = form.getNames().iterator(); |
|---|
| 410 | while (ni.hasNext()) |
|---|
| 411 | { |
|---|
| 412 | String inputFormat = ni.next(); |
|---|
| 413 | String inputBody = form.getValuesMap().get(inputFormat); |
|---|
| 414 | |
|---|
| 415 | try |
|---|
| 416 | { |
|---|
| 417 | FormatParser parser = ParserFactory.Formats.valueOf( |
|---|
| 418 | inputFormat.toUpperCase()).getInstance(); |
|---|
| 419 | HashMap<String, Parameter> params = parser.parse(inputBody); |
|---|
| 420 | addParameters(request.getParameters(), params, |
|---|
| 421 | ParameterSources.URL.toString()); |
|---|
| 422 | } |
|---|
| 423 | // TODO process the error more carefully |
|---|
| 424 | catch (InstantiationException e) |
|---|
| 425 | { |
|---|
| 426 | log.logger.warning("Wrong format " + inputFormat); |
|---|
| 427 | } |
|---|
| 428 | catch (IllegalAccessException e) |
|---|
| 429 | { |
|---|
| 430 | log.logger.warning("Wrong format " + inputFormat); |
|---|
| 431 | } |
|---|
| 432 | catch (NullPointerException e) |
|---|
| 433 | { |
|---|
| 434 | log.logger.warning("Format " + inputFormat |
|---|
| 435 | + " is not supported yet."); |
|---|
| 436 | } |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | return request; |
|---|
| 440 | } |
|---|
| 441 | |
|---|
| 442 | /** |
|---|
| 443 | * Adds the parameters. |
|---|
| 444 | * |
|---|
| 445 | * @param parameters |
|---|
| 446 | * the parameters map |
|---|
| 447 | * @param toAdd |
|---|
| 448 | * the map to add to parameters map |
|---|
| 449 | * @param source |
|---|
| 450 | * the source of the parameters to add (URL, resource or profile) |
|---|
| 451 | */ |
|---|
| 452 | private void addParameters(HashMap<String, Parameter> parameters, |
|---|
| 453 | HashMap<String, Parameter> toAdd, String source) |
|---|
| 454 | { |
|---|
| 455 | Iterator<String> it = toAdd.keySet().iterator(); |
|---|
| 456 | while (it.hasNext()) |
|---|
| 457 | { |
|---|
| 458 | String key = it.next(); |
|---|
| 459 | String value = toAdd.get(key).getValue(); |
|---|
| 460 | Parameter paramToAdd = toAdd.get(key); |
|---|
| 461 | |
|---|
| 462 | if (parameters.containsKey(key)) |
|---|
| 463 | { |
|---|
| 464 | Parameter oldP = parameters.get(key); |
|---|
| 465 | |
|---|
| 466 | if (ParameterSources.valueOf(source).compareTo( |
|---|
| 467 | ParameterSources.valueOf(oldP.getSource())) <= 0) |
|---|
| 468 | { |
|---|
| 469 | // Override parameter value only if it is allowed |
|---|
| 470 | if (oldP.isConstant() == null || !oldP.isConstant()) |
|---|
| 471 | { |
|---|
| 472 | paramToAdd.setValue(value); |
|---|
| 473 | paramToAdd.setSource(source); |
|---|
| 474 | } |
|---|
| 475 | else |
|---|
| 476 | { |
|---|
| 477 | paramToAdd.setValue(oldP.getValue()); |
|---|
| 478 | paramToAdd.setSource(oldP.getSource()); |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | parameters.put(key, paramToAdd); |
|---|
| 482 | } |
|---|
| 483 | } |
|---|
| 484 | else |
|---|
| 485 | { |
|---|
| 486 | paramToAdd.setSource(source); |
|---|
| 487 | parameters.put(key, paramToAdd); |
|---|
| 488 | } |
|---|
| 489 | } |
|---|
| 490 | // parameters.putAll(toAdd); |
|---|
| 491 | } |
|---|
| 492 | |
|---|
| 493 | public static void main(String[] args) |
|---|
| 494 | { |
|---|
| 495 | try |
|---|
| 496 | { |
|---|
| 497 | new WRS(args); |
|---|
| 498 | } |
|---|
| 499 | catch (Exception e) |
|---|
| 500 | { |
|---|
| 501 | System.out.println("Error: Can't start the service."); |
|---|
| 502 | e.printStackTrace(); |
|---|
| 503 | } |
|---|
| 504 | } |
|---|
| 505 | |
|---|
| 506 | /** |
|---|
| 507 | * Read configuration XML file. |
|---|
| 508 | * |
|---|
| 509 | * @param url |
|---|
| 510 | * the URL of a configuration file |
|---|
| 511 | * |
|---|
| 512 | * @return the configuration |
|---|
| 513 | */ |
|---|
| 514 | public Configuration readConfig(String url) |
|---|
| 515 | { |
|---|
| 516 | Configuration conf = null; |
|---|
| 517 | |
|---|
| 518 | try |
|---|
| 519 | { |
|---|
| 520 | conf = IOHelper.readConfig(Configuration.class, url); |
|---|
| 521 | |
|---|
| 522 | ListIterator<Include> li = conf.getIncludes().getInclude() |
|---|
| 523 | .listIterator(); |
|---|
| 524 | |
|---|
| 525 | while (li.hasNext()) |
|---|
| 526 | { |
|---|
| 527 | Include inc = li.next(); |
|---|
| 528 | |
|---|
| 529 | switch (Includes.valueOf(inc.getName().toUpperCase())) |
|---|
| 530 | { |
|---|
| 531 | case SERVICES: |
|---|
| 532 | readServices(inc.getUrl()); |
|---|
| 533 | break; |
|---|
| 534 | case PROFILES: |
|---|
| 535 | readProfiles(inc.getUrl()); |
|---|
| 536 | this.pUrl = inc.getUrl(); |
|---|
| 537 | break; |
|---|
| 538 | case RESOURCES: |
|---|
| 539 | readResources(inc.getUrl()); |
|---|
| 540 | break; |
|---|
| 541 | } |
|---|
| 542 | } |
|---|
| 543 | |
|---|
| 544 | } |
|---|
| 545 | catch (SAXException e) |
|---|
| 546 | { |
|---|
| 547 | System.out.println("Wrong configuration file."); |
|---|
| 548 | } |
|---|
| 549 | catch (IOException e) |
|---|
| 550 | { |
|---|
| 551 | System.out.println("Can't open configuration file."); |
|---|
| 552 | } |
|---|
| 553 | catch (ParserConfigurationException e) |
|---|
| 554 | { |
|---|
| 555 | System.out.println("Wrong configuration file."); |
|---|
| 556 | } |
|---|
| 557 | catch (JAXBException e) |
|---|
| 558 | { |
|---|
| 559 | System.out.println("Wrong configuration file."); |
|---|
| 560 | } |
|---|
| 561 | |
|---|
| 562 | return conf; |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | /** |
|---|
| 566 | * Reads resources configuration file. |
|---|
| 567 | * |
|---|
| 568 | * @param url |
|---|
| 569 | * the URL of a configuration file |
|---|
| 570 | */ |
|---|
| 571 | private void readResources(String url) |
|---|
| 572 | { |
|---|
| 573 | System.out.println("Read resources from " + url + " file."); |
|---|
| 574 | Resources resources; |
|---|
| 575 | try |
|---|
| 576 | { |
|---|
| 577 | resources = IOHelper.readConfig(Resources.class, url); |
|---|
| 578 | ListIterator<util.resource.Resources.Resource> li = resources |
|---|
| 579 | .getResource().listIterator(); |
|---|
| 580 | while (li.hasNext()) |
|---|
| 581 | { |
|---|
| 582 | util.resource.Resources.Resource res = li.next(); |
|---|
| 583 | Resource resource = new Resource(res.getName(), res.getType(), |
|---|
| 584 | res.getUrl(), res.getUser(), res.getPassword(), res |
|---|
| 585 | .getQuery()); |
|---|
| 586 | |
|---|
| 587 | ListIterator<Parameter> pi = res.getParameters().getParameter() |
|---|
| 588 | .listIterator(); |
|---|
| 589 | while (pi.hasNext()) |
|---|
| 590 | { |
|---|
| 591 | Parameter p = pi.next(); |
|---|
| 592 | if (p.getSource() == null) |
|---|
| 593 | p.setSource("resource"); |
|---|
| 594 | resource.getParameters().put(p.getName(), p); |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | ListIterator<Sref> si = res.getServices().getSref() |
|---|
| 598 | .listIterator(); |
|---|
| 599 | while (si.hasNext()) |
|---|
| 600 | { |
|---|
| 601 | Sref sref = si.next(); |
|---|
| 602 | Service service = this.services.get(sref.getRef()); |
|---|
| 603 | if (service != null) |
|---|
| 604 | { |
|---|
| 605 | resource.getServices().put(service.getName(), service); |
|---|
| 606 | } |
|---|
| 607 | else |
|---|
| 608 | { |
|---|
| 609 | System.out.println("No such service: " + sref.getRef()); |
|---|
| 610 | } |
|---|
| 611 | } |
|---|
| 612 | |
|---|
| 613 | this.resources.put(resource.getName(), resource); |
|---|
| 614 | } |
|---|
| 615 | |
|---|
| 616 | } |
|---|
| 617 | catch (SAXException e) |
|---|
| 618 | { |
|---|
| 619 | System.out.println("Wrong resources configuration file."); |
|---|
| 620 | } |
|---|
| 621 | catch (IOException e) |
|---|
| 622 | { |
|---|
| 623 | System.out.println("Can't open resources configuration file."); |
|---|
| 624 | } |
|---|
| 625 | catch (ParserConfigurationException e) |
|---|
| 626 | { |
|---|
| 627 | System.out.println("Wrong resources configuration file."); |
|---|
| 628 | } |
|---|
| 629 | catch (JAXBException e) |
|---|
| 630 | { |
|---|
| 631 | System.out.println("Wrong resources configuration file."); |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | } |
|---|
| 635 | |
|---|
| 636 | /** |
|---|
| 637 | * Reads profiles configuration file. |
|---|
| 638 | * |
|---|
| 639 | * @param url |
|---|
| 640 | * the URL of a configuration file |
|---|
| 641 | */ |
|---|
| 642 | private void readProfiles(String url) |
|---|
| 643 | { |
|---|
| 644 | System.out.println("Read profiles from " + url + " file."); |
|---|
| 645 | try |
|---|
| 646 | { |
|---|
| 647 | Profiles profiles = IOHelper.readConfig(Profiles.class, url); |
|---|
| 648 | ListIterator<util.profile.Profiles.Profile> li = profiles |
|---|
| 649 | .getProfile().listIterator(); |
|---|
| 650 | while (li.hasNext()) |
|---|
| 651 | { |
|---|
| 652 | util.profile.Profiles.Profile prof = li.next(); |
|---|
| 653 | Profile profile = new Profile(prof.getName()); |
|---|
| 654 | profile.setDescription(prof.getDescription()); |
|---|
| 655 | profile.setTitle(prof.getTitle()); |
|---|
| 656 | |
|---|
| 657 | // add self resource |
|---|
| 658 | Resource self = this.resources.get("self"); |
|---|
| 659 | Enumeration<String> si = self.getServices().keys(); |
|---|
| 660 | while (si.hasMoreElements()) |
|---|
| 661 | { |
|---|
| 662 | String sk = si.nextElement(); |
|---|
| 663 | profile.getServices().put(sk, self.getServices().get(sk)); |
|---|
| 664 | |
|---|
| 665 | } |
|---|
| 666 | profile.getResources().put(self.getName(), self); |
|---|
| 667 | |
|---|
| 668 | ListIterator<Rref> ri = prof.getResources().getRref() |
|---|
| 669 | .listIterator(); |
|---|
| 670 | while (ri.hasNext()) |
|---|
| 671 | { |
|---|
| 672 | Rref rref = ri.next(); |
|---|
| 673 | |
|---|
| 674 | try |
|---|
| 675 | { |
|---|
| 676 | Resource res = this.resources.get(rref.getRef()); |
|---|
| 677 | if (res != null) |
|---|
| 678 | { |
|---|
| 679 | /* |
|---|
| 680 | * Enumeration<String> pi = |
|---|
| 681 | * res.getParameters().keys(); while |
|---|
| 682 | * (pi.hasMoreElements()) { String pk = |
|---|
| 683 | * pi.nextElement(); profile.getParameters().put(pk, |
|---|
| 684 | * res.getParameters().get(pk)); } |
|---|
| 685 | */ |
|---|
| 686 | si = res.getServices().keys(); |
|---|
| 687 | while (si.hasMoreElements()) |
|---|
| 688 | { |
|---|
| 689 | String sk = si.nextElement(); |
|---|
| 690 | profile.getServices().put(sk, |
|---|
| 691 | res.getServices().get(sk)); |
|---|
| 692 | |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | profile.getResources().put(res.getName(), res); |
|---|
| 696 | } |
|---|
| 697 | else |
|---|
| 698 | { |
|---|
| 699 | System.out.println("No such resource: " |
|---|
| 700 | + rref.getRef()); |
|---|
| 701 | } |
|---|
| 702 | } |
|---|
| 703 | catch (NullPointerException e) |
|---|
| 704 | { |
|---|
| 705 | System.out.println("Resource " + rref.getRef() |
|---|
| 706 | + " is not properly configured"); |
|---|
| 707 | } |
|---|
| 708 | |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | ListIterator<Parameter> pi = prof.getParameters() |
|---|
| 712 | .getParameter().listIterator(); |
|---|
| 713 | while (pi.hasNext()) |
|---|
| 714 | { |
|---|
| 715 | Parameter p = pi.next(); |
|---|
| 716 | if (p.getSource() == null) |
|---|
| 717 | p.setSource("profile"); |
|---|
| 718 | profile.getParameters().put(p.getName(), p); |
|---|
| 719 | } |
|---|
| 720 | |
|---|
| 721 | this.profiles.put(profile.getName(), profile); |
|---|
| 722 | |
|---|
| 723 | } |
|---|
| 724 | } |
|---|
| 725 | catch (SAXException e) |
|---|
| 726 | { |
|---|
| 727 | System.out.println("Wrong profiles configuration file."); |
|---|
| 728 | } |
|---|
| 729 | catch (NullPointerException e) |
|---|
| 730 | { |
|---|
| 731 | System.out.println("Wrong profiles configuration file."); |
|---|
| 732 | } |
|---|
| 733 | catch (IOException e) |
|---|
| 734 | { |
|---|
| 735 | System.out.println("Can't open profiles configuration file."); |
|---|
| 736 | } |
|---|
| 737 | catch (ParserConfigurationException e) |
|---|
| 738 | { |
|---|
| 739 | System.out.println("Wrong profiles configuration file."); |
|---|
| 740 | } |
|---|
| 741 | catch (JAXBException e) |
|---|
| 742 | { |
|---|
| 743 | System.out.println("Wrong profiles configuration file."); |
|---|
| 744 | } |
|---|
| 745 | |
|---|
| 746 | } |
|---|
| 747 | |
|---|
| 748 | /** |
|---|
| 749 | * Reads templates configuration file. |
|---|
| 750 | * |
|---|
| 751 | * @param url |
|---|
| 752 | * the URL of a configuration file |
|---|
| 753 | */ |
|---|
| 754 | private Template readTemplate(util.service.Template temp) |
|---|
| 755 | { |
|---|
| 756 | Template template = new Template(temp.getName(), temp.getFormat()); |
|---|
| 757 | |
|---|
| 758 | String body; |
|---|
| 759 | try |
|---|
| 760 | { |
|---|
| 761 | body = IOHelper.readFile(temp.getUrl()); |
|---|
| 762 | template.setBody(body); |
|---|
| 763 | } |
|---|
| 764 | catch (IOException e) |
|---|
| 765 | { |
|---|
| 766 | System.out |
|---|
| 767 | .println("Can't open template body file " + temp.getUrl()); |
|---|
| 768 | } |
|---|
| 769 | |
|---|
| 770 | return template; |
|---|
| 771 | } |
|---|
| 772 | |
|---|
| 773 | /** |
|---|
| 774 | * Reads services configuration file. |
|---|
| 775 | * |
|---|
| 776 | * @param url |
|---|
| 777 | * the URL of a configuration file |
|---|
| 778 | */ |
|---|
| 779 | private void readServices(String url) |
|---|
| 780 | { |
|---|
| 781 | try |
|---|
| 782 | { |
|---|
| 783 | System.out.println("Read services from " + url + " file."); |
|---|
| 784 | util.service.Services services = IOHelper.readConfig( |
|---|
| 785 | util.service.Services.class, url); |
|---|
| 786 | ListIterator<util.service.Services.Service> li = services |
|---|
| 787 | .getService().listIterator(); |
|---|
| 788 | |
|---|
| 789 | while (li.hasNext()) |
|---|
| 790 | { |
|---|
| 791 | util.service.Services.Service serv = li.next(); |
|---|
| 792 | Service service = new Service(serv.getName()); |
|---|
| 793 | |
|---|
| 794 | ListIterator<util.service.Template> tempi = serv.getTemplates() |
|---|
| 795 | .getTemplate().listIterator(); |
|---|
| 796 | while (tempi.hasNext()) |
|---|
| 797 | { |
|---|
| 798 | util.service.Template temp = tempi.next(); |
|---|
| 799 | Template template = readTemplate(temp); |
|---|
| 800 | service.getTemplates().put(template.getName(), template); |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | ListIterator<Parameter> inpi = serv.getIn().getParameter() |
|---|
| 804 | .listIterator(); |
|---|
| 805 | while (inpi.hasNext()) |
|---|
| 806 | { |
|---|
| 807 | Parameter param = inpi.next(); |
|---|
| 808 | service.getIn().put(param.getName(), param); |
|---|
| 809 | } |
|---|
| 810 | ListIterator<Parameter> outpi = serv.getOut().getParameter() |
|---|
| 811 | .listIterator(); |
|---|
| 812 | while (outpi.hasNext()) |
|---|
| 813 | { |
|---|
| 814 | Parameter param = outpi.next(); |
|---|
| 815 | service.getOut().put(param.getName(), param); |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | this.services.put(service.getName(), service); |
|---|
| 819 | } |
|---|
| 820 | } |
|---|
| 821 | catch (SAXException e) |
|---|
| 822 | { |
|---|
| 823 | System.out.println("Wrong services configuration file."); |
|---|
| 824 | } |
|---|
| 825 | catch (NullPointerException e) |
|---|
| 826 | { |
|---|
| 827 | System.out.println("Wrong services configuration file."); |
|---|
| 828 | } |
|---|
| 829 | catch (IOException e) |
|---|
| 830 | { |
|---|
| 831 | System.out.println("Can't open services configuration file."); |
|---|
| 832 | } |
|---|
| 833 | catch (ParserConfigurationException e) |
|---|
| 834 | { |
|---|
| 835 | System.out.println("Wrong services configuration file."); |
|---|
| 836 | } |
|---|
| 837 | catch (JAXBException e) |
|---|
| 838 | { |
|---|
| 839 | System.out.println("Wrong services configuration file."); |
|---|
| 840 | } |
|---|
| 841 | } |
|---|
| 842 | |
|---|
| 843 | private boolean checkParams(String args[]) |
|---|
| 844 | { |
|---|
| 845 | boolean isOk = false; |
|---|
| 846 | if (args.length == 1) |
|---|
| 847 | isOk = true; |
|---|
| 848 | return isOk; |
|---|
| 849 | } |
|---|
| 850 | |
|---|
| 851 | /** |
|---|
| 852 | * Find resource by service request. |
|---|
| 853 | * |
|---|
| 854 | * @param serviceRequest |
|---|
| 855 | * the service request |
|---|
| 856 | */ |
|---|
| 857 | private void findResource(ServiceRequest serviceRequest) |
|---|
| 858 | { |
|---|
| 859 | Resource resource = null; |
|---|
| 860 | |
|---|
| 861 | Iterator<String> resKeys = serviceRequest.getProfile().getResources() |
|---|
| 862 | .keySet().iterator(); |
|---|
| 863 | while (resKeys.hasNext()) |
|---|
| 864 | { |
|---|
| 865 | Resource res = serviceRequest.getProfile().getResources().get( |
|---|
| 866 | resKeys.next()); |
|---|
| 867 | |
|---|
| 868 | // Take the first resource which has requested service |
|---|
| 869 | // TODO There could be more sophisticated rule for choosing a |
|---|
| 870 | // resource |
|---|
| 871 | |
|---|
| 872 | if (res.getServices().containsKey( |
|---|
| 873 | serviceRequest.getService().getName())) |
|---|
| 874 | { |
|---|
| 875 | resource = res; |
|---|
| 876 | break; |
|---|
| 877 | } |
|---|
| 878 | } |
|---|
| 879 | |
|---|
| 880 | serviceRequest.setResource(resource); |
|---|
| 881 | } |
|---|
| 882 | |
|---|
| 883 | } |
|---|