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:

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
}

Also available in: Unified diff