Project

General

Profile

« Previous | Next » 

Revision 7402

remove use of HttpMessage (in morpho.jar) in favor of standard httpclient methods for calling the servlet in tests

View differences:

test/edu/ucsb/nceas/metacatnettest/MetaCatServletNetTest.java
26 26

  
27 27
package edu.ucsb.nceas.metacatnettest;
28 28

  
29
import edu.ucsb.nceas.MCTestCase;
30
import edu.ucsb.nceas.metacat.*;
31
import edu.ucsb.nceas.metacat.properties.PropertyService;
32
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
29
import java.io.File;
30
import java.io.InputStream;
31
import java.io.InputStreamReader;
32
import java.io.StringWriter;
33
import java.nio.charset.Charset;
34
import java.util.ArrayList;
35
import java.util.Enumeration;
36
import java.util.List;
37
import java.util.Properties;
33 38

  
34 39
import junit.framework.Test;
35 40
import junit.framework.TestSuite;
36 41

  
37
import java.io.*;
38
import java.net.*;
39
import java.util.*;
42
import org.apache.http.HttpResponse;
43
import org.apache.http.HttpVersion;
44
import org.apache.http.NameValuePair;
45
import org.apache.http.client.HttpClient;
46
import org.apache.http.client.entity.UrlEncodedFormEntity;
47
import org.apache.http.client.methods.HttpPost;
48
import org.apache.http.entity.mime.HttpMultipartMode;
49
import org.apache.http.entity.mime.MultipartEntity;
50
import org.apache.http.entity.mime.content.FileBody;
51
import org.apache.http.entity.mime.content.StringBody;
52
import org.apache.http.impl.client.DefaultHttpClient;
53
import org.apache.http.message.BasicNameValuePair;
54
import org.apache.http.params.CoreProtocolPNames;
55
import org.apache.http.util.EntityUtils;
40 56

  
41
import org.apache.log4j.Logger;
57
import edu.ucsb.nceas.MCTestCase;
58
import edu.ucsb.nceas.metacat.properties.PropertyService;
59
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
42 60

  
43 61
/**
44 62
 * A JUnit test for testing Step class processing
45 63
 */
46 64
public class MetaCatServletNetTest extends MCTestCase {
47
	private static String metacatURL;
48 65
	static {
49 66
		try {		
50
			metacatURL = PropertyService.getProperty("test.metacatUrl");
67
			metacatUrl = PropertyService.getProperty("test.metacatUrl");
51 68
		} catch (PropertyNotFoundException pnfe) {
52 69
			System.err.println("could not find metacat URL in MetacatServletNetTest: "
53 70
					+ pnfe.getMessage());
......
58 75
	}
59 76

  
60 77
	private String serialNumber;
78
	private static String sessionId;
61 79

  
62 80
	/**
63 81
	 * Constructor to build the test
......
380 398
		boolean connected = false;
381 399
		if (response.indexOf("<login>") != -1) {
382 400
			connected = true;
401
			// set the session id
402
            int start = response.indexOf("<sessionId>") + 11;
403
            int end = response.indexOf("</sessionId>");
404
            if ((start != -1) && (end != -1)) {
405
                sessionId = response.substring(start,end);
406
            }
383 407
		} else {
384

  
408
			sessionId = "";
385 409
			connected = false;
386 410
		}
411
		
412
		debug("sessionId: " + this.sessionId);
387 413

  
388 414
		return connected;
389 415
	}
......
403 429

  
404 430
		String response = getMetacatString(prop);
405 431
		debug("Logout Message: " + response);
406
		edu.ucsb.nceas.morpho.framework.HttpMessage.setCookie(null);
407 432

  
408 433
		if (response.indexOf("<logout>") != -1) {
409 434
			disConnected = true;
......
519 544
	 * @param id the id to assign to the file on metacat (e.g., knb.1.1)
520 545
	 * @param file the file to send
521 546
	 */
522
	public String sendDataFile(String id, File file) {
523
		String response = "";
524
		InputStream returnStream = null;
547
    public String sendDataFile(String docid, File file) {
548
        
549
        String response = null;
525 550

  
526
		// Now contact metacat and send the request
527
		try {
528
			//FileInputStream data = new FileInputStream(file);
529
			System.setProperty("java.protocol.handler.pkgs", "HTTPClient");
530
			URL url = new URL(metacatURL);
531
			edu.ucsb.nceas.morpho.framework.HttpMessage msg = new edu.ucsb.nceas.morpho.framework.HttpMessage(
532
					url);
533
			Properties args = new Properties();
534
			args.put("action", "upload");
535
			args.put("docid", id);
551
    	try {
552
	    	HttpClient httpclient = new DefaultHttpClient();
553
	        httpclient.getParams().setParameter(
554
	        		CoreProtocolPNames.PROTOCOL_VERSION, 
555
	        	    HttpVersion.HTTP_1_1);
556
	    	httpclient.getParams().setParameter(
557
	    			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
558
	    			"UTF-8");
559
	    	 
560
	    	HttpPost post = new HttpPost(metacatUrl);
561
	    	MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
562
	    	 
563
	    	// For File parameters
564
	    	entity.addPart("datafile", new FileBody(file));
565
	    	
566
	        //set up properties
567
	        Properties prop = new Properties();
568
	        prop.put("action", "upload");
569
	        prop.put("docid", docid);
570
	        
571
	        // For usual String parameters
572
	        Enumeration<Object> keys = prop.keys();
573
	        while (keys.hasMoreElements()) {
574
	        	String key = (String) keys.nextElement();
575
	        	String value = prop.getProperty(key);
576
	        	entity.addPart(key, new StringBody(value, Charset.forName("UTF-8")));
577
	        }
578
	        
579
	        post.setHeader("Cookie", "JSESSIONID=" + sessionId);
580
	        post.setEntity(entity);
581
	    	
582
	    	response = EntityUtils.toString(httpclient.execute(post).getEntity(), "UTF-8");
583
	    	httpclient.getConnectionManager().shutdown();
584
    	} catch (Exception e) {
585
    		e.printStackTrace();
586
    	}
587
    	
588
        return response;
589
    }
536 590

  
537
			Properties dataStreams = new Properties();
538
			String filename = file.getAbsolutePath();
539
			System.out.println("the absolute path is " + filename);
540
			dataStreams.put("datafile", filename);
541

  
542
			returnStream = msg.sendPostData(args, dataStreams);
543

  
544
			InputStreamReader returnStreamReader = new InputStreamReader(returnStream);
545
			StringWriter sw = new StringWriter();
546
			int len;
547
			char[] characters = new char[512];
548
			while ((len = returnStreamReader.read(characters, 0, 512)) != -1) {
549
				sw.write(characters, 0, len);
550
			}
551
			returnStreamReader.close();
552
			response = sw.toString();
553
			sw.close();
554

  
555
		} catch (Exception e) {
556
			e.printStackTrace(System.err);
557
		}
558
		return response;
559
	}
560

  
561 591
	public String getMetacatString(Properties prop) {
562 592
		String response = null;
563 593

  
......
587 617
	 * @param prop the properties to be sent to Metacat
588 618
	 * @return InputStream as returned by Metacat
589 619
	 */
590
	public InputStream getMetacatInputStream(Properties prop) {
591
		InputStream returnStream = null;
592
		// Now contact metacat and send the request
593
		try {
620
	public InputStream getMetacatInputStream(Properties prop) throws Exception {
621
		
622
		InputStream result = null;
623
        	HttpClient httpclient = new DefaultHttpClient();
624
            httpclient.getParams().setParameter(
625
            		CoreProtocolPNames.PROTOCOL_VERSION, 
626
            	    HttpVersion.HTTP_1_1);
627
        	httpclient.getParams().setParameter(
628
        			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
629
        			"UTF-8");
630
            HttpPost post = new HttpPost(metacatUrl);
631
            //set the params
632
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
633
            Enumeration<Object> keys = prop.keys();
634
            while (keys.hasMoreElements()) {
635
            	String key = (String) keys.nextElement();
636
            	String value = prop.getProperty(key);
637
            	NameValuePair nvp = new BasicNameValuePair(key, value);
638
            	nameValuePairs.add(nvp);
639
            }
640
            
641
    		debug("getMetacatInputStream.sessionId: " + sessionId);
594 642

  
595
			URL url = new URL(metacatURL);
596
			edu.ucsb.nceas.morpho.framework.HttpMessage msg = new edu.ucsb.nceas.morpho.framework.HttpMessage(
597
					url);
598
			returnStream = msg.sendPostMessage(prop);
599
			return returnStream;
600
		} catch (Exception e) {
601
			e.printStackTrace(System.err);
643
	        post.setHeader("Cookie", "JSESSIONID=" + sessionId);
644
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
602 645

  
603
		}
604
		return returnStream;
646
            HttpResponse httpResponse = httpclient.execute(post);
647
            result = httpResponse.getEntity().getContent();
648
        
649
        return result;
650
        
605 651

  
606 652
	}
607 653

  

Also available in: Unified diff