]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
04ebb6857f7d521c7733d55e4278aa016e381d5f
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23
24 package org.collectionspace.services.IntegrationTests.xmlreplay;
25
26 import org.apache.commons.httpclient.Header;
27 import org.apache.commons.httpclient.HttpClient;
28 import org.apache.commons.httpclient.methods.DeleteMethod;
29 import org.apache.commons.httpclient.methods.GetMethod;
30 import org.apache.commons.httpclient.methods.PostMethod;
31 import org.apache.commons.io.FileUtils;
32
33 import java.io.BufferedReader;
34 import java.io.File;
35 import java.io.InputStreamReader;
36 import java.io.OutputStreamWriter;
37 import java.net.HttpURLConnection;
38 import java.net.URL;
39 import java.util.Arrays;
40 import java.util.List;
41 import java.util.Map;
42
43 import org.collectionspace.services.common.api.Tools;
44
45 /**
46  *   @author Laramie Crocker
47  */
48 public class XmlReplayTransport {
49
50     private static String BOUNDARY = "34d97c83-0d61-4958-80ab-6bf8d362290f";
51         private static String DD = "--";
52         private static String CRLF = "\r\n";
53
54     private static String formatAuth(String authForTest) {
55         if (authForTest.startsWith("Bearer ")) {
56             return authForTest;
57         }
58         
59         return ("Basic " + authForTest);
60     }
61     
62     public static ServiceResult doGET(String urlString, String authForTest, String fromTestID) throws Exception {
63         ServiceResult pr = new ServiceResult();
64         pr.fromTestID = fromTestID;
65         pr.method = "GET";
66         //HACK for speed testing.
67         //pr.CSID = "2";
68         //pr.overrideGotExpectedResult();
69         //if (true) return pr;
70         //END-HACK
71         HttpClient client = new HttpClient();
72         GetMethod getMethod = new GetMethod(urlString);
73         getMethod.addRequestHeader("Accept", "multipart/mixed");
74         getMethod.addRequestHeader("Accept", "application/xml");
75         getMethod.setRequestHeader("Authorization", formatAuth(authForTest)); //"dGVzdDp0ZXN0");
76         getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
77         try {
78             int statusCode1 = client.executeMethod(getMethod);
79             pr.responseCode = statusCode1;
80             pr.result = getMethod.getResponseBodyAsString();
81             pr.responseMessage = getMethod.getStatusText();
82             Header[] headers = getMethod.getResponseHeaders();
83             pr.responseHeaders = Arrays.copyOf(headers, headers.length);
84             Header hdr = getMethod.getResponseHeader("CONTENT-TYPE");
85             if (hdr!=null){
86                 String hdrStr = hdr.toExternalForm();
87                 pr.boundary = PayloadLogger.parseBoundary(hdrStr);
88             }
89             pr.contentLength = getMethod.getResponseContentLength();
90             getMethod.releaseConnection();
91         } catch (Throwable t){
92             //System.err.println("ERROR getting content from response: "+t);
93             pr.error = t.toString();
94         }
95         return pr;
96     }
97
98     public static ServiceResult doDELETE(String urlString, String authForTest, String testID, String fromTestID) throws Exception {
99         ServiceResult pr = new ServiceResult();
100         pr.failureReason = "";
101         pr.method = "DELETE";
102         pr.fullURL = urlString;
103         pr.fromTestID = fromTestID;
104         if (Tools.isEmpty(urlString)){
105             pr.error = "url was empty.  Check the result for fromTestID: "+fromTestID+". currentTest: "+testID;
106             return pr;
107         }
108         HttpClient client = new HttpClient();
109         DeleteMethod deleteMethod = new DeleteMethod(urlString);
110         deleteMethod.setRequestHeader("Accept", "multipart/mixed");
111         deleteMethod.addRequestHeader("Accept", "application/xml");
112         deleteMethod.setRequestHeader("Authorization", formatAuth(authForTest));
113         if (Tools.notBlank(fromTestID)) {
114                 deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
115         }
116         int statusCode1 = 0;
117         String res = "";
118         try {
119             statusCode1 = client.executeMethod(deleteMethod);
120             pr.responseCode = statusCode1;
121             //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());
122             pr.responseMessage = deleteMethod.getStatusText();
123             res = deleteMethod.getResponseBodyAsString();
124             deleteMethod.releaseConnection();
125         } catch (Throwable t){
126             pr.error = t.toString();
127         }
128         pr.result = res;
129         pr.responseCode = statusCode1;
130         return pr;
131     }
132
133     public static ServiceResult doLIST(String urlString, String listQueryParams, String authForTest, String fromTestID) throws Exception {
134         //String u = Tools.glue(urlString, "/", "items/");
135         if (Tools.notEmpty(listQueryParams)){
136             urlString = Tools.glue(urlString, "?", listQueryParams);
137         }
138         return doGET(urlString, authForTest, fromTestID);
139     }
140
141     public static final String MULTIPART_MIXED = "multipart/mixed";
142     public static final String APPLICATION_XML = "application/xml";
143
144     /** Use this overload for multipart messages. */
145     /**
146     public static ServiceResult doPOST_PUTFromXML_Multipart(List<String> filesList,
147                                                             List<String> partsList,
148                                                             List<Map<String,String>> varsList,
149                                                             String protoHostPort,
150                                                             String uri,
151                                                             String method,
152                                                             XmlReplayEval evalStruct,
153                                                             String authForTest,
154                                                             String fromTestID)
155                                                              throws Exception {
156         if (  filesList==null||filesList.size()==0
157             ||partsList==null||partsList.size()==0
158             ||(partsList.size() != filesList.size())){
159             throw new Exception("filesList and partsList must not be empty and must have the same number of items each.");
160         }
161         String content = DD + BOUNDARY;
162         Map<String, String> contentRaw = new HashMap<String, String>();
163         for (int i=0; i<partsList.size(); i++){
164             String fileName = filesList.get(i);
165             String commonPartName = partsList.get(i);
166             byte[] b = FileUtils.readFileToByteArray(new File(fileName));
167             String xmlString = new String(b);
168
169             xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, varsList.get(i), evalStruct.jexl, evalStruct.jc);
170             contentRaw.put(commonPartName, xmlString);
171             content = content + CRLF + "label: "+commonPartName + CRLF
172                               + "Content-Type: application/xml" + CRLF
173                               + CRLF
174                               + xmlString + CRLF
175                               + DD + BOUNDARY;
176         }
177         content = content + DD;
178         String urlString = protoHostPort+uri;
179         return doPOST_PUT(urlString, content, contentRaw, BOUNDARY, method, MULTIPART_MIXED, authForTest, fromTestID); //method is POST or PUT.
180     }
181     */
182
183     /** Use this overload for NON-multipart messages, that is, regular POSTs. */
184         public static ServiceResult doPOST_PUTFromXML(
185                         String fileName, 
186                         Map<String, 
187                         String> vars, 
188                         String protoHostPort,
189                         String uri, 
190                         String method, 
191                         String contentType, 
192                         XmlReplayEval evalStruct, 
193                         String authForTest,
194                         String fromTestID) throws Exception {
195                 byte[] b = FileUtils.readFileToByteArray(new File(fileName));
196                 String xmlString = new String(b);
197                 String contentRaw = xmlString;
198                 //
199                 // Add the test ID so we can substitute instances of "${testID}" in the
200                 // payload with the test ID.
201                 //
202                 String testId = fromTestID.split("\\.")[1]; // Get the unqualified (without the group ID part) test name
203                 vars.put("Test.ID", testId);
204
205                 xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, vars, evalStruct.jexl, evalStruct.jc);
206                 String urlString = protoHostPort + uri;
207                 
208                 return doPOST_PUT(urlString, xmlString, contentRaw, BOUNDARY, method, contentType, authForTest, fromTestID); // method  is POST or PUT.
209         }
210
211         //HACK for speed testing in doPOST_PUT.
212         //  Result: XmlReplay takes 9ms to process one test
213         // right up to the point of actually firing an HTTP request.
214         // or ~ 120 records per second.
215         //result.CSID = "2";
216         //result.overrideGotExpectedResult();
217         //if (true) return result;
218         //END-HACK
219
220     private static ServiceResult doPOST_PUT(
221                 String urlString,
222                 String content,
223                         String contentRaw,
224                         String boundary,
225                         String method,
226                         String contentType,
227                         String authForTest,
228                         String fromTestID) throws Exception {
229         ServiceResult result = new ServiceResult();
230         result.method = method;
231         
232         String deleteURL = "";
233         String location = "";
234         
235         try {
236             URL url = new URL(urlString);
237             HttpURLConnection conn;
238             conn = (HttpURLConnection) url.openConnection();
239
240             if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){
241                 conn.setRequestProperty("Accept", "multipart/mixed");
242                 conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary);
243             } else {
244                 conn.setRequestProperty("Accept", "application/xml");
245                 conn.setRequestProperty("content-type", contentType);
246             }
247             conn.setRequestProperty("Authorization", formatAuth(authForTest));  //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0"
248             conn.setRequestProperty("Connection", "close");
249             conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID);
250             conn.setDoOutput(true);
251             conn.setDoInput(true);
252             conn.setRequestMethod(method); // "POST" or "PUT"
253             OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
254             wr.write(content);
255             wr.flush();
256
257             try {
258                 result.requestPayload = content;
259                 result.requestPayloadsRaw = contentRaw;
260                 result.responseCode = conn.getResponseCode();
261                 //System.out.println("responseCode: "+result.responseCode);
262                 if (400 <= result.responseCode && result.responseCode <= 499){
263                     return result;
264                 }
265                 readStream(conn, result);
266             } catch (Throwable t){
267                 //System.err.println("ERROR getting content from response: "+t);
268                 result.error = t.toString();
269             }
270             wr.close();
271
272             Map<String, List<String>> headers = conn.getHeaderFields();
273             List<String> locations = headers.get("Location");
274             if (locations != null){
275                 String locationZero = locations.get(0);
276                 if (locationZero != null){
277                     String[] segments = locationZero.split("/");
278                     location = segments[segments.length - 1];
279                     deleteURL = Tools.glue(urlString, "/", location);
280                 }
281             }
282             result.location = location;
283             result.deleteURL = deleteURL;
284             result.CSID = location;
285         } catch (Throwable t2){
286             result.error = "ERROR in XmlReplayTransport: "+t2;
287         }
288         
289         return result;
290     }
291
292     @Deprecated
293     /**
294      * This method is not used and is a good candidate for removal.
295      * 
296      * @param urlString
297      * @param content
298      * @param contentRaw
299      * @param boundary
300      * @param method
301      * @param contentType
302      * @param authForTest
303      * @param fromTestID
304      * @return
305      * @throws Exception
306      */
307         private static ServiceResult doPOST_PUT_PostMethod(
308                         String urlString, 
309                         String content, 
310                         Map<String, String> contentRaw,
311                         String boundary, 
312                         String method, 
313                         String contentType, 
314                         String authForTest, 
315                         String fromTestID)
316                         throws Exception {
317                 ServiceResult result = new ServiceResult();
318                 result.method = method;
319                 String deleteURL = "";
320                 String location = "";
321                 
322                 try {
323                         HttpClient client = new HttpClient();
324                         PostMethod postMethod = new PostMethod(urlString);
325                         postMethod.setRequestHeader("Accept", "multipart/mixed");
326                         postMethod.addRequestHeader("Accept", "application/xml");
327                         postMethod.setRequestHeader("Authorization", formatAuth(authForTest));
328                         postMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
329                         // this method takes an array of params. Not sure what they expect
330                         // us to do with a raw post:
331                         // postMethod.setRequestBody();
332                         int statusCode1 = 0;
333                         String res = "";
334                         try {
335                                 statusCode1 = client.executeMethod(postMethod);
336                                 result.responseCode = statusCode1;
337                                 // System.out.println("statusCode: "+statusCode1+" statusLine
338                                 // ==>" + postMethod.getStatusLine());
339                                 result.responseMessage = postMethod.getStatusText();
340                                 res = postMethod.getResponseBodyAsString();
341                                 Header[] headers = postMethod.getResponseHeaders("Location");
342                                 if (headers.length > 0) {
343                                         System.out.println("headers[0]:  " + headers[0]);
344                                         String locationZero = headers[0].getValue();
345                                         if (locationZero != null) {
346                                                 String[] segments = locationZero.split("/");
347                                                 location = segments[segments.length - 1];
348                                                 deleteURL = Tools.glue(urlString, "/", location);
349                                         }
350                                 }
351                                 postMethod.releaseConnection();
352                         } catch (Throwable t) {
353                                 result.error = t.toString();
354                         }
355                         result.result = res;
356                         result.location = location;
357                         result.deleteURL = deleteURL;
358                         result.CSID = location;
359                 } catch (Throwable t2) {
360                         result.error = "ERROR in XmlReplayTransport: " + t2;
361                 }
362                 
363                 return result;
364         }
365
366     private static void readStream(HttpURLConnection  conn, ServiceResult result) throws Throwable {
367         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
368         try {
369                 String line;
370                 StringBuffer sb = new StringBuffer();
371                 while ((line = rd.readLine()) != null) {
372                     sb.append(line).append("\r\n");
373                 }
374                 String msg = sb.toString();
375                 result.result = msg;
376                 result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE"));
377         } finally {
378             rd.close();
379         }
380     }
381
382 }