X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=webservices%2Fcompbio%2Fws%2Fclient%2FWSTester.java;h=5450a1dc7f229add009c4d145edd44ecc1a1bdc3;hb=8b45c19de3f4e652033186bf6b9dc9b3e9205a64;hp=483c1628f29b3021d623494d2742ae7ebd172983;hpb=7b5d0822e864d35c099a9b3b9e463c59dfdd091e;p=jabaws.git diff --git a/webservices/compbio/ws/client/WSTester.java b/webservices/compbio/ws/client/WSTester.java index 483c162..5450a1d 100644 --- a/webservices/compbio/ws/client/WSTester.java +++ b/webservices/compbio/ws/client/WSTester.java @@ -1,6 +1,7 @@ /* Copyright (c) 2011 Peter Troshin + * Copyright (c) 2013 Alexander Sherstnev * - * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 + * JAva Bioinformatics Analysis Web Services (JABAWS) @version: 2.0 * * This library is free software; you can redistribute it and/or modify it under the terms of the * Apache License version 2 as published by the Apache Software Foundation @@ -27,9 +28,13 @@ import java.io.Closeable; import java.io.IOException; import java.io.PrintWriter; import java.net.ConnectException; +import java.net.MalformedURLException; +import java.net.URL; import java.util.Arrays; import java.util.List; +import java.util.logging.Level; +import javax.xml.ws.Service; import javax.xml.ws.WebServiceException; import compbio.data.msa.JABAService; @@ -56,32 +61,36 @@ import compbio.util.Util; * * @author pvtroshin * - * @version 1.0 February 2010 + * @version 1.5 February 2013 */ public class WSTester { /** - * Sequences to be used as input for all WS + * Test sequences to be used as input for WS + */ + public static final String fastaInput2records = + ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n" + + ">Bar\nASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n"; + public static final String fastaInput1record = + ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV\n"; + public static final String fastaAlignment = + ">Foo\nMTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n" + + ">Bar\nASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n"; + public static final String fastaRNAAlignment = + ">Foo\nC-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n" + + ">Bar\nG-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n"; + + public static final String clustalRNAAlignment = + "CLUSTAL\n" + + "Foo C-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGA\n" + + "Bar G-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGA\n" + + "Foo UGGUUG----GCAAC\n" + + "Bar U-UACGAUUAACCAC"; + /** + * Status strings */ - public static final String fastaInput = ">Foo\n" - + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV" - + "\n>Bar\n" - + "ASDAAPEHPGIALWLHALEDAGQAEAAAAYTRAHQLLPEEPYITAQLLNAVA\n"; - - public static final String fastaAlignment = ">Foo\n" - + "MTADGPRELLQLRAAVRHRPQDFVAWLMLADAELGMGDTTAGEMAVQRGLALHPGHPEAV--------\n" - + ">Bar\n" - + "ASDAAPEH------------PGIALWLHALE-DAGQAEAAA---AYTRAHQLLPEEPYITAQLLNAVA\n" - + ""; - - public static final String fastaRNAAlignment = ">Foo\n" - + "C-UUGCGUUAAUGAGAACAGAAACG-UAAA--CUAUAA-CCUAG-G-GGUUUCUGUUGGAUGGUUG----GCAAC\n" - + ">Bar\n" - + "G-UGGCGCUUAUGACGCAGUUGUCU-UAAA-CUCGAAC--UCGA-GCGGGCAAUUGCUGAU-UACGAUUAACCAC\n"; - private static final String FAILED = "FAILED"; private static final String OK = "OK"; - private static final String UNSUPPORTED = "UNSUPPORTED"; /** @@ -89,10 +98,12 @@ public class WSTester { * * @return List of FastaSequence records */ - private static List loadSeqs() { + private static List loadSeqs(int nLines) { try { - return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput - .getBytes())); + if (nLines == 1) { + return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput1record.getBytes())); + } + return SequenceUtil.readFasta(new ByteArrayInputStream(fastaInput2records.getBytes())); } catch (IOException ignored) { // Should not happen as a source is not a external stream ignored.printStackTrace(); @@ -103,7 +114,7 @@ public class WSTester { /** * Converting input to a form accepted by WS * - * @return List of FastaSequence records with aligned protein sequences + * @return List of FastaSequence records */ private static List loadAlignment() { try { @@ -158,12 +169,10 @@ public class WSTester { + pseparator + "host_and_context " + "<" + servicekey + pseparator + "serviceName>"); System.out.println(); - System.out - .println(hostkey + System.out.println(hostkey + pseparator + " - a full URL to the JABAWS web server including context path e.g. http://10.31.1.159:8080/ws"); - System.out - .println(servicekey + System.out.println(servicekey + pseparator + " - optional if unspecified all services are tested otherwise one of " + Arrays.toString(Services.values())); @@ -187,7 +196,7 @@ public class WSTester { writer.print("Aligning with preset '" + preset.getName() + "'... "); Alignment al = null; try { - String taskId = msaws.presetAlign(loadSeqs(), preset); + String taskId = msaws.presetAlign(loadSeqs(2), preset); al = msaws.getResult(taskId); if (al != null) { writer.println(OK); @@ -208,10 +217,10 @@ public class WSTester { return succeed; } - private boolean testMsaWS(MsaWS msaws) throws Exception { + private boolean testMsaWS(MsaWS msaws, Services service) throws Exception { assert msaws != null; - boolean succeed = testDefaultAlignment(msaws); + boolean succeed = testDefaultAlignment(msaws, service); // If exception above is thrown than the tests below is not run PresetManager pmanager = msaws.getPresets(); @@ -223,7 +232,6 @@ public class WSTester { testMetadata(msaws); return succeed; } - /** * Call most of web services functions and check the output * @@ -239,18 +247,16 @@ public class WSTester { private boolean checkService(JABAService wservice, Services service) { try { if (wservice == null) { - throw new NullPointerException( - "JABAService instance must be provided!"); + throw new NullPointerException("JABAService instance must be provided!"); } if (wservice instanceof MsaWS) { - return testMsaWS((MsaWS) wservice); + return testMsaWS((MsaWS) wservice, service); } else if (wservice instanceof SequenceAnnotation) { return testSequenceAnnotationWS( (SequenceAnnotation) wservice, service); } else { - throw new UnsupportedOperationException("The service: " - + wservice.getClass() + " is not supported! "); + throw new UnsupportedOperationException("The service: " + wservice.getClass() + " is not supported! "); } } catch (Exception e) { reportException(e); @@ -260,9 +266,9 @@ public class WSTester { private boolean testSequenceAnnotationWS( SequenceAnnotation wservice, Services service) throws Exception { - writer.print("Calling analyse........."); + writer.print("Calling annotation test........."); - List input = loadSeqs(); + List input = loadSeqs(2); if (service == Services.AAConWS ) { input = loadAlignment(); } else if (service == Services.RNAalifoldWS) { @@ -308,8 +314,7 @@ public class WSTester { private void reportException(Exception e) { writer.println(FAILED); - writer.println("Exception while waiting for results. " - + "Exception details are below:"); + writer.println("Exception while waiting for results. Exception details are below:"); writer.println(e.getLocalizedMessage()); e.printStackTrace(writer); } @@ -359,12 +364,17 @@ public class WSTester { * @param msaws * @throws UnsupportedRuntimeException */ - private boolean testDefaultAlignment(MsaWS msaws) throws Exception { + private boolean testDefaultAlignment(MsaWS msaws, Services service) throws Exception { writer.print("Testing alignment with default parameters:"); Alignment al = null; boolean succeed = false; + String taskId; - String taskId = msaws.align(loadSeqs()); + if (service == Services.JpredWS) { + taskId = msaws.align(loadAlignment()); + } else { + taskId = msaws.align(loadSeqs(2)); + } writer.print("\nQuerying job status..."); JobStatus status = msaws.getJobStatus(taskId); while (status != JobStatus.FINISHED) { @@ -380,7 +390,6 @@ public class WSTester { } return succeed; } - /** * Test JWS2 web services * @@ -403,8 +412,7 @@ public class WSTester { String host = CmdHelper.getHost(args); String serviceName = CmdHelper.getServiceName(args); if (!Jws2Client.validURL(host)) { - System.err - .println(" parameter is not provided or is incorrect!"); + System.err.println(" parameter is not provided or is incorrect!"); System.exit(1); } WSTester tester = new WSTester(host, new PrintWriter(System.out, true)); @@ -412,8 +420,7 @@ public class WSTester { if (serviceName != null) { Services service = Services.getService(serviceName); if (service == null) { - tester.writer.println("Service '" + serviceName - + "' is not supported. Valid values are: " + tester.writer.println("Service '" + serviceName + "' is not supported. Valid values are: " + Arrays.toString(Services.values())); tester.writer.println(); printUsage(); @@ -423,13 +430,51 @@ public class WSTester { System.exit(0); } - tester.writer - .println(" is not provided checking all known services..."); + tester.writer.println(" is not provided checking all known services..."); for (Services serv : Services.values()) { tester.writer.println(); tester.checkService(serv); } + + } + + /** + * Connects to a web service by the host and the service name web service type + * + * @param host + * the fully qualified name of JABAWS server including JABAWS + * context name e.g + * http://nanna.cluster.lifesci.dundee.ac.uk:8080/jaba + * @param service + * the name of the JABAWS service to connect to + * @return JABAService + * @throws WebServiceException + * @throws ConnectException + * if fails to connect to the service on the host + */ + private JABAService connect(String host, Services service) + throws WebServiceException, ConnectException { + URL url = null; + System.out.println ("Attempting to connect with " + service.toString() + "..."); + try { + url = new URL(host + "/" + service.toString() + "?wsdl"); + System.out.println ("URL: " + url.toString()); + } catch (MalformedURLException e) { + e.printStackTrace(); + } + Service serv = null; + try { + serv = service.getService(url, service.getServiceNamespace()); + } catch (WebServiceException wse) { + wse.printStackTrace(); + } + if (serv == null) { + throw new ConnectException("Could not connect to " + url + ". Is the server down?"); + } + JABAService srv = service.getInterface(serv); + System.out.println ("Connected successfully!"); + return srv; } /** @@ -443,10 +488,10 @@ public class WSTester { */ public boolean checkService(Services service) throws ConnectException, WebServiceException { - JABAService ws = Jws2Client.connect(hostname, service); + JABAService ws = connect(hostname, service); if (ws == null) { - writer.println("Cannot estabilish the connection to host " - + hostname + " with service " + service.toString()); + String line = "Cannot estabilish the connection to host " + hostname + " with service "; + writer.println(line + service.toString()); return false; } boolean succeed = false;