]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
78a411e417ba42dffe1e2013fc5427e3f3c6939a
[tmp/jakarta-migration.git] /
1 /**\r
2  * This document is a part of the source code and related artifacts\r
3  * for CollectionSpace, an open source collections management system\r
4  * for museums and related institutions:\r
5  *\r
6  * http://www.collectionspace.org\r
7  * http://wiki.collectionspace.org\r
8  *\r
9  * Copyright (c) 2009 Regents of the University of California\r
10  *\r
11  * Licensed under the Educational Community License (ECL), Version 2.0.\r
12  * You may not use this file except in compliance with this License.\r
13  *\r
14  * You may obtain a copy of the ECL 2.0 License at\r
15  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
16  *\r
17  *  Unless required by applicable law or agreed to in writing, software\r
18  *  distributed under the License is distributed on an "AS IS" BASIS,\r
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
20  *  See the License for the specific language governing permissions and\r
21  *  limitations under the License.\r
22  */\r
23 \r
24 package org.collectionspace.services.IntegrationTests.xmlreplay;\r
25 \r
26 import org.apache.commons.httpclient.Header;\r
27 import org.apache.commons.httpclient.HttpClient;\r
28 import org.apache.commons.httpclient.methods.DeleteMethod;\r
29 import org.apache.commons.httpclient.methods.GetMethod;\r
30 import org.apache.commons.io.FileUtils;\r
31 \r
32 import java.io.BufferedReader;\r
33 import java.io.File;\r
34 import java.io.InputStreamReader;\r
35 import java.io.OutputStreamWriter;\r
36 import java.net.HttpURLConnection;\r
37 import java.net.URL;\r
38 import java.util.Arrays;\r
39 import java.util.List;\r
40 import java.util.Map;\r
41 \r
42 import org.collectionspace.services.IntegrationTests.xmlreplay.ServiceResult;\r
43 \r
44 /**\r
45  *   @author Laramie Crocker\r
46  */\r
47 public class XmlReplayTransport {\r
48 \r
49     private static String BOUNDARY = "34d97c83-0d61-4958-80ab-6bf8d362290f";\r
50         private static String DD = "--";\r
51         private static String CRLF = "\r\n";\r
52 \r
53     public static ServiceResult doGET(String urlString, String authForTest, String fromTestID) throws Exception {\r
54         HttpClient client = new HttpClient();\r
55         GetMethod getMethod = new GetMethod(urlString);\r
56         getMethod.addRequestHeader("Accept", "multipart/mixed");\r
57         getMethod.addRequestHeader("Accept", "application/xml");\r
58         getMethod.setRequestHeader("Authorization", "Basic " + authForTest); //"dGVzdDp0ZXN0");\r
59         getMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);\r
60         ServiceResult pr = new ServiceResult();\r
61 \r
62         int statusCode1 = client.executeMethod(getMethod);\r
63         pr.responseCode = statusCode1;\r
64         pr.fromTestID = fromTestID;\r
65         pr.method = "GET";\r
66         try {\r
67             pr.result = getMethod.getResponseBodyAsString();\r
68             pr.responseMessage = getMethod.getStatusText();\r
69             Header[] headers = getMethod.getResponseHeaders();\r
70             pr.responseHeaders = Arrays.copyOf(headers, headers.length);\r
71             Header hdr = getMethod.getResponseHeader("CONTENT-TYPE");\r
72             if (hdr!=null){\r
73                 String hdrStr = hdr.toExternalForm();\r
74                 pr.boundary = PayloadLogger.parseBoundary(hdrStr);\r
75             }\r
76             pr.contentLength = getMethod.getResponseContentLength();\r
77         } catch (Throwable t){\r
78             //System.err.println("ERROR getting content from response: "+t);\r
79             pr.error = t.toString();\r
80         }\r
81 \r
82 \r
83         getMethod.releaseConnection();\r
84         return pr;\r
85     }\r
86 \r
87     public static ServiceResult doDELETE(String urlString, String authForTest, String testID, String fromTestID) throws Exception {\r
88         ServiceResult pr = new ServiceResult();\r
89         pr.method = "DELETE";\r
90         pr.fullURL = urlString;\r
91         pr.fromTestID = fromTestID;\r
92         if (Tools.isEmpty(urlString)){\r
93             pr.error = "url was empty.  Check the result for fromTestID: "+fromTestID+". currentTest: "+testID;\r
94             return pr;\r
95         }\r
96         HttpClient client = new HttpClient();\r
97         DeleteMethod deleteMethod = new DeleteMethod(urlString);\r
98         deleteMethod.setRequestHeader("Accept", "multipart/mixed");\r
99         deleteMethod.addRequestHeader("Accept", "application/xml");\r
100         deleteMethod.setRequestHeader("Authorization", "Basic " + authForTest);\r
101         deleteMethod.setRequestHeader("X-XmlReplay-fromTestID", fromTestID);\r
102         int statusCode1 = 0;\r
103         String res = "";\r
104         try {\r
105             statusCode1 = client.executeMethod(deleteMethod);\r
106             pr.responseCode = statusCode1;\r
107             //System.out.println("statusCode: "+statusCode1+" statusLine ==>" + deleteMethod.getStatusLine());\r
108             pr.responseMessage = deleteMethod.getStatusText();\r
109             res = deleteMethod.getResponseBodyAsString();\r
110             deleteMethod.releaseConnection();\r
111         } catch (Throwable t){\r
112             pr.error = t.toString();\r
113         }\r
114         pr.result = res;\r
115         pr.responseCode = statusCode1;\r
116         return pr;\r
117     }\r
118 \r
119     public static ServiceResult doLIST(String urlString, String listQueryParams, String authForTest, String fromTestID) throws Exception {\r
120         //String u = Tools.glue(urlString, "/", "items/");\r
121         if (Tools.notEmpty(listQueryParams)){\r
122             urlString = Tools.glue(urlString, "?", listQueryParams);\r
123         }\r
124         return doGET(urlString, authForTest, fromTestID);\r
125     }\r
126 \r
127     public static final String MULTIPART_MIXED = "multipart/mixed";\r
128     public static final String APPLICATION_XML = "application/xml";\r
129 \r
130     /** Use this overload for multipart messages. */\r
131     public static ServiceResult doPOST_PUTFromXML_Multipart(List<String> filesList,\r
132                                                                       List<String> partsList,\r
133                                                                       String protoHostPort,\r
134                                                                       String uri,\r
135                                                                       String method,\r
136                                                                       XmlReplayEval evalStruct,\r
137                                                                       String authForTest,\r
138                                                                       String fromTestID)\r
139                                                                       throws Exception {\r
140         if (  filesList==null||filesList.size()==0\r
141             ||partsList==null||partsList.size()==0\r
142             ||(partsList.size() != filesList.size())){\r
143             throw new Exception("filesList and partsList must not be empty and must have the same number of items each.");\r
144         }\r
145         String content = DD + BOUNDARY;\r
146 \r
147         for (int i=0; i<partsList.size(); i++){\r
148             String fileName = filesList.get(i);\r
149             String commonPartName = partsList.get(i);\r
150             byte[] b = FileUtils.readFileToByteArray(new File(fileName));\r
151             String xmlString = new String(b);\r
152 \r
153             xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);\r
154 \r
155             content = content + CRLF + "label: "+commonPartName + CRLF\r
156                               + "Content-Type: application/xml" + CRLF\r
157                               + CRLF\r
158                               + xmlString + CRLF\r
159                               + DD + BOUNDARY;\r
160         }\r
161         content = content + DD;\r
162         String urlString = protoHostPort+uri;\r
163         return doPOST_PUT(urlString, content, BOUNDARY, method, MULTIPART_MIXED, authForTest, fromTestID); //method is POST or PUT.\r
164     }\r
165 \r
166     /** Use this overload for NON-multipart messages, that is, regular POSTs. */\r
167         public static ServiceResult doPOST_PUTFromXML(String fileName,\r
168                                                                 String protoHostPort,\r
169                                                                 String uri,\r
170                                                                 String method,\r
171                                                                 String contentType,\r
172                                                                 XmlReplayEval evalStruct,\r
173                                                                 String authForTest,\r
174                                                                 String fromTestID)\r
175     throws Exception {\r
176         byte[] b = FileUtils.readFileToByteArray(new File(fileName));\r
177         String xmlString = new String(b);\r
178         xmlString = evalStruct.eval(xmlString, evalStruct.serviceResultsMap, evalStruct.jexl, evalStruct.jc);\r
179         String urlString = protoHostPort+uri;\r
180         return doPOST_PUT(urlString, xmlString, BOUNDARY, method, contentType, authForTest, fromTestID); //method is POST or PUT.\r
181     }\r
182 \r
183 \r
184     public static ServiceResult doPOST_PUT(String urlString, String content, String boundary, String method, String contentType,\r
185                                            String authForTest, String fromTestID) throws Exception {\r
186         URL url = new URL(urlString);\r
187         HttpURLConnection conn;\r
188         conn = (HttpURLConnection) url.openConnection();\r
189 \r
190         if (MULTIPART_MIXED.equalsIgnoreCase(contentType)){\r
191             conn.setRequestProperty("Accept", "multipart/mixed");\r
192             conn.setRequestProperty("content-type", "multipart/mixed; boundary=" + boundary);\r
193         } else {\r
194             conn.setRequestProperty("Accept", "application/xml");\r
195             conn.setRequestProperty("content-type", contentType);\r
196         }\r
197         conn.setRequestProperty("Authorization", "Basic " + authForTest);  //TODO: remove test user : hard-coded as "dGVzdDp0ZXN0"\r
198         conn.setRequestProperty("Connection", "close");\r
199         conn.setRequestProperty("X-XmlReplay-fromTestID", fromTestID);\r
200         conn.setDoOutput(true);\r
201         conn.setDoInput(true);\r
202         conn.setRequestMethod(method); // "POST" or "PUT"\r
203         OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());\r
204         wr.write(content);\r
205         wr.flush();\r
206 \r
207         ServiceResult result = new ServiceResult();\r
208         try {\r
209             result.requestPayload = content;\r
210             result.responseCode = conn.getResponseCode();\r
211             //System.out.println("responseCode: "+result.responseCode);\r
212             if (400 <= result.responseCode && result.responseCode <= 499){\r
213                 return result;\r
214             }\r
215             BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));\r
216             String line;\r
217             StringBuffer sb = new StringBuffer();\r
218             while ((line = rd.readLine()) != null) {\r
219                 sb.append(line).append("\r\n");\r
220             }\r
221             String msg = sb.toString();\r
222             result.result = msg;\r
223             result.boundary = PayloadLogger.parseBoundary(conn.getHeaderField("CONTENT-TYPE"));\r
224 \r
225             rd.close();\r
226         } catch (Throwable t){\r
227             //System.err.println("ERROR getting content from response: "+t);\r
228             result.error = t.toString();\r
229         }\r
230         wr.close();\r
231 \r
232 \r
233         String deleteURL = "";\r
234         String location = "";\r
235         Map<String, List<String>> headers = conn.getHeaderFields();\r
236         List<String> locations = headers.get("Location");\r
237         if (locations != null){\r
238             String locationZero = locations.get(0);\r
239             if (locationZero != null){\r
240                 String[] segments = locationZero.split("/");\r
241                 location = segments[segments.length - 1];\r
242                 deleteURL = Tools.glue(urlString, "/", location);\r
243             }\r
244         }\r
245         result.location = location;\r
246         result.deleteURL = deleteURL;\r
247         result.CSID = location;\r
248         result.method = method;\r
249         return result;\r
250     }\r
251 \r
252 }\r