Project

General

Profile

« Previous | Next » 

Revision 5914

remove httpclient 3.1 and custom-built httpclient.jar
rework MetacatClient (and other classes) to use httpclient 4
updated build to not create httpclient.jar
encoding tests now pass.

View differences:

test/edu/ucsb/nceas/MCTestCase.java
48 48
import java.util.TimeZone;
49 49
import java.util.Vector;
50 50

  
51
import org.apache.commons.httpclient.HttpClient;
51
import org.apache.http.client.HttpClient;
52
import org.apache.http.impl.client.DefaultHttpClient;
52 53

  
53 54
import edu.ucsb.nceas.metacat.database.DBConnection;
54 55
import edu.ucsb.nceas.metacat.database.DBConnectionPool;
......
538 539
		DBConnectionPool.returnDBConnection(dbconn, serialNumber);
539 540
	}
540 541
	
541
	protected static void httpPost(String url, HashMap<String,String> paramMap)  throws IOException {
542
	protected static void httpPost(String url, HashMap<String,String> paramMap) throws Exception {
542 543
		debug("Posting to: " + url);
543 544
		if (httpClient == null) {
544
			httpClient = new HttpClient();
545
			httpClient = new DefaultHttpClient();
545 546
		}
546 547
		String postResponse = RequestUtil.post(httpClient, url, paramMap);
547 548
		debug("Post response: " + postResponse);
src/edu/ucsb/nceas/metacat/restservice/ResourceHandler.java
38 38
import java.text.SimpleDateFormat;
39 39

  
40 40

  
41
import org.apache.commons.httpclient.util.DateParser;
42 41
import org.apache.commons.io.IOUtils;
43 42
import org.apache.log4j.Logger;
44 43
import org.apache.maven.artifact.ant.shaded.IOUtil;
src/edu/ucsb/nceas/metacat/clientview/ClientViewHelper.java
772 772
                prop.put("returnfield", returnFld);
773 773
            }
774 774
            
775
            response = metacatClient.sendData(prop, null, null, 0);
775
            response = metacatClient.sendParameters(prop);
776 776
            if (response != null) {
777 777
                buffy = new BufferedReader(new InputStreamReader(response));
778 778
                result = XMLUtilities.getXMLReaderAsDOMDocument(buffy);
src/edu/ucsb/nceas/metacat/util/RequestUtil.java
26 26

  
27 27
package edu.ucsb.nceas.metacat.util;
28 28

  
29
import java.io.InputStream;
29 30
import java.io.InputStreamReader;
30 31
import java.io.IOException;
31 32
import java.io.BufferedReader;
......
33 34
import java.net.MalformedURLException;
34 35
import java.net.URL;
35 36
import java.net.URLConnection;
37
import java.util.ArrayList;
36 38
import java.util.Enumeration;
37 39
import java.util.HashMap;
38 40
import java.util.Hashtable;
41
import java.util.Iterator;
42
import java.util.List;
39 43
import java.util.Set;
40 44
import java.util.Vector;
41 45

  
......
46 50
import javax.servlet.http.HttpServletResponse;
47 51
import javax.servlet.http.HttpSession;
48 52

  
49
import org.apache.commons.httpclient.HttpClient;
50
import org.apache.commons.httpclient.HttpException;
51
import org.apache.commons.httpclient.methods.PostMethod;
53
import org.apache.commons.io.IOUtils;
54
import org.apache.http.HttpException;
55
import org.apache.http.HttpResponse;
56
import org.apache.http.HttpVersion;
57
import org.apache.http.NameValuePair;
58
import org.apache.http.client.HttpClient;
59
import org.apache.http.client.entity.UrlEncodedFormEntity;
60
import org.apache.http.client.methods.HttpPost;
61
import org.apache.http.impl.client.DefaultHttpClient;
62
import org.apache.http.message.BasicNameValuePair;
63
import org.apache.http.params.CoreProtocolPNames;
52 64
import org.apache.log4j.Logger;
53 65

  
54 66
import edu.ucsb.nceas.metacat.properties.PropertyService;
......
59 71
public class RequestUtil {
60 72
	
61 73
	private static Logger logMetacat = Logger.getLogger(RequestUtil.class);
74
	private static String encoding = "UTF-8";
62 75
	
63 76
	/**
64 77
	 * private constructor - all methods are static so there is no
......
169 182
	 *            map of parameters to add to the post
170 183
	 * @returns a string holding the response body
171 184
	 */
172
	public static String post(HttpClient httpClient, String url,
185
	public static String post(HttpClient httpclient, String url,
173 186
			HashMap<String, String> paramMap) throws IOException, HttpException {
174 187

  
175
		PostMethod method = new PostMethod(url);
176

  
177
		// Configure the form parameters
178
		if (paramMap != null) {
179
			Set<String> paramNames = paramMap.keySet();
180
			for (String paramName : paramNames) {
181
				method.addParameter(paramName, paramMap.get(paramName));
182
			}
183
		}
184

  
185
		// Execute the POST method
186
		int statusCode = httpClient.executeMethod(method);
187
		if (statusCode != -1) {
188
			String contents = method.getResponseBodyAsString();
189
			method.releaseConnection();
190
			return (contents);
191
		}
192

  
188
        httpclient.getParams().setParameter(
189
        		CoreProtocolPNames.PROTOCOL_VERSION, 
190
        	    HttpVersion.HTTP_1_1);
191
    	httpclient.getParams().setParameter(
192
    			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
193
    			encoding );
194
        HttpPost post = new HttpPost(url);
195
        //set the params
196
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
197
        Iterator<String> keys = paramMap.keySet().iterator();
198
        while (keys.hasNext()) {
199
        	String key = keys.next();
200
        	String value = paramMap.get(key);
201
        	NameValuePair nvp = new BasicNameValuePair(key, value);
202
        	nameValuePairs.add(nvp);
203
        }
204
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
205
        //post.setHeader("Cookie", "JSESSIONID="+ sessionId);
206
        HttpResponse httpResponse = httpclient.execute(post);
207
        if (httpResponse.getStatusLine().getStatusCode() != -1) {
208
            InputStream result = httpResponse.getEntity().getContent();
209
            String contents = IOUtils.toString(result, encoding);
210
            return contents;
211
        }
212
        
193 213
		return null;
194 214
	}
195 215
	
src/edu/ucsb/nceas/metacat/client/MetacatClient.java
33 33
import java.io.IOException;
34 34
import java.io.StringWriter;
35 35
import java.io.Reader;
36
import java.net.URL;
36
import java.nio.charset.Charset;
37
import java.util.ArrayList;
38
import java.util.Enumeration;
39
import java.util.List;
37 40
import java.util.Properties;
38 41
import java.util.Vector;
39 42
import javax.servlet.http.HttpServletRequest;
40 43

  
44
import org.apache.commons.io.IOUtils;
45
import org.apache.http.HttpResponse;
46
import org.apache.http.HttpVersion;
47
import org.apache.http.NameValuePair;
48
import org.apache.http.client.HttpClient;
49
import org.apache.http.client.entity.UrlEncodedFormEntity;
50
import org.apache.http.client.methods.HttpPost;
51
import org.apache.http.entity.mime.HttpMultipartMode;
52
import org.apache.http.entity.mime.MultipartEntity;
53
import org.apache.http.entity.mime.content.FileBody;
54
import org.apache.http.entity.mime.content.InputStreamBody;
55
import org.apache.http.entity.mime.content.StringBody;
56
import org.apache.http.impl.client.DefaultHttpClient;
57
import org.apache.http.message.BasicNameValuePair;
58
import org.apache.http.params.CoreProtocolPNames;
59
import org.apache.http.util.EntityUtils;
41 60
import org.w3c.dom.Node;
42 61
import org.w3c.dom.NodeList;
43 62

  
44
import edu.ucsb.nceas.utilities.HttpMessage;
45 63
import edu.ucsb.nceas.utilities.IOUtil;
46 64
import edu.ucsb.nceas.utilities.XMLUtilities;
47 65
import java.io.File;
......
59 77
    /** The session identifier for the session */
60 78
    private String sessionId;
61 79
    
80
    /** The default character encoding, can be changed by client */
81
    private String encoding = "UTF-8";
82
    
62 83
    public static void main(String[] args) {
63 84
    	try {
64 85
    		Metacat mc = 
......
117 138
        
118 139
        String response = null;
119 140
        try {
120
            response = sendDataForString(prop, null, null, 0);
141
        	InputStream result = sendParameters(prop);
142
            response = IOUtils.toString(result, encoding);
121 143
        } catch (Exception e) {
122 144
            throw new MetacatInaccessibleException(e.getMessage());
123 145
        }
......
154 176
        
155 177
        String response = null;
156 178
        try {
157
            response = sendDataForString(prop, null, null, 0);
179
        	InputStream result = sendParameters(prop);
180
            response = IOUtils.toString(result, encoding);
158 181
        } catch (Exception e) {
159 182
            throw new MetacatInaccessibleException(e.getMessage());
160 183
        }
......
180 203
        
181 204
        String response = null;
182 205
        try {
183
            response = sendDataForString(prop, null, null, 0);
206
        	InputStream result = sendParameters(prop);
207
            response = IOUtils.toString(result, encoding);
184 208
        } catch (Exception e) {
185 209
            throw new MetacatInaccessibleException(e.getMessage());
186 210
        }
......
213 237
        
214 238
        String response = null;
215 239
        try {
216
            response = sendDataForString(prop, null, null, 0);
240
        	InputStream result = sendParameters(prop);
241
            response = IOUtils.toString(result, encoding);
217 242
        } catch (Exception e) {
218 243
            throw new MetacatInaccessibleException(e.getMessage());
219 244
        }
......
251 276
        
252 277
        String response = null;
253 278
        try {
254
            response = sendDataForString(prop, null, null, 0);
279
        	InputStream result = sendParameters(prop);
280
            response = IOUtils.toString(result, encoding);
255 281
        } catch (Exception e) {
256 282
            throw new MetacatInaccessibleException(e.getMessage());
257 283
        }
......
286 312
        prop.put("docid", docid);
287 313
        InputStream response = null;
288 314
        try {
289
            response = sendData(prop, null, null, 0);
315
            response = sendParameters(prop);
290 316
        } catch (Exception e) {
291 317
            throw new MetacatInaccessibleException(e.getMessage());
292 318
        }
......
342 368
        
343 369
        InputStream response = null;
344 370
        try {
345
            response = sendData(prop, null, null, 0);
371
            response = sendParameters(prop);
346 372
        } catch (Exception e) {
347 373
            throw new MetacatInaccessibleException(e.getMessage());
348 374
        }
......
412 438
        
413 439
        InputStream response = null;
414 440
        try {
415
            response = sendData(prop, null, null, 0);
441
            response = sendParameters(prop);
416 442
        } catch (Exception e) {
417 443
            throw new MetacatInaccessibleException(e.getMessage());
418 444
        }
......
439 465
    	throws InsufficientKarmaException, MetacatException, IOException,
440 466
            MetacatInaccessibleException {
441 467

  
442
        Reader reader = null;
443 468
        String doctext = null;
444 469
        String schematext = null;
445 470
        try {
......
460 485
            prop.put("dtdtext", schematext);
461 486
        }
462 487

  
463
        if (sessionId != null) {
464
            prop.put("sessionid", sessionId);
465
        }
488
//        if (sessionId != null) {
489
//            prop.put("sessionid", sessionId);
490
//        }
466 491
        
467 492
        String response = null;
468 493
        try {
469
            response = sendDataForString(prop, null, null, 0);
494
        	InputStream result = sendParameters(prop);
495
            response = IOUtils.toString(result, encoding);
470 496
        } catch (Exception e) {
471 497
            throw new MetacatInaccessibleException(e.getMessage());
472 498
        }
......
501 527
    public String update(String docid, Reader xmlDocument, Reader schema)
502 528
    throws InsufficientKarmaException, MetacatException, IOException,
503 529
            MetacatInaccessibleException {
504
        Reader reader = null;
505 530
        String doctext = null;
506 531
        String schematext = null;
507 532
        try {
......
524 549
        
525 550
        String response = null;
526 551
        try {
527
            response = sendDataForString(prop, null, null, 0);
552
        	InputStream result = sendParameters(prop);
553
            response = IOUtils.toString(result, encoding);
528 554
        } catch (Exception e) {
529 555
            throw new MetacatInaccessibleException(e.getMessage());
530 556
        }
......
564 590
    throws InsufficientKarmaException, MetacatException, IOException,
565 591
            MetacatInaccessibleException {
566 592
        
567
        URL url = new URL(metacatUrl.trim());
568
        HttpMessage msg = new HttpMessage(url);
593
    	HttpClient httpclient = new DefaultHttpClient();
594
        httpclient.getParams().setParameter(
595
        		CoreProtocolPNames.PROTOCOL_VERSION, 
596
        	    HttpVersion.HTTP_1_1);
597
    	httpclient.getParams().setParameter(
598
    			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
599
    			encoding);
600
    	 
601
    	HttpPost post = new HttpPost(metacatUrl);
602
    	MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
603
    	 
604
    	// For File parameters
605
    	entity.addPart("datafile", new FileBody(file));
606
    	
569 607
        //set up properties
570
        Properties arg = new Properties();
571
        arg.put("action", "upload");
572
        arg.put("docid", docid);
608
        Properties prop = new Properties();
609
        prop.put("action", "upload");
610
        prop.put("docid", docid);
573 611
        
574
        Properties filenames = new Properties();
575
        String filename = file.getAbsolutePath();
576
        filenames.put("datafile", filename);
612
        // For usual String parameters
613
        Enumeration<Object> keys = prop.keys();
614
        while (keys.hasMoreElements()) {
615
        	String key = (String) keys.nextElement();
616
        	String value = prop.getProperty(key);
617
        	entity.addPart(key, new StringBody(value, Charset.forName(encoding)));
618
        }
577 619
        
620
        post.setHeader("Cookie", "JSESSIONID="+ this.sessionId);
621
        post.setEntity(entity);
622
    	
578 623
        String response = null;
579 624
        try {
580
            response = sendDataForString(arg, filenames, null, 0);
625
        	response = EntityUtils.toString(httpclient.execute(post).getEntity(), encoding);
626
        	httpclient.getConnectionManager().shutdown();
581 627
        } catch (Exception e) {
582 628
            throw new MetacatInaccessibleException(e.getMessage());
583 629
        }
......
618 664
            throws InsufficientKarmaException, MetacatException, IOException,
619 665
            MetacatInaccessibleException {
620 666
        
621
        URL url = new URL(metacatUrl.trim());
622
        HttpMessage msg = new HttpMessage(url);
667
    	HttpClient httpclient = new DefaultHttpClient();
668
        httpclient.getParams().setParameter(
669
        		CoreProtocolPNames.PROTOCOL_VERSION, 
670
        	    HttpVersion.HTTP_1_1);
671
    	httpclient.getParams().setParameter(
672
    			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
673
    			encoding);
674
    	 
675
    	HttpPost post = new HttpPost(metacatUrl);
676
    	MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
677
    	 
678
    	// For File parameters
679
    	InputStreamBody content = new InputStreamKnownSizeBody(fileData, filename, size);
680
    	entity.addPart("datafile", content);
681
    	
623 682
        //set up properties
624
        Properties arg = new Properties();
625
        arg.put("action", "upload");
626
        arg.put("docid", docid);
683
        Properties prop = new Properties();
684
        prop.put("action", "upload");
685
        prop.put("docid", docid);
627 686
        
628
        Properties filenames = new Properties();
629
        filenames.put("datafile", filename);
630
        
687
        // For usual String parameters
688
        Enumeration<Object> keys = prop.keys();
689
        while (keys.hasMoreElements()) {
690
        	String key = (String) keys.nextElement();
691
        	String value = prop.getProperty(key);
692
        	entity.addPart(key, new StringBody(value, Charset.forName(encoding)));
693
        }
694
    
695
        post.setHeader("Cookie", "JSESSIONID="+ this.sessionId);
696
        post.setEntity(entity);
697
    	
631 698
        String response = null;
632 699
        try {
633
            response = sendDataForString(arg, filenames, fileData, size);
700
        	response = EntityUtils.toString(httpclient.execute(post).getEntity(), encoding);
701
        	httpclient.getConnectionManager().shutdown();
634 702
        } catch (Exception e) {
635 703
            throw new MetacatInaccessibleException(e.getMessage());
636 704
        }
......
668 736
        
669 737
        String response = null;
670 738
        try {
671
            response = sendDataForString(prop, null, null, 0);
739
        	InputStream result = sendParameters(prop);
740
            response = IOUtils.toString(result, encoding);
672 741
        } catch (Exception e) {
673 742
            throw new MetacatInaccessibleException(e.getMessage());
674 743
        }
......
700 769
       
701 770
        String response = null;
702 771
        try {
703
            response = sendDataForString(prop, null, null, 0);
772
        	InputStream result = sendParameters(prop);
773
            response = IOUtils.toString(result, encoding);
704 774
        } catch (Exception e) {
705 775
            throw new MetacatInaccessibleException(e.getMessage());
706 776
        }
......
756 826
        
757 827
        String response = null;
758 828
        try {
759
            response = sendDataForString(prop, null, null, 0);
829
        	InputStream result = sendParameters(prop);
830
            response = IOUtils.toString(result, encoding);
760 831
        } catch (Exception e) {
761 832
            throw new MetacatInaccessibleException(e.getMessage());
762 833
        }
......
794 865
        
795 866
        String response = null;
796 867
        try {
797
            response = sendDataForString(prop, null, null, 0);
868
        	InputStream result = sendParameters(prop);
869
            response = IOUtils.toString(result, encoding);
798 870
        } catch (Exception e) {
799 871
            throw new MetacatInaccessibleException(e.getMessage());
800 872
        }
......
860 932
        
861 933
        String response = null;
862 934
        try {
863
            response = sendDataForString(prop, null, null, 0);
935
        	InputStream result = sendParameters(prop);
936
            response = IOUtils.toString(result, encoding);
864 937
            //parseRevisionResponse will return null if there is an
865 938
            //error that it can't handle
866 939
            String revStr = parserRevisionResponse(response);
......
892 965
        
893 966
        String response = null;
894 967
        try {
895
            response = sendDataForString(prop, null, null, 0);
968
        	InputStream result = sendParameters(prop);
969
            response = IOUtils.toString(result, encoding);
896 970
            // Check for an error condition
897 971
            if (response.indexOf("<error>") != -1) {
898 972
                throw new MetacatException(response);
......
927 1001
        
928 1002
        String response = null;
929 1003
        try {
930
            response = sendDataForString(prop, null, null, 0);
1004
        	InputStream result = sendParameters(prop);
1005
            response = IOUtils.toString(result, encoding);
931 1006
            // Check for an error condition
932 1007
            if (response.indexOf("<error>") != -1) {
933 1008
                throw new MetacatException(response);
......
970 1045
        
971 1046
        String response = null;
972 1047
        try {
973
            response = sendDataForString(prop, null, null, 0);
1048
        	InputStream result = sendParameters(prop);
1049
            response = IOUtils.toString(result, encoding);
974 1050
            // Check for an error condition
975 1051
            if (response.indexOf("<error>") != -1) {
976 1052
                throw new MetacatException(response);
977 1053
            } else {
978
                Reader responseReader = new StringReader(response);
979
                StringBuffer sb = new StringBuffer();
980
                char[] c = new char[1024];
981
                int numread = responseReader.read(c, 0, 1024);
982
                while(numread != -1) {
983
                    sb.append(new String(c, 0, numread));
984
                    numread = responseReader.read(c, 0, 1024);
985
                }
986
                
987
                String responseStr = sb.toString();
988
                if(responseStr.indexOf("true") != -1) {
1054
                if (response.indexOf("true") != -1) {
989 1055
                    return true;
990 1056
                }
991 1057
                return false;
......
1009 1075
     *      value = param name
1010 1076
     * @throws java.lang.Exception thrown
1011 1077
     */
1012
    synchronized public InputStream sendParameters(Properties args) throws Exception {
1013
        InputStream                     result = null;
1014
        URL                             url;
1015
        HttpMessage                     httpMsg;
1016
        
1017
        url = new URL(metacatUrl);
1018
        httpMsg = new HttpMessage(url);
1019
        httpMsg.setCookie("JSESSIONID="+this.sessionId);
1020
        result = httpMsg.sendPostParameters(args);
1021
        return(result);
1022
    }
1023

  
1024
    /************************************************************************
1025
     * PRIVATE METHODS
1026
     ************************************************************************/
1027
    
1028
    /**
1029
     * Send a request to metacat.
1030
     *
1031
     * @param prop the properties to be URL encoded and sent
1032
     * @param filename  the properties to be sent to Metacat
1033
     *                  in case of upload, otherwise null
1034
     * @param fileData  the inputStream for the file data to be sent to Metacat
1035
     *                  in case of upload, otherwise null
1036
     * @param size      the size of the data being sent to Metacat
1037
     *                  in case of upload, otherwise 0
1038
     */
1039
    synchronized private InputStream sendDataOnce(Properties args,
1040
            Properties filename,
1041
            InputStream fileData,
1042
            int size)
1043
            throws Exception {
1044
        InputStream returnStream = null;
1045
        URL url = new URL(metacatUrl);
1046
        HttpMessage msg = new HttpMessage(url);
1047
        msg.setCookie("JSESSIONID="+this.sessionId);
1048
        if (filename == null){
1049
            returnStream = msg.sendPostData(args);
1050
        } else if (fileData == null){
1051
            returnStream = msg.sendPostData(args, filename);
1052
        } else if (size > 0) {
1053
            returnStream = msg.sendPostData(args, filename, fileData, size);
1054
        } else {
1055
            throw new MetacatException("Invalid size specified for " +
1056
                    "the input stream being passed");
1057
        }
1058
        return returnStream;
1059
    }
1060
    
1061
    /**
1062
     * Send a request to Metacat
1063
     *
1064
     * @param args  the properties to be sent to Metacat
1065
     * @param filename  the properties to be sent to Metacat
1066
     *                  in case of upload, otherwise null
1067
     * @param fileData  the inputStream for the file data to be sent to Metacat
1068
     *                  in case of upload, otherwise null
1069
     * @param size      the size of the data being sent to Metacat
1070
     *                  in case of upload, otherwise 0
1071
     * @return      InputStream as returned by Metacat
1072
     */
1073
    synchronized public InputStream sendData(Properties args,
1074
            Properties filename,
1075
            InputStream fileData,
1076
            int size)
1077
            throws Exception {
1078
        InputStream returnStream = null;
1079
        /*
1080
            Note:  The reason that there are three try statements all executing
1081
            the same code is that there is a problem with the initial connection
1082
            using the HTTPClient protocol handler.  These try statements make
1083
            sure that a connection is made because it gives each connection a
1084
            2nd and 3rd chance to work before throwing an error.
1085
            THIS IS A TOTAL HACK.  THIS NEEDS TO BE LOOKED INTO AFTER THE BETA1
1086
            RELEASE OF MORPHO!!!  cwb (7/24/01)
1087
         */
1078
    synchronized public InputStream sendParameters(Properties prop) throws Exception {
1079
        InputStream result = null;
1088 1080
        try {
1089
            return sendDataOnce(args, filename, fileData, size);
1090
        } catch (Exception e) {
1091
            try {
1092
                return sendDataOnce(args, filename, fileData, size);
1093
            } catch (Exception e2) {
1094
                try {
1095
                    return sendDataOnce(args, filename, fileData, size);
1096
                } catch (Exception e3) {
1097
                    System.err.println(
1098
                            "Failed to send data to metacat 3 times: " + e3.getMessage());
1099
                    System.err.println("metacaturl: " + metacatUrl);
1100
                    throw e3;
1101
                }
1081
        	HttpClient httpclient = new DefaultHttpClient();
1082
            httpclient.getParams().setParameter(
1083
            		CoreProtocolPNames.PROTOCOL_VERSION, 
1084
            	    HttpVersion.HTTP_1_1);
1085
        	httpclient.getParams().setParameter(
1086
        			CoreProtocolPNames.HTTP_CONTENT_CHARSET, 
1087
        			encoding);
1088
            HttpPost post = new HttpPost(metacatUrl);
1089
            //set the params
1090
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
1091
            Enumeration<Object> keys = prop.keys();
1092
            while (keys.hasMoreElements()) {
1093
            	String key = (String) keys.nextElement();
1094
            	String value = prop.getProperty(key);
1095
            	NameValuePair nvp = new BasicNameValuePair(key, value);
1096
            	nameValuePairs.add(nvp);
1102 1097
            }
1103
        }
1104
    }
1105
    
1106
    /**
1107
     * Send a request to Metacat
1108
     *
1109
     * @param args      the properties to be sent to Metacat
1110
     * @param filename  the properties to be sent to Metacat
1111
     *                  in case of upload, otherwise null
1112
     * @param fileData  the inputStream for the file data to be sent to Metacat
1113
     *                  in case of upload, otherwise null
1114
     * @param size      the size of the data being sent to Metacat
1115
     *                  in case of upload, otherwise 0
1116
     * @return          a string as returned by Metacat
1117
     */
1118
    synchronized private String sendDataForString(Properties args,
1119
            Properties filename,
1120
            InputStream fileData,
1121
            int size)
1122
            throws Exception {
1123
        String response = null;
1124
        
1125
        try {
1126
            InputStreamReader returnStream =
1127
                    new InputStreamReader(sendData(args, filename,
1128
                    fileData, size));
1129
            StringWriter sw = new StringWriter();
1130
            int len;
1131
            char[] characters = new char[512];
1132
            while ((len = returnStream.read(characters, 0, 512)) != -1) {
1133
                sw.write(characters, 0, len);
1134
            }
1135
            returnStream.close();
1136
            response = sw.toString();
1137
            sw.close();
1098
            post.setEntity(new UrlEncodedFormEntity(nameValuePairs, encoding));
1099
            post.setHeader("Cookie", "JSESSIONID="+ this.sessionId);
1100
            HttpResponse httpResponse = httpclient.execute(post);
1101
            result = httpResponse.getEntity().getContent();
1102
            //httpclient.getConnectionManager().shutdown();
1138 1103
        } catch (Exception e) {
1139
            throw e;
1104
            throw new MetacatInaccessibleException(e.getMessage());
1140 1105
        }
1141
        return response;
1106
        return result;
1142 1107
    }
1143 1108
    
1144 1109
    /*
......
1189 1154
        }
1190 1155
        return(result);
1191 1156
    }
1157

  
1158
	public String getEncoding() {
1159
		return encoding;
1160
	}
1161

  
1162
	public void setEncoding(String encoding) {
1163
		this.encoding = encoding;
1164
	}
1192 1165
    
1193 1166
}
1167

  
1168
class InputStreamKnownSizeBody extends InputStreamBody {
1169
	private int length;
1170

  
1171
	public InputStreamKnownSizeBody(
1172
			final InputStream in, 
1173
			final String filename,
1174
			final int length) {
1175
		super(in, filename);
1176
		this.length = length;
1177
	}
1178

  
1179
	@Override
1180
	public long getContentLength() {
1181
		return this.length;
1182
	}
1183
}
src/edu/ucsb/nceas/metacat/client/Metacat.java
320 320
     * @throws MetacatException when an error occurs
321 321
     */
322 322
    public boolean isRegistered(String docid) throws MetacatException;
323
    
324
    /**
325
     * Returns the character encoding used used when communicating with Metacat.
326
     * @return character encoding name
327
     */
328
    public String getEncoding();
329
    
330
    /**
331
     * Returns the character encoding used used when communicating with Metacat.
332
     * @param encoding The encoding (i.e. "UTF-8")
333
     */
334
    public void setEncoding(String encoding);
335
    
323 336
}
build.xml
357 357
		<condition property="utilities.required">
358 358
			<or>
359 359
				<not>
360
					<available file="lib/httpclient.jar" />
361
				</not>
362
				<not>
363 360
					<available file="lib/utilities.jar" />
364 361
				</not>
365 362
				<not>
......
469 466
		if="utilities.required">
470 467
		<ant dir="${utilitiesdir}" target="clean" inheritAll="false" />
471 468
		<ant dir="${utilitiesdir}" target="jar" inheritAll="false" />
472
		<copy file="${utilitiesdir}/lib/httpclient.jar" todir="lib" />
473 469
		<copy file="${utilitiesdir}/build/utilities.jar" todir="lib" />
474 470
	</target>
475 471

  
......
1141 1137
		<!-- use the ant "junit" task to run JUnit tests. -->
1142 1138
		<junit printsummary="yes" haltonfailure="no" fork="yes"
1143 1139
			haltonerror="no" showoutput="yes">
1144
			<!--<jvmarg value="-Djava.protocol.handler.pkgs=HTTPClient"/>-->
1145 1140
			<jvmarg value="-Dfile.encoding=UTF-8"/>
1146 1141
			<classpath>
1147 1142
				<path refid="test.classpath" />
......
1185 1180
		</junit>
1186 1181
	</target>
1187 1182

  
1188
	<target name="gethttpclient" depends="prepare"
1189
		description="checks out and compiles morpho httpclient code">
1190
		<!--mkdir dir="${morphosourcedir}" -->
1191
		<cvs cvsRoot="${cvsroot}" package="morpho" dest="." />
1192
		<ant dir="${morphosourcedir}" inheritAll="false" target="jar" />
1193
	</target>
1194

  
1195 1183
	<!-- run this target, we need to check out a another morpho souce dir was checked out. -->
1196 1184
	<target name="nettest" depends="install"
1197 1185
		description="compiles and runs the metacatnettest code">
......
1251 1239

  
1252 1240
	<target name="localclean" depends="clean"
1253 1241
		description="deletes even jars that depend on external sources such as stylesheets and jars">
1254
		<delete file="lib/httpclient.jar" />
1255 1242
		<delete file="lib/utilities.jar" />
1256 1243
	</target>
1257 1244

  
......
1466 1453
		<!--<include name="GenCastor.class" />-->
1467 1454
	</patternset>
1468 1455
	<patternset id="lib.jars">
1469
		<include name="httpclient.jar" />
1470 1456
		<include name="log4j-1.2.12.jar" />
1471 1457
		<include name="utilities.jar" />
1472 1458
		<include name="xerces*.jar" />

Also available in: Unified diff