]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
efd9cbed4a51cb33805774fa60f1d85f67c53b66
[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         deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
114         int statusCode1 = 0;
115         String res = "";
116         try {
117             statusCode1 = client.executeMethod(deleteMethod);
118             pr.responseCode = statusCode1;
119             //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());
120             pr.responseMessage = deleteMethod.getStatusText();
121             res = deleteMethod.getResponseBodyAsString();
122             deleteMethod.releaseConnection();
123         } catch (Throwable t){
124             pr.error = t.toString();
125         }
126         pr.result = res;
127         pr.responseCode = statusCode1;
128         return pr;
129     }
130
131     public static ServiceResult doLIST(String urlString, String listQueryParams, String authForTest, String fromTestID) throws Exception {
132         //String u = Tools.glue(urlString, "/", "items/");
133         if (Tools.notEmpty(listQueryParams)){
134             urlString = Tools.glue(urlString, "?", listQueryParams);
135         }
136         return doGET(urlString, authForTest, fromTestID);
137     }
138
139     public static final String MULTIPART_MIXED = "multipart/mixed";
140     public static final String APPLICATION_XML = "application/xml";
141
142     /** Use this overload for multipart messages. */
143     /**
144     public static ServiceResult doPOST_PUTFromXML_Multipart(List<String> filesList,
145                                                             List<String> partsList,
146                                                             List<Map<String,String>> varsList,
147                                                             String protoHostPort,
148                                                             String uri,
149                                                             String method,
150                                                             XmlReplayEval evalStruct,
151                                                             String authForTest,
152                                                             String fromTestID)
153                                                              throws Exception {
154         if (  filesList==null||filesList.size()==0
155             ||partsList==null||partsList.size()==0
156             ||(partsList.size() != filesList.size())){
157             throw new Exception("filesList and partsList must not be empty and must have the same number of items each.");
158         }
159         String content = DD + BOUNDARY;
160         Map<String, String> contentRaw = new HashMap<String, String>();
161         for (int i=0; i<partsList.size(); i++){
162             String fileName = filesList.get(i);
163             String commonPartName = partsList.get(i);
164             byte[] b = FileUtils.readFileToByteArray(new File(fileName));
165             String xmlString = new String(b);
166
167             xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, varsList.get(i), evalStruct.jexl, evalStruct.jc);
168             contentRaw.put(commonPartName, xmlString);
169             content = content + CRLF + "label: "+commonPartName + CRLF
170                               + "Content-Type: application/xml" + CRLF
171                               + CRLF
172                               + xmlString + CRLF
173                               + DD + BOUNDARY;
174         }
175         content = content + DD;
176         String urlString = protoHostPort+uri;
177         return doPOST_PUT(urlString, content, contentRaw, BOUNDARY, method, MULTIPART_MIXED, authForTest, fromTestID); //method is POST or PUT.
178     }
179     */
180
181     /** Use this overload for NON-multipart messages, that is, regular POSTs. */
182     public static ServiceResult doPOST_PUTFromXML(String fileName,
183                                                       Map<String,String> vars,
184                                                       String protoHostPort,
185                                                       String uri,
186                                                       String method,
187                                                       String contentType,
188                                                       XmlReplayEval evalStruct,
189                                                       String authForTest,
190                                                       String fromTestID)
191     throws Exception {
192         byte[] b = FileUtils.readFileToByteArray(new File(fileName));
193         String xmlString = new String(b);
194         String contentRaw = xmlString;
195         xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, vars, evalStruct.jexl, evalStruct.jc);
196         String urlString = protoHostPort+uri;
197         return doPOST_PUT(urlString, xmlString, contentRaw, BOUNDARY, method, contentType, authForTest, fromTestID); //method is POST or PUT.
198     }
199
200         //HACK for speed testing in doPOST_PUT.
201         //  Result: XmlReplay takes 9ms to process one test
202         // right up to the point of actually firing an HTTP request.
203         // or ~ 120 records per second.
204         //result.CSID = "2";
205         //result.overrideGotExpectedResult();
206         //if (true) return result;
207         //END-HACK
208
209     public static ServiceResult doPOST_PUT(String urlString,
210                                                                      String content,
211                                                                      String contentRaw,
212                                                                      String boundary,
213                                                                      String method,
214                                                                      String contentType,
215                                                                      String authForTest,
216                                                                      String fromTestID) throws Exception {
217         ServiceResult result = new ServiceResult();
218         result.method = method;
219         String deleteURL = "";
220         String location = "";
221         try {
222             URL url = new URL(urlString);
223             HttpURLConnection conn;
224             conn = (HttpURLConnection) url.openConnection();
225
226             if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){
227                 conn.setRequestProperty("Accept", "multipart/mixed");
228                 conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary);
229             } else {
230                 conn.setRequestProperty("Accept", "application/xml");
231                 conn.setRequestProperty("content-type", contentType);
232             }
233             conn.setRequestProperty("Authorization", formatAuth(authForTest));  //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0"
234             conn.setRequestProperty("Connection", "close");
235             conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID);
236             conn.setDoOutput(true);
237             conn.setDoInput(true);
238             conn.setRequestMethod(method); // "POST" or "PUT"
239             OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
240             wr.write(content);
241             wr.flush();
242
243             try {
244                 result.requestPayload = content;
245                 result.requestPayloadsRaw = contentRaw;
246                 result.responseCode = conn.getResponseCode();
247                 //System.out.println("responseCode: "+result.responseCode);
248                 if (400 <= result.responseCode && result.responseCode <= 499){
249                     return result;
250                 }
251                 readStream(conn, result);
252             } catch (Throwable t){
253                 //System.err.println("ERROR getting content from response: "+t);
254                 result.error = t.toString();
255             }
256             wr.close();
257
258             Map<String, List<String>> headers = conn.getHeaderFields();
259             List<String> locations = headers.get("Location");
260             if (locations != null){
261                 String locationZero = locations.get(0);
262                 if (locationZero != null){
263                     String[] segments = locationZero.split("/");
264                     location = segments[segments.length - 1];
265                     deleteURL = Tools.glue(urlString, "/", location);
266                 }
267             }
268             result.location = location;
269             result.deleteURL = deleteURL;
270             result.CSID = location;
271         } catch (Throwable t2){
272             result.error = "ERROR in XmlReplayTransport: "+t2;
273         }
274         return result;
275     }
276
277     public static ServiceResult doPOST_PUT_PostMethod(String urlString, String content, Map<String,String> contentRaw,
278                                            String boundary, String method, String contentType,
279                                            String authForTest, String fromTestID) throws Exception {
280         ServiceResult result = new ServiceResult();
281         result.method = method;
282         String deleteURL = "";
283         String location = "";
284         try {
285             HttpClient client = new HttpClient();
286             PostMethod postMethod = new PostMethod(urlString);
287             postMethod.setRequestHeader("Accept", "multipart/mixed");
288             postMethod.addRequestHeader("Accept", "application/xml");
289             postMethod.setRequestHeader("Authorization", formatAuth(authForTest));
290             postMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);
291             //this method takes an array of params.  Not sure what they expect us to do with a raw post:
292             //   postMethod.setRequestBody();
293             int statusCode1 = 0;
294             String res = "";
295             try {
296                 statusCode1 = client.executeMethod(postMethod);
297                 result.responseCode = statusCode1;
298                 //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + postMethod.getStatusLine());
299                 result.responseMessage = postMethod.getStatusText();
300                 res = postMethod.getResponseBodyAsString();
301                 Header[] headers = postMethod.getResponseHeaders("Location");
302                 if (headers.length>0) {
303                     System.out.println("headers[0]:  "+headers[0]);
304                     String locationZero = headers[0].getValue();
305                     if (locationZero != null){
306                         String[] segments = locationZero.split("/");
307                         location = segments[segments.length - 1];
308                         deleteURL = Tools.glue(urlString, "/", location);
309                     }
310                 }
311                 postMethod.releaseConnection();
312             } catch (Throwable t){
313                 result.error = t.toString();
314             }
315             result.result = res;
316             result.location = location;
317             result.deleteURL = deleteURL;
318             result.CSID = location;
319         } catch (Throwable t2){
320             result.error = "ERROR in XmlReplayTransport: "+t2;
321         }
322         return result;
323     }
324
325     private static void readStream(HttpURLConnection  conn, ServiceResult result) throws Throwable {
326         BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
327         try {
328                 String line;
329                 StringBuffer sb = new StringBuffer();
330                 while ((line = rd.readLine()) != null) {
331                     sb.append(line).append("\r\n");
332                 }
333                 String msg = sb.toString();
334                 result.result = msg;
335                 result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE"));
336         } finally {
337             rd.close();
338         }
339     }
340
341 }