Project

General

Profile

« Previous | Next » 

Revision 1826

Added by Matt Jones over 20 years ago

New versions of the Metacat client API that allow access to the sessionId
so that a session can be reused over several different instances.

View differences:

test/edu/ucsb/nceas/metacattest/client/MetacatClientTest.java
126 126
        suite.addTest(new MetacatClientTest("update"));
127 127
        suite.addTest(new MetacatClientTest("invalidDelete"));
128 128
        suite.addTest(new MetacatClientTest("delete"));
129
        suite.addTest(new MetacatClientTest("inaccessiblemetacat"));
129
        suite.addTest(new MetacatClientTest("inaccessibleMetacat"));
130
        suite.addTest(new MetacatClientTest("reuseSession"));
130 131
        return suite;
131 132
    }
132 133
  
......
202 203
        } catch (Exception e) {
203 204
            fail("General exception:\n" + e.getMessage());
204 205
        }
205
           
206 206
    }
207 207

  
208 208
    /**
......
403 403
     * inaccessible url. Then try to login it. If get a MetacatInaccessible
404 404
     * exception, this is right.
405 405
     */
406
    public void inaccessiblemetacat()
406
    public void inaccessibleMetacat()
407 407
    {
408 408
        Metacat mWrong = null;
409 409
        try {
......
422 422
    }
423 423

  
424 424
    /**
425
     * Try to perform an action that requires a session to be valid without
426
     * having logged in first by setting the sessionId from a previous
427
     * session.  Then insert a document.
428
     */
429
    public void reuseSession()
430
    {
431
        String oldSessionId = "";
432
        try {
433
            Metacat mtemp = MetacatFactory.createMetacatConnection(metacatUrl);
434
            String response = mtemp.login(username, password);
435
            oldSessionId = mtemp.getSessionId();
436
        } catch (MetacatAuthException mae) {
437
            fail("Authorization failed:\n" + mae.getMessage());
438
        } catch (MetacatInaccessibleException mie) {
439
            System.err.println("Metacat is: " + metacatUrl);
440
            fail("Metacat connection failed." + mie.getMessage());
441
        }
442

  
443
        try {
444
            String identifier = generateDocid() + ".1";
445
            Metacat m2 = null;
446
            try {
447
                m2 = MetacatFactory.createMetacatConnection(metacatUrl);
448
            } catch (MetacatInaccessibleException mie) {
449
                System.err.println("Metacat is: " + metacatUrl);
450
                fail("Metacat connection failed." + mie.getMessage());
451
            }
452
            m2.setSessionId(oldSessionId);
453
            String response = m2.insert(identifier, 
454
                    new StringReader(testdocument), null);
455
            System.err.println("Reuse Insert response: " + response);
456
            assertTrue(response.indexOf("<success>") != -1);
457
        } catch (MetacatInaccessibleException mie) {
458
            fail("Metacat Inaccessible:\n" + mie.getMessage());
459
        } catch (InsufficientKarmaException ike) {
460
            fail("Insufficient karma:\n" + ike.getMessage());
461
        } catch (MetacatException me) {
462
            fail("Metacat Error:\n" + me.getMessage());
463
        } catch (Exception e) {
464
            fail("General exception:\n" + e.getMessage());
465
        }
466
    }
467

  
468
    /**
425 469
     * Create a hopefully unique docid for testing insert and update. Does
426 470
     * not include the 'revision' part of the id.
427 471
     *
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
399 399
        return this.sessionId;
400 400
    }
401 401

  
402
    /**
403
     * Set the session identifier for this session.  This identifier was 
404
     * previously established with a call to login.  To continue to use the 
405
     * same session, set the session id before making a call to one of the
406
     * metacat access methods (e.g., read, query, insert, etc.).
407
     *
408
     * @param String the sessionId from a previously established session
409
     */
410
    public void setSessionId(String sessionId)
411
    {
412
        this.sessionId = sessionId;
413
        HttpMessage.setCookie("JSESSIONID="+this.sessionId);
414
    }
415

  
402 416
    /************************************************************************
403 417
     * PRIVATE METHODS
404 418
     ************************************************************************/
src/edu/ucsb/nceas/metacat/client/Metacat.java
152 152
     * @returns the sessionId as a String, or null if the session is invalid
153 153
     */
154 154
    public String getSessionId();
155

  
156
    /**
157
     * Set the session identifier for this session.  This identifier was 
158
     * previously established with a call to login.  To continue to use the 
159
     * same session, set the session id before making a call to one of the
160
     * metacat access methods (e.g., read, query, insert, etc.).
161
     *
162
     * @param String the sessionId from a previously established session
163
     */
164
    public void setSessionId(String sessionId);
155 165
}

Also available in: Unified diff