wiki:Client/Development/ConnectingToTheServer

Contacting the server (Java)

Let's start with a small sample which just sets up everything needed to use the stub functions and then calls getServerInfo() to retrieve a ServerInfo object:

  • Create and install a TrustManager which does not validate certificate chains as our certification authority (DFN-CERT) is not included in standard Java distributions.
                // Create a trust manager that does not validate certificate chains
                TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        public X509Certificate[] getAcceptedIssuers() {
                            return null;
                        }
                        public void checkClientTrusted(X509Certificate[] certs,
                                String authType) {
                        }
                        public void checkServerTrusted(X509Certificate[] certs,
                                String authType) {
                        }
                    }
                };
                
                // Install the all-trusting trust manager
                try {
                    SSLContext sc = SSLContext.getInstance("SSL");
                    sc.init(null, trustAllCerts, new java.security.SecureRandom());
                    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
                } catch (Exception e) {
                    System.out.println("Warning: Unable to install all-trusting trust "
                        + "manager! SSL connection may not work!");
                }
    
  • Get a JPlagTyp_Stub and the corresponding JPlagClientAccessHandler object and set username and password.
                // Get JPlag client stub
                
                JPlagTyp_Stub stub = (JPlagTyp_Stub) (new JPlagService_Impl()
                        .getJPlagServicePort());
                        
                // Search for the JPlagClientAccessHandler in the handler chain
    
                HandlerChain handlerchain = stub._getHandlerChain();
                Iterator handlers = handlerchain.iterator();
                JPlagClientAccessHandler accessHandler = null;
                while(handlers.hasNext()) {
                    Handler handler = (Handler) handlers.next();
                    if(handler instanceof JPlagClientAccessHandler) {
                        accessHandler = (JPlagClientAccessHandler) handler;
                        break;
                    }
                }
                
                if(accessHandler == null) {
                    System.out.println("Unable to find access handler!");
                    return;
                }
                
                // Initialize access handler
                
                accessHandler.setUserPassObjects(username, password);
    
  • Retrieve a ServerInfo object for this user and print out some information.
                ServerInfo info;
                
                try {
                    info = stub.getServerInfo();
                } catch(Exception e) {
                    checkException(e);
                    return;
                }
                
                UserInfo userinfo = info.getUserInfo();
                System.out.println("User info:\n Email: " + userinfo.getEmail()
                    + "\n Alternative email: " + userinfo.getEmailSecond()
                    + "\n Homepage: " + userinfo.getHomepage());
                
                System.out.println("\nAvailable languages:");
                LanguageInfo[] languages = info.getLanguageInfos();
                for(int i=0; i<languages.length; i++) {
                    System.out.print(" - \"" + languages[i].getName()
                        + "\"\n   default minimum match length = "
                        + languages[i].getDefMinMatchLen()
                        + "\n   default suffixes: ");
                    String[] suffixes = languages[i].getSuffixes();
                    for(int j=0; j<suffixes.length; j++) {
                        System.out.print(suffixes[j]
                            + ((j==suffixes.length-1) ? "\n" : ", "));
                    }
                }
    
                System.out.println("\nAvailable country languages:");
                String[] countryLangs = info.getCountryLanguages();
                for(int i=0; i<ountryLangs.length; i++)
                    System.out.println(" - \"" + countryLangs[i] + "\"");
                
                Submission[] subs = info.getSubmissions();
                if(subs.length == 0) {
                    System.out.println("\nCurrently there are no submissions on the "
                        + "server for this user!");
                }
                else {
                    System.out.println("\nAvailable submissions with states:\n");
                    for(int i=0; i<subs.length; i++) {
                        System.out.println(" - \"" + subs[i].getTitle() + "\" ("
                            + subs[i].getLastState() + ")");
                    }
                }
    
    "checkException()" is a small helper function which makes exception handling for stub functions much easier. As you can see, the ServerInfo provides the following information:
    • User information which is likely to change over time. So here, the user can see, what email adresses and which homepage is currently saved in the account.
    • Name, default minimum match length and default suffixes for each available language
    • The names of the country languages supported for the result files
    • A list of the user's submissions on the server containing the submission ID, title, date and state of progress

To create and run this small sample application execute the following commands on the command prompt in the "JPlagTutorial" directory:

ant cnj-exampleconnect
java -jar dist/exampleConnect.jar -user <your username> -pass <your password>

The output of the application should look like this:

User info:
 Email: JPlag@ira.uka.de
 Alternative email: null
 Homepage: http://www.jplag.de

Available languages:
 - "java12"
   default minimum match length = 9
   default suffixes: .java, .jav, .JAVA, .JAV
 - "java15"
   default minimum match length = 8
   default suffixes: .java, .jav, .JAVA, .JAV
 - "scheme"
   default minimum match length = 13
   default suffixes: .scm, .SCM
 - "c/c++"
   default minimum match length = 12
   default suffixes: .cpp, .CPP, .c++, .C++, .c, .C, .h, .H, .hpp, .HPP
 - "text"
   default minimum match length = 5
   default suffixes: .txt, .TXT, .asc, .ASC, .tex, .TEX
 - "char"
   default minimum match length = 10
   default suffixes: .txt, .TXT, .asc, .ASC, .tex, .TEX
 - "c#-alpha"
   default minimum match length = 8
   default suffixes: .cs, .CS

Available country languages:
 - "en"
 - "de"
 - "fr"

Currently there are no submissions on the server for this user!

Next: Sending a sumbission

Last modified 5 years ago Last modified on Mar 8, 2013 9:31:32 AM